diff options
author | Luke Kanies <luke@madstop.com> | 2009-02-17 17:15:17 -0600 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2009-02-18 22:38:44 -0600 |
commit | eec1caddf0dc431229ceb1741b357a9fefce5c38 (patch) | |
tree | 8ca642aca39d0e34882faef244e778bc263e2eb7 | |
parent | bdf3a80e29313008367d83f5a1ec6e4121e4ec74 (diff) | |
download | puppet-eec1caddf0dc431229ceb1741b357a9fefce5c38.tar.gz puppet-eec1caddf0dc431229ceb1741b357a9fefce5c38.tar.xz puppet-eec1caddf0dc431229ceb1741b357a9fefce5c38.zip |
Adding support for merging multiple filesets.
This is required for plugins, which recurse across multiple
directories.
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r-- | lib/puppet/file_serving/fileset.rb | 17 | ||||
-rwxr-xr-x | spec/unit/file_serving/fileset.rb | 43 |
2 files changed, 60 insertions, 0 deletions
diff --git a/lib/puppet/file_serving/fileset.rb b/lib/puppet/file_serving/fileset.rb index caad1b319..74a3c27fe 100644 --- a/lib/puppet/file_serving/fileset.rb +++ b/lib/puppet/file_serving/fileset.rb @@ -11,6 +11,23 @@ class Puppet::FileServing::Fileset attr_reader :path, :ignore, :links attr_accessor :recurse + # Produce a hash of files, with merged so that earlier files + # with the same postfix win. E.g., /dir1/subfile beats /dir2/subfile. + # It's a hash because we need to know the relative path of each file, + # and the base directory. + # This will probably only ever be used for searching for plugins. + def self.merge(*filesets) + result = {} + + filesets.each do |fileset| + fileset.files.each do |file| + result[file] ||= fileset.path + end + end + + result + end + # Return a list of all files in our fileset. This is different from the # normal definition of find in that we support specific levels # of recursion, which means we need to know when we're going another diff --git a/spec/unit/file_serving/fileset.rb b/spec/unit/file_serving/fileset.rb index 2100b30f7..505875be5 100755 --- a/spec/unit/file_serving/fileset.rb +++ b/spec/unit/file_serving/fileset.rb @@ -244,3 +244,46 @@ describe Puppet::FileServing::Fileset, " when ignoring" do @fileset.ignore?("my_file").should be_true end end + +describe Puppet::FileServing::Fileset, "when merging other filesets" do + before do + @paths = %w{/first/path /second/path /third/path} + + @filesets = @paths.collect do |path| + File.stubs(:lstat).with(path).returns stub("stat", :directory? => true) + Puppet::FileServing::Fileset.new(path, :recurse => true) + end + + Dir.stubs(:entries).returns [] + end + + it "should return a hash of all files in each fileset with the value being the base path" do + Dir.expects(:entries).with("/first/path").returns(%w{one uno}) + Dir.expects(:entries).with("/second/path").returns(%w{two dos}) + Dir.expects(:entries).with("/third/path").returns(%w{three tres}) + + Puppet::FileServing::Fileset.merge(*@filesets).should == { + "." => "/first/path", + "one" => "/first/path", + "uno" => "/first/path", + "two" => "/second/path", + "dos" => "/second/path", + "three" => "/third/path", + "tres" => "/third/path", + } + end + + it "should include the base directory from the first fileset" do + Dir.expects(:entries).with("/first/path").returns(%w{one}) + Dir.expects(:entries).with("/second/path").returns(%w{two}) + + Puppet::FileServing::Fileset.merge(*@filesets)["."].should == "/first/path" + end + + it "should use the base path of the first found file when relative file paths conflict" do + Dir.expects(:entries).with("/first/path").returns(%w{one}) + Dir.expects(:entries).with("/second/path").returns(%w{one}) + + Puppet::FileServing::Fileset.merge(*@filesets)["one"].should == "/first/path" + end +end |