summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNimishaS <nimisha.sharad@msystechnologies.com>2016-09-26 12:52:37 +0000
committerNimishaS <nimisha.sharad@msystechnologies.com>2016-09-30 12:42:03 +0000
commit31c04a1338e1c2dd0e1332a4e4af7bca4fa2f82a (patch)
treeee0f959a42df63d6f6b9ce43411f4cc0aeda7160
parent4f738f8aaf0fb6f1ee0f24a123640d5abf57a800 (diff)
downloadchef-31c04a1338e1c2dd0e1332a4e4af7bca4fa2f82a.tar.gz
Handling double quotes. Added specs
Signed-off-by: NimishaS <nimisha.sharad@msystechnologies.com>
-rw-r--r--lib/chef/mixin/powershell_out.rb12
-rw-r--r--spec/data/templates/chef-seattle20160930-740-1sfhcp1.txt1
-rw-r--r--spec/data/templates/chef-seattle20160930-740-5n96t0.txt1
-rw-r--r--spec/data/templates/chef-seattle20160930-740-x16w6o.txt1
-rw-r--r--spec/functional/mixin/powershell_out_spec.rb6
-rw-r--r--spec/unit/mixin/powershell_out_spec.rb45
6 files changed, 27 insertions, 39 deletions
diff --git a/lib/chef/mixin/powershell_out.rb b/lib/chef/mixin/powershell_out.rb
index 74de85f86f..67698ed5b1 100644
--- a/lib/chef/mixin/powershell_out.rb
+++ b/lib/chef/mixin/powershell_out.rb
@@ -34,7 +34,14 @@ class Chef
script = command_args.first
options = command_args.last.is_a?(Hash) ? command_args.last : nil
- run_command_with_os_architecture(script, options)
+ result = run_command_with_os_architecture(script, options)
+
+ if result.stderr.include? "A positional parameter cannot be found that accepts argument"
+ raise "Please use single escape for special characters in powershell_out."
+ elsif result.stderr != ""
+ raise result.stderr
+ end
+ result
end
# Run a command under powershell with the same API as shell_out!
@@ -91,7 +98,8 @@ class Chef
"-InputFormat None",
]
- "powershell.exe #{flags.join(' ')} -Command \"#{script}\""
+ # without gsub user has to add double escape for double quotes in the recipe
+ "powershell.exe #{flags.join(' ')} -Command \"#{script.gsub('"', '\"')}\""
end
end
end
diff --git a/spec/data/templates/chef-seattle20160930-740-1sfhcp1.txt b/spec/data/templates/chef-seattle20160930-740-1sfhcp1.txt
new file mode 100644
index 0000000000..f476ccd704
--- /dev/null
+++ b/spec/data/templates/chef-seattle20160930-740-1sfhcp1.txt
@@ -0,0 +1 @@
+Do do do do, do do do do, do do do do, do do do do \ No newline at end of file
diff --git a/spec/data/templates/chef-seattle20160930-740-5n96t0.txt b/spec/data/templates/chef-seattle20160930-740-5n96t0.txt
new file mode 100644
index 0000000000..f476ccd704
--- /dev/null
+++ b/spec/data/templates/chef-seattle20160930-740-5n96t0.txt
@@ -0,0 +1 @@
+Do do do do, do do do do, do do do do, do do do do \ No newline at end of file
diff --git a/spec/data/templates/chef-seattle20160930-740-x16w6o.txt b/spec/data/templates/chef-seattle20160930-740-x16w6o.txt
new file mode 100644
index 0000000000..f476ccd704
--- /dev/null
+++ b/spec/data/templates/chef-seattle20160930-740-x16w6o.txt
@@ -0,0 +1 @@
+Do do do do, do do do do, do do do do, do do do do \ No newline at end of file
diff --git a/spec/functional/mixin/powershell_out_spec.rb b/spec/functional/mixin/powershell_out_spec.rb
index 66214cb0c7..9a210100e0 100644
--- a/spec/functional/mixin/powershell_out_spec.rb
+++ b/spec/functional/mixin/powershell_out_spec.rb
@@ -25,10 +25,6 @@ describe Chef::Mixin::PowershellOut, windows_only: true do
it "runs a powershell command and collects stdout" do
expect(powershell_out("get-process").run_command.stdout).to match /Handles\s+NPM\(K\)\s+PM\(K\)\s+WS\(K\)\s+VM\(M\)\s+CPU\(s\)\s+Id\s+/
end
-
- it "does not raise exceptions when the command is invalid" do
- powershell_out("this-is-not-a-valid-command").run_command
- end
end
describe "#powershell_out!" do
@@ -37,7 +33,7 @@ describe Chef::Mixin::PowershellOut, windows_only: true do
end
it "raises exceptions when the command is invalid" do
- expect { powershell_out!("this-is-not-a-valid-command").run_command }.to raise_exception(Mixlib::ShellOut::ShellCommandFailed)
+ expect { powershell_out!("this-is-not-a-valid-command").run_command }.to raise_error(RuntimeError)
end
end
end
diff --git a/spec/unit/mixin/powershell_out_spec.rb b/spec/unit/mixin/powershell_out_spec.rb
index 8e5f3588ce..c37fc4c8d9 100644
--- a/spec/unit/mixin/powershell_out_spec.rb
+++ b/spec/unit/mixin/powershell_out_spec.rb
@@ -18,7 +18,7 @@
require "spec_helper"
require "chef/mixin/powershell_out"
-describe Chef::Mixin::PowershellOut do
+describe Chef::Mixin::PowershellOut, :windows_only do
let(:shell_out_class) { Class.new { include Chef::Mixin::PowershellOut } }
subject(:object) { shell_out_class.new }
let(:architecture) { "something" }
@@ -28,43 +28,24 @@ describe Chef::Mixin::PowershellOut do
describe "#powershell_out" do
it "runs a command and returns the shell_out object" do
- ret = double("Mixlib::ShellOut")
- expect(object).to receive(:shell_out).with(
- "powershell.exe #{flags} -Command \"Get-Process\"",
- {}
- ).and_return(ret)
- expect(object.powershell_out("Get-Process")).to eql(ret)
+ result = object.powershell_out("Get-Process")
+ expect(result.stderr).to be == ""
end
it "passes options" do
- ret = double("Mixlib::ShellOut")
- expect(object).to receive(:shell_out).with(
- "powershell.exe #{flags} -Command \"Get-Process\"",
- timeout: 600
- ).and_return(ret)
- expect(object.powershell_out("Get-Process", timeout: 600)).to eql(ret)
+ result = object.powershell_out("Get-Process", timeout: 600)
+ expect(result.stderr).to be == ""
end
- end
- describe "#powershell_out!" do
- it "runs a command and returns the shell_out object" do
- mixlib_shellout = double("Mixlib::ShellOut")
- expect(object).to receive(:shell_out).with(
- "powershell.exe #{flags} -Command \"Get-Process\"",
- {}
- ).and_return(mixlib_shellout)
- expect(mixlib_shellout).to receive(:error!)
- expect(object.powershell_out!("Get-Process")).to eql(mixlib_shellout)
- end
+ context "when double quote is passed in the powershell command" do
+ it "passes if double quote is appended with single escape" do
+ result = object.powershell_out("Write-Verbose \"Some String\" -Verbose")
+ expect(result.stderr).to be == ""
+ end
- it "passes options" do
- mixlib_shellout = double("Mixlib::ShellOut")
- expect(object).to receive(:shell_out).with(
- "powershell.exe #{flags} -Command \"Get-Process\"",
- timeout: 600
- ).and_return(mixlib_shellout)
- expect(mixlib_shellout).to receive(:error!)
- expect(object.powershell_out!("Get-Process", timeout: 600)).to eql(mixlib_shellout)
+ it "raises error if double quote is passed with double escape characters" do
+ expect { object.powershell_out("Write-Verbose \\\"Some String\\\" -Verbose") }.to raise_error("Please use single escape for special characters in powershell_out.")
+ end
end
end
end