summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-04-27 10:38:33 -0700
committerGitHub <noreply@github.com>2020-04-27 10:38:33 -0700
commita1a58fbbb6bbcf106c8960c04240d68d42f7fbe9 (patch)
tree72a9fd7bec328e51298808441f11fec1c33393fa
parent47db22988400fb323441cafbba8399a3a742afc9 (diff)
parent3f1d5f3b916aa4c2aea10bb68dfe6765ce1d8969 (diff)
downloadchef-a1a58fbbb6bbcf106c8960c04240d68d42f7fbe9.tar.gz
Merge pull request #9742 from chef/powershell_exec_bang
Add powershell_exec! helper to make conversion from powershell_out! easier
-rw-r--r--lib/chef/mixin/powershell_exec.rb11
-rw-r--r--lib/chef/powershell.rb14
-rw-r--r--spec/unit/mixin/powershell_exec_spec.rb10
3 files changed, 34 insertions, 1 deletions
diff --git a/lib/chef/mixin/powershell_exec.rb b/lib/chef/mixin/powershell_exec.rb
index 1a60a3d480..e3410e007f 100644
--- a/lib/chef/mixin/powershell_exec.rb
+++ b/lib/chef/mixin/powershell_exec.rb
@@ -20,7 +20,7 @@ require_relative "../powershell"
# The powershell_exec mixin provides in-process access to PowerShell engine via
# a COM interop (installed by the Chef Client installer).
#
-# powershell_exec returns a Chef::PowerShell object that provides 3 methods:
+# powershell_exec returns a Chef::PowerShell object that provides 4 methods:
#
# .result - returns a hash representing the results returned by executing the
# PowerShell script block
@@ -28,6 +28,7 @@ require_relative "../powershell"
# PowerShell error stream during execution
# .error? - returns true if there were error messages written to the PowerShell
# error stream during execution
+# .error! - raise Chef::PowerShell::CommandFailed if there was an error
#
# Some examples of usage:
#
@@ -100,6 +101,14 @@ class Chef
def powershell_exec(script)
Chef::PowerShell.new(script)
end
+
+ # The same as the #powershell_exec method except this will raise
+ # Chef::PowerShell::CommandFailed if the command fails
+ def powershell_exec!(script)
+ cmd = Chef::PowerShell.new(script)
+ cmd.error!
+ cmd
+ end
end
end
end
diff --git a/lib/chef/powershell.rb b/lib/chef/powershell.rb
index b1f8c5396c..9790a3c10f 100644
--- a/lib/chef/powershell.rb
+++ b/lib/chef/powershell.rb
@@ -39,12 +39,26 @@ class Chef
exec(script)
end
+ #
+ # Was there an error running the command
+ #
+ # @return [Boolean]
+ #
def error?
return true if errors.count > 0
false
end
+ class CommandFailed < RuntimeError; end
+
+ #
+ # @raise [Chef::PowerShell::CommandFailed] raise if the command failed
+ #
+ def error!
+ raise Chef::PowerShell::CommandFailed, "Unexpected exit in PowerShell command: #{@errors}" if error?
+ end
+
private
def exec(script)
diff --git a/spec/unit/mixin/powershell_exec_spec.rb b/spec/unit/mixin/powershell_exec_spec.rb
index bf2285175b..8c3e0a4b88 100644
--- a/spec/unit/mixin/powershell_exec_spec.rb
+++ b/spec/unit/mixin/powershell_exec_spec.rb
@@ -40,4 +40,14 @@ describe Chef::Mixin::PowershellExec, :windows_only do
expect(execution.errors[0]).to include("Runtime exception: this-should-error")
end
end
+
+ describe "#powershell_exec!" do
+ it "runs a basic command and returns a Chef::PowerShell object" do
+ expect(object.powershell_exec("$PSVersionTable")).to be_kind_of(Chef::PowerShell::CommandFailed)
+ end
+
+ it "raises an error if the command fails" do
+ expect { object.powershell_exec("$PSVersionTable") }.to raise_error(Chef::Exceptions::Script)
+ end
+ end
end