summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/module.rb40
-rwxr-xr-xspec/unit/module.rb10
2 files changed, 23 insertions, 27 deletions
diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb
index ab1bc75bd..b9a8b7d79 100644
--- a/lib/puppet/module.rb
+++ b/lib/puppet/module.rb
@@ -37,9 +37,8 @@ class Puppet::Module
# absolute, or if there is no module whose name is the first component
# of +path+, return +nil+
def self.find(modname, environment = nil)
- if modname =~ %r/^#{File::SEPARATOR}/
- return nil
- end
+ # Modules shouldn't be fully qualified.
+ return nil if modname =~ %r/^#{File::SEPARATOR}/
modpath = modulepath(environment).collect { |path|
File::join(path, modname)
@@ -77,20 +76,17 @@ class Puppet::Module
return template
end
- template_paths = templatepath(environment)
- if template_paths
+ if template_paths = templatepath(environment)
# If we can find the template in :templatedir, we return that.
td_file = template_paths.collect { |path|
File::join(path, template)
- }.find { |f| File.exists?(f) }
-
- return td_file unless td_file == nil
+ }.each do |f|
+ return f if FileTest.exist?(f)
+ end
end
- td_file = find_template_for_module(template, environment)
-
# check in the default template dir, if there is one
- if td_file.nil?
+ unless td_file = find_template_for_module(template, environment)
raise Puppet::Error, "No valid template directory found, please check templatedir settings" if template_paths.nil?
td_file = File::join(template_paths.first, template)
end
@@ -103,15 +99,16 @@ class Puppet::Module
# Because templates don't have an assumed template name, like manifests do,
# we treat templates with no name as being templates in the main template
# directory.
- if not file.nil?
- mod = find(path, environment)
- if mod
- return mod.template(file)
- end
+ return nil unless file
+
+ if mod = find(path, environment) and t = mod.template(file)
+ return t
end
nil
end
+ private_class_method :find_template_for_module
+
# Return a list of manifests (as absolute filenames) that match +pat+
# with the current directory set to +cwd+. If the first component of
# +pat+ does not contain any wildcards and is an existing module, return
@@ -121,7 +118,7 @@ class Puppet::Module
cwd = options[:cwd] || Dir.getwd
module_name, pattern = split_path(start)
if module_name and mod = find(module_name, options[:environment])
- return mod.manifests(pattern)
+ return mod.match_manifests(pattern)
else
abspat = File::expand_path(start, cwd)
files = Dir.glob(abspat).reject { |f| FileTest.directory?(f) }
@@ -152,7 +149,9 @@ class Puppet::Module
end
def template(file)
- return File::join(path, TEMPLATES, file)
+ full_path = File::join(path, TEMPLATES, file)
+ return nil unless full_path
+ return full_path
end
def files
@@ -161,7 +160,7 @@ class Puppet::Module
# Return the list of manifests matching the given glob pattern,
# defaulting to 'init.pp' for empty modules.
- def manifests(rest)
+ def match_manifests(rest)
rest ||= "init.pp"
p = File::join(path, MANIFESTS, rest)
files = Dir.glob(p).reject { |f| FileTest.directory?(f) }
@@ -170,7 +169,4 @@ class Puppet::Module
end
return files
end
-
- private :initialize
- private_class_method :find_template_for_module
end
diff --git a/spec/unit/module.rb b/spec/unit/module.rb
index 1deaec232..70dd825d5 100755
--- a/spec/unit/module.rb
+++ b/spec/unit/module.rb
@@ -85,7 +85,7 @@ describe Puppet::Module, " when searching for templates" do
Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
Puppet[:modulepath] = "/one:/two"
File.stubs(:directory?).returns(true)
- File.stubs(:exists?).returns(true)
+ FileTest.stubs(:exist?).returns(true)
Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
end
@@ -98,7 +98,7 @@ describe Puppet::Module, " when searching for templates" do
Puppet::Module.stubs(:templatepath).with(nil).returns(nil)
Puppet[:modulepath] = "/one:/two"
File.stubs(:directory?).returns(true)
- File.stubs(:exists?).returns(true)
+ FileTest.stubs(:exist?).returns(true)
Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
end
@@ -134,15 +134,15 @@ describe Puppet::Module, " when searching for templates" do
it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do
Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
- File.expects(:exists?).with("/one/templates/mytemplate").returns(true)
+ FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(true)
Puppet::Module.expects(:find).never
Puppet::Module.find_template("mytemplate").should == "/one/templates/mytemplate"
end
it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" do
Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
- File.expects(:exists?).with("/one/templates/mytemplate").returns(false)
- File.expects(:exists?).with("/two/templates/mytemplate").returns(true)
+ FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(false)
+ FileTest.expects(:exist?).with("/two/templates/mytemplate").returns(true)
Puppet::Module.expects(:find).never
Puppet::Module.find_template("mytemplate").should == "/two/templates/mytemplate"
end