From a809097d121f7ea294c7a5a21be1875adf6d7c76 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Sat, 9 Apr 2016 20:09:04 +0200 Subject: [PATCH] simplify full_id generation --- lib/inspec/profile_context.rb | 18 +++++++++--------- lib/inspec/rule.rb | 12 ++++-------- test/functional/inspec_exec_test.rb | 4 ++-- test/unit/profile_context_test.rb | 4 ++-- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/inspec/profile_context.rb b/lib/inspec/profile_context.rb index 70c79ef46..60402b827 100644 --- a/lib/inspec/profile_context.rb +++ b/lib/inspec/profile_context.rb @@ -41,24 +41,19 @@ module Inspec end def unregister_rule(id) - full_id = Inspec::Rule.full_id(@profile_id, id) - @rules[full_id] = nil + @rules.delete(full_id(@profile_id, id)) end def register_rule(r) # get the full ID r.instance_variable_set(:@__file, @current_load[:file]) r.instance_variable_set(:@__group_title, @current_load[:title]) - full_id = Inspec::Rule.full_id(r) - if full_id.nil? - # TODO: error - return - end # add the rule to the registry - existing = @rules[full_id] + fid = full_id(Inspec::Rule.profile_id(r), Inspec::Rule.rule_id(r)) + existing = @rules[fid] if existing.nil? - @rules[full_id] = r + @rules[fid] = r else Inspec::Rule.merge(existing, r) end @@ -70,6 +65,11 @@ module Inspec private + def full_id(pid, rid) + return rid.to_s if pid.to_s.empty? + pid.to_s + '/' + rid.to_s + end + # Create the context for controls. This includes all components of the DSL, # including matchers and resources. # diff --git a/lib/inspec/rule.rb b/lib/inspec/rule.rb index 789d97913..b4ac3ba5f 100644 --- a/lib/inspec/rule.rb +++ b/lib/inspec/rule.rb @@ -119,6 +119,10 @@ module Inspec rule.instance_variable_set(:@__rule_id, value) end + def self.profile_id(rule) + rule.instance_variable_get(:@__profile_id) + end + def self.checks(rule) rule.instance_variable_get(:@__checks) end @@ -167,14 +171,6 @@ module Inspec set_skip_rule(dst, sr) unless sr.nil? end - # Get the full id consisting of profile id + rule id - # for the rule. - def self.full_id(rule) - rid = rule.instance_variable_get(:@__rule_id) - pid = rule.instance_variable_get(:@__profile_id) - "#{pid}/#{rid}" - end - private def __add_check(describe_or_expect, values, block) diff --git a/test/functional/inspec_exec_test.rb b/test/functional/inspec_exec_test.rb index d1975ba69..85164f3fb 100644 --- a/test/functional/inspec_exec_test.rb +++ b/test/functional/inspec_exec_test.rb @@ -55,9 +55,9 @@ describe 'inspec exec' do describe 'execute a profile with json formatting' do let(:json) { JSON.load(inspec('exec ' + example_profile + ' --format json').stdout) } let(:examples) { json['examples'] } - let(:ex1) { examples.find{|x| x['id'] == 'tmp-1.0'} } + let(:ex1) { examples.find{|x| x['id'] == 'profile/tmp-1.0'} } let(:ex2) { examples.find{|x| x['id'] =~ /generated/} } - let(:ex3) { examples.find{|x| x['id'] == 'gordon-1.0'} } + let(:ex3) { examples.find{|x| x['id'] == 'profile/gordon-1.0'} } it 'must have 5 examples' do json['examples'].length.must_equal 5 diff --git a/test/unit/profile_context_test.rb b/test/unit/profile_context_test.rb index 09955192d..99e7bb0cd 100644 --- a/test/unit/profile_context_test.rb +++ b/test/unit/profile_context_test.rb @@ -177,13 +177,13 @@ describe Inspec::ProfileContext do it 'provides the control keyword in the global DSL' do profile.load('control 1') - profile.rules.keys.must_equal [1] + profile.rules.keys.must_equal ['1'] profile.rules.values[0].must_be_kind_of Inspec::Rule end it 'provides the rule keyword in the global DSL (legacy mode)' do profile.load('rule 1') - profile.rules.keys.must_equal [1] + profile.rules.keys.must_equal ['1'] profile.rules.values[0].must_be_kind_of Inspec::Rule end end