This is only true for resource types (e.g., classes and defines)
of course.

The actual variable is 'module_name':

    class mymod {
        notify { "in mymod '$module_name'": }
    }

Signed-off-by: Luke Kanies <[email protected]>
---
 lib/puppet/parser/files.rb       |    4 ++--
 lib/puppet/parser/type_loader.rb |    8 ++++++--
 lib/puppet/resource/type.rb      |    3 ++-
 spec/unit/parser/files.rb        |   22 +++++++++++-----------
 spec/unit/parser/type_loader.rb  |   28 +++++++++++++++++++---------
 spec/unit/resource/type.rb       |    8 ++++++++
 6 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/lib/puppet/parser/files.rb b/lib/puppet/parser/files.rb
index 1ff5552..37a7265 100644
--- a/lib/puppet/parser/files.rb
+++ b/lib/puppet/parser/files.rb
@@ -18,7 +18,7 @@ module Puppet::Parser::Files
         module_name, pattern = split_file_path(start)
         begin
             if mod = Puppet::Module.find(module_name, options[:environment])
-                return mod.match_manifests(pattern)
+                return [mod.name, mod.match_manifests(pattern)]
             end
         rescue Puppet::Module::InvalidName
             # Than that would be a "no."
@@ -28,7 +28,7 @@ module Puppet::Parser::Files
         if files.size == 0
             files = Dir.glob(abspat + ".pp").reject { |f| 
FileTest.directory?(f) }
         end
-        return files
+        return [nil, files]
     end
 
     # Find the concrete file denoted by +file+. If +file+ is absolute,
diff --git a/lib/puppet/parser/type_loader.rb b/lib/puppet/parser/type_loader.rb
index 3268eae..b98baa4 100644
--- a/lib/puppet/parser/type_loader.rb
+++ b/lib/puppet/parser/type_loader.rb
@@ -42,7 +42,7 @@ class Puppet::Parser::TypeLoader
         end
 
         pat = file
-        files = Puppet::Parser::Files.find_manifests(pat, :cwd => dir, 
:environment => environment)
+        modname, 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}'")
         end
@@ -54,6 +54,8 @@ class Puppet::Parser::TypeLoader
             @imported[file] = true
             parse_file(file)
         end
+
+        return modname
     end
 
     def imported?(file)
@@ -75,12 +77,14 @@ class Puppet::Parser::TypeLoader
     def load_until(namespaces, name)
         return nil if name == "" # special-case main.
         name2files(namespaces, name).each do |filename|
+            modname = nil
             import_if_possible(filename) do
-                  import(filename)
+                  modname = import(filename)
                   @loaded << filename
             end
             if result = yield(filename)
                 Puppet.info "Automatically imported #{name} from #{filename}"
+                result.module_name = modname if modname and 
result.respond_to?(:module_name=)
                 return result
             end
         end
diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb
index 627a1d5..227c544 100644
--- a/lib/puppet/resource/type.rb
+++ b/lib/puppet/resource/type.rb
@@ -13,7 +13,7 @@ class Puppet::Resource::Type
 
     RESOURCE_SUPERTYPES = [:hostclass, :node, :definition]
 
-    attr_accessor :file, :line, :doc, :code, :ruby_code, :parent, 
:resource_type_collection
+    attr_accessor :file, :line, :doc, :code, :ruby_code, :parent, 
:resource_type_collection, :module_name
     attr_reader :type, :namespace, :arguments, :behaves_like
 
     RESOURCE_SUPERTYPES.each do |t|
@@ -213,6 +213,7 @@ class Puppet::Resource::Type
 
         scope.setvar("title", resource.title) unless set.include? :title
         scope.setvar("name", resource.name) unless set.include? :name
+        scope.setvar("module_name", module_name) if module_name and ! 
set.include? :module_name
         scope.class_set(self.name,scope) if hostclass?
     end
 
diff --git a/spec/unit/parser/files.rb b/spec/unit/parser/files.rb
index 4c6e5b7..8921534 100644
--- a/spec/unit/parser/files.rb
+++ b/spec/unit/parser/files.rb
@@ -130,7 +130,7 @@ describe Puppet::Parser::Files do
             Puppet.expects(:value).with(:modulepath).never
             Dir.stubs(:glob).returns %w{foo}
 
-            Puppet::Parser::Files.find_manifests("mymod/init.pp").should == 
%w{foo}
+            Puppet::Parser::Files.find_manifests("mymod/init.pp")
         end
     end
 
@@ -146,22 +146,22 @@ describe Puppet::Parser::Files do
             Puppet::Parser::Files.find_manifests(file)
         end
 
-        it "should directly return fully qualified files" do
+        it "should return nil and an array of fully qualified files" do
             file = @basepath + "/fully/qualified/file.pp"
             Dir.stubs(:glob).with(file).returns([file])
-            Puppet::Parser::Files.find_manifests(file).should == [file]
+            Puppet::Parser::Files.find_manifests(file).should == [nil, [file]]
         end
 
         it "should match against provided fully qualified patterns" do
             pattern = @basepath + "/fully/qualified/pattern/*"
             Dir.expects(:glob).with(pattern).returns(%w{my file list})
-            Puppet::Parser::Files.find_manifests(pattern).should == %w{my file 
list}
+            Puppet::Parser::Files.find_manifests(pattern)[1].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"]
+            Puppet::Parser::Files.find_manifests("foobar/init.pp")[1].should 
== ["#{cwd}/foobar/init.pp"]
         end
 
         it "should only return files, not directories" do
@@ -171,23 +171,23 @@ describe Puppet::Parser::Files do
             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]
