simplify full_id generation

This commit is contained in:
Dominik Richter 2016-04-09 20:09:04 +02:00 committed by Christoph Hartmann
parent 8ba859fe92
commit a809097d12
4 changed files with 17 additions and 21 deletions

View file

@ -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.
#

View file

@ -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)

View file

@ -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

View file

@ -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