Commit 718de5b0 authored by Adam Edwards's avatar Adam Edwards
Browse files

CR feedback: clean up convert boolean expressions and add tests

parent bada8aeb
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment