diff options
-rw-r--r-- | lib/chef/resource/remote_file.rb | 26 | ||||
-rw-r--r-- | spec/unit/resource/remote_file_spec.rb | 29 |
2 files changed, 49 insertions, 6 deletions
diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb index 24d2562a9b..a10a0996a6 100644 --- a/lib/chef/resource/remote_file.rb +++ b/lib/chef/resource/remote_file.rb @@ -41,12 +41,24 @@ class Chef end def source(*args) - if not args.empty? - args = Array(args).flatten - validate_source(args) - @source = args - elsif self.instance_variable_defined?(:@source) == true - @source + arg = if args.empty? + nil + elsif args[0].is_a?(Chef::DelayedEvaluator) && args.count == 1 + args[0] + elsif args.any? {|a| a.is_a?(Chef::DelayedEvaluator)} && args.count > 1 + raise Exceptions::InvalidRemoteFileURI, "Only 1 DelayedEvaluator is allowed" + else + Array(args).flatten + end + ret = set_or_return(:source, + arg, + { :callbacks => { + :validate_source => method(:validate_source) + }}) + if ret.is_a? String + Array(ret) + else + ret end end @@ -107,6 +119,7 @@ class Chef private def validate_source(source) + source = Array(source).flatten raise ArgumentError, "#{resource_name} has an empty source" if source.empty? source.each do |src| unless absolute_uri?(src) @@ -114,6 +127,7 @@ class Chef "#{src.inspect} is not a valid `source` parameter for #{resource_name}. `source` must be an absolute URI or an array of URIs." end end + true end def absolute_uri?(source) diff --git a/spec/unit/resource/remote_file_spec.rb b/spec/unit/resource/remote_file_spec.rb index 643bc8ba21..8f1633119d 100644 --- a/spec/unit/resource/remote_file_spec.rb +++ b/spec/unit/resource/remote_file_spec.rb @@ -50,20 +50,49 @@ describe Chef::Resource::RemoteFile do @resource.source.should eql([ "http://opscode.com/" ]) end + it "should accept a delayed evalutator (string) for the remote file source" do + @resource.source Chef::DelayedEvaluator.new {"http://opscode.com/"} + @resource.source.should eql([ "http://opscode.com/" ]) + end + it "should accept an array of URIs for the remote file source" do @resource.source([ "http://opscode.com/", "http://puppetlabs.com/" ]) @resource.source.should eql([ "http://opscode.com/", "http://puppetlabs.com/" ]) end + it "should accept a delated evaluator (array) for the remote file source" do + @resource.source Chef::DelayedEvaluator.new { [ "http://opscode.com/", "http://puppetlabs.com/" ] } + @resource.source.should eql([ "http://opscode.com/", "http://puppetlabs.com/" ]) + end + it "should accept an multiple URIs as arguments for the remote file source" do @resource.source("http://opscode.com/", "http://puppetlabs.com/") @resource.source.should eql([ "http://opscode.com/", "http://puppetlabs.com/" ]) end + it "should only accept a single argument if a delayed evalutor is used" do + lambda { + @resource.source("http://opscode.com/", Chef::DelayedEvaluator.new {"http://opscode.com/"}) + }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) + end + + it "should only accept a single array item if a delayed evalutor is used" do + lambda { + @resource.source(["http://opscode.com/", Chef::DelayedEvaluator.new {"http://opscode.com/"}]) + }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) + end + it "does not accept a non-URI as the source" do lambda { @resource.source("not-a-uri") }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) end + it "does not accept a non-URI as the source when read from a delayed evaluator" do + lambda { + @resource.source(Chef::DelayedEvaluator.new {"not-a-uri"}) + @resource.source + }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) + end + it "should raise an exception when source is an empty array" do lambda { @resource.source([]) }.should raise_error(ArgumentError) end |