Skip to content

Commit

Permalink
Merge pull request #309 from chambersmp/feature_unix_rspec_support
Browse files Browse the repository at this point in the history
Add support for unit testing via Unix OS
  • Loading branch information
jordanbreen28 authored Apr 19, 2024
2 parents fec0326 + 82fd9ca commit 63335e2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
6 changes: 5 additions & 1 deletion lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/pwsh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/pwsh/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +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.
!!File::ALT_SEPARATOR
host_os = RbConfig::CONFIG['host_os']
!!(host_os =~ /mswin|mingw/)
end

# Verify paths specified are valid directories which exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 63335e2

Please sign in to comment.