Class: Inspec::Profile::AstHelper::InputCollectorBase

Inherits:
CollectorBase
  • Object
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

Attributes inherited from CollectorBase

#memo

Instance Method Summary collapse

Constructor Details

#initialize(memo) ⇒ InputCollectorBase

Returns a new instance of InputCollectorBase.



24
25
26
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 24

def initialize(memo)
  @memo = memo
end

Instance Method Details

#check_and_collect_input(node) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 71

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

#collect_input(input_children) ⇒ Object



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
69
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 28

def collect_input(input_children)
  input_name = input_children.children[2].value

  # Check if memo[:inputs] already has a value for the input_name, if yes, then skip adding it to the array
  unless memo[:inputs].any? { |input| input[:name] == input_name }
    # The value will be updated if available in the input_children
    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
            # Here value will be a hash
            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

    # TODO: Add rules for handling the input options or use existing rules if available
    # 1. Handle pattern matching for the given input value
    # 2. Handle data-type matching for the given input value
    # 3. Handle required flag for the given input value
    # 4. Handle sensitive flag for the given input value
    memo[:inputs] ||= []
    input_hash = {
      name: input_name,
      options: opts,
    }
    memo[:inputs] << input_hash
  end
end

#input_pattern_match?(node) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 81

def input_pattern_match?(node)
  RuboCop::AST::NodePattern.new("(send nil? :input ...)").match(node)
end