summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-02-17 16:20:34 -0600
committerLuke Kanies <luke@madstop.com>2009-02-18 22:38:44 -0600
commitbdf3a80e29313008367d83f5a1ec6e4121e4ec74 (patch)
treecb096f05b24eee0cd8cb93e1d0a3c879da038b29
parenta7be174d249ca581d9ae849ba3823abc2c006b08 (diff)
downloadpuppet-bdf3a80e29313008367d83f5a1ec6e4121e4ec74.tar.gz
puppet-bdf3a80e29313008367d83f5a1ec6e4121e4ec74.tar.xz
puppet-bdf3a80e29313008367d83f5a1ec6e4121e4ec74.zip
Adding new methods to Puppet::Module.
There are now boolean methods to test whether a given kind of file is present in a given module. E.g, you can do: Puppet::Module.new("mod", "/my/path").plugins? There are also accessor-style methods that return the full path for a given kind of file. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/module.rb27
-rwxr-xr-xspec/unit/module.rb37
2 files changed, 57 insertions, 7 deletions
diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb
index b9a8b7d79..01459d7aa 100644
--- a/lib/puppet/module.rb
+++ b/lib/puppet/module.rb
@@ -4,6 +4,9 @@ class Puppet::Module
TEMPLATES = "templates"
FILES = "files"
MANIFESTS = "manifests"
+ PLUGINS = "plugins"
+
+ FILETYPES = [MANIFESTS, FILES, TEMPLATES, PLUGINS]
# Return an array of paths by splitting the +modulepath+ config
# parameter. Only consider paths that are absolute and existing
@@ -148,14 +151,24 @@ class Puppet::Module
@path = path
end
- def template(file)
- full_path = File::join(path, TEMPLATES, file)
- return nil unless full_path
- return full_path
- end
+ FILETYPES.each do |type|
+ # Create a method for returning the full path to a given
+ # file type's directory.
+ define_method(type.to_s) do
+ File.join(path, type.to_s)
+ end
+ # Create a boolean method for testing whether our module has
+ # files of a given type.
+ define_method(type.to_s + "?") do
+ FileTest.exist?(send(type))
+ end
- def files
- return File::join(path, FILES)
+ # Finally, a method for returning an individual file
+ define_method(type.to_s.sub(/s$/, '')) do |file|
+ path = File.join(send(type), file)
+ return nil unless FileTest.exist?(path)
+ return path
+ end
end
# Return the list of manifests matching the given glob pattern,
diff --git a/spec/unit/module.rb b/spec/unit/module.rb
index 70dd825d5..fd5f4fc19 100755
--- a/spec/unit/module.rb
+++ b/spec/unit/module.rb
@@ -2,6 +2,39 @@
require File.dirname(__FILE__) + '/../spec_helper'
+describe Puppet::Module do
+ [:plugins, :templates, :files, :manifests].each do |filetype|
+ it "should be able to indicate whether it has #{filetype}" do
+ Puppet::Module.new("foo", "/foo/bar").should respond_to(filetype.to_s + "?")
+ end
+
+ it "should correctly detect when it has #{filetype}" do
+ FileTest.expects(:exist?).with("/foo/bar/#{filetype}").returns true
+ Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s + "?").should be_true
+ end
+
+ it "should correctly detect when it does not have #{filetype}" do
+ FileTest.expects(:exist?).with("/foo/bar/#{filetype}").returns false
+ Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s + "?").should be_false
+ end
+
+ it "should have a method for returning the full path to the #{filetype}" do
+ Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s).should == File.join("/foo/bar", filetype.to_s)
+ end
+
+ it "should be able to return individual #{filetype}" do
+ path = File.join("/foo/bar", filetype.to_s, "my/file")
+ FileTest.expects(:exist?).with(path).returns true
+ Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s.sub(/s$/, ''), "my/file").should == path
+ end
+
+ it "should return nil if asked to return individual #{filetype} that don't exist" do
+ FileTest.expects(:exist?).with(File.join("/foo/bar", filetype.to_s, "my/file")).returns false
+ Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s.sub(/s$/, ''), "my/file").should be_nil
+ end
+ end
+end
+
describe Puppet::Module, " when building its search path" do
include PuppetTest
@@ -78,6 +111,8 @@ describe Puppet::Module, " when searching for templates" do
it "should return the template from the first found module" do
Puppet[:modulepath] = "/one:/two"
File.stubs(:directory?).returns(true)
+ FileTest.stubs(:exist?).returns false
+ FileTest.expects(:exist?).with("/one/mymod/templates/mytemplate").returns true
Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
end
@@ -151,6 +186,8 @@ describe Puppet::Module, " when searching for templates" do
Puppet.settings.stubs(:value).returns.returns("/my/directory")
Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/my/modules")
File.stubs(:directory?).returns(true)
+ FileTest.stubs(:exist?).returns false
+ FileTest.expects(:exist?).with("/my/modules/mymod/templates/envtemplate").returns true
Puppet::Module.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
end