From 872a380a9aa8917931fae656e10a275c703de894 Mon Sep 17 00:00:00 2001 From: Mitchell Chambers Date: Thu, 11 Apr 2024 17:54:14 +1000 Subject: [PATCH 1/5] feat: on_windows return os running ruby interpreter over mocked os within spec context --- lib/pwsh/util.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pwsh/util.rb b/lib/pwsh/util.rb index 1976112a..4d795c21 100644 --- a/lib/pwsh/util.rb +++ b/lib/pwsh/util.rb @@ -12,7 +12,9 @@ module Util def on_windows? # Ruby only sets File::ALT_SEPARATOR on Windows and the Ruby standard # library uses that to test what platform it's on. - !!File::ALT_SEPARATOR + require 'rbconfig' + is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) + !!is_windows end # Verify paths specified are valid directories which exist. From 3e04ee2a71a189ae88a60764f881085b294e8437 Mon Sep 17 00:00:00 2001 From: Mitchell Chambers Date: Thu, 11 Apr 2024 18:31:13 +1000 Subject: [PATCH 2/5] feat: add support for non-windows to load ps_manager via pwsh_path for unit testing on unix os --- lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb b/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb index 6c586cbb..7740f4a2 100644 --- a/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb +++ b/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb @@ -1076,6 +1076,10 @@ def remove_secret_identifiers(text) def ps_manager debug_output = Puppet::Util::Log.level == :debug # TODO: Allow you to specify an alternate path, either to pwsh generally or a specific pwsh path. - Pwsh::Manager.instance(Pwsh::Manager.powershell_path, Pwsh::Manager.powershell_args, debug: debug_output) + if Pwsh::Util.on_windows? + Pwsh::Manager.instance(Pwsh::Manager.powershell_path, Pwsh::Manager.powershell_args, debug: debug_output) + else + Pwsh::Manager.instance(Pwsh::Manager.pwsh_path, Pwsh::Manager.pwsh_args, debug: debug_output) + end end end From 03ba8c7baafa0b1cdc3dca0671df53cae0b9567b Mon Sep 17 00:00:00 2001 From: Mitchell Chambers Date: Thu, 11 Apr 2024 18:35:06 +1000 Subject: [PATCH 3/5] feat: force interpreter on non-windows to split Env:PATH using `:` instead of `;` set via Windows os context in rspec --- lib/pwsh.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pwsh.rb b/lib/pwsh.rb index 49c80a2d..9cbfb336 100644 --- a/lib/pwsh.rb +++ b/lib/pwsh.rb @@ -380,7 +380,7 @@ def self.pwsh_path(additional_paths = []) pwsh_paths << File.join(path, 'pwsh.exe') if File.exist?(File.join(path, 'pwsh.exe')) end else - search_paths.split(File::PATH_SEPARATOR).each do |path| + search_paths.split(':').each do |path| pwsh_paths << File.join(path, 'pwsh') if File.exist?(File.join(path, 'pwsh')) end end From 56a4b81ca069a2de7eaff812fe1a607c334c5b69 Mon Sep 17 00:00:00 2001 From: Mitchell Chambers Date: Mon, 15 Apr 2024 10:55:00 +1000 Subject: [PATCH 4/5] feat: add .ps_manager mocks for non-Windows (pwsh_path and pwsh_args) --- .../dsc_base_provider_spec.rb | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/spec/unit/puppet/provider/dsc_base_provider/dsc_base_provider_spec.rb b/spec/unit/puppet/provider/dsc_base_provider/dsc_base_provider_spec.rb index 23089cc6..f5f42035 100644 --- a/spec/unit/puppet/provider/dsc_base_provider/dsc_base_provider_spec.rb +++ b/spec/unit/puppet/provider/dsc_base_provider/dsc_base_provider_spec.rb @@ -2110,21 +2110,44 @@ end describe '.ps_manager' do - before do - allow(Pwsh::Manager).to receive(:powershell_path).and_return('pwsh') - allow(Pwsh::Manager).to receive(:powershell_args).and_return('args') - end + describe '.ps_manager on non-Windows' do + before do + allow(Pwsh::Util).to receive(:on_windows?).and_return(false) + allow(Pwsh::Manager).to receive(:pwsh_path).and_return('pwsh') + allow(Pwsh::Manager).to receive(:pwsh_args).and_return('args') + end - it 'Initializes an instance of the Pwsh::Manager' do - expect(Puppet::Util::Log).to receive(:level).and_return(:normal) - expect(Pwsh::Manager).to receive(:instance).with('pwsh', 'args', debug: false) - expect { provider.ps_manager }.not_to raise_error + it 'Initializes an instance of the Pwsh::Manager' do + expect(Puppet::Util::Log).to receive(:level).and_return(:normal) + expect(Pwsh::Manager).to receive(:instance).with('pwsh', 'args', debug: false) + expect { provider.ps_manager }.not_to raise_error + end + + it 'passes debug as true if Puppet::Util::Log.level is debug' do + expect(Puppet::Util::Log).to receive(:level).and_return(:debug) + expect(Pwsh::Manager).to receive(:instance).with('pwsh', 'args', debug: true) + expect { provider.ps_manager }.not_to raise_error + end end - it 'passes debug as true if Puppet::Util::Log.level is debug' do - expect(Puppet::Util::Log).to receive(:level).and_return(:debug) - expect(Pwsh::Manager).to receive(:instance).with('pwsh', 'args', debug: true) - expect { provider.ps_manager }.not_to raise_error + describe '.ps_manager on Windows' do + before do + allow(Pwsh::Util).to receive(:on_windows?).and_return(true) + allow(Pwsh::Manager).to receive(:powershell_path).and_return('pwsh') + allow(Pwsh::Manager).to receive(:powershell_args).and_return('args') + end + + it 'Initializes an instance of the Pwsh::Manager' do + expect(Puppet::Util::Log).to receive(:level).and_return(:normal) + expect(Pwsh::Manager).to receive(:instance).with('pwsh', 'args', debug: false) + expect { provider.ps_manager }.not_to raise_error + end + + it 'passes debug as true if Puppet::Util::Log.level is debug' do + expect(Puppet::Util::Log).to receive(:level).and_return(:debug) + expect(Pwsh::Manager).to receive(:instance).with('pwsh', 'args', debug: true) + expect { provider.ps_manager }.not_to raise_error + end end end end From 82fd9ca8224d4c6ca295cbd1fbc00ad15cdc922c Mon Sep 17 00:00:00 2001 From: Mitchell Chambers Date: Thu, 18 Apr 2024 10:31:04 +1000 Subject: [PATCH 5/5] feat: attribute on_windows? method to rubyworks-facets rbconfig --- lib/pwsh/util.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/pwsh/util.rb b/lib/pwsh/util.rb index 4d795c21..ae2c5f32 100644 --- a/lib/pwsh/util.rb +++ b/lib/pwsh/util.rb @@ -7,14 +7,12 @@ module Util module_function # Verifies whether or not the current context is running on a Windows node. + # Implementation copied from `facets`: https://github.com/rubyworks/facets/blob/main/lib/standard/facets/rbconfig.rb # # @return [Bool] true if on windows def on_windows? - # Ruby only sets File::ALT_SEPARATOR on Windows and the Ruby standard - # library uses that to test what platform it's on. - require 'rbconfig' - is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) - !!is_windows + host_os = RbConfig::CONFIG['host_os'] + !!(host_os =~ /mswin|mingw/) end # Verify paths specified are valid directories which exist.