Commit dfe1ebf9 authored by Adam Edwards's avatar Adam Edwards
Browse files

Fix namespacing issues caused by moving guard_interpreter logic out of Chef::Resource

parent e0b02d69
......@@ -17,18 +17,20 @@
#
class Chef
class DefaultGuardInterpreter
class GuardInterpreter
class DefaultGuardInterpreter
protected
def initialize
end
protected
def initialize
end
public
public
def translate_command_block(command, opts, &block)
[command, block]
def translate_command_block(command, opts, &block)
[command, block]
end
end
end
end
......@@ -16,18 +16,11 @@
# limitations under the License.
#
require 'chef/resource/conditional/default_guard_interpreter'
require 'chef/guard_interpreter/default_guard_interpreter'
class Chef
class GuardInterpreter < DefaultGuardInterpreter
def self.translate_command_block(parent_resource, command, opts, &block)
evaluator = parent_resource.guard_interpreter == :default ?
DefaultGuardInterpreter.new :
new(parent_resource.guard_interpreter, parent_resource)
evaluator.translate_command_block(command, opts, &block)
end
class GuardInterpreter
class ResourceGuardInterpreter < DefaultGuardInterpreter
def translate_command_block(command, opts, &block)
merge_inherited_attributes
......@@ -40,8 +33,6 @@ class Chef
end
end
protected
def initialize(resource_symbol, parent_resource)
@parent_resource = parent_resource
......@@ -59,6 +50,8 @@ class Chef
end
end
protected
def evaluate_action(action=nil, &block)
@resource.instance_eval(&block)
......@@ -81,8 +74,6 @@ class Chef
end
end
private
def get_resource_class(parent_resource, resource_symbol)
if parent_resource.nil? || parent_resource.node.nil?
raise ArgumentError, "Node for guard resource parent must not be nil"
......@@ -118,4 +109,5 @@ class Chef
end
end
end
end
end
......@@ -23,7 +23,7 @@ require 'chef/dsl/data_query'
require 'chef/dsl/registry_helper'
require 'chef/dsl/reboot_pending'
require 'chef/mixin/convert_to_class_name'
require 'chef/resource/conditional/guard_interpreter'
require 'chef//guard_interpreter/resource_guard_interpreter'
require 'chef/resource/conditional'
require 'chef/resource/conditional_action_not_nothing'
require 'chef/resource_collection'
......
......@@ -17,7 +17,7 @@
#
require 'chef/mixin/shell_out'
require 'chef/resource/conditional/guard_interpreter'
require 'chef/guard_interpreter/resource_guard_interpreter'
class Chef
class Resource
......@@ -31,12 +31,12 @@ class Chef
end
def self.not_if(parent_resource, command=nil, command_opts={}, &block)
translated_command, translated_block = Chef::GuardInterpreter.translate_command_block(parent_resource, command, command_opts, &block)
translated_command, translated_block = translate_command_block(parent_resource, command, command_opts, &block)
new(:not_if, translated_command, command_opts, &translated_block)
end
def self.only_if(parent_resource, command=nil, command_opts={}, &block)
translated_command, translated_block = Chef::GuardInterpreter.translate_command_block(parent_resource, command, command_opts, &block)
translated_command, translated_block = translate_command_block(parent_resource, command, command_opts, &block)
new(:only_if, translated_command, command_opts, &translated_block)
end
......@@ -103,6 +103,18 @@ class Chef
end
end
def self.translate_command_block(parent_resource, command, opts, &block)
guard_interpreter = nil
if parent_resource.guard_interpreter == :default
guard_interpreter = Chef::GuardInterpreter::DefaultGuardInterpreter.new
else
guard_interpreter = Chef::GuardInterpreter::ResourceGuardInterpreter.new(parent_resource.guard_interpreter, parent_resource)
end
guard_interpreter.translate_command_block(command, opts, &block)
end
end
end
end
......@@ -66,14 +66,14 @@ shared_examples_for "a script resource" do
it "when guard_interpreter is set to the default value, the guard command string should be evaluated by command execution and not through a resource" do
Chef::Resource::Conditional.any_instance.should_not_receive(:evaluate_block)
Chef::Resource::Conditional.any_instance.should_receive(:evaluate_command).and_return(true)
Chef::GuardInterpreter.any_instance.should_not_receive(:evaluate_action)
Chef::GuardInterpreter::ResourceGuardInterpreter.any_instance.should_not_receive(:evaluate_action)
resource.only_if 'echo hi'
resource.should_skip?(:run).should == nil
end
it "when a valid guard_interpreter resource is specified, a block should be used to evaluate the guard" do
Chef::Resource::Conditional.any_instance.should_not_receive(:evaluate_command)
Chef::GuardInterpreter.any_instance.should_receive(:evaluate_action).and_return(true)
Chef::GuardInterpreter::ResourceGuardInterpreter.any_instance.should_receive(:evaluate_action).and_return(true)
resource.guard_interpreter :script
resource.only_if 'echo hi'
resource.should_skip?(:run).should == nil
......
......@@ -18,7 +18,7 @@
require 'spec_helper'
describe Chef::Resource::Conditional::GuardInterpreter do
describe Chef::GuardInterpreter::ResourceGuardInterpreter do
before(:each) do
node = Chef::Node.new
......@@ -37,13 +37,13 @@ describe Chef::Resource::Conditional::GuardInterpreter do
it "should allow guard interpreter to be set to Chef::Resource::Script" do
resource.guard_interpreter(:script)
allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
resource.only_if("echo hi")
end
it "should allow guard interpreter to be set to Chef::Resource::PowershellScript derived indirectly from Chef::Resource::Script" do
resource.guard_interpreter(:powershell_script)
allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
resource.only_if("echo hi")
end
......
......@@ -45,19 +45,19 @@ describe Chef::Resource::PowershellScript do
it "should allow guard interpreter to be set to Chef::Resource::Script" do
resource.guard_interpreter(:script)
allow_any_instance_of(Chef::GuardInterpreter).to receive(:evaluate_action).and_return(false)
allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
resource.only_if("echo hi")
end
it "should allow guard interpreter to be set to Chef::Resource::Bash derived from Chef::Resource::Script" do
resource.guard_interpreter(:bash)
allow_any_instance_of(Chef::GuardInterpreter).to receive(:evaluate_action).and_return(false)
allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
resource.only_if("echo hi")
end
it "should allow guard interpreter to be set to Chef::Resource::PowershellScript derived indirectly from Chef::Resource::Script" do
resource.guard_interpreter(:powershell_script)
allow_any_instance_of(Chef::GuardInterpreter).to receive(:evaluate_action).and_return(false)
allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
resource.only_if("echo hi")
end
end
......
......@@ -543,7 +543,7 @@ describe Chef::Resource do
end
it "should not raise an exception when setting the guard interpreter attribute to a Symbol" do
Chef::GuardInterpreter.stub(:new).and_return(nil)
Chef::GuardInterpreter::ResourceGuardInterpreter.stub(:new).and_return(nil)
expect { resource.guard_interpreter(:command_dot_com) }.not_to raise_error
end
end
......
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