Class: Inspec::Profile::AstHelper::ControlIDCollector

Inherits:
CollectorBase
  • Object
show all
Defined in:
lib/inspec/utils/profile_ast_helpers.rb

Instance Attribute Summary collapse

Attributes inherited from CollectorBase

#memo

Instance Method Summary collapse

Constructor Details

#initialize(memo, source_location_ref, include_tests: false) ⇒ ControlIDCollector

Returns a new instance of ControlIDCollector.



270
271
272
273
274
275
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 270

def initialize(memo, source_location_ref, include_tests: false)
  @memo = memo
  @seen_control_ids = {}
  @source_location_ref = source_location_ref
  @include_tests = include_tests
end

Instance Attribute Details

#include_testsObject (readonly)

Returns the value of attribute include_tests.



269
270
271
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 269

def include_tests
  @include_tests
end

#seen_control_idsObject (readonly)

Returns the value of attribute seen_control_ids.



269
270
271
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 269

def seen_control_ids
  @seen_control_ids
end

#source_location_refObject (readonly)

Returns the value of attribute source_location_ref.



269
270
271
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 269

def source_location_ref
  @source_location_ref
end

Instance Method Details

#on_block(block_node) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/inspec/utils/profile_ast_helpers.rb', line 277

def on_block(block_node)
  if RuboCop::AST::NodePattern.new("(block (send nil? :control ...) ...)").match(block_node)
    # NOTE: Assuming begin block is at the index 2
    begin_block = block_node.children[2]
    control_node = block_node.children[0]

    # TODO - This assumes the control ID is always a plain string, which we know it is often not!
    control_id = control_node.children[2].value
    # TODO - BUG - this keeps seeing the same nodes over and over againa, and so repeating control IDs. We are ignoring duplicate control IDs, which is incorrect.
    return if seen_control_ids[control_id]

    seen_control_ids[control_id] = true

    control_data = {
      id: control_id,
      code: block_node.source,
      source_location: {
        line: block_node.first_line,
        ref: source_location_ref,
      },
      title: nil,
      desc: nil,
      descriptions: {},
      impact: 0.5,
      refs: [],
      tags: {},
    }
    control_data[:checks] = [] if include_tests

    # Scan the code block for per-control metadata
    collectors = []
    collectors.push ImpactCollector.new(control_data)
    collectors.push DescCollector.new(control_data)
    collectors.push TitleCollector.new(control_data)
    collectors.push TagCollector.new(control_data)
    collectors.push RefCollector.new(control_data)
    collectors.push InputCollectorWithinControlBlock.new(@memo)
    collectors.push TestsCollector.new(control_data) if include_tests

    begin_block.each_node do |node_within_control|
      collectors.each { |collector| collector.process(node_within_control) }
    end

    memo[:controls].push control_data
  end
end