We now use references to the ResourceTypeCollection instances through the environment, which is much cleaner.
The next step is to remove the Interpreter class. Signed-off-by: Luke Kanies <[email protected]> --- lib/puppet/node.rb | 24 ++------- lib/puppet/parser/compiler.rb | 26 ++++----- lib/puppet/parser/interpreter.rb | 3 +- lib/puppet/parser/resource/reference.rb | 20 ++++--- lib/puppet/parser/resource_type.rb | 1 + .../parser/resource_type_collection_helper.rb | 2 + lib/puppet/parser/scope.rb | 17 ++++--- lib/puppet/parser/templatewrapper.rb | 6 +-- spec/integration/parser/compiler.rb | 2 +- spec/unit/node.rb | 34 +++++-------- spec/unit/parser/compiler.rb | 54 ++++++++++++-------- spec/unit/parser/interpreter.rb | 14 +---- spec/unit/parser/resource.rb | 31 ++++++++--- spec/unit/parser/resource/reference.rb | 45 ++++++++++++---- spec/unit/parser/resource_type.rb | 26 ++++++---- spec/unit/parser/scope.rb | 26 +++++++-- spec/unit/parser/templatewrapper.rb | 20 ++++++-- 17 files changed, 201 insertions(+), 150 deletions(-) diff --git a/lib/puppet/node.rb b/lib/puppet/node.rb index ad5a40b..721c9f5 100644 --- a/lib/puppet/node.rb +++ b/lib/puppet/node.rb @@ -13,23 +13,8 @@ class Puppet::Node indirects :node, :terminus_setting => :node_terminus, :doc => "Where to find node information. A node is composed of its name, its facts, and its environment." - attr_accessor :name, :classes, :parameters, :source, :ipaddress - attr_reader :time - attr_writer :environment - - # Do not return environments that are the empty string, and use - # explicitly set environments, then facts, then a central env - # value. - def environment - unless @environment - if env = parameters["environment"] - @environment = env - else - @environment = Puppet::Node::Environment.new.name.to_s - end - end - @environment - end + attr_accessor :name, :classes, :source, :ipaddress, :environment + attr_reader :time, :parameters def initialize(name, options = {}) unless name @@ -49,7 +34,8 @@ class Puppet::Node @parameters = options[:parameters] || {} - self.environment = options[:environment] if options[:environment] + env = options[:environment] || @parameters[:environment] || @parameters["environment"] || Puppet::Node::Environment.new + @environment = env.is_a?(String) ? Puppet::Node::Environment.new(env) : env @time = Time.now end @@ -73,7 +59,7 @@ class Puppet::Node @parameters[name] = value unless @parameters.include?(name) end - @parameters["environment"] ||= self.environment if self.environment + @parameters["environment"] ||= self.environment.name.to_s if self.environment end # Calculate the list of names we might use for looking diff --git a/lib/puppet/parser/compiler.rb b/lib/puppet/parser/compiler.rb index 6b6cd68..d3e7eb3 100644 --- a/lib/puppet/parser/compiler.rb +++ b/lib/puppet/parser/compiler.rb @@ -5,12 +5,16 @@ require 'puppet/node' require 'puppet/resource/catalog' require 'puppet/util/errors' +require 'puppet/parser/resource_type_collection_helper' + # Maintain a graph of scopes, along with a bunch of data # about the individual catalog we're compiling. class Puppet::Parser::Compiler include Puppet::Util include Puppet::Util::Errors - attr_reader :parser, :node, :facts, :collections, :catalog, :node_scope, :resources + include Puppet::Parser::ResourceTypeCollectionHelper + + attr_reader :node, :facts, :collections, :catalog, :node_scope, :resources # Add a collection to the global list. def add_collection(coll) @@ -48,7 +52,7 @@ class Puppet::Parser::Compiler # Do we use nodes found in the code, vs. the external node sources? def ast_nodes? - parser.nodes? + known_resource_types.nodes? end # Store the fact that we've evaluated a class, and store a reference to @@ -159,13 +163,8 @@ class Puppet::Parser::Compiler @catalog.resource(*args) end - # Set up our compile. We require a parser - # and a node object; the parser is so we can look up classes - # and AST nodes, and the node has all of the client's info, - # like facts and environment. - def initialize(node, parser, options = {}) + def initialize(node, options = {}) @node = node - @parser = parser options.each do |param, value| begin @@ -185,7 +184,6 @@ class Puppet::Parser::Compiler def newscope(parent, options = {}) parent ||= topscope options[:compiler] = self - options[:parser] ||= self.parser scope = Puppet::Parser::Scope.new(options) @scope_graph.add_edge(parent, scope) scope @@ -221,10 +219,10 @@ class Puppet::Parser::Compiler # Now see if we can find the node. astnode = nil @node.names.each do |name| - break if astnode = @parser.node(name.to_s.downcase) + break if astnode = known_resource_types.node(name.to_s.downcase) end - unless (astnode ||= @parser.node("default")) + unless (astnode ||= known_resource_types.node("default")) raise Puppet::ParseError, "Could not find default node or by name with '%s'" % node.names.join(", ") end @@ -302,7 +300,7 @@ class Puppet::Parser::Compiler # Find and evaluate our main object, if possible. def evaluate_main - @main = @parser.find_hostclass("", "") || @parser.newclass("") + @main = known_resource_types.find_hostclass("", "") || known_resource_types.add(Puppet::Parser::ResourceType.new(:hostclass, "")) @topscope.source = @main @main_resource = Puppet::Parser::Resource.new(:type => "class", :title => :main, :scope => @topscope, :source => @main) @topscope.resource = @main_resource @@ -383,7 +381,7 @@ class Puppet::Parser::Compiler # Initialize the top-level scope, class, and resource. def init_main # Create our initial scope and a resource that will evaluate main. - @topscope = Puppet::Parser::Scope.new(:compiler => self, :parser => self.parser) + @topscope = Puppet::Parser::Scope.new(:compiler => self) @scope_graph.add_vertex(@topscope) end @@ -411,7 +409,7 @@ class Puppet::Parser::Compiler # For maintaining the relationship between scopes and their resources. @catalog = Puppet::Resource::Catalog.new(@node.name) - @catalog.version = @parser.version + @catalog.version = known_resource_types.version # local resource array to maintain resource ordering @resources = [] diff --git a/lib/puppet/parser/interpreter.rb b/lib/puppet/parser/interpreter.rb index 1b15820..eea9afc 100644 --- a/lib/puppet/parser/interpreter.rb +++ b/lib/puppet/parser/interpreter.rb @@ -19,9 +19,8 @@ class Puppet::Parser::Interpreter # evaluate our whole tree def compile(node) - raise Puppet::ParseError, "Could not parse configuration; cannot compile on node %s" % node.name unless env_parser = parser(node.environment) begin - return Puppet::Parser::Compiler.new(node, env_parser).compile.to_resource + return Puppet::Parser::Compiler.new(node).compile.to_resource rescue => detail puts detail.backtrace if Puppet[:trace] raise Puppet::Error, detail.to_s + " on node %s" % node.name diff --git a/lib/puppet/parser/resource/reference.rb b/lib/puppet/parser/resource/reference.rb index 3ef9812..bb50efd 100644 --- a/lib/puppet/parser/resource/reference.rb +++ b/lib/puppet/parser/resource/reference.rb @@ -3,12 +3,15 @@ require 'puppet/resource/reference' require 'puppet/file_collection/lookup' require 'puppet/parser/yaml_trimmer' +require 'puppet/parser/resource_type_collection_helper' + # A reference to a resource. Mostly just the type and title. class Puppet::Parser::Resource::Reference < Puppet::Resource::Reference include Puppet::Parser::YamlTrimmer include Puppet::FileCollection::Lookup include Puppet::Util::MethodHelper include Puppet::Util::Errors + include Puppet::Parser::ResourceTypeCollectionHelper attr_accessor :builtin, :file, :line, :scope @@ -39,21 +42,18 @@ class Puppet::Parser::Resource::Reference < Puppet::Resource::Reference unless defined? @definedtype case self.type when "Class" # look for host classes - if self.title == :main - tmp = @scope.find_hostclass("") - else - unless tmp = @scope.parser.hostclass(self.title) - fail Puppet::ParseError, "Could not find class '%s'" % self.title - end + name = self.title == :main ? "" : self.title + unless tmp = known_resource_types.find_hostclass("", name) + fail Puppet::ParseError, "Could not find '#{title}' class" end when "Node" # look for node definitions - unless tmp = @scope.parser.node(self.title) + unless tmp = known_resource_types.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.definition(self.type.downcase) + tmp = known_resource_types.definition(self.type.downcase) end if tmp @@ -66,6 +66,10 @@ class Puppet::Parser::Resource::Reference < Puppet::Resource::Reference @definedtype end + def environment + scope.environment + end + def initialize(hash) set_options(hash) requiredopts(:type, :title) diff --git a/lib/puppet/parser/resource_type.rb b/lib/puppet/parser/resource_type.rb index a679659..7070fcb 100644 --- a/lib/puppet/parser/resource_type.rb +++ b/lib/puppet/parser/resource_type.rb @@ -27,6 +27,7 @@ class Puppet::Parser::ResourceType def evaluate_code(resource) # Create a new scope. scope = subscope(resource.scope, resource) + scope.compiler.class_set(name, scope) set_resource_parameters(resource, scope) diff --git a/lib/puppet/parser/resource_type_collection_helper.rb b/lib/puppet/parser/resource_type_collection_helper.rb index 4f66c77..51ccdc0 100644 --- a/lib/puppet/parser/resource_type_collection_helper.rb +++ b/lib/puppet/parser/resource_type_collection_helper.rb @@ -1,3 +1,5 @@ +require 'puppet/parser/resource_type_collection' + module Puppet::Parser::ResourceTypeCollectionHelper def known_resource_types environment.known_resource_types diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index d6d6630..dcf502c 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -6,7 +6,10 @@ require 'puppet/parser/templatewrapper' require 'puppet/transportable' require 'strscan' +require 'puppet/parser/resource_type_collection_helper' + class Puppet::Parser::Scope + include Puppet::Parser::ResourceTypeCollectionHelper require 'puppet/parser/resource' AST = Puppet::Parser::AST @@ -15,7 +18,7 @@ class Puppet::Parser::Scope include Enumerable include Puppet::Util::Errors - attr_accessor :level, :parser, :source, :resource + attr_accessor :level, :source, :resource attr_accessor :base, :keyword, :nodescope attr_accessor :top, :translated, :compiler attr_writer :parent @@ -25,15 +28,15 @@ class Puppet::Parser::Scope compiler.catalog end + def environment + compiler.environment + end + # Proxy accessors def host @compiler.node.name end - def interpreter - @compiler.interpreter - end - # Is the value true? This allows us to control the definition of truth # in one place. def self.true?(value) @@ -84,7 +87,7 @@ class Puppet::Parser::Scope def find_hostclass(name) @namespaces.each do |namespace| - if r = parser.find_hostclass(namespace, name) + if r = known_resource_types.find_hostclass(namespace, name) return r end end @@ -93,7 +96,7 @@ class Puppet::Parser::Scope def find_definition(name) @namespaces.each do |namespace| - if r = parser.find_definition(namespace, name) + if r = known_resource_types.find_definition(namespace, name) return r end end diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb index b2eb3c4..61c74e9 100644 --- a/lib/puppet/parser/templatewrapper.rb +++ b/lib/puppet/parser/templatewrapper.rb @@ -67,14 +67,12 @@ class Puppet::Parser::TemplateWrapper end def file=(filename) - unless @file = Puppet::Parser::Files.find_template(filename, scope.compiler.environment) + unless @file = Puppet::Parser::Files.find_template(filename, scope.compiler.environment.to_s) raise Puppet::ParseError, "Could not find template '%s'" % filename end # We'll only ever not have a parser in testing, but, eh. - if scope.parser - scope.parser.watch_file(file) - end + scope.known_resource_types.watch_file(file) @string = File.read(file) end diff --git a/spec/integration/parser/compiler.rb b/spec/integration/parser/compiler.rb index 7ce24f5..512924f 100755 --- a/spec/integration/parser/compiler.rb +++ b/spec/integration/parser/compiler.rb @@ -22,7 +22,7 @@ describe Puppet::Parser::Compiler do Puppet.settings[:config_version] = 'git rev-parse HEAD' @parser = Puppet::Parser::Parser.new "development" - @compiler = Puppet::Parser::Compiler.new(@node, @parser) + @compiler = Puppet::Parser::Compiler.new(@node) @compiler.catalog.version.should == version end diff --git a/spec/unit/node.rb b/spec/unit/node.rb index 025bdc8..c2350da 100755 --- a/spec/unit/node.rb +++ b/spec/unit/node.rb @@ -44,29 +44,22 @@ describe Puppet::Node, "when initializing" do @node.classes.should == ["myclass"] end - it "should accept an environment value" do - Puppet.settings.stubs(:value).with(:environment).returns("myenv") - @node = Puppet::Node.new("testing", :environment => "myenv") - @node.environment.should == "myenv" + it "should use any specified environment" do + env = Puppet::Node::Environment.new("foo") + + Puppet::Node.new("testnode", :environment => env).environment.should equal(env) end -end -describe Puppet::Node, "when returning the environment" do - before do - Puppet.settings.stubs(:value).with(:environment).returns("one,two") - Puppet.settings.stubs(:value).with(:environment).returns("one") - @node = Puppet::Node.new("testnode") + it "should convert an environment specified as a string into an Environment instance" do + Puppet::Node.new("testnode", :environment => "foo").environment.should be_instance_of(Puppet::Node::Environment) end - it "should return the 'environment' fact if present and there is no explicit environment" do - @node.parameters = {"environment" => "two"} - @node.environment.should == "two" + it "should return the 'environment' parameter if present and there is no explicit environment" do + Puppet::Node.new("testnode", :parameters => {"environment" => "two"}).environment.name.should == Puppet::Node::Environment.new("two").name end it "should use the default environment if there is no environment fact nor explicit environment" do - env = mock 'environment', :name => :myenv - Puppet::Node::Environment.expects(:new).returns(env) - @node.environment.should == "myenv" + @node.environment.name.should == Puppet::Node::Environment.new.name end end @@ -82,19 +75,19 @@ describe Puppet::Node, "when merging facts" do end it "should prefer parameters already set on the node over facts from the node" do - @node.parameters = {"one" => "a"} + @node = Puppet::Node.new("testnode", :parameters => {"one" => "a"}) @node.fact_merge @node.parameters["one"].should == "a" end it "should add passed parameters to the parameter list" do - @node.parameters = {"one" => "a"} + @node = Puppet::Node.new("testnode", :parameters => {"one" => "a"}) @node.fact_merge @node.parameters["two"].should == "b" end it "should accept arbitrary parameters to merge into its parameters" do - @node.parameters = {"one" => "a"} + @node = Puppet::Node.new("testnode", :parameters => {"one" => "a"}) @node.merge "two" => "three" @node.parameters["two"].should == "three" end @@ -139,8 +132,7 @@ end describe Puppet::Node, "when generating the list of names to search through" do before do - @node = Puppet::Node.new("foo.domain.com") - @node.parameters = {"hostname" => "yay", "domain" => "domain.com"} + @node = Puppet::Node.new("foo.domain.com", :parameters => {"hostname" => "yay", "domain" => "domain.com"}) end it "should return an array of names" do diff --git a/spec/unit/parser/compiler.rb b/spec/unit/parser/compiler.rb index 27511d0..85a51ed 100755 --- a/spec/unit/parser/compiler.rb +++ b/spec/unit/parser/compiler.rb @@ -33,11 +33,20 @@ end describe Puppet::Parser::Compiler do before :each do @node = Puppet::Node.new "testnode" - @parser = Puppet::Parser::Parser.new "development" + @known_resource_types = Puppet::Parser::ResourceTypeCollection.new "development" @scope_resource = stub 'scope_resource', :builtin? => true, :finish => nil, :ref => 'Class[main]', :type => "class" @scope = stub 'scope', :resource => @scope_resource, :source => mock("source") - @compiler = Puppet::Parser::Compiler.new(@node, @parser) + @compiler = Puppet::Parser::Compiler.new(@node) + @compiler.environment.stubs(:known_resource_types).returns @known_resource_types + end + + it "should use the node's environment as its environment" do + @compiler.environment.should equal(@node.environment) + end + + it "should include the resource type collection helper" do + Puppet::Parser::Compiler.ancestors.should be_include(Puppet::Parser::ResourceTypeCollectionHelper) end it "should be able to store references to class scopes" do @@ -69,28 +78,23 @@ describe Puppet::Parser::Compiler do it "should set its node attribute" do @compiler.node.should equal(@node) end - - it "should set its parser attribute" do - @compiler.parser.should equal(@parser) - end - it "should detect when ast nodes are absent" do @compiler.ast_nodes?.should be_false end it "should detect when ast nodes are present" do - @parser.expects(:nodes?).returns true + @known_resource_types.expects(:nodes?).returns true @compiler.ast_nodes?.should be_true end - it "should copy the parser version to the catalog" do - @compiler.catalog.version.should == @parser.version + it "should copy the known_resource_types version to the catalog" do + @compiler.catalog.version.should == @known_resource_types.version end it "should copy any node classes into the class list" do node = Puppet::Node.new("mynode") node.classes = %w{foo bar} - compiler = Puppet::Parser::Compiler.new(node, @parser) + compiler = Puppet::Parser::Compiler.new(node) compiler.classlist.should include("foo") compiler.classlist.should include("bar") @@ -166,11 +170,17 @@ describe Puppet::Parser::Compiler do 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(:find_hostclass).with("", "").returns(main_class) + @known_resource_types.stubs(:find_hostclass).with("", "").returns(main_class) @compiler.compile end + it "should create a new, empty 'main' if no main class exists" do + compile_stub(:evaluate_main) + @compiler.compile + @known_resource_types.find_hostclass("", "").should be_instance_of(Puppet::Parser::ResourceType) + end + it "should evaluate any node classes" do @node.stubs(:classes).returns(%w{one two three four}) @compiler.expects(:evaluate_classes).with(%w{one two three four}, @compiler.topscope) @@ -467,7 +477,7 @@ describe Puppet::Parser::Compiler do it "should do nothing" do @compiler.expects(:ast_nodes?).returns(false) - @compiler.parser.expects(:nodes).never + @compiler.known_resource_types.expects(:nodes).never Puppet::Parser::Resource.expects(:new).never @compiler.send(:evaluate_ast_node) @@ -477,16 +487,16 @@ describe Puppet::Parser::Compiler do describe "when evaluating AST nodes with AST nodes present" do before do - @compiler.parser.stubs(:nodes?).returns true + @compiler.known_resource_types.stubs(:nodes?).returns true # Set some names for our test @node.stubs(:names).returns(%w{a b c}) - @compiler.parser.stubs(:node).with("a").returns(nil) - @compiler.parser.stubs(:node).with("b").returns(nil) - @compiler.parser.stubs(:node).with("c").returns(nil) + @compiler.known_resource_types.stubs(:node).with("a").returns(nil) + @compiler.known_resource_types.stubs(:node).with("b").returns(nil) + @compiler.known_resource_types.stubs(:node).with("c").returns(nil) # It should check this last, of course. - @compiler.parser.stubs(:node).with("default").returns(nil) + @compiler.known_resource_types.stubs(:node).with("default").returns(nil) end it "should fail if the named node cannot be found" do @@ -495,7 +505,7 @@ describe Puppet::Parser::Compiler do it "should evaluate the first node class matching the node name" do node_class = stub 'node', :name => "c", :evaluate_code => nil - @compiler.parser.stubs(:node).with("c").returns(node_class) + @compiler.known_resource_types.stubs(:node).with("c").returns(node_class) node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil, :type => "node" node_class.expects(:mk_plain_resource).returns(node_resource) @@ -505,7 +515,7 @@ describe Puppet::Parser::Compiler do it "should match the default node if no matching node can be found" do node_class = stub 'node', :name => "default", :evaluate_code => nil - @compiler.parser.stubs(:node).with("default").returns(node_class) + @compiler.known_resource_types.stubs(:node).with("default").returns(node_class) node_resource = stub 'node resource', :ref => "Node[default]", :evaluate => nil, :type => "node" node_class.expects(:mk_plain_resource).returns(node_resource) @@ -515,7 +525,7 @@ describe Puppet::Parser::Compiler do it "should evaluate the node resource immediately rather than using lazy evaluation" do node_class = stub 'node', :name => "c" - @compiler.parser.stubs(:node).with("c").returns(node_class) + @compiler.known_resource_types.stubs(:node).with("c").returns(node_class) node_resource = stub 'node resource', :ref => "Node[c]", :type => "node" node_class.expects(:mk_plain_resource).returns(node_resource) @@ -529,7 +539,7 @@ describe Puppet::Parser::Compiler do node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil, :type => "node" node_class = stub 'node', :name => "c", :mk_plain_resource => node_resource - @compiler.parser.stubs(:node).with("c").returns(node_class) + @compiler.known_resource_types.stubs(:node).with("c").returns(node_class) # The #evaluate method normally does this. scope = stub 'scope', :source => "mysource" diff --git a/spec/unit/parser/interpreter.rb b/spec/unit/parser/interpreter.rb index 56ad71a..99a0a4b 100755 --- a/spec/unit/parser/interpreter.rb +++ b/spec/unit/parser/interpreter.rb @@ -105,28 +105,20 @@ describe Puppet::Parser::Interpreter do describe "when compiling a catalog" do before do - @node = stub 'node', :environment => :myenv + @node = Puppet::Node.new("foo") @compiler = mock 'compile' end - it "should create a compile with the node and parser" do + it "should create a compile with the node" do catalog = stub 'catalog', :to_resource => nil @compiler.expects(:compile).returns(catalog) - @interp.expects(:parser).with(:myenv).returns(@parser) - Puppet::Parser::Compiler.expects(:new).with(@node, @parser).returns(@compiler) + Puppet::Parser::Compiler.expects(:new).with(@node).returns(@compiler) @interp.compile(@node) end - it "should fail intelligently when no parser can be found" do - @node.stubs(:name).returns("whatever") - @interp.expects(:parser).with(:myenv).returns(nil) - proc { @interp.compile(@node) }.should raise_error(Puppet::ParseError) - end - it "should return the results of the compile, converted to a plain resource catalog" do catalog = mock 'catalog' @compiler.expects(:compile).returns(catalog) - @interp.stubs(:parser).returns(@parser) Puppet::Parser::Compiler.stubs(:new).returns(@compiler) catalog.expects(:to_resource).returns "my_resource_catalog" diff --git a/spec/unit/parser/resource.rb b/spec/unit/parser/resource.rb index 913fd1e..3ccdd0a 100755 --- a/spec/unit/parser/resource.rb +++ b/spec/unit/parser/resource.rb @@ -7,10 +7,11 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe Puppet::Parser::Resource do before do - @parser = Puppet::Parser::Parser.new :Code => "" - @source = @parser.newclass "" @node = Puppet::Node.new("yaynode") - @compiler = Puppet::Parser::Compiler.new(@node, @parser) + @known_resource_types = Puppet::Parser::ResourceTypeCollection.new("env") + @compiler = Puppet::Parser::Compiler.new(@node) + @compiler.environment.stubs(:known_resource_types).returns @known_resource_types + @source = newclass "" @scope = @compiler.topscope end @@ -44,6 +45,18 @@ describe Puppet::Parser::Resource do end end + def newclass(name) + @known_resource_types.add Puppet::Parser::ResourceType.new(:hostclass, name) + end + + def newdefine(name) + @known_resource_types.add Puppet::Parser::ResourceType.new(:definition, name) + end + + def newnode(name) + @known_resource_types.add Puppet::Parser::ResourceType.new(:node, name) + end + it "should use the file lookup module" do Puppet::Parser::Resource.ancestors.should be_include(Puppet::FileCollection::Lookup) end @@ -59,7 +72,7 @@ describe Puppet::Parser::Resource do end it "should be isomorphic if it is not builtin" do - @parser.newdefine "whatever" + newdefine "whatever" @resource = Puppet::Parser::Resource.new(:type => "whatever", :title => "whatever", :scope => @scope, :source => @source).isomorphic?.should be_true end @@ -115,9 +128,9 @@ describe Puppet::Parser::Resource do before do @type = Puppet::Parser::Resource - @definition = @parser.newdefine "mydefine" - @class = @parser.newclass "myclass" - @nodedef = @parser.newnode("mynode")[0] + @definition = newdefine "mydefine" + @class = newclass "myclass" + @nodedef = newnode("mynode") end it "should evaluate the associated AST definition" do @@ -142,8 +155,8 @@ describe Puppet::Parser::Resource do describe "when finishing" do before do - @class = @parser.newclass "myclass" - @nodedef = @parser.newnode("mynode")[0] + @class = newclass "myclass" + @nodedef = newnode("mynode") @resource = Puppet::Parser::Resource.new(:type => "file", :title => "whatever", :scope => @scope, :source => @source) end diff --git a/spec/unit/parser/resource/reference.rb b/spec/unit/parser/resource/reference.rb index 064c51b..2e200d1 100755 --- a/spec/unit/parser/resource/reference.rb +++ b/spec/unit/parser/resource/reference.rb @@ -7,6 +7,16 @@ describe Puppet::Parser::Resource::Reference do @type = Puppet::Parser::Resource::Reference end + it "should get its environment from its scope" do + env = stub 'environment' + scope = stub 'scope', :environment => env + @type.new(:title => "foo", :type => "bar", :scope => scope).environment.should equal(env) + end + + it "should use the resource type collection helper to find its known resource types" do + Puppet::Parser::Resource::Reference.ancestors.should include(Puppet::Parser::ResourceTypeCollectionHelper) + end + it "should use the file lookup module" do Puppet::Parser::Resource::Reference.ancestors.should be_include(Puppet::FileCollection::Lookup) end @@ -52,16 +62,29 @@ describe Puppet::Parser::Resource::Reference do end describe Puppet::Parser::Resource::Reference, " when modeling defined types" do + def newclass(name) + @known_resource_types.add Puppet::Parser::ResourceType.new(:hostclass, name) + end + + def newdefine(name) + @known_resource_types.add Puppet::Parser::ResourceType.new(:definition, name) + end + + def newnode(name) + @known_resource_types.add Puppet::Parser::ResourceType.new(:node, name) + end + before do @type = Puppet::Parser::Resource::Reference - @parser = Puppet::Parser::Parser.new :Code => "" - @definition = @parser.newdefine "mydefine" - @class = @parser.newclass "myclass" - @nodedef = @parser.newnode("mynode")[0] + @known_resource_types = Puppet::Parser::ResourceTypeCollection.new("myenv") + @definition = newdefine("mydefine") + @class = newclass("myclass") + @nodedef = newnode("mynode") @node = Puppet::Node.new("yaynode") - @compiler = Puppet::Parser::Compiler.new(@node, @parser) + @compiler = Puppet::Parser::Compiler.new(@node) + @compiler.environment.stubs(:known_resource_types).returns @known_resource_types end it "should be able to find defined types" do @@ -83,20 +106,20 @@ describe Puppet::Parser::Resource::Reference, " when modeling defined types" do end it "should only look for fully qualified classes" do - top = @parser.newclass "top" - sub = @parser.newclass "other::top" + top = newclass "top" + sub = newclass "other::top" - scope = @compiler.topscope.class.new(:parent => @compiler.topscope, :namespace => "other", :parser => @parser) + scope = @compiler.topscope.class.new(:parent => @compiler.topscope, :namespace => "other", :compiler => @compiler) ref = @type.new(:type => "class", :title => "top", :scope => scope) ref.definedtype.name.should equal(top.name) end it "should only look for fully qualified definitions" do - top = @parser.newdefine "top" - sub = @parser.newdefine "other::top" + top = newdefine "top" + sub = newdefine "other::top" - scope = @compiler.topscope.class.new(:parent => @compiler.topscope, :namespace => "other", :parser => @parser) + scope = @compiler.topscope.class.new(:parent => @compiler.topscope, :namespace => "other", :compiler => @compiler) ref = @type.new(:type => "top", :title => "foo", :scope => scope) ref.definedtype.name.should equal(top.name) diff --git a/spec/unit/parser/resource_type.rb b/spec/unit/parser/resource_type.rb index fad300b..24a1e38 100755 --- a/spec/unit/parser/resource_type.rb +++ b/spec/unit/parser/resource_type.rb @@ -322,24 +322,33 @@ describe Puppet::Parser::ResourceType do describe "when evaluating its code" do before do - @scope = stub 'scope', :newscope => nil, :setvar => nil + @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("mynode")) + @scope = Puppet::Parser::Scope.new :compiler => @compiler @resource = stub 'resource', :title => "yay", :name => "yea", :ref => "Foo[bar]", :scope => @scope @type = Puppet::Parser::ResourceType.new(:hostclass, "foo") @type.stubs(:set_resource_parameters) end it "should set all of its parameters in a subscope" do - subscope = stub 'subscope' + subscope = stub 'subscope', :compiler => @compiler @type.expects(:subscope).with(@scope, @resource).returns subscope @type.expects(:set_resource_parameters).with(@resource, subscope) @type.evaluate_code(@resource) end + it "should store the class scope" do + subscope = stub 'subscope', :compiler => @compiler + @type.expects(:subscope).with(@scope, @resource).returns subscope + + @type.evaluate_code(@resource) + @compiler.class_scope(@type).should equal(subscope) + end + it "should evaluate the code if any is provided" do code = stub 'code' - @type.expects(:code).returns code - @type.stubs(:subscope).returns stub("subscope") + @type.stubs(:code).returns code + @type.stubs(:subscope).returns stub("subscope", :compiler => @compiler) code.expects(:safeevaluate).with @type.subscope @type.evaluate_code(@resource) @@ -347,7 +356,6 @@ describe Puppet::Parser::ResourceType do it "should noop if there is no code" do @type.expects(:code).returns nil - @type.stubs(:subscope).returns stub("subscope") @type.evaluate_code(@resource) end @@ -355,11 +363,9 @@ describe Puppet::Parser::ResourceType do describe "when creating a resource" do before do - @catalog = Puppet::Resource::Catalog.new - @node = stub 'node', :name => "foo", :classes => [] - @compiler = Puppet::Parser::Compiler.new(@node, @catalog) - @scope = Puppet::Parser::Scope.new - @scope.stubs(:compiler).returns @compiler + @node = Puppet::Node.new("foo") + @compiler = Puppet::Parser::Compiler.new(@node) + @scope = Puppet::Parser::Scope.new(:compiler => @compiler) @top = Puppet::Parser::ResourceType.new :hostclass, "top" @middle = Puppet::Parser::ResourceType.new :hostclass, "middle", :parent => "top" diff --git a/spec/unit/parser/scope.rb b/spec/unit/parser/scope.rb index d7800e4..17ee7db 100755 --- a/spec/unit/parser/scope.rb +++ b/spec/unit/parser/scope.rb @@ -11,6 +11,17 @@ describe Puppet::Parser::Scope do @scope.parent = @topscope end + it "should get its environment from its compiler" do + env = stub 'environment' + compiler = stub 'compiler', :environment => env + scope = Puppet::Parser::Scope.new :compiler => compiler + scope.environment.should equal(env) + end + + it "should use the resource type collection helper to find its known resource types" do + Puppet::Parser::Scope.ancestors.should include(Puppet::Parser::ResourceTypeCollectionHelper) + end + describe "when looking up a variable" do it "should default to an empty string" do @scope.lookupvar("var").should == "" @@ -42,14 +53,17 @@ describe Puppet::Parser::Scope do describe "and the variable is qualified" do before do - @parser = Puppet::Parser::Parser.new() - @compiler = Puppet::Parser::Compiler.new(stub("node", :name => "foonode", :classes => []), @parser) + @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foonode")) @scope.compiler = @compiler - @scope.parser = @parser + @known_resource_types = @scope.known_resource_types + end + + def newclass(name) + @known_resource_types.add Puppet::Parser::ResourceType.new(:hostclass, name) end def create_class_scope(name) - klass = @parser.newclass(name) + klass = newclass(name) Puppet::Parser::Resource.new(:type => "class", :title => name, :scope => @scope, :source => mock('source')).evaluate return @compiler.class_scope(klass) @@ -86,7 +100,7 @@ describe Puppet::Parser::Scope do end it "should warn and return an empty string for qualified variables whose classes have not been evaluated" do - klass = @parser.newclass("other::deep::klass") + klass = newclass("other::deep::klass") @scope.expects(:warning) @scope.lookupvar("other::deep::klass::var").should == "" end @@ -103,7 +117,7 @@ describe Puppet::Parser::Scope do it "should return ':undefined' when asked for a non-string qualified variable from a class that has not been evaluated" do @scope.stubs(:warning) - klass = @parser.newclass("other::deep::klass") + klass = newclass("other::deep::klass") @scope.lookupvar("other::deep::klass::var", false).should == :undefined end end diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb index a725952..b1f9d2a 100755 --- a/spec/unit/parser/templatewrapper.rb +++ b/spec/unit/parser/templatewrapper.rb @@ -4,9 +4,11 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe Puppet::Parser::TemplateWrapper do before(:each) do - compiler = stub('compiler', :environment => "foo") - parser = stub('parser', :watch_file => true) - @scope = stub('scope', :compiler => compiler, :parser => parser, :to_hash => {}) + @known_resource_types = Puppet::Parser::ResourceTypeCollection.new("env") + @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("mynode")) + @compiler.environment.stubs(:known_resource_types).returns @known_resource_types + @scope = Puppet::Parser::Scope.new :compiler => @compiler + @file = "fake_template" Puppet::Parser::Files.stubs(:find_template).returns("/tmp/fake_template") FileTest.stubs(:exists?).returns("true") @@ -21,14 +23,22 @@ describe Puppet::Parser::TemplateWrapper do end it "should check template file existance and read its content" do - Puppet::Parser::Files.expects(:find_template).with("fake_template", "foo").returns("/tmp/fake_template") + Puppet::Parser::Files.expects(:find_template).with("fake_template", @scope.environment.to_s).returns("/tmp/fake_template") File.expects(:read).with("/tmp/fake_template").returns("template content") @tw.file = @file end + it "should mark the file for watching" do + Puppet::Parser::Files.expects(:find_template).returns("/tmp/fake_template") + File.stubs(:read) + + @known_resource_types.expects(:watch_file).with("/tmp/fake_template") + @tw.file = @file + end + it "should fail if a template cannot be found" do - Puppet::Parser::Files.expects(:find_template).with("fake_template", "foo").returns nil + Puppet::Parser::Files.expects(:find_template).returns nil lambda { @tw.file = @file }.should raise_error(Puppet::ParseError) end -- 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.
