diff options
author | Jacob Helwig <jacob@puppetlabs.com> | 2011-04-22 11:14:36 -0700 |
---|---|---|
committer | Jacob Helwig <jacob@puppetlabs.com> | 2011-04-22 11:14:36 -0700 |
commit | 89620ab51997e032ccde218de13acab9f55da4b8 (patch) | |
tree | e018525ded746d7d74018bf3c86a47a333536b96 | |
parent | 2591a539481710e228e96c57b85434c1b9951c15 (diff) | |
parent | f21162bdbae516d2613052d35a2f8bbd34d0bf86 (diff) | |
download | puppet-89620ab51997e032ccde218de13acab9f55da4b8.tar.gz puppet-89620ab51997e032ccde218de13acab9f55da4b8.tar.xz puppet-89620ab51997e032ccde218de13acab9f55da4b8.zip |
Merge branch 'tickets/2.7.next/6368-files-autorequire-nearest-ancestor-directory' into 2.7.next
* tickets/2.7.next/6368-files-autorequire-nearest-ancestor-directory:
(#6368) Make the File type autorequire its nearest ancestor directory
-rw-r--r-- | lib/puppet/type/file.rb | 10 | ||||
-rwxr-xr-x | spec/unit/type/file_spec.rb | 57 |
2 files changed, 65 insertions, 2 deletions
diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index 715836b22..1790c5e92 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -257,11 +257,17 @@ Puppet::Type.newtype(:file) do newvalues(:first, :all) end - # Autorequire any parent directories. + # Autorequire the nearest ancestor directory found in the catalog. autorequire(:file) do basedir = ::File.dirname(self[:path]) if basedir != self[:path] - basedir + parents = [] + until basedir == parents.last + parents << basedir + basedir = File.dirname(basedir) + end + # The filename of the first ancestor found, or nil + parents.find { |dir| catalog.resource(:file, dir) } else nil end diff --git a/spec/unit/type/file_spec.rb b/spec/unit/type/file_spec.rb index 683c3654b..6cd3ab701 100755 --- a/spec/unit/type/file_spec.rb +++ b/spec/unit/type/file_spec.rb @@ -168,6 +168,25 @@ describe Puppet::Type.type(:file) do reqs[0].target.must == file end + it "should autorequire its nearest ancestor directory" do + file = Puppet::Type::File.new(:path => "/foo/bar/baz") + dir = Puppet::Type::File.new(:path => "/foo") + root = Puppet::Type::File.new(:path => "/") + @catalog.add_resource file + @catalog.add_resource dir + @catalog.add_resource root + reqs = file.autorequire + reqs.length.must == 1 + reqs[0].source.must == dir + reqs[0].target.must == file + end + + it "should not autorequire anything when there is no nearest ancestor directory" do + file = Puppet::Type::File.new(:path => "/foo/bar/baz") + @catalog.add_resource file + file.autorequire.should be_empty + end + it "should not autorequire its parent dir if its parent dir is itself" do file = Puppet::Type::File.new(:path => "/") @catalog.add_resource file @@ -241,6 +260,25 @@ describe Puppet::Type.type(:file) do reqs[0].target.must == file end + it "should autorequire its nearest ancestor directory" do + file = Puppet::Type::File.new(:path => "X:/foo/bar/baz") + dir = Puppet::Type::File.new(:path => "X:/foo") + root = Puppet::Type::File.new(:path => "X:/") + @catalog.add_resource file + @catalog.add_resource dir + @catalog.add_resource root + reqs = file.autorequire + reqs.length.must == 1 + reqs[0].source.must == dir + reqs[0].target.must == file + end + + it "should not autorequire anything when there is no nearest ancestor directory" do + file = Puppet::Type::File.new(:path => "X:/foo/bar/baz") + @catalog.add_resource file + file.autorequire.should be_empty + end + it "should not autorequire its parent dir if its parent dir is itself" do file = Puppet::Type::File.new(:path => "X:/") @catalog.add_resource file @@ -302,6 +340,25 @@ describe Puppet::Type.type(:file) do reqs[0].target.must == file end + it "should autorequire its nearest ancestor directory" do + file = Puppet::Type::File.new(:path => "//server/foo/bar/baz/qux") + dir = Puppet::Type::File.new(:path => "//server/foo/bar") + root = Puppet::Type::File.new(:path => "//server/foo") + @catalog.add_resource file + @catalog.add_resource dir + @catalog.add_resource root + reqs = file.autorequire + reqs.length.must == 1 + reqs[0].source.must == dir + reqs[0].target.must == file + end + + it "should not autorequire anything when there is no nearest ancestor directory" do + file = Puppet::Type::File.new(:path => "//server/foo/bar/baz/qux") + @catalog.add_resource file + file.autorequire.should be_empty + end + it "should not autorequire its parent dir if its parent dir is itself" do file = Puppet::Type::File.new(:path => "//server/foo") @catalog.add_resource file |