Switching to LoadedCode from ASTSet

I also took the opportunity to clean up and simplify
the interface to the parts of the parser that interact
with this.  Mostly it was method renames.

Signed-off-by: Luke Kanies <luke@madstop.com>
This commit is contained in:
Luke Kanies 2009-06-04 00:33:25 -05:00 коммит произвёл James Turnbull
Родитель fc1f8cdbee
Коммит cddc365e9b
18 изменённых файлов: 164 добавлений и 217 удалений

Просмотреть файл

@ -80,7 +80,7 @@ class Puppet::Parser::AST::Definition < Puppet::Parser::AST::Branch
end
def find_parentclass
@parser.findclass(namespace, parentclass)
@parser.find_hostclass(namespace, parentclass)
end
# Set our parent class, with a little check to avoid some potential

Просмотреть файл

@ -33,6 +33,6 @@ class Puppet::Parser::AST::Node < Puppet::Parser::AST::HostClass
# Search for the object matching our parent class.
def find_parentclass
@parser.findnode(parentclass)
@parser.node(parentclass)
end
end

Просмотреть файл

@ -43,7 +43,7 @@ class Puppet::Parser::AST
# Look up a fully qualified class name.
def qualified_class(scope, title)
# Look up the full path to the class
if classobj = scope.findclass(title)
if classobj = scope.find_hostclass(title)
title = classobj.classname
else
raise Puppet::ParseError, "Could not find class %s" % title
@ -56,7 +56,7 @@ class Puppet::Parser::AST
# We want a lower-case type. For some reason.
objtype = @type.downcase
unless builtintype?(objtype)
if dtype = scope.finddefine(objtype)
if dtype = scope.find_definition(objtype)
objtype = dtype.classname
else
raise Puppet::ParseError, "Could not find resource type %s" % objtype

Просмотреть файл

@ -44,7 +44,7 @@ class Puppet::Parser::Compiler
# Do we use nodes found in the code, vs. the external node sources?
def ast_nodes?
parser.nodes.length > 0
parser.nodes?
end
# Store the fact that we've evaluated a class, and store a reference to
@ -133,7 +133,7 @@ class Puppet::Parser::Compiler
found = []
classes.each do |name|
# If we can find the class, then make a resource that will evaluate it.
if klass = scope.findclass(name)
if klass = scope.find_hostclass(name)
found << name and next if class_scope(klass)
resource = klass.evaluate(scope)
@ -217,10 +217,10 @@ class Puppet::Parser::Compiler
# Now see if we can find the node.
astnode = nil
@node.names.each do |name|
break if astnode = @parser.nodes[name.to_s.downcase]
break if astnode = @parser.node(name.to_s.downcase)
end
unless (astnode ||= @parser.nodes["default"])
unless (astnode ||= @parser.node("default"))
raise Puppet::ParseError, "Could not find default node or by name with '%s'" % node.names.join(", ")
end
@ -298,7 +298,7 @@ class Puppet::Parser::Compiler
# Find and evaluate our main object, if possible.
def evaluate_main
@main = @parser.findclass("", "") || @parser.newclass("")
@main = @parser.find_hostclass("", "") || @parser.newclass("")
@topscope.source = @main
@main_resource = Puppet::Parser::Resource.new(:type => "class", :title => :main, :scope => @topscope, :source => @main)
@topscope.resource = @main_resource

Просмотреть файл