+            Puppet::Parser::Files.find_manifests(pattern)[1].should == [file]
         end
     end
 
     describe "when searching for manifests in a found module" do
-        it "should return the manifests from the first found module" do
-            mod = mock 'module'
+        it "should return the name of the module and the manifests from the 
first found module" do
+            mod = Puppet::Module.new("mymod")
             
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"]
+            Puppet::Parser::Files.find_manifests("mymod/init.pp").should == 
["mymod", ["/one/mymod/manifests/init.pp"]]
         end
 
         it "should use the node environment if specified" do
-            mod = mock 'module'
+            mod = Puppet::Module.new("mymod")
             
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"]
+            Puppet::Parser::Files.find_manifests("mymod/init.pp", :environment 
=> "myenv")[1].should == ["/one/mymod/manifests/init.pp"]
         end
 
         after { Puppet.settings.clear }
diff --git a/spec/unit/parser/type_loader.rb b/spec/unit/parser/type_loader.rb
index de181eb..2cd837c 100644
--- a/spec/unit/parser/type_loader.rb
+++ b/spec/unit/parser/type_loader.rb
@@ -83,6 +83,16 @@ describe Puppet::Parser::TypeLoader do
             @loader.load_until(["foo"], "bar") { |f| true }
             @loader.should be_loaded("file")
         end
+
+        it "should set the module name on any created resource types" do
+            type = Puppet::Resource::Type.new(:hostclass, "mytype")
+
+            Puppet::Parser::Files.expects(:find_manifests).returns ["modname", 
%w{one}]
+            @loader.stubs(:parse_file)
+            @loader.load_until(["foo"], "one") { |f| type }
+
+            type.module_name.should == "modname"
+        end
     end
 
     describe "when mapping names to files" do
@@ -102,7 +112,7 @@ describe Puppet::Parser::TypeLoader do
 
     describe "when importing" do
         before do
-            Puppet::Parser::Files.stubs(:find_manifests).returns %w{file}
+            Puppet::Parser::Files.stubs(:find_manifests).returns ["modname", 
%w{file}]
             @loader.stubs(:parse_file)
         end
 
@@ -113,46 +123,46 @@ describe Puppet::Parser::TypeLoader do
         end
 
         it "should find all manifests matching the file or pattern" do
-            Puppet::Parser::Files.expects(:find_manifests).with { |pat, opts| 
pat == "myfile" }.returns %w{one}
+            Puppet::Parser::Files.expects(:find_manifests).with { |pat, opts| 
pat == "myfile" }.returns ["modname", %w{one}]
             @loader.import("myfile")
         end
 
         it "should use the directory of the current file if one is set" do
-            Puppet::Parser::Files.expects(:find_manifests).with { |pat, opts| 
opts[:cwd] == "/current" }.returns %w{one}
+            Puppet::Parser::Files.expects(:find_manifests).with { |pat, opts| 
opts[:cwd] == "/current" }.returns ["modname", %w{one}]
             @loader.import("myfile", "/current/file")
         end
 
         it "should pass the environment when looking for files" do
-            Puppet::Parser::Files.expects(:find_manifests).with { |pat, opts| 
opts[:environment] == @loader.environment }.returns %w{one}
+            Puppet::Parser::Files.expects(:find_manifests).with { |pat, opts| 
opts[:environment] == @loader.environment }.returns ["modname", %w{one}]
             @loader.import("myfile")
         end
 
         it "should fail if no files are found" do
-            Puppet::Parser::Files.expects(:find_manifests).returns []
+            Puppet::Parser::Files.expects(:find_manifests).returns [nil, []]
             lambda { @loader.import("myfile") }.should 
raise_error(Puppet::ImportError)
         end
 
         it "should parse each found file" do
-            Puppet::Parser::Files.expects(:find_manifests).returns %w{/one}
+            Puppet::Parser::Files.expects(:find_manifests).returns ["modname", 
%w{/one}]
             @loader.expects(:parse_file).with("/one")
             @loader.import("myfile")
         end
 
         it "should make each file qualified before attempting to parse it" do
-            Puppet::Parser::Files.expects(:find_manifests).returns %w{one}
+            Puppet::Parser::Files.expects(:find_manifests).returns ["modname", 
%w{one}]
             @loader.expects(:parse_file).with("/current/one")
             @loader.import("myfile", "/current/file")
         end
 
         it "should know when a given file has been imported" do
-            Puppet::Parser::Files.expects(:find_manifests).returns %w{/one}
+            Puppet::Parser::Files.expects(:find_manifests).returns ["modname", 
%w{/one}]
             @loader.import("myfile")
 
             @loader.should be_imported("/one")
         end
 
         it "should not attempt to import files that have already been 
imported" do
-            Puppet::Parser::Files.expects(:find_manifests).returns %w{/one}
+            Puppet::Parser::Files.expects(:find_manifests).returns ["modname", 
%w{/one}]
             @loader.expects(:parse_file).once
             @loader.import("myfile")
 
diff --git a/spec/unit/resource/type.rb b/spec/unit/resource/type.rb
index bab2522..59e4623 100755
--- a/spec/unit/resource/type.rb
+++ b/spec/unit/resource/type.rb
@@ -314,6 +314,14 @@ describe Puppet::Resource::Type do
 
             @scope.lookupvar("name").should == "bar"
         end
+
+        it "should set its module name in the scope if available" do
+            @type.module_name = "mymod"
+
+            @type.set_resource_parameters(@resource, @scope)
+
+            @scope.lookupvar("module_name").should == "mymod"
+        end
     end
 
     describe "when describing and managing parent classes" do
-- 
1.6.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to