summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/files.rb10
-rw-r--r--lib/puppet/parser/templatewrapper.rb7
-rw-r--r--spec/unit/parser/files.rb39
-rwxr-xr-xspec/unit/parser/templatewrapper.rb7
4 files changed, 38 insertions, 25 deletions
diff --git a/lib/puppet/parser/files.rb b/lib/puppet/parser/files.rb
index ca4fb4f10..749428e9f 100644
--- a/lib/puppet/parser/files.rb
+++ b/lib/puppet/parser/files.rb
@@ -41,7 +41,7 @@ module Puppet::Parser::Files
if template_paths = templatepath(environment)
# If we can find the template in :templatedir, we return that.
- td_file = template_paths.collect { |path|
+ template_paths.collect { |path|
File::join(path, template)
}.each do |f|
return f if FileTest.exist?(f)
@@ -49,11 +49,11 @@ module Puppet::Parser::Files
end
# check in the default template dir, if there is one
- unless td_file = find_template_in_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)
+ if td_file = find_template_in_module(template, environment)
+ return td_file
end
- td_file
+
+ return nil
end
def find_template_in_module(template, environment = nil)
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 8b333a0b6..00faf33a1 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -65,11 +65,8 @@ class Puppet::Parser::TemplateWrapper
end
def file=(filename)
- @file = Puppet::Parser::Files.find_template(filename, scope.compiler.environment)
-
- unless FileTest.exists?(file)
- raise Puppet::ParseError,
- "Could not find template %s" % file
+ unless @file = Puppet::Parser::Files.find_template(filename, scope.compiler.environment)
+ raise Puppet::ParseError, "Could not find template '%s'" % filename
end
# We'll only ever not have a parser in testing, but, eh.
diff --git a/spec/unit/parser/files.rb b/spec/unit/parser/files.rb
index a7bd37b44..2c91ea7e4 100644
--- a/spec/unit/parser/files.rb
+++ b/spec/unit/parser/files.rb
@@ -35,11 +35,6 @@ describe Puppet::Parser::Files do
Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
end
- it "should raise an error if no valid templatedir exists" do
- Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(nil)
- lambda { Puppet::Parser::Files.find_template("mytemplate") }.should raise_error
- end
-
it "should not raise an error if no valid templatedir exists and the template exists in a module" do
mod = mock 'module'
Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
@@ -50,40 +45,53 @@ describe Puppet::Parser::Files do
Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
end
- it "should use the main templatedir if no module is found" do
+ it "should return unqualified templates if they exist in the template dir" do
+ FileTest.stubs(:exist?).returns true
Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
- Puppet::Module.expects(:find).with("mymod", nil).returns(nil)
- Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
+ Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate"
end
- it "should return unqualified templates directly in the template dir" do
+ it "should only return templates if they actually exist" do
+ FileTest.expects(:exist?).with("/my/templates/mytemplate").returns true
Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
- Puppet::Module.expects(:find).never
Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate"
end
+ it "should return nil when asked for a template that doesn't exist" do
+ FileTest.expects(:exist?).with("/my/templates/mytemplate").returns false
+ Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
+ Puppet::Parser::Files.find_template("mytemplate").should be_nil
+ end
+
+ it "should search in the template directories before modules" do
+ FileTest.stubs(:exist?).returns true
+ Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"])
+ Puppet::Module.expects(:find).never
+ Puppet::Parser::Files.find_template("mytemplate")
+ end
+
it "should accept relative templatedirs" do
+ FileTest.stubs(:exist?).returns true
Puppet[:templatedir] = "my/templates"
File.expects(:directory?).with(File.join(Dir.getwd,"my/templates")).returns(true)
Puppet::Parser::Files.find_template("mytemplate").should == File.join(Dir.getwd,"my/templates/mytemplate")
end
it "should use the environment templatedir if no module is found and an environment is specified" do
+ FileTest.stubs(:exist?).returns true
Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates"])
- Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
end
it "should use first dir from environment templatedir if no module is found and an environment is specified" do
+ FileTest.stubs(:exist?).returns true
Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"])
- Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate"
end
it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do
Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(true)
- Puppet::Module.expects(:find).never
Puppet::Parser::Files.find_template("mytemplate").should == "/one/templates/mytemplate"
end
@@ -91,7 +99,6 @@ describe Puppet::Parser::Files do
Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
FileTest.expects(:exist?).with("/one/templates/mytemplate").returns(false)
FileTest.expects(:exist?).with("/two/templates/mytemplate").returns(true)
- Puppet::Module.expects(:find).never
Puppet::Parser::Files.find_template("mytemplate").should == "/two/templates/mytemplate"
end
@@ -104,6 +111,10 @@ describe Puppet::Parser::Files do
Puppet::Parser::Files.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
end
+ it "should return nil if no template can be found" do
+ Puppet::Parser::Files.find_template("foomod/envtemplate", "myenv").should be_nil
+ end
+
after { Puppet.settings.clear }
end
diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb
index ce458af75..a72595279 100755
--- a/spec/unit/parser/templatewrapper.rb
+++ b/spec/unit/parser/templatewrapper.rb
@@ -22,12 +22,17 @@ describe Puppet::Parser::TemplateWrapper do
it "should check template file existance and read its content" do
Puppet::Parser::Files.expects(:find_template).with("fake_template", "foo").returns("/tmp/fake_template")
- FileTest.expects(:exists?).with("/tmp/fake_template").returns(true)
File.expects(:read).with("/tmp/fake_template").returns("template content")
@tw.file = @file
end
+ it "should fail if a template cannot be found" do
+ Puppet::Parser::Files.expects(:find_template).with("fake_template", "foo").returns nil
+
+ lambda { @tw.file = @file }.should raise_error(Puppet::ParseError)
+ end
+
it "should turn into a string like template[name] for file based template" do
@tw.file = @file
@tw.to_s.should eql("template[/tmp/fake_template]")