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

Add support for unit testing via Unix OS #309

Merged
merged 5 commits into from
Apr 19, 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
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