summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/ast/objectdef.rb46
-rw-r--r--lib/puppet/parser/scope.rb17
-rwxr-xr-xtest/language/scope.rb31
-rw-r--r--test/puppettest.rb36
4 files changed, 85 insertions, 45 deletions
diff --git a/lib/puppet/parser/ast/objectdef.rb b/lib/puppet/parser/ast/objectdef.rb
index 0a9ba23cd..9acf56b49 100644
--- a/lib/puppet/parser/ast/objectdef.rb
+++ b/lib/puppet/parser/ast/objectdef.rb
@@ -65,8 +65,7 @@ class Puppet::Parser::AST
end
end
- # Retrieve the defaults for our type
- hash = getdefaults(objtype, scope)
+ hash = {}
# then set all of the specified params
@params.each { |param|
@@ -105,52 +104,9 @@ class Puppet::Parser::AST
error.backtrace = detail.backtrace
raise error
end
-# else
-# # but things like components create a new type; if we find
-# # one of those, evaluate that with our arguments
-# #Puppet.debug("Calling object '%s' with arguments %s" %
-# # [object.name, hash.inspect])
-# #obj = object.safeevaluate(scope,hash,objtype,objname)
-# obj = object.safeevaluate(
-# :scope => scope,
-# :arguments => hash,
-# :type => objtype,
-# :name => objname
-# )
-#
-# # and pass the result on
-# obj
-# end
}.reject { |obj| obj.nil? }
end
- # Retrieve the defaults for our type
- def getdefaults(objtype, scope)
- # first, retrieve the defaults
- begin
- defaults = scope.lookupdefaults(objtype)
- if defaults.length > 0
- #Puppet.debug "Got defaults for %s: %s" %
- # [objtype,defaults.inspect]
- end
- rescue => detail
- raise Puppet::DevError,
- "Could not lookup defaults for %s: %s" %
- [objtype, detail.to_s]
- end
-
- hash = {}
- # Add any found defaults to our argument list
- defaults.each { |var,value|
- Puppet.debug "Found default %s for %s" %
- [var,objtype]
-
- hash[var] = value
- }
-
- return hash
- end
-
# Create our ObjectDef. Handles type checking for us.
def initialize(hash)
@checked = false
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index 1dde7f642..f247bd7b8 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -35,6 +35,20 @@ module Puppet
@@declarative = val
end
+ # Add all of the defaults for a given object to that object.
+ def adddefaults(obj)
+ defaults = self.lookupdefaults(obj.type)
+
+ defaults.each do |var, value|
+ unless obj[var]
+ self.debug "Adding default %s for %s" %
+ [var, obj.type]
+
+ obj[var] = value
+ end
+ end
+ end
+
# Add a single object's tags to the global list of tags for
# that object.
def addtags(obj)
@@ -975,6 +989,9 @@ module Puppet
# probably should not matter
child.tags = self.tags
+ # Add any defaults.
+ self.adddefaults(child)
+
# Then make sure this child's tags are stored in the
# central table. This should maybe be in the evaluate
# methods, but, eh.
diff --git a/test/language/scope.rb b/test/language/scope.rb
index a18b75bff..9c5302988 100755
--- a/test/language/scope.rb
+++ b/test/language/scope.rb
@@ -513,4 +513,35 @@ class TestScope < Test::Unit::TestCase
# trans = scope.evaluate(:ast => top)
#}
end
+
+ def test_defaultswithmultiplestatements
+ path = tempfile()
+
+ stats = []
+ stats << defaultobj("file", "group" => "root")
+ stats << fileobj(path, "owner" => "root")
+ stats << fileobj(path, "mode" => "755")
+
+ top = AST::ASTArray.new(
+ :file => __FILE__,
+ :line => __LINE__,
+ :children => stats
+ )
+ scope = Puppet::Parser::Scope.new()
+ assert_nothing_raised {
+ scope.evaluate(:ast => top)
+ }
+
+ trans = nil
+ assert_nothing_raised {
+ trans = scope.to_trans
+ }
+
+ obj = trans.find do |obj| obj.is_a? Puppet::TransObject end
+
+ assert(obj, "Could not retrieve file obj")
+ assert_equal("root", obj["group"], "Default did not take")
+ assert_equal("root", obj["owner"], "Owner did not take")
+ assert_equal("755", obj["mode"], "Mode did not take")
+ end
end
diff --git a/test/puppettest.rb b/test/puppettest.rb
index a16f4d80b..a8af679fb 100644
--- a/test/puppettest.rb
+++ b/test/puppettest.rb
@@ -766,6 +766,16 @@ module ParserTesting
}
end
+ def typeobj(name)
+ assert_nothing_raised("Could not create type %s" % name) {
+ return AST::Type.new(
+ :file => tempfile(),
+ :line => rand(100),
+ :value => name
+ )
+ }
+ end
+
def nodeobj(name)
assert_nothing_raised("Could not create node %s" % name) {
return AST::NodeDef.new(
@@ -832,6 +842,32 @@ module ParserTesting
)
}
end
+
+ def defaultobj(type, params)
+ pary = []
+ params.each { |p,v|
+ pary << AST::ObjectParam.new(
+ :file => __FILE__,
+ :line => __LINE__,
+ :param => nameobj(p),
+ :value => stringobj(v)
+ )
+ }
+ past = AST::ASTArray.new(
+ :file => __FILE__,
+ :line => __LINE__,
+ :children => pary
+ )
+
+ assert_nothing_raised("Could not create defaults for %s" % type) {
+ return AST::TypeDefaults.new(
+ :file => __FILE__,
+ :line => __LINE__,
+ :type => typeobj(type),
+ :params => past
+ )
+ }
+ end
end
class PuppetTestSuite