summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-02-17 17:50:46 -0600
committerLuke Kanies <luke@madstop.com>2009-02-18 22:38:44 -0600
commit9c18d5aa4214f565b81b6cac07736fe072954bb1 (patch)
tree7ea6f06b2d94319935221da32241f7f939d1f5d2
parent4b5ec82d32babdf3ccc935748eb80bca30b379ff (diff)
downloadpuppet-9c18d5aa4214f565b81b6cac07736fe072954bb1.tar.gz
puppet-9c18d5aa4214f565b81b6cac07736fe072954bb1.tar.xz
puppet-9c18d5aa4214f565b81b6cac07736fe072954bb1.zip
Adding support for finding all modules in a given path.
This 'each_module' method will be used by environments to find all or a given module, and will likely eventually be used internally, too. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/module.rb22
-rwxr-xr-xspec/unit/module.rb87
2 files changed, 109 insertions, 0 deletions
diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb
index 01459d7aa..5ae3f3121 100644
--- a/lib/puppet/module.rb
+++ b/lib/puppet/module.rb
@@ -7,6 +7,28 @@ class Puppet::Module
PLUGINS = "plugins"
FILETYPES = [MANIFESTS, FILES, TEMPLATES, PLUGINS]
+
+ # Search through a list of paths, yielding each found module in turn.
+ def self.each_module(*paths)
+ paths = paths.flatten.collect { |p| p.split(File::PATH_SEPARATOR) }.flatten
+
+ yielded = {}
+ paths.each do |dir|
+ next unless FileTest.directory?(dir)
+
+ Dir.entries(dir).each do |name|
+ next if name =~ /^\./
+ next if yielded.include?(name)
+
+ module_path = File.join(dir, name)
+ next unless FileTest.directory?(module_path)
+
+ yielded[name] = true
+
+ yield Puppet::Module.new(name, module_path)
+ end
+ end
+ end
# Return an array of paths by splitting the +modulepath+ config
# parameter. Only consider paths that are absolute and existing
diff --git a/spec/unit/module.rb b/spec/unit/module.rb
index fd5f4fc19..af997a0b1 100755
--- a/spec/unit/module.rb
+++ b/spec/unit/module.rb
@@ -35,6 +35,93 @@ describe Puppet::Module do
end
end
+describe Puppet::Module, "when yielding each module in a list of directories" do
+ before do
+ FileTest.stubs(:directory?).returns true
+ end
+
+ it "should search for modules in each directory in the list" do
+ Dir.expects(:entries).with("/one").returns []
+ Dir.expects(:entries).with("/two").returns []
+
+ Puppet::Module.each_module("/one", "/two")
+ end
+
+ it "should accept the list of directories as an array" do
+ Dir.expects(:entries).with("/one").returns []
+ Dir.expects(:entries).with("/two").returns []
+
+ Puppet::Module.each_module(%w{/one /two})
+ end
+
+ it "should accept the list of directories joined by #{File::PATH_SEPARATOR}" do
+ Dir.expects(:entries).with("/one").returns []
+ Dir.expects(:entries).with("/two").returns []
+
+ list = %w{/one /two}.join(File::PATH_SEPARATOR)
+
+ Puppet::Module.each_module(list)
+ end
+
+ it "should not create modules for '.' or '..' in the provided directory list" do
+ Dir.expects(:entries).with("/one").returns(%w{. ..})
+
+ result = []
+ Puppet::Module.each_module("/one") do |mod|
+ result << mod
+ end
+
+ result.should be_empty
+ end
+
+ it "should not create modules for non-directories in the provided directory list" do
+ Dir.expects(:entries).with("/one").returns(%w{notdir})
+
+ FileTest.expects(:directory?).with("/one/notdir").returns false
+
+ result = []
+ Puppet::Module.each_module("/one") do |mod|
+ result << mod
+ end
+
+ result.should be_empty
+ end
+
+ it "should yield each found module" do
+ Dir.expects(:entries).with("/one").returns(%w{f1 f2})
+
+ one = mock 'one'
+ two = mock 'two'
+
+ Puppet::Module.expects(:new).with("f1", "/one/f1").returns one
+ Puppet::Module.expects(:new).with("f2", "/one/f2").returns two
+
+ result = []
+ Puppet::Module.each_module("/one") do |mod|
+ result << mod
+ end
+
+ result.should == [one, two]
+ end
+
+ it "should not yield a module with the same name as a previously yielded module" do
+ Dir.expects(:entries).with("/one").returns(%w{f1})
+ Dir.expects(:entries).with("/two").returns(%w{f1})
+
+ one = mock 'one'
+
+ Puppet::Module.expects(:new).with("f1", "/one/f1").returns one
+ Puppet::Module.expects(:new).with("f1", "/two/f1").never
+
+ result = []
+ Puppet::Module.each_module("/one", "/two") do |mod|
+ result << mod
+ end
+
+ result.should == [one]
+ end
+end
+
describe Puppet::Module, " when building its search path" do
include PuppetTest