diff options
author | mwrock <matt@mattwrock.com> | 2020-10-07 11:02:54 -0700 |
---|---|---|
committer | mwrock <matt@mattwrock.com> | 2020-10-07 11:02:54 -0700 |
commit | 03f70aede103ffdea053affff3231c2b07fcebe9 (patch) | |
tree | 8c47753057f9aa81ef1da0e68f2f10b14b3b9341 /spec | |
parent | 0e268d038575d781ff396ce6307a20834ef52cae (diff) | |
download | chef-03f70aede103ffdea053affff3231c2b07fcebe9.tar.gz |
add interpreter to handle pwsh and powershell to powershell_scriptps_script
Signed-off-by: mwrock <matt@mattwrock.com>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/functional/resource/powershell_script_spec.rb | 71 | ||||
-rw-r--r-- | spec/unit/provider/powershell_script_spec.rb | 11 | ||||
-rw-r--r-- | spec/unit/resource/powershell_script_spec.rb | 4 |
3 files changed, 70 insertions, 16 deletions
diff --git a/spec/functional/resource/powershell_script_spec.rb b/spec/functional/resource/powershell_script_spec.rb index 70442eb2b1..68fa94afe9 100644 --- a/spec/functional/resource/powershell_script_spec.rb +++ b/spec/functional/resource/powershell_script_spec.rb @@ -47,7 +47,7 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do r end - describe "when the run action is invoked on Windows" do + shared_examples_for "a running powershell script" do it "successfully executes a non-cmdlet Windows binary as the last command of the script" do resource.code(successful_executable_script_content + " | out-file -encoding ASCII #{script_output_path}") resource.returns(0) @@ -231,22 +231,54 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do resource.only_if { true } expect { resource.should_skip?(:run) }.to raise_error(ArgumentError, /guard_interpreter does not support blocks/) end + end + + context "when using the powershell interpreter" do + before do + resource.interpreter "powershell" + end + + it_behaves_like "a running powershell script" + + it "runs Windows Powershell" do + resource.code("$PSVersionTable.PSVersion.Major | out-file -encoding ASCII #{script_output_path}") + resource.returns(0) + resource.run_action(:run) + + expect(get_script_output.to_i).to be < 6 + end + end + + context "when using the pwsh interpreter", :pwsh_installed do + before do + resource.interpreter "pwsh" + end + + it_behaves_like "a running powershell script" - context "when dsc is supported", :windows_powershell_dsc_only do - it "can execute LCM configuration code" do - resource.code <<~EOF - configuration LCM + it "runs a version of powershell greater than 6" do + resource.code("$PSVersionTable.PSVersion.Major | out-file -encoding ASCII #{script_output_path}") + resource.returns(0) + resource.run_action(:run) + + expect(get_script_output.to_i).to be > 6 + end + end + + context "when dsc is supported", :windows_powershell_dsc_only do + it "can execute LCM configuration code" do + resource.code <<~EOF + configuration LCM + { + param ($thumbprint) + localconfigurationmanager { - param ($thumbprint) - localconfigurationmanager - { - RebootNodeIfNeeded = $false - ConfigurationMode = 'ApplyOnly' - } + RebootNodeIfNeeded = $false + ConfigurationMode = 'ApplyOnly' } - EOF - expect { resource.run_action(:run) }.not_to raise_error - end + } + EOF + expect { resource.run_action(:run) }.not_to raise_error end end @@ -347,6 +379,17 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do context "with powershell_script as the guard_interpreter" do + context "when pwsh is the interpreter", :pwsh_installed do + before do + resource.interpreter "pwsh" + end + + it "uses powershell core to evaluate the guard" do + resource.not_if "$PSVersionTable.PSEdition -eq 'Core'" + expect(resource.should_skip?(:run)).to be_truthy + end + end + it "has a guard_interpreter attribute set to :powershell_script" do expect(resource.guard_interpreter).to eq(:powershell_script) end diff --git a/spec/unit/provider/powershell_script_spec.rb b/spec/unit/provider/powershell_script_spec.rb index 1c49c52160..0bece73bd0 100644 --- a/spec/unit/provider/powershell_script_spec.rb +++ b/spec/unit/provider/powershell_script_spec.rb @@ -46,5 +46,16 @@ describe Chef::Provider::PowershellScript, "action_run" do expect(provider.command).to eq(expected) end + + it "uses pwsh when given the pwsh interpreter" do + new_resource.interpreter = "pwsh" + provider.send(:script_file_path=, "C:\\Temp\\Script.ps1") + + expected = <<~CMD.strip + "pwsh" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -InputFormat None -File "C:\\Temp\\Script.ps1" + CMD + + expect(provider.command).to eq(expected) + end end end diff --git a/spec/unit/resource/powershell_script_spec.rb b/spec/unit/resource/powershell_script_spec.rb index 9bc91204be..75fdf6ffc6 100644 --- a/spec/unit/resource/powershell_script_spec.rb +++ b/spec/unit/resource/powershell_script_spec.rb @@ -53,9 +53,9 @@ describe Chef::Resource::PowershellScript do allow(resource).to receive(:updated).and_return(true) end - it "inherits exactly the :cwd, :environment, :group, :path, :user, :umask, :architecture, :elevated properties from a parent resource class" do + it "inherits exactly the :cwd, :environment, :group, :path, :user, :umask, :architecture, :elevated, :interpreter properties from a parent resource class" do inherited_difference = Chef::Resource::PowershellScript.guard_inherited_attributes - - %i{cwd environment group path user umask architecture elevated} + %i{cwd environment group path user umask architecture elevated interpreter} expect(inherited_difference).to eq([]) end |