Skip to content

Commit

Permalink
Accept DataLoader objects in open method
Browse files Browse the repository at this point in the history
Also:
* Fix a bug in update method
* Preface UUID keys with the data class name

- Fixes #21
  • Loading branch information
silug committed Nov 6, 2024
1 parent 11df5fc commit c5b56a3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/compliance_engine/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/compliance_engine/data_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def key
return @key unless @key.nil?

require 'securerandom'
@key = SecureRandom.uuid
@key = "#{data.class}:#{SecureRandom.uuid}"
end
end
2 changes: 1 addition & 1 deletion spec/classes/compliance_engine/data_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions spec/classes/compliance_engine/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'spec_helper'
require 'compliance_engine'
require 'compliance_engine/data_loader'

RSpec.describe ComplianceEngine::Data do
before(:each) do
Expand Down Expand Up @@ -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') }

Expand Down

0 comments on commit c5b56a3

Please sign in to comment.