diff --git a/lib/compliance_engine/data.rb b/lib/compliance_engine/data.rb index 8aab334..b634436 100644 --- a/lib/compliance_engine/data.rb +++ b/lib/compliance_engine/data.rb @@ -124,6 +124,11 @@ def open_environment(*paths) def open(*paths, fileclass: File, dirclass: Dir) modules = {} paths.each do |path| + if path.is_a?(ComplianceEngine::DataLoader) + update(path, key: path.key, fileclass: fileclass) + next + end + if fileclass.directory?(path) # Read the Puppet module's metadata.json metadata_json = File.join(path.to_s, 'metadata.json') @@ -212,8 +217,8 @@ def update( data[filename.key][:loader] = filename data[filename.key][:loader].add_observer(self, :update) end - data[filename.key][:version] = ComplianceEngine::Version.new(loader.data['version']) - data[filename.key][:content] = loader.data + data[filename.key][:version] = ComplianceEngine::Version.new(filename.data['version']) + data[filename.key][:content] = filename.data end reset_collection diff --git a/lib/compliance_engine/data_loader.rb b/lib/compliance_engine/data_loader.rb index da0ade1..df01113 100644 --- a/lib/compliance_engine/data_loader.rb +++ b/lib/compliance_engine/data_loader.rb @@ -40,6 +40,6 @@ def key return @key unless @key.nil? require 'securerandom' - @key = SecureRandom.uuid + @key = "#{data.class}:#{SecureRandom.uuid}" end end diff --git a/spec/classes/compliance_engine/data_loader_spec.rb b/spec/classes/compliance_engine/data_loader_spec.rb index a444a58..f967bd3 100644 --- a/spec/classes/compliance_engine/data_loader_spec.rb +++ b/spec/classes/compliance_engine/data_loader_spec.rb @@ -13,7 +13,7 @@ it 'has a UUID key' do expect(data_loader.key).not_to be_nil expect(data_loader.key).to be_instance_of(String) - expect(data_loader.key).to match(%r{^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$}i) + expect(data_loader.key).to match(%r{^Hash:[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$}i) end end diff --git a/spec/classes/compliance_engine/data_spec.rb b/spec/classes/compliance_engine/data_spec.rb index f126f36..ed27a95 100644 --- a/spec/classes/compliance_engine/data_spec.rb +++ b/spec/classes/compliance_engine/data_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' require 'compliance_engine' +require 'compliance_engine/data_loader' RSpec.describe ComplianceEngine::Data do before(:each) do @@ -111,6 +112,32 @@ end end + context 'with a ComplianceEngine::DataLoader object' do + subject(:compliance_engine) { described_class.new(data_loader) } + + let(:data) do + { + 'version' => '2.0.0', + } + end + + let(:data_loader) { ComplianceEngine::DataLoader.new(data) } + + it 'initializes' do + expect(compliance_engine).not_to be_nil + expect(compliance_engine).to be_instance_of(described_class) + end + + it 'returns a UUID' do + expect(compliance_engine.files.count).to eq(1) + expect(compliance_engine.files.first).to match(%r{^Hash:[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$}i) + end + + it 'get returns a hash' do + expect(compliance_engine.get(compliance_engine.files.first)).to eq(data) + end + end + context 'with a malformed file path' do subject(:compliance_engine) { described_class.new('file') }