From 3f489052352102167eb5ca1c0c8f2ec40d7f6759 Mon Sep 17 00:00:00 2001 From: Steven Pritchard Date: Wed, 4 Dec 2024 14:44:53 -0600 Subject: [PATCH] Fix environment loading Fixes #35 --- CHANGELOG.md | 3 +++ lib/compliance_engine/data.rb | 2 +- lib/compliance_engine/environment_loader.rb | 10 ++++------ lib/compliance_engine/environment_loader/zip.rb | 2 +- lib/compliance_engine/version.rb | 2 +- spec/classes/compliance_engine/data_spec.rb | 8 ++++++++ .../compliance_engine/environment_loader_spec.rb | 2 +- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8dd38d..0cd786c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/compliance_engine/data.rb b/lib/compliance_engine/data.rb index 3229a14..7740432 100644 --- a/lib/compliance_engine/data.rb +++ b/lib/compliance_engine/data.rb @@ -355,7 +355,7 @@ def data_variables # # @return [Array] def context_variables - [:@enforcement_tolerance, :@environment_data, :@facts] + [:@enforcement_tolerance, :@environment_data, :@facts, :@modulepath] end # Get the cache variables diff --git a/lib/compliance_engine/environment_loader.rb b/lib/compliance_engine/environment_loader.rb index 76a21e0..66e7f5b 100644 --- a/lib/compliance_engine/environment_loader.rb +++ b/lib/compliance_engine/environment_loader.rb @@ -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 diff --git a/lib/compliance_engine/environment_loader/zip.rb b/lib/compliance_engine/environment_loader/zip.rb index 8a37dbd..17d9265 100644 --- a/lib/compliance_engine/environment_loader/zip.rb +++ b/lib/compliance_engine/environment_loader/zip.rb @@ -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 diff --git a/lib/compliance_engine/version.rb b/lib/compliance_engine/version.rb index 45342cd..98fcb86 100644 --- a/lib/compliance_engine/version.rb +++ b/lib/compliance_engine/version.rb @@ -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 diff --git a/spec/classes/compliance_engine/data_spec.rb b/spec/classes/compliance_engine/data_spec.rb index 9c1e3d9..1f296d8 100644 --- a/spec/classes/compliance_engine/data_spec.rb +++ b/spec/classes/compliance_engine/data_spec.rb @@ -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 @@ -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 diff --git a/spec/classes/compliance_engine/environment_loader_spec.rb b/spec/classes/compliance_engine/environment_loader_spec.rb index 1e17967..6ef12b3 100644 --- a/spec/classes/compliance_engine/environment_loader_spec.rb +++ b/spec/classes/compliance_engine/environment_loader_spec.rb @@ -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