@ -10,7 +10,7 @@ Puppet::Parser::Functions::newfunction(:defined, :type => :rvalue, :doc => "Dete
case val
when String
# For some reason, it doesn't want me to return from here.
if Puppet::Type.type(val) or finddefine(val) or findclass(val)
if Puppet::Type.type(val) or find_definition(val) or find_hostclass(val)
result = true
break
end

Просмотреть файл

@ -3,16 +3,7 @@
class Puppet::Parser::Parser
require 'puppet/parser/functions'
require 'puppet/parser/files'
ASTSet = Struct.new(:classes, :definitions, :nodes)
# Define an accessor method for each table. We hide the existence of
# the struct.
[:classes, :definitions, :nodes].each do |name|
define_method(name) do
@astset.send(name)
end
end
require 'puppet/parser/loaded_code'
AST = Puppet::Parser::AST
@ -106,56 +97,51 @@ class Puppet::Parser::Parser
end
end
# Find a class definition, relative to the current namespace.
def findclass(namespace, name)
fqfind namespace, name, classes
[:hostclass, :definition, :node, :nodes?].each do |method|
define_method(method) do |*args|
@loaded_code.send(method, *args)
end
end
# Find a component definition, relative to the current namespace.
def finddefine(namespace, name)
fqfind namespace, name, definitions
def find_hostclass(namespace, name)
find_or_load(namespace, name, :hostclass)
end
# This is only used when nodes are looking up the code for their
# parent nodes.
def findnode(name)
fqfind "", name, nodes
def find_definition(namespace, name)
find_or_load(namespace, name, :definition)
end
# The recursive method used to actually look these objects up.
def fqfind(namespace, name, table)
namespace = namespace.downcase
name = name.to_s.downcase
# If our classname is fully qualified or we have no namespace,
# just try directly for the class, and return either way.
if name =~ /^::/ or namespace == ""
classname = name.sub(/^::/, '')
self.load(classname) unless table[classname]
return table[classname]
def find_or_load(namespace, name, type)
method = "find_" + type.to_s
if result = @loaded_code.send(method, namespace, name)
return result
end
# Else, build our namespace up piece by piece, checking
# for the class in each namespace.
ary = namespace.split("::")
fullname = (namespace + "::" + name).sub(/^::/, '')
self.load(fullname)
while ary.length > 0
newname = (ary + [name]).join("::").sub(/^::/, '')
if obj = table[newname] or (self.load(newname) and obj = table[newname])
return obj
if result = @loaded_code.send(method, namespace, name)
return result
end
# Try to load the module init file if we're a qualified
# name
if fullname.include?("::")
module_name = fullname.split("::")[0]
self.load(module_name)
if result = @loaded_code.send(method, namespace, name)
return result
end
# Delete the second to last object, which reduces our namespace by one.
ary.pop
end
# If we've gotten to this point without finding it, see if the name
# exists at the top namespace
if obj = table[name] or (self.load(name) and obj = table[name])
return obj
end
# Now try to load the bare name on its own. This is
# appropriate if the class we're looking for is in a
# module that's different from our namespace.
self.load(name)
return nil
@loaded_code.send(method, namespace, name)
end
# Import our files.
@ -191,7 +177,7 @@ class Puppet::Parser::Parser
end
files.collect { |file|
parser = Puppet::Parser::Parser.new(:astset => @astset, :environment => @environment)
parser = Puppet::Parser::Parser.new(:loaded_code => @loaded_code, :environment => @environment)
parser.files = self.files
Puppet.debug("importing '%s'" % file)
@ -211,7 +197,7 @@ class Puppet::Parser::Parser
end
def initialize(options = {})
@astset = options[:astset] || ASTSet.new({}, {}, {})
@loaded_code = options[:loaded_code] || Puppet::Parser::LoadedCode.new
@environment = options[:environment]
initvars()
end
@ -242,7 +228,7 @@ class Puppet::Parser::Parser
# We don't know whether we're looking for a class or definition, so we have
# to test for both.
return true if classes.include?(classname) || definitions.include?(classname)
return true if @loaded_code.hostclass(classname) || @loaded_code.definition(classname)
unless @loaded.include?(filename)
@loaded << filename
@ -256,7 +242,7 @@ class Puppet::Parser::Parser
end
# We don't know whether we're looking for a class or definition, so we have
# to test for both.
return classes.include?(classname) || definitions.include?(classname)
return true if @loaded_code.hostclass(classname) || @loaded_code.definition(classname)
end
# Split an fq name into a namespace and name
@ -271,7 +257,7 @@ class Puppet::Parser::Parser
def newclass(name, options = {})
name = name.downcase
if definitions.include?(name)
if @loaded_code.definition(name)
raise Puppet::ParseError, "Cannot redefine class %s as a definition" % name
end
code = options[:code]
@ -279,7 +265,7 @@ class Puppet::Parser::Parser
doc = options[:doc]
# If the class is already defined, then add code to it.
if other = @astset.classes[name]
if other = @loaded_code.hostclass(name) || @loaded_code.definition(name)
# Make sure the parents match
if parent and other.parentclass and (parent != other.parentclass)
error("Class %s is already defined at %s:%s; cannot redefine" % [name, other.file, other.line])
@ -325,21 +311,21 @@ class Puppet::Parser::Parser
args[:parentclass] = parent if parent
args[:doc] = doc
@astset.classes[name] = ast AST::HostClass, args
@loaded_code.add_hostclass(name, ast(AST::HostClass, args))
end
return @astset.classes[name]
return @loaded_code.hostclass(name)
end
# Create a new definition.
def newdefine(name, options = {})
name = name.downcase
if @astset.classes.include?(name)
if @loaded_code.hostclass(name)
raise Puppet::ParseError, "Cannot redefine class %s as a definition" %
name
end
# Make sure our definition doesn't already exist
if other = @astset.definitions[name]
if other = @loaded_code.definition(name)
error("%s is already defined at %s:%s; cannot redefine" % [name, other.file, other.line])
end
@ -357,7 +343,7 @@ class Puppet::Parser::Parser
args[param] = options[param] if options[param]
end
@astset.definitions[name] = ast AST::Definition, args
@loaded_code.add_definition(name, ast(AST::Definition, args))
end
# Create a new node. Nodes are special, because they're stored in a global
@ -367,7 +353,7 @@ class Puppet::Parser::Parser
doc = lexer.getcomment
names.collect do |name|
name = name.to_s.downcase
if other = @astset.nodes[name]
if other = @loaded_code.node(name)
error("Node %s is already defined at %s:%s; cannot redefine" % [other.name, other.file, other.line])
end
name = name.to_s if name.is_a?(Symbol)
@ -382,9 +368,10 @@ class Puppet::Parser::Parser
if options[:parent]
args[:parentclass] = options[:parent]
end
@astset.nodes[name] = ast(AST::Node, args)
@astset.nodes[name].classname = name
@astset.nodes[name]
node = ast(AST::Node, args)
node.classname = name
@loaded_code.add_node(name, node)
node
end
end
@ -448,7 +435,7 @@ class Puppet::Parser::Parser
newclass("", :code => main)
end
@version = Time.now.to_i
return @astset
return @loaded_code
ensure
@lexer.clear
end

Просмотреть файл

@ -40,20 +40,20 @@ class Puppet::Parser::Resource::Reference < Puppet::Resource::Reference
case self.type
when "Class" # look for host classes
if self.title == :main
tmp = @scope.findclass("")
tmp = @scope.find_hostclass("")
else
unless tmp = @scope.parser.classes[self.title]
unless tmp = @scope.parser.hostclass(self.title)
fail Puppet::ParseError, "Could not find class '%s'" % self.title
end
end
when "Node" # look for node definitions
unless tmp = @scope.parser.nodes[self.title]
unless tmp = @scope.parser.node(self.title)
fail Puppet::ParseError, "Could not find node '%s'" % self.title
end
else # normal definitions
# The resource type is capitalized, so we have to downcase. Really,
# we should have a better interface for finding these, but eh.
tmp = @scope.parser.definitions[self.type.downcase]
tmp = @scope.parser.definition(self.type.downcase)
end
if tmp

Просмотреть файл

@ -82,18 +82,18 @@ class Puppet::Parser::Scope
@level == 1
end
def findclass(name)
def find_hostclass(name)
@namespaces.each do |namespace|
if r = parser.findclass(namespace, name)
if r = parser.find_hostclass(namespace, name)
return r
end
end
return nil
end
def finddefine(name)
def find_definition(name)
@namespaces.each do |namespace|
if r = parser.finddefine(namespace, name)
if r = parser.find_definition(namespace, name)
return r
end
end
@ -165,14 +165,14 @@ class Puppet::Parser::Scope
# Look up a defined type.
def lookuptype(name)
finddefine(name) || findclass(name)
find_definition(name) || find_hostclass(name)
end
def lookup_qualified_var(name, usestring)
parts = name.split(/::/)
shortname = parts.pop
klassname = parts.join("::")
klass = findclass(klassname)
klass = find_hostclass(klassname)
unless klass
warning "Could not look up qualified variable '%s'; class %s could not be found" % [name, klassname]
return usestring ? "" : :undefined

Просмотреть файл

@ -22,22 +22,22 @@ describe Puppet::Parser::AST::ResourceReference do
%{ "one::two" "one-two"}.each do |type|
it "should evaluate correctly reference to define" do
klass = stub 'klass', :title => "three", :classname => type
@scope.stubs(:finddefine).returns(klass)
@scope.stubs(:find_definition).returns(klass)
newref("three", type).evaluate(@scope).to_ref.should == Puppet::Parser::Resource::Reference.new( :type => type, :title => "three" ).to_ref
end
end
it "should be able to call qualified_class" do
klass = stub 'klass', :title => "three", :classname => "one"
@scope.expects(:findclass).with("one").returns(klass)
newref("three","class").qualified_class(@scope,"one").should == "one"
@scope.expects(:find_hostclass).with("one").returns(klass)
newref("three","class").qualified_class(@scope,"one").should == "one"
end
it "should be able to find qualified classes when evaluating" do
klass = stub 'klass', :title => "one", :classname => "one"
@scope.stubs(:findclass).returns(klass)
@scope.stubs(:find_hostclass).returns(klass)
evaled = newref("one", "class").evaluate(@scope)
evaled.type.should == "Class"
evaled.title.should == "one"

Просмотреть файл

@ -82,7 +82,7 @@ describe Puppet::Parser::Compiler do
end
it "should detect when ast nodes are present" do
@parser.nodes["testing"] = "yay"
@parser.expects(:nodes?).returns true
@compiler.ast_nodes?.should be_true
end
end
@ -142,22 +142,12 @@ describe Puppet::Parser::Compiler do
@compiler.class.publicize_methods(:evaluate_node_classes) { @compiler.evaluate_node_classes }
end
it "should enable ast_nodes if the parser has any nodes" do
@parser.expects(:nodes).returns(:one => :yay)
@compiler.ast_nodes?.should be_true
end
it "should disable ast_nodes if the parser has no nodes" do
@parser.expects(:nodes).returns({})
@compiler.ast_nodes?.should be_false
end
it "should evaluate the main class if it exists" do
compile_stub(:evaluate_main)
main_class = mock 'main_class'
main_class.expects(:evaluate_code).with { |r| r.is_a?(Puppet::Parser::Resource) }
@compiler.topscope.expects(:source=).with(main_class)
@parser.stubs(:findclass).with("", "").returns(main_class)
@parser.stubs(:find_hostclass).with("", "").returns(main_class)
@compiler.compile
end
@ -355,7 +345,7 @@ describe Puppet::Parser::Compiler do
it "should tag the catalog with the name of each not-found class" do
@compiler.catalog.expects(:tag).with("notfound")
@scope.expects(:findclass).with("notfound").returns(nil)
@scope.expects(:find_hostclass).with("notfound").returns(nil)
@compiler.evaluate_classes(%w{notfound}, @scope)
end
end
@ -364,7 +354,7 @@ describe Puppet::Parser::Compiler do
before do
@class = stub 'class', :classname => "my::class"
@scope.stubs(:findclass).with("myclass").returns(@class)
@scope.stubs(:find_hostclass).with("myclass").returns(@class)
@resource = stub 'resource', :ref => "Class[myclass]"
end
@ -413,7 +403,7 @@ describe Puppet::Parser::Compiler do
@compiler.catalog.stubs(:tag)
@compiler.stubs(:add_resource)
@scope.stubs(:findclass).with("notfound").returns(nil)
@scope.stubs(:find_hostclass).with("notfound").returns(nil)
Puppet::Parser::Resource.stubs(:new).returns(@resource)
@class.stubs :evaluate
@ -435,18 +425,16 @@ describe Puppet::Parser::Compiler do
describe Puppet::Parser::Compiler, " when evaluating AST nodes with AST nodes present" do
before do
@nodes = mock 'node_hash'
@compiler.stubs(:ast_nodes?).returns(true)
@compiler.parser.stubs(:nodes).returns(@nodes)
@compiler.parser.stubs(:nodes?).returns true
# Set some names for our test
@node.stubs(:names).returns(%w{a b c})
@nodes.stubs(:[]).with("a").returns(nil)
@nodes.stubs(:[]).with("b").returns(nil)
@nodes.stubs(:[]).with("c").returns(nil)
@compiler.parser.stubs(:node).with("a").returns(nil)
@compiler.parser.stubs(:node).with("b").returns(nil)
@compiler.parser.stubs(:node).with("c").returns(nil)
# It should check this last, of course.
@nodes.stubs(:[]).with("default").returns(nil)
@compiler.parser.stubs(:node).with("default").returns(nil)
end
it "should fail if the named node cannot be found" do
@ -455,7 +443,7 @@ describe Puppet::Parser::Compiler do
it "should evaluate the first node class matching the node name" do
node_class = stub 'node', :classname => "c", :evaluate_code => nil
@nodes.stubs(:[]).with("c").returns(node_class)
@compiler.parser.stubs(:node).with("c").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil
node_class.expects(:evaluate).returns(node_resource)
@ -465,7 +453,7 @@ describe Puppet::Parser::Compiler do
it "should match the default node if no matching node can be found" do
node_class = stub 'node', :classname => "default", :evaluate_code => nil
@nodes.stubs(:[]).with("default").returns(node_class)
@compiler.parser.stubs(:node).with("default").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[default]", :evaluate => nil
node_class.expects(:evaluate).returns(node_resource)
@ -475,7 +463,7 @@ describe Puppet::Parser::Compiler do
it "should evaluate the node resource immediately rather than using lazy evaluation" do
node_class = stub 'node', :classname => "c"
@nodes.stubs(:[]).with("c").returns(node_class)
@compiler.parser.stubs(:node).with("c").returns(node_class)
node_resource = stub 'node resource', :ref => "Node[c]"
node_class.expects(:evaluate).returns(node_resource)
@ -489,7 +477,7 @@ describe Puppet::Parser::Compiler do
node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil
node_class = stub 'node', :classname => "c", :evaluate => node_resource
@nodes.stubs(:[]).with("c").returns(node_class)
@compiler.parser.stubs(:node).with("c").returns(node_class)
# The #evaluate method normally does this.
scope = stub 'scope', :source => "mysource"

Просмотреть файл

@ -208,7 +208,7 @@ describe Puppet::Parser do
class test {}
""")
ast[:classes]["test"].doc.should == "comment\n"
ast.hostclass("test").doc.should == "comment\n"
end
end

Просмотреть файл

@ -31,7 +31,7 @@ class TestASTResource< Test::Unit::TestCase
@parser.newdefine "three"
twoscope = @scope.newscope(:namespace => "one")
twoscope.resource = @scope.resource
assert(twoscope.finddefine("two"), "Could not find 'two' definition")
assert(twoscope.find_definition("two"), "Could not find 'two' definition")
title = "title"
# First try a qualified type

Просмотреть файл

@ -29,7 +29,7 @@ class TestASTResourceReference < Test::Unit::TestCase
@parser.newdefine "one::two"
@parser.newdefine "three"
twoscope = @scope.newscope(:namespace => "one")
assert(twoscope.finddefine("two"), "Could not find 'two' definition")
assert(twoscope.find_definition("two"), "Could not find 'two' definition")
title = "title"
# First try a qualified type

Просмотреть файл

@ -493,8 +493,8 @@ class TestLangFunctions < Test::Unit::TestCase
ffun = ffoo = nil
assert_nothing_raised("Search path change did not work") do
ffun = scope.finddefine("ness")
ffoo = scope.finddefine('bar')
ffun = scope.find_definition("ness")
ffoo = scope.find_definition('bar')
end
assert(ffun, "Could not find definition in 'fun' namespace")

Просмотреть файл

@ -38,7 +38,7 @@ class TestParser < Test::Unit::TestCase
ast = parser.parse
config = mkcompiler(parser)
config.compile
#ast.classes[""].evaluate config.topscope
#ast.hostclass("").evaluate config.topscope
}
}
end
@ -283,7 +283,7 @@ class TestParser < Test::Unit::TestCase
ret = parser.parse
}
ret.classes[""].code.each do |obj|
ret.hostclass("").code.each do |obj|
assert_instance_of(AST::Collection, obj)
end
end
@ -374,13 +374,13 @@ file { "/tmp/yayness":
str1 = %{if true { #{exec.call("true")} }}
ret = nil
assert_nothing_raised {
ret = parser.parse(str1).classes[""].code[0]
ret = parser.parse(str1).hostclass("").code[0]
}
assert_instance_of(Puppet::Parser::AST::IfStatement, ret)
parser = mkparser
str2 = %{if true { #{exec.call("true")} } else { #{exec.call("false")} }}
assert_nothing_raised {
ret = parser.parse(str2).classes[""].code[0]
ret = parser.parse(str2).hostclass("").code[0]
}
assert_instance_of(Puppet::Parser::AST::IfStatement, ret)
assert_instance_of(Puppet::Parser::AST::Else, ret.else)
@ -392,8 +392,8 @@ file { "/tmp/yayness":
assert_nothing_raised {
parser.parse %{class myclass { class other {} }}
}
assert(parser.classes["myclass"], "Could not find myclass")
assert(parser.classes["myclass::other"], "Could not find myclass::other")
assert(parser.hostclass("myclass"), "Could not find myclass")
assert(parser.hostclass("myclass::other"), "Could not find myclass::other")
assert_nothing_raised {
parser.parse "class base {}
@ -401,14 +401,14 @@ file { "/tmp/yayness":
class deep::sub inherits base {}
}"
}
sub = parser.classes["container::deep::sub"]
sub = parser.hostclass("container::deep::sub")
assert(sub, "Could not find sub")
# Now try it with a parent class being a fq class
assert_nothing_raised {
parser.parse "class container::one inherits container::deep::sub {}"
}
sub = parser.classes["container::one"]
sub = parser.hostclass("container::one")
assert(sub, "Could not find one")
assert_equal("container::deep::sub", sub.parentclass)
@ -426,17 +426,17 @@ file { "/tmp/yayness":
assert_nothing_raised do
out = parser.parse ""
assert_instance_of(Puppet::Parser::Parser::ASTSet, out)
assert_nil(parser.classes[""], "Got a 'main' class when we had no code")
assert_instance_of(Puppet::Parser::LoadedCode, out)
assert_nil(parser.hostclass(""), "Got a 'main' class when we had no code")
end
# Now try something a touch more complicated
parser.initvars
assert_nothing_raised do
out = parser.parse "Exec { path => '/usr/bin:/usr/sbin' }"
assert_instance_of(Puppet::Parser::Parser::ASTSet, out)
assert_equal("", parser.classes[""].classname)
assert_equal("", parser.classes[""].namespace)
assert_instance_of(Puppet::Parser::LoadedCode, out)
assert_equal("", parser.hostclass("").classname)
assert_equal("", parser.hostclass("").namespace)
end
end
@ -482,8 +482,8 @@ file { "/tmp/yayness":
ret = parser.parse("#{at}file { '/tmp/testing': owner => root }")
end
assert_instance_of(AST::ASTArray, ret.classes[""].code)
resdef = ret.classes[""].code[0]
assert_instance_of(AST::ASTArray, ret.hostclass("").code)
resdef = ret.hostclass("").code[0]
assert_instance_of(AST::Resource, resdef)
assert_equal("/tmp/testing", resdef.title.value)
# We always get an astarray back, so...
@ -494,7 +494,7 @@ file { "/tmp/yayness":
ret = parser.parse("#{at}file { ['/tmp/1', '/tmp/2']: owner => root }")
end
ret.classes[""].each do |res|
ret.hostclass("").each do |res|
assert_instance_of(AST::Resource, res)
check.call(res, "multiresource")
end
@ -531,7 +531,7 @@ file { "/tmp/yayness":
ret = parser.parse("File #{arrow}")
end
coll = ret.classes[""].code[0]
coll = ret.hostclass("").code[0]
assert_instance_of(AST::Collection, coll)
assert_equal(form, coll.form)
end
@ -551,7 +551,7 @@ file { "/tmp/yayness":
res = nil
assert_nothing_raised do
res = parser.parse(str).classes[""].code[0]
res = parser.parse(str).hostclass("").code[0]
end
assert_instance_of(AST::Collection, res)
@ -574,7 +574,7 @@ file { "/tmp/yayness":
res = nil
assert_nothing_raised do
res = parser.parse(str).classes[""].code[0]
res = parser.parse(str).hostclass("").code[0]
end
assert_instance_of(AST::Collection, res)
@ -598,7 +598,7 @@ file { "/tmp/yayness":
res = nil
assert_nothing_raised("Could not parse '#{test}'") do
res = parser.parse(str).classes[""].code[0]
res = parser.parse(str).hostclass("").code[0]
end
assert_instance_of(AST::Collection, res)
@ -642,8 +642,8 @@ file { "/tmp/yayness":
assert_nothing_raised("Could not parse fully-qualified definition") {
parser.parse %{define one::two { }}
}
assert(parser.definitions["one::two"], "Could not find one::two with no namespace")
assert(parser.definition("one::two"), "Could not find one::two with no namespace")
# Now try using the definition
assert_nothing_raised("Could not parse fully-qualified definition usage") {
parser.parse %{one::two { yayness: }}
@ -782,7 +782,7 @@ file { "/tmp/yayness":
result = parser.parse %{$variable = undef}
}
main = result.classes[""].code
main = result.hostclass("").code
children = main.children
assert_instance_of(AST::VarDef, main.children[0])
assert_instance_of(AST::Undef, main.children[0].value)
@ -797,12 +797,12 @@ file { "/tmp/yayness":
assert_nothing_raised("Could not parse") do
result = parser.parse(str)
end
assert_instance_of(Puppet::Parser::Parser::ASTSet, result, "Did not get a ASTSet back from parsing")
assert_instance_of(Puppet::Parser::LoadedCode, result, "Did not get a ASTSet back from parsing")
assert_instance_of(AST::HostClass, result.classes["yay"], "Did not create 'yay' class")
assert_instance_of(AST::HostClass, result.classes[""], "Did not create main class")
assert_instance_of(AST::Definition, result.definitions["bar"], "Did not create 'bar' definition")
assert_instance_of(AST::Node, result.nodes["foo"], "Did not create 'foo' node")
assert_instance_of(AST::HostClass, result.hostclass("yay"), "Did not create 'yay' class")
assert_instance_of(AST::HostClass, result.hostclass(""), "Did not create main class")
assert_instance_of(AST::Definition, result.definition("bar"), "Did not create 'bar' definition")
assert_instance_of(AST::Node, result.node("foo"), "Did not create 'foo' node")
end
# Make sure our node gets added to the node table.
@ -814,7 +814,7 @@ file { "/tmp/yayness":
parser.newnode("mynode", :code => :yay)
}
assert_equal(:yay, parser.nodes["mynode"].code)
assert_equal(:yay, parser.node("mynode").code)
# Now make sure that trying to redefine it throws an error.
assert_raise(Puppet::ParseError) {
@ -830,8 +830,8 @@ file { "/tmp/yayness":
parser.newnode(:foo)
# And make sure we get things back correctly
assert_equal(:foo, parser.nodes["simplenode"].parentclass)
assert_nil(parser.nodes["simplenode"].code)
assert_equal(:foo, parser.node("simplenode").parentclass)
assert_nil(parser.node("simplenode").code)
# Now make sure that trying to redefine it throws an error.
assert_raise(Puppet::ParseError) {
@ -845,8 +845,8 @@ file { "/tmp/yayness":
}
names.each do |name|
assert_equal(:yay, parser.nodes[name].code)
assert_equal(:foo, parser.nodes[name].parentclass)
assert_equal(:yay, parser.node(name).code)
assert_equal(:foo, parser.node(name).parentclass)
# Now make sure that trying to redefine it throws an error.
assert_raise(Puppet::ParseError) {
parser.newnode(name, {})
@ -862,7 +862,7 @@ file { "/tmp/yayness":
:arguments => ["a", stringobj("b")])
}
mydefine = parser.definitions["mydefine"]
mydefine = parser.definition("mydefine")
assert(mydefine, "Could not find definition")
assert_equal("", mydefine.namespace)
assert_equal("mydefine", mydefine.classname)
@ -877,9 +877,9 @@ file { "/tmp/yayness":
parser.newdefine("other::mydefine", :code => :other,
:arguments => ["a", stringobj("b")])
}
other = parser.definitions["other::mydefine"]
other = parser.definition("other::mydefine")
assert(other, "Could not find definition")
assert(parser.definitions["other::mydefine"],
assert(parser.definition("other::mydefine"),
"Could not find other::mydefine")
assert_equal(:other, other.code)
assert_equal("other", other.namespace)
@ -908,10 +908,10 @@ file { "/tmp/yayness":
assert(klass, "Did not return class")
assert(parser.classes["myclass"], "Could not find definition")
assert_equal("myclass", parser.classes["myclass"].classname)
assert(parser.hostclass("myclass"), "Could not find definition")
assert_equal("myclass", parser.hostclass("myclass").classname)
assert_equal(%w{original code},
parser.classes["myclass"].code.evaluate(scope))
parser.hostclass("myclass").code.evaluate(scope))
# Newclass behaves differently than the others -- it just appends
# the code to the existing class.
@ -921,7 +921,7 @@ file { "/tmp/yayness":
end
assert(klass, "Did not return class when appending")
assert_equal(%w{original code something new},
parser.classes["myclass"].code.evaluate(scope))
parser.hostclass("myclass").code.evaluate(scope))
# Now create the same class name in a different scope
assert_nothing_raised {
@ -929,7 +929,7 @@ file { "/tmp/yayness":
:code => mkcode.call(%w{something diff}))
}
assert(klass, "Did not return class")
other = parser.classes["other::myclass"]
other = parser.hostclass("other::myclass")
assert(other, "Could not find class")
assert_equal("other::myclass", other.classname)
assert_equal("other::myclass", other.namespace)
@ -945,7 +945,7 @@ file { "/tmp/yayness":
end
assert(klass, "Did not return class with no code")
assert_equal(%w{yay test},
parser.classes["nocode"].code.evaluate(scope))
parser.hostclass("nocode").code.evaluate(scope))
# Then try merging something into nothing
parser.newclass("nocode2", :code => mkcode.call(%w{foo test}))
@ -956,7 +956,7 @@ file { "/tmp/yayness":
end
assert(klass, "Did not return class with no code")
assert_equal(%w{foo test},
parser.classes["nocode2"].code.evaluate(scope))
parser.hostclass("nocode2").code.evaluate(scope))
# And lastly, nothing and nothing
klass = parser.newclass("nocode3")
@ -966,7 +966,7 @@ file { "/tmp/yayness":
klass = parser.newclass("nocode3")
end
assert(klass, "Did not return class with no code")
assert_nil(parser.classes["nocode3"].code)
assert_nil(parser.hostclass("nocode3").code)
end
# Make sure you can't have classes and defines with the same name in the
@ -1015,8 +1015,8 @@ file { "/tmp/yayness":
assert_nothing_raised {
parser.newclass("sub")
}
assert(parser.classes["sub"], "Could not find definition")
assert_nil(parser.classes["sub"].parentclass)
assert(parser.hostclass("sub"), "Could not find definition")
assert_nil(parser.hostclass("sub").parentclass)
# Make sure we can't set the parent class to ourself.
assert_raise(Puppet::ParseError) {
@ -1030,7 +1030,7 @@ file { "/tmp/yayness":
# Make sure we get the right parent class, and make sure it's not an object.
assert_equal("base1",
parser.classes["sub"].parentclass)
parser.hostclass("sub").parentclass)
# Now make sure we get a failure if we try to conflict.
assert_raise(Puppet::ParseError) {
@ -1039,45 +1039,16 @@ file { "/tmp/yayness":
# Make sure that failure didn't screw us up in any way.
assert_equal("base1",
parser.classes["sub"].parentclass)
parser.hostclass("sub").parentclass)
# But make sure we can create a class with a fq parent
assert_nothing_raised {
parser.newclass("another", :parent => "one::two::three")
}
assert_equal("one::two::three",
parser.classes["another"].parentclass)
parser.hostclass("another").parentclass)
end
def test_fqfind
parser = mkparser
table = {}
# Define a bunch of things.
%w{a c a::b a::b::c a::c a::b::c::d a::b::c::d::e::f c::d}.each do |string|
table[string] = string
end
check = proc do |namespace, hash|
hash.each do |thing, result|
assert_equal(result, parser.fqfind(namespace, thing, table),
"Could not find %s in %s" % [thing, namespace])
end
end
# Now let's do some test lookups.
# First do something really simple
check.call "a", "b" => "a::b", "b::c" => "a::b::c", "d" => nil, "::c" => "c"
check.call "a::b", "c" => "a::b::c", "b" => "a::b", "a" => "a"
check.call "a::b::c::d::e", "c" => "a::b::c", "::c" => "c",
"c::d" => "a::b::c::d", "::c::d" => "c::d"
check.call "", "a" => "a", "a::c" => "a::c"
end
# Setup a module.
def mk_module(name, files = {})
mdir = File.join(@dir, name)
@ -1100,6 +1071,7 @@ file { "/tmp/yayness":
end
end
end
system("find %s" % mandir)
end
# #596 - make sure classes and definitions load automatically if they're in modules, so we don't have to manually load each one.
@ -1112,20 +1084,20 @@ file { "/tmp/yayness":
parser = mkparser
# Make sure we fail like normal for actually missing classes
assert_nil(parser.findclass("", "nosuchclass"), "Did not return nil on missing classes")
assert_nil(parser.find_hostclass("", "nosuchclass"), "Did not return nil on missing classes")
# test the simple case -- the module class itself
name = "simple"
mk_module(name, :init => [name])
# Try to load the module automatically now
klass = parser.findclass("", name)
klass = parser.find_hostclass("", name)
assert_instance_of(AST::HostClass, klass, "Did not autoload class from module init file")
assert_equal(name, klass.classname, "Incorrect class was returned")
# Try loading the simple module when we're in something other than the base namespace.
parser = mkparser
klass = parser.findclass("something::else", name)
klass = parser.find_hostclass("something::else", name)
assert_instance_of(AST::HostClass, klass, "Did not autoload class from module init file")
assert_equal(name, klass.classname, "Incorrect class was returned")
@ -1133,7 +1105,7 @@ file { "/tmp/yayness":
name = "simpdef"
mk_module(name, :define => true, :init => [name])
klass = parser.finddefine("", name)
klass = parser.find_definition("", name)
assert_instance_of(AST::Definition, klass, "Did not autoload class from module init file")
assert_equal(name, klass.classname, "Incorrect class was returned")
@ -1144,13 +1116,13 @@ file { "/tmp/yayness":
mk_module(modname, :init => %w{both both::sub})
# First try it with a namespace
klass = parser.findclass("both", name)
klass = parser.find_hostclass("both", name)
assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from module init file with a namespace")
assert_equal("both::sub", klass.classname, "Incorrect class was returned")
# Now try it using the fully qualified name
parser = mkparser
klass = parser.findclass("", "both::sub")
klass = parser.find_hostclass("", "both::sub")
assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from module init file with no namespace")
assert_equal("both::sub", klass.classname, "Incorrect class was returned")
@ -1162,13 +1134,13 @@ file { "/tmp/yayness":
mk_module(modname, :init => %w{separate}, :sub => %w{separate::sub})
# First try it with a namespace
klass = parser.findclass("separate", name)
klass = parser.find_hostclass("separate", name)
assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from separate file with a namespace")
assert_equal("separate::sub", klass.classname, "Incorrect class was returned")
# Now try it using the fully qualified name
parser = mkparser
klass = parser.findclass("", "separate::sub")
klass = parser.find_hostclass("", "separate::sub")
assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from separate file with no namespace")
assert_equal("separate::sub", klass.classname, "Incorrect class was returned")
@ -1180,14 +1152,14 @@ file { "/tmp/yayness":
# First try it with a namespace
assert_nothing_raised("Could not autoload file when module file is missing") do
klass = parser.findclass("alone", name)
klass = parser.find_hostclass("alone", name)
end
assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from alone file with a namespace")
assert_equal("alone::sub", klass.classname, "Incorrect class was returned")
# Now try it using the fully qualified name
parser = mkparser
klass = parser.findclass("", "alone::sub")
klass = parser.find_hostclass("", "alone::sub")
assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from alone file with no namespace")
assert_equal("alone::sub", klass.classname, "Incorrect class was returned")
@ -1195,7 +1167,7 @@ file { "/tmp/yayness":
name = "mymod"
mk_module(name, :define => true, :mydefine => ["mymod::mydefine"])
klass = parser.finddefine("", "mymod::mydefine")
klass = parser.find_definition("", "mymod::mydefine")
assert_instance_of(AST::Definition, klass, "Did not autoload definition from its own file")
assert_equal("mymod::mydefine", klass.classname, "Incorrect definition was returned")
end
@ -1208,12 +1180,12 @@ file { "/tmp/yayness":
assert_nothing_raised do
result = parser.newclass "Yayness"
end
assert_equal(result, parser.findclass("", "yayNess"))
assert_equal(result, parser.find_hostclass("", "yayNess"))
assert_nothing_raised do
result = parser.newdefine "FunTest"
end
assert_equal(result, parser.finddefine("", "fUntEst"),
assert_equal(result, parser.find_definition("", "fUntEst"),
"%s was not matched" % "fUntEst")
end

Просмотреть файл

@ -125,7 +125,7 @@ class TestResource < PuppetTest::TestCase
"owner" => varref("name"), "mode" => varref("title"))
klass = parser.findclass("", "")
klass = parser.find_hostclass("", "")
should = {:name => :owner, :title => :mode}
[
{:name => "one", :title => "two"},

Просмотреть файл

@ -417,16 +417,16 @@ Host <<||>>"
"Did not add extra namespace correctly")
end
def test_findclass_and_finddefine
def test_find_hostclass_and_find_definition
parser = mkparser
# Make sure our scope calls the parser findclass method with
# Make sure our scope calls the parser find_hostclass method with
# the right namespaces
scope = mkscope :parser => parser
parser.metaclass.send(:attr_accessor, :last)
methods = [:findclass, :finddefine]
methods = [:find_hostclass, :find_definition]
methods.each do |m|
parser.meta_def(m) do |namespace, name|
@checked ||= []

Просмотреть файл

@ -65,7 +65,7 @@ module PuppetTest::ParserTesting
def mkscope(hash = {})
hash[:parser] ||= mkparser
compiler ||= mkcompiler(hash[:parser])
compiler.topscope.source = (hash[:parser].findclass("", "") || hash[:parser].newclass(""))
compiler.topscope.source = (hash[:parser].find_hostclass("", "") || hash[:parser].newclass(""))
unless compiler.topscope.source
raise "Could not find source for scope"