diff options
author | Adam Edwards <adamed@opscode.com> | 2014-03-28 14:08:27 -0700 |
---|---|---|
committer | Adam Edwards <adamed@opscode.com> | 2014-03-29 00:21:12 -0700 |
commit | dfe1ebf9bf59d02aa78c1fdef8787af1665a22fd (patch) | |
tree | 3df2b552435c442bf5f1e9b4d4562e969dc31998 | |
parent | e0b02d69aaf9f408cfd9dbe9ea47561f694815bf (diff) | |
download | chef-dfe1ebf9bf59d02aa78c1fdef8787af1665a22fd.tar.gz |
Fix namespacing issues caused by moving guard_interpreter logic out of Chef::Resource
-rw-r--r-- | lib/chef/guard_interpreter/default_guard_interpreter.rb (renamed from lib/chef/resource/conditional/default_guard_interpreter.rb) | 20 | ||||
-rw-r--r-- | lib/chef/guard_interpreter/resource_guard_interpreter.rb (renamed from lib/chef/resource/conditional/guard_interpreter.rb) | 20 | ||||
-rw-r--r-- | lib/chef/resource.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource/conditional.rb | 18 | ||||
-rw-r--r-- | spec/support/shared/unit/script_resource.rb | 4 | ||||
-rw-r--r-- | spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb (renamed from spec/unit/resource/conditional/guard_interpreter_spec.rb) | 6 | ||||
-rw-r--r-- | spec/unit/resource/powershell_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 2 |
8 files changed, 42 insertions, 36 deletions
diff --git a/lib/chef/resource/conditional/default_guard_interpreter.rb b/lib/chef/guard_interpreter/default_guard_interpreter.rb index 198db6a7aa..f7d039c1cf 100644 --- a/lib/chef/resource/conditional/default_guard_interpreter.rb +++ b/lib/chef/guard_interpreter/default_guard_interpreter.rb @@ -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 diff --git a/lib/chef/resource/conditional/guard_interpreter.rb b/lib/chef/guard_interpreter/resource_guard_interpreter.rb index 4f2fefb3cc..946ee2ba49 100644 --- a/lib/chef/resource/conditional/guard_interpreter.rb +++ b/lib/chef/guard_interpreter/resource_guard_interpreter.rb @@ -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 diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 7c191b700e..7d96b26b4b 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -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' diff --git a/lib/chef/resource/conditional.rb b/lib/chef/resource/conditional.rb index 94c4fe03f4..19881feb70 100644 --- a/lib/chef/resource/conditional.rb +++ b/lib/chef/resource/conditional.rb @@ -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 diff --git a/spec/support/shared/unit/script_resource.rb b/spec/support/shared/unit/script_resource.rb index 8a1f66b706..add99d45bd 100644 --- a/spec/support/shared/unit/script_resource.rb +++ b/spec/support/shared/unit/script_resource.rb @@ -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 diff --git a/spec/unit/resource/conditional/guard_interpreter_spec.rb b/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb index 3f49e341dc..a016cbfeb8 100644 --- a/spec/unit/resource/conditional/guard_interpreter_spec.rb +++ b/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb @@ -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 diff --git a/spec/unit/resource/powershell_spec.rb b/spec/unit/resource/powershell_spec.rb index 0d678e7a5a..52012a0f41 100644 --- a/spec/unit/resource/powershell_spec.rb +++ b/spec/unit/resource/powershell_spec.rb @@ -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 diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 8b46e56fcb..60f3bdb8ea 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -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 |