Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix environment loading #36

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 0.1.2 / 2024-12-04
* Fix environment loading (#35)

### 0.1.1 / 2024-12-03
* Update README.md and other documentation
* Add a `version` CLI command
Expand Down
2 changes: 1 addition & 1 deletion lib/compliance_engine/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def data_variables
#
# @return [Array<Symbol>]
def context_variables
[:@enforcement_tolerance, :@environment_data, :@facts]
[:@enforcement_tolerance, :@environment_data, :@facts, :@modulepath]
end

# Get the cache variables
Expand Down
10 changes: 4 additions & 6 deletions lib/compliance_engine/environment_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ class ComplianceEngine::EnvironmentLoader
# Initialize an EnvironmentLoader from the components of a Puppet `modulepath`
#
# @param paths [Array] the paths to search for Puppet modules
# @param root [String] the root directory to search for Puppet modules
# @param fileclass [File] the class to use for file operations (default: `File`)
# @param dirclass [Dir] the class to use for directory operations (default: `Dir`)
def initialize(*paths, root: nil, fileclass: File, dirclass: Dir)
def initialize(*paths, fileclass: File, dirclass: Dir)
raise ArgumentError, 'No paths specified' if paths.empty?
@modulepath ||= paths
modules = paths.map do |path|
root ||= path
dirclass.entries(root)
dirclass.entries(path)
.grep(%r{\A[a-z][a-z0-9_]*\Z})
.select { |child| fileclass.directory?(File.join(root, child)) }
.map { |child| File.join(root, child) }
.select { |child| fileclass.directory?(File.join(path, child)) }
.map { |child| File.join(path, child) }
rescue
[]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/compliance_engine/environment_loader/zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(path, root: '/'.dup)
dir = zipfile.dir
file = zipfile.file

super(path, root: root, fileclass: file, dirclass: dir)
super(root, fileclass: file, dirclass: dir)
end
end
end
2 changes: 1 addition & 1 deletion lib/compliance_engine/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module ComplianceEngine
VERSION = '0.1.1'
VERSION = '0.1.2'

# Handle supported compliance data versions
class Version
Expand Down
8 changes: 8 additions & 0 deletions spec/classes/compliance_engine/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,10 @@ def test_data
expect(compliance_engine).to be_instance_of(described_class)
end

it 'returns the modulepath' do
expect(compliance_engine.modulepath).to eq(test_data)
end

it 'returns a list of files' do
expect(compliance_engine.files).to eq(test_files.map { |file| File.join(test_data, '.', file) })
end
Expand Down Expand Up @@ -1127,6 +1131,10 @@ def test_data
expect(compliance_engine).to be_instance_of(described_class)
end

it 'returns the modulepath' do
expect(compliance_engine.modulepath).to eq([test_data])
end

it 'returns a list of files' do
expect(compliance_engine.files).to eq(test_files.map { |file| File.join(test_data, file) })
end
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/compliance_engine/environment_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
allow(Dir).to receive(:entries).with('/path1').and_return(['.', '..', 'a'])
allow(Dir).to receive(:entries).with('/path2').and_return(['.', '..', 'b'])
allow(File).to receive(:directory?).with('/path1/a').and_return(true)
allow(File).to receive(:directory?).with('/path1/b').and_return(true)
allow(File).to receive(:directory?).with('/path2/b').and_return(true)
allow(ComplianceEngine::ModuleLoader).to receive(:new).and_return(instance_double(ComplianceEngine::ModuleLoader))
end

Expand Down
Loading