summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@getchef.com>2015-10-06 12:44:48 -0700
committerClaire McQuin <claire@getchef.com>2015-10-06 13:19:28 -0700
commit9b57fbf80828747699695119a354f51dbb7fa4b2 (patch)
tree32c5d2f3317c6cabaa1338ba9dabcbf6d3119fa4
parent0053c17bba34090083a34395657ff1ba1616e213 (diff)
downloadchef-mcquin/chef-3883.tar.gz
Quote paths.mcquin/chef-3883
-rw-r--r--lib/chef/provider/powershell_script.rb19
-rw-r--r--spec/support/shared/functional/windows_script.rb23
-rw-r--r--spec/unit/provider/powershell_script_spec.rb14
3 files changed, 46 insertions, 10 deletions
diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb
index 91ce11c337..cea9a45dad 100644
--- a/lib/chef/provider/powershell_script.rb
+++ b/lib/chef/provider/powershell_script.rb
@@ -41,10 +41,6 @@ class Chef
# Powershell.exe is always in "v1.0" folder (for backwards compatibility)
interpreter_path = Chef::Util::PathHelper.join(basepath, "WindowsPowerShell", "v1.0", interpreter)
- "\"#{interpreter_path}\" #{flags} \"#{script_file.path}\""
- end
-
- def flags
# Must use -File rather than -Command to launch the script
# file created by the base class that contains the script
# code -- otherwise, powershell.exe does not propagate the
@@ -52,8 +48,17 @@ class Chef
# end of the script, it gets changed to '1'.
#
# Nano only supports -Command
- file_or_command = Chef::Platform.windows_nano_server? ? '-Command' : '-File'
- interpreter_flags = [*default_interpreter_flags, file_or_command].join(' ')
+ cmd = "\"#{interpreter_path}\" #{flags}"
+ if Chef::Platform.windows_nano_server?
+ cmd << " -Command \". '#{script_file.path}'\""
+ else
+ cmd << " -File \"#{script_file.path}\""
+ end
+ cmd
+ end
+
+ def flags
+ interpreter_flags = [*default_interpreter_flags].join(' ')
if ! (@new_resource.flags.nil?)
interpreter_flags = [@new_resource.flags, interpreter_flags].join(' ')
@@ -91,7 +96,7 @@ EOH
# written to the file system at this point, which is required since
# the intent is to execute the code just written to it.
user_script_file.close
- validation_command = "\"#{interpreter}\" #{interpreter_arguments} -Command #{user_script_file.path}"
+ validation_command = "\"#{interpreter}\" #{interpreter_arguments} -Command \". '#{user_script_file.path}'\""
# Note that other script providers like bash allow syntax errors
# to be suppressed by setting 'returns' to a value that the
diff --git a/spec/support/shared/functional/windows_script.rb b/spec/support/shared/functional/windows_script.rb
index 70fb2d8578..d84c06c86b 100644
--- a/spec/support/shared/functional/windows_script.rb
+++ b/spec/support/shared/functional/windows_script.rb
@@ -128,7 +128,28 @@ shared_context Chef::Resource::WindowsScript do
describe "when the run action is invoked on Windows" do
it "executes the script code" do
- resource.code("whoami > #{script_output_path}")
+ resource.code("whoami > \"#{script_output_path}\"")
+ resource.returns(0)
+ resource.run_action(:run)
+ end
+ end
+
+ context "when $env:TMP has a space" do
+ before(:each) do
+ @dir = Dir.mktmpdir("Jerry Smith")
+ @original_env = ENV.to_hash.dup
+ ENV.delete('TMP')
+ ENV['TMP'] = @dir
+ end
+
+ after(:each) do
+ FileUtils.remove_entry_secure(@dir)
+ ENV.clear
+ ENV.update(@original_env)
+ end
+
+ it "executes the script code" do
+ resource.code("whoami > \"#{script_output_path}\"")
resource.returns(0)
resource.run_action(:run)
end
diff --git a/spec/unit/provider/powershell_script_spec.rb b/spec/unit/provider/powershell_script_spec.rb
index d06d2762be..121973763d 100644
--- a/spec/unit/provider/powershell_script_spec.rb
+++ b/spec/unit/provider/powershell_script_spec.rb
@@ -41,20 +41,30 @@ describe Chef::Provider::PowershellScript, "action_run" do
context 'on nano' do
before(:each) do
allow(Chef::Platform).to receive(:windows_nano_server?).and_return(true)
+ allow(provider).to receive(:is_forced_32bit).and_return(false)
+ os_info_double = double("os_info")
+ allow(provider.run_context.node.kernel).to receive(:os_info).and_return(os_info_double)
+ allow(os_info_double).to receive(:system_directory).and_return("C:\\Windows\\system32")
end
it "sets the -Command flag as the last flag" do
- expect(provider.flags.split(' ').pop).to eq("-Command")
+ flags = provider.command.split(' ').keep_if { |flag| flag =~ /^-/ }
+ expect(flags.pop).to eq("-Command")
end
end
context 'not on nano' do
before(:each) do
allow(Chef::Platform).to receive(:windows_nano_server?).and_return(false)
+ allow(provider).to receive(:is_forced_32bit).and_return(false)
+ os_info_double = double("os_info")
+ allow(provider.run_context.node.kernel).to receive(:os_info).and_return(os_info_double)
+ allow(os_info_double).to receive(:system_directory).and_return("C:\\Windows\\system32")
end
it "sets the -File flag as the last flag" do
- expect(provider.flags.split(' ').pop).to eq("-File")
+ flags = provider.command.split(' ').keep_if { |flag| flag =~ /^-/ }
+ expect(flags.pop).to eq("-File")
end
let(:execution_policy_flag) do