summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/file_serving/fileset.rb36
-rw-r--r--lib/puppet/file_serving/terminus_helper.rb13
-rwxr-xr-xspec/unit/file_serving/fileset.rb36
-rwxr-xr-xspec/unit/file_serving/terminus_helper.rb25
4 files changed, 70 insertions, 40 deletions
diff --git a/lib/puppet/file_serving/fileset.rb b/lib/puppet/file_serving/fileset.rb
index 74a3c27fe..61ca897e1 100644
--- a/lib/puppet/file_serving/fileset.rb
+++ b/lib/puppet/file_serving/fileset.rb
@@ -68,13 +68,10 @@ class Puppet::FileServing::Fileset
@links = :manage
@recurse = false
- options.each do |option, value|
- method = option.to_s + "="
- begin
- send(method, value)
- rescue NoMethodError
- raise ArgumentError, "Invalid option '%s'" % option
- end
+ if options.is_a?(Puppet::Indirector::Request)
+ initialize_from_request(options)
+ else
+ initialize_from_hash(options)
end
raise ArgumentError.new("Fileset paths must exist") unless stat = stat(path)
@@ -102,6 +99,31 @@ class Puppet::FileServing::Fileset
# Else, return false.
return false
end
+
+ def initialize_from_hash(options)
+ options.each do |option, value|
+ method = option.to_s + "="
+ begin
+ send(method, value)
+ rescue NoMethodError
+ raise ArgumentError, "Invalid option '%s'" % option
+ end
+ end
+ end
+
+ def initialize_from_request(request)
+ [:links, :ignore, :recurse].each do |param|
+ if request.options.include?(param) # use 'include?' so the values can be false
+ value = request.options[param]
+ elsif request.options.include?(param.to_s)
+ value = request.options[param.to_s]
+ end
+ next if value.nil?
+ value = true if value == "true"
+ value = false if value == "false"
+ send(param.to_s + "=", value)
+ end
+ end
private
diff --git a/lib/puppet/file_serving/terminus_helper.rb b/lib/puppet/file_serving/terminus_helper.rb
index 5b993c9e0..c88bacc4a 100644
--- a/lib/puppet/file_serving/terminus_helper.rb
+++ b/lib/puppet/file_serving/terminus_helper.rb
@@ -9,18 +9,9 @@ require 'puppet/file_serving/fileset'
module Puppet::FileServing::TerminusHelper
# Create model instances for all files in a fileset.
def path2instances(request, *paths)
- args = [:links, :ignore, :recurse].inject({}) do |hash, param|
- if request.options.include?(param) # use 'include?' so the values can be false
- hash[param] = request.options[param]
- elsif request.options.include?(param.to_s)
- hash[param] = request.options[param.to_s]
- end
- hash[param] = true if hash[param] == "true"
- hash[param] = false if hash[param] == "false"
- hash
- end
filesets = paths.collect do |path|
- Puppet::FileServing::Fileset.new(path, args)
+ # Filesets support indirector requests as an options collection
+ Puppet::FileServing::Fileset.new(path, request)
end
Puppet::FileServing::Fileset.merge(*filesets).collect do |file, base_path|
diff --git a/spec/unit/file_serving/fileset.rb b/spec/unit/file_serving/fileset.rb
index 505875be5..dfba9c1a1 100755
--- a/spec/unit/file_serving/fileset.rb
+++ b/spec/unit/file_serving/fileset.rb
@@ -54,6 +54,42 @@ describe Puppet::FileServing::Fileset, " when initializing" do
File.expects(:lstat).with("/some/file").returns stub("stat")
Puppet::FileServing::Fileset.new("/some/file").links.should == :manage
end
+
+ it "should support using an Indirector Request for its options" do
+ File.expects(:lstat).with("/some/file").returns stub("stat")
+ request = Puppet::Indirector::Request.new(:file_serving, :find, "foo")
+ lambda { Puppet::FileServing::Fileset.new("/some/file", request) }.should_not raise_error
+ end
+
+ describe "using an indirector request" do
+ before do
+ File.stubs(:lstat).returns stub("stat")
+ @values = {:links => :manage, :ignore => %w{a b}, :recurse => true}
+ @request = Puppet::Indirector::Request.new(:file_serving, :find, "foo")
+ end
+
+ [:recurse, :ignore, :links].each do |option|
+ it "should pass :recurse, :ignore, and :links settings on to the fileset if present" do
+ @request.stubs(:options).returns(option => @values[option])
+ Puppet::FileServing::Fileset.new("/my/file", @request).send(option).should == @values[option]
+ end
+
+ it "should pass :recurse, :ignore, and :links settings on to the fileset if present with the keys stored as strings" do
+ @request.stubs(:options).returns(option.to_s => @values[option])
+ Puppet::FileServing::Fileset.new("/my/file", @request).send(option).should == @values[option]
+ end
+ end
+
+ it "should convert the string 'true' to the boolean true when setting options" do
+ @request.stubs(:options).returns(:recurse => "true")
+ Puppet::FileServing::Fileset.new("/my/file", @request).recurse.should == true
+ end
+
+ it "should convert the string 'false' to the boolean false when setting options" do
+ @request.stubs(:options).returns(:recurse => "false")
+ Puppet::FileServing::Fileset.new("/my/file", @request).recurse.should == false
+ end
+ end
end
describe Puppet::FileServing::Fileset, " when determining whether to recurse" do
diff --git a/spec/unit/file_serving/terminus_helper.rb b/spec/unit/file_serving/terminus_helper.rb
index c79261eca..d0c06f126 100755
--- a/spec/unit/file_serving/terminus_helper.rb
+++ b/spec/unit/file_serving/terminus_helper.rb
@@ -23,7 +23,7 @@ describe Puppet::FileServing::TerminusHelper do
it "should use a fileset to find paths" do
@fileset = stub 'fileset', :files => [], :path => "/my/files"
- Puppet::FileServing::Fileset.expects(:new).with("/my/file", {}).returns(@fileset)
+ Puppet::FileServing::Fileset.expects(:new).with { |key, options| key == "/my/file" }.returns(@fileset)
@helper.path2instances(@request, "/my/file")
end
@@ -38,27 +38,8 @@ describe Puppet::FileServing::TerminusHelper do
@helper.path2instances(@request, "/first/file", "/second/file")
end
- it "should pass :recurse, :ignore, and :links settings on to the fileset if present" do
- Puppet::FileServing::Fileset.expects(:new).with { |path, options| options == {:links => :a, :ignore => :b, :recurse => :c } }.returns(@fileset)
- @request.stubs(:options).returns(:links => :a, :ignore => :b, :recurse => :c)
- @helper.path2instances(@request, "/my/file")
- end
-
- it "should pass :recurse, :ignore, and :links settings on to the fileset if present with the keys stored as strings" do
- Puppet::FileServing::Fileset.expects(:new).with { |path, options| options == {:links => :a, :ignore => :b, :recurse => :c} }.returns(@fileset)
- @request.stubs(:options).returns("links" => :a, "ignore" => :b, "recurse" => :c)
- @helper.path2instances(@request, "/my/file")
- end
-
- it "should convert the string 'true' to the boolean true when setting options" do
- Puppet::FileServing::Fileset.expects(:new).with { |path, options| options[:recurse] == true }.returns(@fileset)
- @request.stubs(:options).returns(:recurse => "true")
- @helper.path2instances(@request, "/my/file")
- end
-
- it "should convert the string 'false' to the boolean false when setting options" do
- Puppet::FileServing::Fileset.expects(:new).with { |path, options| options[:recurse] == false }.returns(@fileset)
- @request.stubs(:options).returns(:recurse => "false")
+ it "should pass the indirection request to the Fileset at initialization" do
+ Puppet::FileServing::Fileset.expects(:new).with { |path, options| options == @request }.returns @fileset
@helper.path2instances(@request, "/my/file")
end