summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Higgins <pete@peterhiggins.org>2020-08-07 12:10:06 -0700
committerPete Higgins <pete@peterhiggins.org>2020-08-07 12:10:06 -0700
commit351905bc0a80e3b78bd0c928d3c3c2419b9f217d (patch)
treed43f393806675d9a12223fd597ff8f218da58bd6
parent3501e592267a4f23577071eba90224d7f95e1199 (diff)
downloadchef-resource-code-cleanup.tar.gz
Simplify property definition.resource-code-cleanup
Signed-off-by: Pete Higgins <pete@peterhiggins.org>
-rw-r--r--lib/chef/provider/powershell_script.rb26
-rw-r--r--lib/chef/resource/powershell_script.rb23
-rw-r--r--spec/unit/resource/powershell_script_spec.rb19
3 files changed, 23 insertions, 45 deletions
diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb
index 2b7c384246..80d20670ec 100644
--- a/lib/chef/provider/powershell_script.rb
+++ b/lib/chef/provider/powershell_script.rb
@@ -31,21 +31,32 @@ class Chef
super()
end
- def command
- # Powershell.exe is always in "v1.0" folder (for backwards compatibility)
- interpreter_path = Chef::Util::PathHelper.join(basepath, "WindowsPowerShell", "v1.0", interpreter)
+ # Set InputFormat to None as PowerShell will hang if STDIN is redirected
+ # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
+ DEFAULT_FLAGS = "-NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -InputFormat None"
+ def command
# 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
# error status of a failed Windows process that ran at the
# end of the script, it gets changed to '1'.
#
- "\"#{interpreter_path}\" #{new_resource.flags} -File \"#{script_file_path}\""
+ [
+ %Q{"#{interpreter_path}"},
+ DEFAULT_FLAGS,
+ new_resource.flags,
+ %Q{-File "#{script_file_path}"},
+ ].join(" ")
end
protected
+ def interpreter_path
+ # Powershell.exe is always in "v1.0" folder (for backwards compatibility)
+ Chef::Util::PathHelper.join(basepath, "WindowsPowerShell", "v1.0", interpreter)
+ end
+
def code
code = wrapper_script
logger.trace("powershell_script provider called with script code:\n\n#{new_resource.code}\n")
@@ -71,7 +82,12 @@ class Chef
# 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}\" #{new_resource.flags} -Command \". '#{user_script_file.path}'\""
+ validation_command = [
+ %Q{"#{interpreter_path}"},
+ DEFAULT_FLAGS,
+ new_resource.flags,
+ %Q{-Command ". '#{user_script_file.path}'"},
+ ].join(" ")
# Note that other script providers like bash allow syntax errors
# to be suppressed by setting 'returns' to a value that the
diff --git a/lib/chef/resource/powershell_script.rb b/lib/chef/resource/powershell_script.rb
index 20c968b842..7699c0a976 100644
--- a/lib/chef/resource/powershell_script.rb
+++ b/lib/chef/resource/powershell_script.rb
@@ -25,19 +25,7 @@ class Chef
provides :powershell_script, os: "windows"
property :flags, String,
- description: "A string that is passed to the Windows PowerShell command",
- default: lazy { default_flags },
- coerce: proc { |input|
- if input == default_flags
- # Means there was no input provided,
- # and should use defaults in this case
- input
- else
- # The last occurrence of a flag would override its
- # previous one at the time of command execution.
- [default_flags, input].join(" ")
- end
- }
+ description: "A string that is passed to the Windows PowerShell command"
property :convert_boolean_return, [true, false],
default: false,
@@ -88,15 +76,6 @@ class Chef
def self.get_default_attributes(opts)
{ convert_boolean_return: true }
end
-
- # Options that will be passed to Windows PowerShell command
- #
- # @returns [String]
- def default_flags
- # Set InputFormat to None as PowerShell will hang if STDIN is redirected
- # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
- "-NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -InputFormat None"
- end
end
end
end
diff --git a/spec/unit/resource/powershell_script_spec.rb b/spec/unit/resource/powershell_script_spec.rb
index 6666c4749b..10e3530a5f 100644
--- a/spec/unit/resource/powershell_script_spec.rb
+++ b/spec/unit/resource/powershell_script_spec.rb
@@ -130,24 +130,7 @@ describe Chef::Resource::PowershellScript do
let(:resource_instance_name ) { @resource.command }
let(:resource_name) { :powershell_script }
let(:interpreter_file_name) { "powershell.exe" }
- before do
- allow(@resource).to receive(:default_flags).and_return(nil)
- end
- it_behaves_like "a Windows script resource"
- end
-
- describe "#flags" do
- let(:resource) { @resource }
- it "appends user's flags to the defaults" do
- flags = %q{-Lunch "tacos"}
- resource.flags = flags
-
- expect(resource.flags).to eq("#{resource.default_flags} #{flags}")
- end
-
- it "uses the defaults when user doesn't provide flags" do
- expect(resource.flags).to eq(resource.default_flags)
- end
+ it_behaves_like "a Windows script resource"
end
end