diff options
-rw-r--r-- | lib/chef/guard_interpreter/resource_guard_interpreter.rb | 11 | ||||
-rw-r--r-- | lib/chef/provider/powershell_script.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource/powershell_script.rb | 18 | ||||
-rw-r--r-- | spec/unit/resource/powershell_spec.rb | 40 |
4 files changed, 62 insertions, 9 deletions
diff --git a/lib/chef/guard_interpreter/resource_guard_interpreter.rb b/lib/chef/guard_interpreter/resource_guard_interpreter.rb index 0a88d75d5c..c4b6d33a16 100644 --- a/lib/chef/guard_interpreter/resource_guard_interpreter.rb +++ b/lib/chef/guard_interpreter/resource_guard_interpreter.rb @@ -24,8 +24,19 @@ class Chef def translate_command_block(command, opts, &block) merge_inherited_attributes + if command && ! block_given? block_attributes = opts.merge({:code => command}) + + # Handles cases like powershell_script where default + # attributes are different when used in a guard vs. not. For + # powershell_script in particular, this will go away when + # the one attribue that causes this changes its default to be + # the same after some period to prepare for deprecation + if @resource.class.respond_to?(:get_default_attributes) + block_attributes = @resource.class.send(:get_default_attributes, opts).merge(block_attributes) + end + translated_block = to_block(block_attributes) [nil, translated_block] else diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb index a5820eb3a2..f3135f1a35 100644 --- a/lib/chef/provider/powershell_script.rb +++ b/lib/chef/provider/powershell_script.rb @@ -42,7 +42,7 @@ class Chef "\n" + code.to_s + EXIT_STATUS_NORMALIZATION_SCRIPT ) - convert_boolean_return = @new_resource.convert_boolean_return.nil? ? false : @new_resource.convert_boolean_return + convert_boolean_return = @new_resource.convert_boolean_return @code = <<EOH new-variable -name interpolatedexitcode -visibility private -value #{convert_boolean_return} new-variable -name chefscriptresult -visibility private diff --git a/lib/chef/resource/powershell_script.rb b/lib/chef/resource/powershell_script.rb index 08568b6964..f053d0b2aa 100644 --- a/lib/chef/resource/powershell_script.rb +++ b/lib/chef/resource/powershell_script.rb @@ -36,16 +36,18 @@ class Chef ) end - def only_if(command=nil, opts={}, &block) - augmented_opts = opts.merge((guard_interpreter.nil? || guard_interpreter == :default) ? {} : {:convert_boolean_return => true}) {|key, original_value, augmented_value| original_value} - super(command, augmented_opts, &block) - end + protected - def not_if(command=nil, opts={}, &block) - augmented_opts = opts.merge((guard_interpreter.nil? || guard_interpreter == :default) ? {} : {:convert_boolean_return => true}) {|key, original_value, augmented_value| original_value} - super(command, augmented_opts, &block) + # Allow callers evaluating guards to request default + # attribute values. This is needed to allow + # convert_boolean_return to be true in guard context by default, + # and false by default otherwise. When this mode becomes the + # default for this resource, this method can be removed since + # guard context and recipe resource context will have the + # same behavior. + def self.get_default_attributes(opts) + {:convert_boolean_return => true} end - end end end diff --git a/spec/unit/resource/powershell_spec.rb b/spec/unit/resource/powershell_spec.rb index 8929d6a9d0..1ef50f5eeb 100644 --- a/spec/unit/resource/powershell_spec.rb +++ b/spec/unit/resource/powershell_spec.rb @@ -78,6 +78,46 @@ describe Chef::Resource::PowershellScript do allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false) resource.only_if("echo hi") end + + it "should enable convert_boolean_return by default for guards in the context of powershell_script when no guard params are specified" do + allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(true) + allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:to_block).with( + {:convert_boolean_return => true, :code => "$true"}).and_return(Proc.new {}) + resource.only_if("$true") + end + + it "should enable convert_boolean_return by default for guards in non-Chef::Resource::Script derived resources when no guard params are specified" do + node = Chef::Node.new + run_context = Chef::RunContext.new(node, nil, nil) + file_resource = Chef::Resource::File.new('idontexist', run_context) + file_resource.guard_interpreter :powershell_script + + allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:to_block).with( + {:convert_boolean_return => true, :code => "$true"}).and_return(Proc.new {}) + resource.only_if("$true") + end + + it "should enable convert_boolean_return by default for guards in the context of powershell_script when guard params are specified" do + guard_parameters = {:cwd => '/etc/chef', :architecture => :x86_64} + allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:to_block).with( + {:convert_boolean_return => true, :code => "$true"}.merge(guard_parameters)).and_return(Proc.new {}) + resource.only_if("$true", guard_parameters) + end + + it "should pass convert_boolean_return as true if it was specified as true in a guard parameter" do + guard_parameters = {:cwd => '/etc/chef', :convert_boolean_return => true, :architecture => :x86_64} + allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:to_block).with( + {:convert_boolean_return => true, :code => "$true"}.merge(guard_parameters)).and_return(Proc.new {}) + resource.only_if("$true", guard_parameters) + end + + it "should pass convert_boolean_return as false if it was specified as true in a guard parameter" do + other_guard_parameters = {:cwd => '/etc/chef', :architecture => :x86_64} + parameters_with_boolean_disabled = other_guard_parameters.merge({:convert_boolean_return => false, :code => "$true"}) + allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:to_block).with( + parameters_with_boolean_disabled).and_return(Proc.new {}) + resource.only_if("$true", parameters_with_boolean_disabled) + end end context "as a script running in Windows-based scripting language" do |