summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlutter <lutter@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-09 00:48:28 +0000
committerlutter <lutter@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-09 00:48:28 +0000
commitebcb6b6df7af42632a6c1beaa1b60171ff32b61e (patch)
tree2d82562a541862f20daf9e05f1a3642a27e4ffb1
parentba6257c02ef5e4bd32d58d40087f84dda95141c3 (diff)
downloadpuppet-ebcb6b6df7af42632a6c1beaa1b60171ff32b61e.tar.gz
puppet-ebcb6b6df7af42632a6c1beaa1b60171ff32b61e.tar.xz
puppet-ebcb6b6df7af42632a6c1beaa1b60171ff32b61e.zip
The template function now tries to first find a template within a module
(if the template path looks like it belongs to a module) and only when that fails looks for it in templatedir git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2277 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet/modules.rb30
-rw-r--r--lib/puppet/parser/templatewrapper.rb6
-rw-r--r--test/puppet/modules.rb17
3 files changed, 48 insertions, 5 deletions
diff --git a/lib/puppet/modules.rb b/lib/puppet/modules.rb
index cf611b82c..2d48a6a4e 100644
--- a/lib/puppet/modules.rb
+++ b/lib/puppet/modules.rb
@@ -1,6 +1,8 @@
# Support for modules
class Puppet::Module
+ TEMPLATES = "templates"
+
# Return an array of paths by splitting the +modulepath+ config
# parameter. Only consider paths that are absolute and existing
# directories
@@ -32,11 +34,39 @@ class Puppet::Module
# Instance methods
+ # Find the concrete file denoted by +file+. If +file+ is absolute,
+ # return it directly. Otherwise try to find it as a template in a
+ # module. If that fails, return it relative to the +templatedir+ config
+ # param.
+ # In all cases, an absolute path is returned, which does not
+ # necessarily refer to an existing file
+ def self.find_template(file)
+ if file =~ /^#{File::SEPARATOR}/
+ return file
+ end
+
+ mod = find(file)
+ if mod
+ return mod.template(file)
+ else
+ return File.join(Puppet[:templatedir], file)
+ end
+ end
+
attr_reader :name, :path
def initialize(name, path)
@name = name
@path = path
end
+ def strip(file)
+ n, rest = file.split(File::SEPARATOR, 2)
+ return rest
+ end
+
+ def template(file)
+ return File::join(path, TEMPLATES, strip(file))
+ end
+
private :initialize
end
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 62b45852b..3b8cc3a3a 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -7,11 +7,7 @@ class Puppet::Parser::TemplateWrapper
def initialize(scope, file)
@scope = scope
- if file =~ /^#{File::SEPARATOR}/
- @file = file
- else
- @file = File.join(Puppet[:templatedir], file)
- end
+ @file = Puppet::Module::find_template(file)
unless FileTest.exists?(@file)
raise Puppet::ParseError,
diff --git a/test/puppet/modules.rb b/test/puppet/modules.rb
index f3cf3b73c..e652182a8 100644
--- a/test/puppet/modules.rb
+++ b/test/puppet/modules.rb
@@ -38,4 +38,21 @@ class TestModules < Test::Unit::TestCase
assert_equal("testmod", mod.name)
assert_equal(path, mod.path)
end
+
+ def test_find_template
+ templ = "testmod/templ.erb"
+ assert_equal(File::join(Puppet[:templatedir], templ),
+ Puppet::Module::find_template(templ))
+
+ templ_path = File::join(@varmods, "testmod",
+ Puppet::Module::TEMPLATES, "templ.erb")
+ FileUtils::mkdir_p(File::dirname(templ_path))
+ File::open(templ_path, "w") { |f| f.puts "Howdy" }
+
+ assert_equal(templ_path, Puppet::Module::find_template(templ))
+
+ mod = Puppet::Module::find(templ)
+ assert_not_nil(mod)
+ assert_equal(templ_path, mod.template(templ))
+ end
end