summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-04-17 12:47:41 -0500
committerJames Turnbull <james@lovedthanlost.net>2009-05-15 10:01:26 +1000
commitc6084093e67b1e415e49c192b3ac6f6b9aebc4ba (patch)
treedcff2f7076b8e5f16d727e926bf3c0b72e45e215
parent83ba0e5efe4514201c1a627ceffdaef992431734 (diff)
Moving file-searching code out of Puppet::Module
The Module class had a bunch of code for finding manifests and templates even when not in a module, and it complicated the class unnecessarily. This moves that code to a new, hackish-but-sufficient module for just that purpose. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/module.rb89
-rw-r--r--lib/puppet/parser/files.rb94
-rw-r--r--lib/puppet/parser/parser_support.rb3
-rw-r--r--lib/puppet/parser/templatewrapper.rb5
-rwxr-xr-xspec/unit/module.rb179
-rw-r--r--spec/unit/parser/files.rb194
-rwxr-xr-xspec/unit/parser/templatewrapper.rb4
7 files changed, 296 insertions, 272 deletions
diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb
index 45b40698b..e4bdd16e6 100644
--- a/lib/puppet/module.rb
+++ b/lib/puppet/module.rb
@@ -37,15 +37,6 @@ class Puppet::Module
Puppet::Node::Environment.new(environment).modulepath
end
- # Return an array of paths by splitting the +templatedir+ config
- # parameter.
- def self.templatepath(environment = nil)
- dirs = Puppet.settings.value(:templatedir, environment).split(":")
- dirs.select do |p|
- File::directory?(p)
- end
- end
-
# Find and return the +module+ that +path+ belongs to. If +path+ is
# absolute, or if there is no module whose name is the first component
# of +path+, return +nil+
@@ -53,85 +44,6 @@ class Puppet::Module
Puppet::Node::Environment.new(environment).module(modname)
end
- # 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(template, environment = nil)
- if template =~ /^#{File::SEPARATOR}/
- return template
- end
-
- 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)
- }.each do |f|
- return f if FileTest.exist?(f)
- end
- end
-
- # check in the default template dir, if there is one
- 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
- td_file
- end
-
- def self.find_template_for_module(template, environment = nil)
- path, file = split_path(template)
-
- # 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.
- 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
- # a list of manifests in that module matching the rest of +pat+
- # Otherwise, try to find manifests matching +pat+ relative to +cwd+
- def self.find_manifests(start, options = {})
- cwd = options[:cwd] || Dir.getwd
- module_name, pattern = split_path(start)
- if module_name and mod = find(module_name, options[:environment])
- return mod.match_manifests(pattern)
- else
- abspat = File::expand_path(start, cwd)
- files = Dir.glob(abspat).reject { |f| FileTest.directory?(f) }
- if files.size == 0
- files = Dir.glob(abspat + ".pp").reject { |f| FileTest.directory?(f) }
- end
- return files
- end
- end
-
- # Split the path into the module and the rest of the path.
- # This method can and often does return nil, so anyone calling
- # it needs to handle that.
- def self.split_path(path)
- if path =~ %r/^#{File::SEPARATOR}/
- return nil
- end
-
- modname, rest = path.split(File::SEPARATOR, 2)
- return nil if modname.nil? || modname.empty?
- return modname, rest
- end
-
attr_reader :name, :path
def initialize(name, path)
@name = name
@@ -144,6 +56,7 @@ class Puppet::Module
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
diff --git a/lib/puppet/parser/files.rb b/lib/puppet/parser/files.rb
new file mode 100644
index 000000000..2cd163f97
--- /dev/null
+++ b/lib/puppet/parser/files.rb
@@ -0,0 +1,94 @@
+require 'puppet/module'
+require 'puppet/parser/parser'
+
+# This is a silly central module for finding
+# different kinds of files while parsing. This code
+# doesn't really belong in the Puppet::Module class,
+# but it doesn't really belong anywhere else, either.
+module Puppet::Parser::Files
+ module_function
+
+ # 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
+ # a list of manifests in that module matching the rest of +pat+
+ # Otherwise, try to find manifests matching +pat+ relative to +cwd+
+ def find_manifests(start, options = {})
+ cwd = options[:cwd] || Dir.getwd
+ module_name, pattern = split_file_path(start)
+ if module_name and mod = Puppet::Module.find(module_name, options[:environment])
+ return mod.match_manifests(pattern)
+ else
+ abspat = File::expand_path(start, cwd)
+ files = Dir.glob(abspat).reject { |f| FileTest.directory?(f) }
+ if files.size == 0
+ files = Dir.glob(abspat + ".pp").reject { |f| FileTest.directory?(f) }
+ end
+ return files
+ end
+ end
+
+ # 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 find_template(template, environment = nil)
+ if template =~ /^#{File::SEPARATOR}/
+ return template
+ end
+
+ 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)
+ }.each do |f|
+ return f if FileTest.exist?(f)
+ end
+ 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)
+ end
+ td_file
+ end
+
+ def find_template_in_module(template, environment = nil)
+ path, file = split_file_path(template)
+
+ # 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.
+ return nil unless file
+
+ if mod = Puppet::Module.find(path, environment) and t = mod.template(file)
+ return t
+ end
+ nil
+ end
+
+ # Return an array of paths by splitting the +templatedir+ config
+ # parameter.
+ def templatepath(environment = nil)
+ dirs = Puppet.settings.value(:templatedir, environment).split(":")
+ dirs.select do |p|
+ File::directory?(p)
+ end
+ end
+
+ # Split the path into the module and the rest of the path.
+ # This method can and often does return nil, so anyone calling
+ # it needs to handle that.
+ def split_file_path(path)
+ if path =~ %r/^#{File::SEPARATOR}/
+ return nil
+ end
+
+ modname, rest = path.split(File::SEPARATOR, 2)
+ return nil if modname.nil? || modname.empty?
+ return modname, rest
+ end
+end
diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb
index d59093799..3a6853f27 100644
--- a/lib/puppet/parser/parser_support.rb
+++ b/lib/puppet/parser/parser_support.rb
@@ -2,6 +2,7 @@
# tired of rebuilding the parser.rb file all the time.
class Puppet::Parser::Parser
require 'puppet/parser/functions'
+ require 'puppet/parser/files'
ASTSet = Struct.new(:classes, :definitions, :nodes)
@@ -183,7 +184,7 @@ class Puppet::Parser::Parser
"in file #{@lexer.file} at line #{@lexer.line}"
)
end
- files = Puppet::Module::find_manifests(pat, :cwd => dir, :environment => @environment)
+ files = Puppet::Parser::Files.find_manifests(pat, :cwd => dir, :environment => @environment)
if files.size == 0
raise Puppet::ImportError.new("No file(s) found for import " +
"of '#{pat}'")
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 55c7745ba..8b333a0b6 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -1,5 +1,7 @@
# A simple wrapper for templates, so they don't have full access to
# the scope objects.
+require 'puppet/parser/files'
+
class Puppet::Parser::TemplateWrapper
attr_accessor :scope, :file, :string
include Puppet::Util
@@ -63,7 +65,7 @@ class Puppet::Parser::TemplateWrapper
end
def file=(filename)
- @file = Puppet::Module::find_template(filename, scope.compiler.environment)
+ @file = Puppet::Parser::Files.find_template(filename, scope.compiler.environment)
unless FileTest.exists?(file)
raise Puppet::ParseError,
@@ -113,4 +115,3 @@ class Puppet::Parser::TemplateWrapper
"template[%s]" % (file ? file : "inline")
end
end
-
diff --git a/spec/unit/module.rb b/spec/unit/module.rb
index 313de6761..17c2065eb 100755
--- a/spec/unit/module.rb
+++ b/spec/unit/module.rb
@@ -164,185 +164,6 @@ describe Puppet::Module, " when searching for modules" do
end
end
-describe Puppet::Module, " when searching for templates" do
- it "should return fully-qualified templates directly" do
- Puppet::Module.expects(:modulepath).never
- Puppet::Module.find_template("/my/template").should == "/my/template"
- end
-
- it "should return the template from the first found module" do
- mod = mock 'module'
- Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
-
- mod.expects(:template).returns("/one/mymod/templates/mytemplate")
- Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
- end
-
- it "should return the file in the templatedir if it exists" do
- Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
- Puppet[:modulepath] = "/one:/two"
- File.stubs(:directory?).returns(true)
- FileTest.stubs(:exist?).returns(true)
- Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
- end
-
- it "should raise an error if no valid templatedir exists" do
- Puppet::Module.stubs(:templatepath).with(nil).returns(nil)
- lambda { Puppet::Module.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
-
- mod.expects(:template).returns("/one/mymod/templates/mytemplate")
- Puppet::Module.stubs(:templatepath).with(nil).returns(nil)
-
- Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
- end
-
- it "should use the main templatedir if no module is found" do
- Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"])
- Puppet::Module.expects(:find).with("mymod", nil).returns(nil)
- Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate"
- end
-
- it "should return unqualified templates directly in the template dir" do
- Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"])
- Puppet::Module.expects(:find).never
- Puppet::Module.find_template("mytemplate").should == "/my/templates/mytemplate"
- end
-
- it "should accept relative templatedirs" do
- Puppet[:templatedir] = "my/templates"
- File.expects(:directory?).with(File.join(Dir.getwd,"my/templates")).returns(true)
- Puppet::Module.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
- Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates"])
- Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
- Puppet::Module.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
- Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"])
- Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil)
- Puppet::Module.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::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"])
- 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"])
- 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
-
- it "should use the node environment if specified" do
- mod = mock 'module'
- Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
-
- mod.expects(:template).returns("/my/modules/mymod/templates/envtemplate")
-
- Puppet::Module.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
- end
-
- after { Puppet.settings.clear }
-end
-
-describe Puppet::Module, " when searching for manifests when no module is found" do
- before do
- File.stubs(:find).returns(nil)
- end
-
- it "should not look for modules when paths are fully qualified" do
- Puppet.expects(:value).with(:modulepath).never
- file = "/fully/qualified/file.pp"
- Dir.stubs(:glob).with(file).returns([file])
- Puppet::Module.find_manifests(file)
- end
-
- it "should directly return fully qualified files" do
- file = "/fully/qualified/file.pp"
- Dir.stubs(:glob).with(file).returns([file])
- Puppet::Module.find_manifests(file).should == [file]
- end
-
- it "should match against provided fully qualified patterns" do
- pattern = "/fully/qualified/pattern/*"
- Dir.expects(:glob).with(pattern).returns(%w{my file list})
- Puppet::Module.find_manifests(pattern).should == %w{my file list}
- end
-
- it "should look for files relative to the current directory" do
- cwd = Dir.getwd
- Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"])
- Puppet::Module.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"]
- end
-
- it "should only return files, not directories" do
- pattern = "/fully/qualified/pattern/*"
- file = "/my/file"
- dir = "/my/directory"
- Dir.expects(:glob).with(pattern).returns([file, dir])
- FileTest.expects(:directory?).with(file).returns(false)
- FileTest.expects(:directory?).with(dir).returns(true)
- Puppet::Module.find_manifests(pattern).should == [file]
- end
-end
-
-describe Puppet::Module, " when searching for manifests in a found module" do
- before do
- @module = Puppet::Module.new("mymod", "/one")
- end
-
- it "should return the manifests from the first found module" do
- mod = mock 'module'
- Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
- mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
- Puppet::Module.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"]
- end
-
- it "should use the node environment if specified" do
- mod = mock 'module'
- Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
- mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
- Puppet::Module.find_manifests("mymod/init.pp", :environment => "myenv").should == ["/one/mymod/manifests/init.pp"]
- end
-
- it "should return all manifests matching the glob pattern" do
- File.stubs(:directory?).returns(true)
- Dir.expects(:glob).with("/one/manifests/yay/*.pp").returns(%w{/one /two})
-
- @module.match_manifests("yay/*.pp").should == %w{/one /two}
- end
-
- it "should not return directories" do
- Dir.expects(:glob).with("/one/manifests/yay/*.pp").returns(%w{/one /two})
-
- FileTest.expects(:directory?).with("/one").returns false
- FileTest.expects(:directory?).with("/two").returns true
-
- @module.match_manifests("yay/*.pp").should == %w{/one}
- end
-
- it "should default to the 'init.pp' file in the manifests directory" do
- Dir.expects(:glob).with("/one/manifests/init.pp").returns(%w{/init.pp})
-
- @module.match_manifests(nil).should == %w{/init.pp}
- end
-
- after { Puppet.settings.clear }
-end
-
describe Puppet::Module, " when returning files" do
it "should return the path to the module's 'files' directory" do
mod = Puppet::Module.send(:new, "mymod", "/my/mod")
diff --git a/spec/unit/parser/files.rb b/spec/unit/parser/files.rb
new file mode 100644
index 000000000..f401a902c
--- /dev/null
+++ b/spec/unit/parser/files.rb
@@ -0,0 +1,194 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/parser/files'
+
+describe Puppet::Parser::Files do
+ it "should have a method for finding a template" do
+ Puppet::Parser::Files.should respond_to(:find_template)
+ end
+
+ it "should have a method for finding manifests" do
+ Puppet::Parser::Files.should respond_to(:find_manifests)
+ end
+
+ describe "when searching for templates" do
+ it "should return fully-qualified templates directly" do
+ Puppet::Parser::Files.expects(:modulepath).never
+ Puppet::Parser::Files.find_template("/my/template").should == "/my/template"
+ end
+
+ it "should return the template from the first found module" do
+ mod = mock 'module'
+ Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
+
+ mod.expects(:template).returns("/one/mymod/templates/mytemplate")
+ Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate"
+ end
+
+ it "should return the file in the templatedir if it exists" do
+ Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates")
+ Puppet[:modulepath] = "/one:/two"
+ File.stubs(:directory?).returns(true)
+ FileTest.stubs(:exist?).returns(true)
+ 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
+
+ mod.expects(:template).returns("/one/mymod/templates/mytemplate")
+ Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(nil)
+
+ 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
+ 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"
+ end
+
+ it "should return unqualified templates directly in the template dir" do
+ 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 accept relative templatedirs" do
+ 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
+ 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
+ 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
+
+ it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" 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
+
+ it "should use the node environment if specified" do
+ mod = mock 'module'
+ Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
+
+ mod.expects(:template).returns("/my/modules/mymod/templates/envtemplate")
+
+ Puppet::Parser::Files.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate"
+ end
+
+ after { Puppet.settings.clear }
+ end
+
+ describe "when searching for manifests when no module is found" do
+ before do
+ File.stubs(:find).returns(nil)
+ end
+
+ it "should not look for modules when paths are fully qualified" do
+ Puppet.expects(:value).with(:modulepath).never
+ file = "/fully/qualified/file.pp"
+ Dir.stubs(:glob).with(file).returns([file])
+ Puppet::Parser::Files.find_manifests(file)
+ end
+
+ it "should directly return fully qualified files" do
+ file = "/fully/qualified/file.pp"
+ Dir.stubs(:glob).with(file).returns([file])
+ Puppet::Parser::Files.find_manifests(file).should == [file]
+ end
+
+ it "should match against provided fully qualified patterns" do
+ pattern = "/fully/qualified/pattern/*"
+ Dir.expects(:glob).with(pattern).returns(%w{my file list})
+ Puppet::Parser::Files.find_manifests(pattern).should == %w{my file list}
+ end
+
+ it "should look for files relative to the current directory" do
+ cwd = Dir.getwd
+ Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"])
+ Puppet::Parser::Files.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"]
+ end
+
+ it "should only return files, not directories" do
+ pattern = "/fully/qualified/pattern/*"
+ file = "/my/file"
+ dir = "/my/directory"
+ Dir.expects(:glob).with(pattern).returns([file, dir])
+ FileTest.expects(:directory?).with(file).returns(false)
+ FileTest.expects(:directory?).with(dir).returns(true)
+ Puppet::Parser::Files.find_manifests(pattern).should == [file]
+ end
+ end
+
+ describe "when searching for manifests in a found module" do
+ before do
+ @module = Puppet::Module.new("mymod", "/one")
+ end
+
+ it "should return the manifests from the first found module" do
+ mod = mock 'module'
+ Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod
+ mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
+ Puppet::Parser::Files.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"]
+ end
+
+ it "should use the node environment if specified" do
+ mod = mock 'module'
+ Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod
+ mod.expects(:match_manifests).with("init.pp").returns(%w{/one/mymod/manifests/init.pp})
+ Puppet::Parser::Files.find_manifests("mymod/init.pp", :environment => "myenv").should == ["/one/mymod/manifests/init.pp"]
+ end
+
+ it "should return all manifests matching the glob pattern" do
+ File.stubs(:directory?).returns(true)
+ Dir.expects(:glob).with("/one/manifests/yay/*.pp").returns(%w{/one /two})
+
+ @module.match_manifests("yay/*.pp").should == %w{/one /two}
+ end
+
+ it "should not return directories" do
+ Dir.expects(:glob).with("/one/manifests/yay/*.pp").returns(%w{/one /two})
+
+ FileTest.expects(:directory?).with("/one").returns false
+ FileTest.expects(:directory?).with("/two").returns true
+
+ @module.match_manifests("yay/*.pp").should == %w{/one}
+ end
+
+ it "should default to the 'init.pp' file in the manifests directory" do
+ Dir.expects(:glob).with("/one/manifests/init.pp").returns(%w{/init.pp})
+
+ @module.match_manifests(nil).should == %w{/init.pp}
+ end
+
+ after { Puppet.settings.clear }
+ end
+end
diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb
index fd9efa8af..ce458af75 100755
--- a/spec/unit/parser/templatewrapper.rb
+++ b/spec/unit/parser/templatewrapper.rb
@@ -8,7 +8,7 @@ describe Puppet::Parser::TemplateWrapper do
parser = stub('parser', :watch_file => true)
@scope = stub('scope', :compiler => compiler, :parser => parser, :to_hash => {})
@file = "fake_template"
- Puppet::Module.stubs(:find_template).returns("/tmp/fake_template")
+ Puppet::Parser::Files.stubs(:find_template).returns("/tmp/fake_template")
FileTest.stubs(:exists?).returns("true")
File.stubs(:read).with("/tmp/fake_template").returns("template content")
@tw = Puppet::Parser::TemplateWrapper.new(@scope)
@@ -21,7 +21,7 @@ describe Puppet::Parser::TemplateWrapper do
end
it "should check template file existance and read its content" do
- Puppet::Module.expects(:find_template).with("fake_template", "foo").returns("/tmp/fake_template")
+ 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")