From 8bbbe695e3a8042c2dc0514182fe60abf2bc102a Mon Sep 17 00:00:00 2001 From: NimishaS Date: Fri, 7 Oct 2016 10:03:31 +0000 Subject: Fixed double quotes issue in powershell_out Signed-off-by: NimishaS --- lib/chef/mixin/powershell_out.rb | 2 +- spec/unit/mixin/powershell_out_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/chef/mixin/powershell_out.rb b/lib/chef/mixin/powershell_out.rb index 74de85f86f..ab7cf00a72 100644 --- a/lib/chef/mixin/powershell_out.rb +++ b/lib/chef/mixin/powershell_out.rb @@ -91,7 +91,7 @@ class Chef "-InputFormat None", ] - "powershell.exe #{flags.join(' ')} -Command \"#{script}\"" + "powershell.exe #{flags.join(' ')} -Command \"#{script.gsub('"', '\"')}\"" end end end diff --git a/spec/unit/mixin/powershell_out_spec.rb b/spec/unit/mixin/powershell_out_spec.rb index 8e5f3588ce..fb449e68ce 100644 --- a/spec/unit/mixin/powershell_out_spec.rb +++ b/spec/unit/mixin/powershell_out_spec.rb @@ -44,6 +44,17 @@ describe Chef::Mixin::PowershellOut do ).and_return(ret) expect(object.powershell_out("Get-Process", timeout: 600)).to eql(ret) 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 "suppresses error if double quote is passed with double escape characters" do + expect { object.powershell_out("Write-Verbose \\\"Some String\\\" -Verbose") }.not_to raise_error + end + end end describe "#powershell_out!" do @@ -66,5 +77,16 @@ describe Chef::Mixin::PowershellOut do expect(mixlib_shellout).to receive(:error!) expect(object.powershell_out!("Get-Process", timeout: 600)).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 "raises error if double quote is passed with double escape characters" do + expect { object.powershell_out!("Write-Verbose \\\"Some String\\\" -Verbose") }.to raise_error(Mixlib::ShellOut::ShellCommandFailed) + end + end end end -- cgit v1.2.1 From 08edf09ca4c8fbecf2585924719996ec71bb9d54 Mon Sep 17 00:00:00 2001 From: NimishaS Date: Fri, 7 Oct 2016 10:39:09 +0000 Subject: running powershell specs for only windows Signed-off-by: NimishaS --- spec/unit/mixin/powershell_out_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/mixin/powershell_out_spec.rb b/spec/unit/mixin/powershell_out_spec.rb index fb449e68ce..6431aae018 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" } -- cgit v1.2.1 From b2ae81e770255f935d0c75b3e0d7db1665609fd8 Mon Sep 17 00:00:00 2001 From: NimishaS Date: Mon, 10 Oct 2016 07:03:51 +0000 Subject: Verifying powershell stdout in specs Signed-off-by: NimishaS --- spec/unit/mixin/powershell_out_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/unit/mixin/powershell_out_spec.rb b/spec/unit/mixin/powershell_out_spec.rb index 6431aae018..4ecdcb8325 100644 --- a/spec/unit/mixin/powershell_out_spec.rb +++ b/spec/unit/mixin/powershell_out_spec.rb @@ -49,6 +49,7 @@ describe Chef::Mixin::PowershellOut, :windows_only 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 == "" + expect(result.stdout).to be == "VERBOSE: Some String\n" end it "suppresses error if double quote is passed with double escape characters" do @@ -82,6 +83,7 @@ describe Chef::Mixin::PowershellOut, :windows_only 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 == "" + expect(result.stdout).to be == "VERBOSE: Some String\n" end it "raises error if double quote is passed with double escape characters" do -- cgit v1.2.1