Class: Inspec::Profile::AstHelper::InputCollectorBase
- Inherits:
-
CollectorBase
- Object
- Parser::AST::Processor
- CollectorBase
- Inspec::Profile::AstHelper::InputCollectorBase
show all
- Defined in:
- lib/inspec/utils/profile_ast_helpers.rb
Constant Summary
collapse
- VALID_INPUT_OPTIONS =
%i{name value type required priority pattern profile sensitive}.freeze
- REQUIRED_VALUES_MAP =
{
true: true,
false: false,
}.freeze
Instance Attribute Summary
#memo
Instance Method Summary
collapse
Constructor Details
Returns a new instance of InputCollectorBase.
23
24
25
|
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 23
def initialize(memo)
@memo = memo
end
|
Instance Method Details
70
71
72
73
74
75
76
77
78
|
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 70
def check_and_collect_input(node)
if input_pattern_match?(node)
collect_input(node)
else
node.children.each do |child_node|
check_and_collect_input(child_node) if input_pattern_match?(child_node)
end
end
end
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 27
def collect_input(input_children)
input_name = input_children.children[2].value
unless memo[:inputs].any? { |input| input[:name] == input_name }
opts = {
value: "Input '#{input_name}' does not have a value. Skipping test.",
}
if input_children.children[3]&.type == :hash
input_children.children[3].children.each do |child_node|
if VALID_INPUT_OPTIONS.include?(child_node.key.value)
if child_node.value.class == RuboCop::AST::Node && REQUIRED_VALUES_MAP.key?(child_node.value.type)
opts.merge!(child_node.key.value => REQUIRED_VALUES_MAP[child_node.value.type])
elsif child_node.value.class == RuboCop::AST::HashNode
values = {}
child_node.value.children.each do |grand_child_node|
values.merge!(grand_child_node.key.value => grand_child_node.value.value)
end
opts.merge!(child_node.key.value => values)
else
opts.merge!(child_node.key.value => child_node.value.value)
end
end
end
end
memo[:inputs] ||= []
input_hash = {
name: input_name,
options: opts,
}
memo[:inputs] << input_hash
end
end
|
80
81
82
|
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 80
def input_pattern_match?(node)
RuboCop::AST::NodePattern.new("(send nil? :input ...)").match(node)
end
|