diff options
author | Luke Kanies <luke@reductivelabs.com> | 2010-01-06 16:08:13 -0800 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | 26b272b218b02115ce66edbc6dd4cffd231105ab (patch) | |
tree | f420988cfd3dba0f141e840367fd2635f467b543 | |
parent | cb169082ea4d8764c25e81183754c862fd170492 (diff) | |
download | puppet-26b272b218b02115ce66edbc6dd4cffd231105ab.tar.gz puppet-26b272b218b02115ce66edbc6dd4cffd231105ab.tar.xz puppet-26b272b218b02115ce66edbc6dd4cffd231105ab.zip |
Parser now uses Environment resource type collection
This is the last step enabling us to make it so no one
needs to maintain these references to the parser. Instead,
everyone will just get access to the type collection from
the Environment.
Signed-off-by: Luke Kanies <luke@reductivelabs.com>
-rw-r--r-- | lib/puppet/parser/parser_support.rb | 52 | ||||
-rwxr-xr-x | spec/unit/parser/parser.rb | 54 |
2 files changed, 53 insertions, 53 deletions
diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb index 652090062..3b0b1916d 100644 --- a/lib/puppet/parser/parser_support.rb +++ b/lib/puppet/parser/parser_support.rb @@ -5,7 +5,6 @@ class Puppet::Parser::Parser require 'puppet/parser/files' require 'puppet/parser/resource_type_collection' require 'puppet/parser/resource_type' - require 'puppet/dsl' require 'monitor' AST = Puppet::Parser::AST @@ -92,16 +91,15 @@ class Puppet::Parser::Parser raise Puppet::Error, "Could not find file %s" % file end end - if check_and_add_to_watched_files(file) - @lexer.file = file - else - raise Puppet::AlreadyImportedError.new("Import loop detected") - end + raise Puppet::AlreadyImportedError, "Import loop detected" if resource_type_collection.watching_file?(file) + + watch_file(file) + @lexer.file = file end [:hostclass, :definition, :node, :nodes?].each do |method| define_method(method) do |*args| - @resource_type_collection.send(method, *args) + resource_type_collection.send(method, *args) end end @@ -134,7 +132,7 @@ class Puppet::Parser::Parser names_to_try.compact! end - until (result = @resource_type_collection.send(method, namespace, name)) or names_to_try.empty? do + until (result = resource_type_collection.send(method, namespace, name)) or names_to_try.empty? do self.load(names_to_try.shift) end return result @@ -174,7 +172,7 @@ class Puppet::Parser::Parser end files.collect { |file| - parser = Puppet::Parser::Parser.new(:resource_type_collection => @resource_type_collection, :environment => @environment) + parser = Puppet::Parser::Parser.new(:environment => @environment) parser.files = self.files Puppet.debug("importing '%s'" % file) @@ -193,9 +191,11 @@ class Puppet::Parser::Parser } end - def initialize(options = {}) - @environment = options[:environment] - @resource_type_collection = options[:resource_type_collection] || Puppet::Parser::ResourceTypeCollection.new(@environment) + def initialize(env) + puts caller and raise "fix your calling of Parser.new" if env.is_a?(Hash) + + # The environment is needed to know how to find the resource type collection. + @environment = env.is_a?(String) ? Puppet::Node::Environment.new(env) : env initvars() end @@ -252,7 +252,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 @resource_type_collection.hostclass(classname) || @resource_type_collection.definition(classname) + return resource_type_collection.hostclass(classname) || resource_type_collection.definition(classname) end # Try to load a class, since we could not find it. @@ -277,12 +277,12 @@ class Puppet::Parser::Parser # Create a new class, or merge with an existing class. def newclass(name, options = {}) - @resource_type_collection.add Puppet::Parser::ResourceType.new(:hostclass, name, ast_context(true).merge(options)) + resource_type_collection.add Puppet::Parser::ResourceType.new(:hostclass, name, ast_context(true).merge(options)) end # Create a new definition. def newdefine(name, options = {}) - @resource_type_collection.add Puppet::Parser::ResourceType.new(:definition, name, ast_context(true).merge(options)) + resource_type_collection.add Puppet::Parser::ResourceType.new(:definition, name, ast_context(true).merge(options)) end # Create a new node. Nodes are special, because they're stored in a global @@ -291,7 +291,7 @@ class Puppet::Parser::Parser names = [names] unless names.instance_of?(Array) context = ast_context(true) names.collect do |name| - @resource_type_collection.add(Puppet::Parser::ResourceType.new(:node, name, context.merge(options))) + resource_type_collection.add(Puppet::Parser::ResourceType.new(:node, name, context.merge(options))) end end @@ -355,7 +355,7 @@ class Puppet::Parser::Parser # Store the results as the top-level class. newclass("", :code => main) end - return @resource_type_collection + return resource_type_collection ensure @lexer.clear end @@ -377,25 +377,19 @@ class Puppet::Parser::Parser @lexer.string = string end - def version - return @version if defined?(@version) - - if Puppet[:config_version] == "" - @version = Time.now.to_i - return @version - end - - @version = Puppet::Util.execute([Puppet[:config_version]]).strip + def resource_type_collection + environment.known_resource_types + end - rescue Puppet::ExecutionFailure => e - raise Puppet::ParseError, "Unable to set config_version: #{e.message}" + def version + resource_type_collection.version end # Add a new file to be checked when we're checking to see if we should be # reparsed. This is basically only used by the TemplateWrapper to let the # parser know about templates that should be parsed. def watch_file(filename) - check_and_add_to_watched_files(filename) + resource_type_collection.watch_file(filename) end private diff --git a/spec/unit/parser/parser.rb b/spec/unit/parser/parser.rb index 16e469fd8..2933b2d9b 100755 --- a/spec/unit/parser/parser.rb +++ b/spec/unit/parser/parser.rb @@ -8,15 +8,36 @@ describe Puppet::Parser do before :each do @resource_type_collection = Puppet::Parser::ResourceTypeCollection.new("development") - @parser = Puppet::Parser::Parser.new :environment => "development", :resource_type_collection => @resource_type_collection + @parser = Puppet::Parser::Parser.new "development" + @parser.stubs(:resource_type_collection).returns @resource_type_collection @true_ast = Puppet::Parser::AST::Boolean.new :value => true end + it "should require an environment at initialization" do + lambda { Puppet::Parser::Parser.new }.should raise_error(ArgumentError) + end + + it "should set the environment" do + env = Puppet::Node::Environment.new + Puppet::Parser::Parser.new(env).environment.should == env + end + + it "should convert the environment into an environment instance if a string is provided" do + env = Puppet::Node::Environment.new("testing") + Puppet::Parser::Parser.new("testing").environment.should == env + end + + it "should be able to look up the environment-specific resource type collection" do + rtc = Puppet::Node::Environment.new("development").known_resource_types + parser = Puppet::Parser::Parser.new "development" + parser.resource_type_collection.should equal(rtc) + end + describe "when parsing files" do before do FileTest.stubs(:exist?).returns true File.stubs(:open) - @parser.stubs(:check_and_add_to_watched_files).returns true + @parser.stubs(:watch_file) end it "should treat files ending in 'rb' as ruby files" do @@ -299,27 +320,10 @@ describe Puppet::Parser do end describe "when determining the configuration version" do - it "should default to the current time" do - time = Time.now - - Time.stubs(:now).returns time - @parser.version.should == time.to_i - end - - it "should use the output of the config_version setting if one is provided" do - Puppet.settings.stubs(:[]).with(:config_version).returns("/my/foo") - - Puppet::Util.expects(:execute).with(["/my/foo"]).returns "output\n" - @parser.version.should == "output" + it "should determine it from the resource type collection" do + @parser.resource_type_collection.expects(:version).returns "foo" + @parser.version.should == "foo" end - - it "should raise a puppet parser error if executing config_version fails" do - Puppet.settings.stubs(:[]).with(:config_version).returns("test") - Puppet::Util.expects(:execute).raises(Puppet::ExecutionFailure.new("msg")) - - lambda { @parser.version }.should raise_error(Puppet::ParseError) - end - end describe "when looking up definitions" do @@ -341,7 +345,8 @@ describe Puppet::Parser do @resource_type_collection = mock 'loaded code' @resource_type_collection.stubs(:find_my_type).with('loaded_namespace', 'loaded_name').returns(true) @resource_type_collection.stubs(:find_my_type).with('bogus_namespace', 'bogus_name' ).returns(false) - @parser = Puppet::Parser::Parser.new :environment => "development",:resource_type_collection => @resource_type_collection + @parser = Puppet::Parser::Parser.new "development" + @parser.stubs(:resource_type_collection).returns @resource_type_collection end describe "that are already loaded" do @@ -392,7 +397,8 @@ describe Puppet::Parser do describe "when loading classnames" do before :each do @resource_type_collection = mock 'loaded code' - @parser = Puppet::Parser::Parser.new :environment => "development",:resource_type_collection => @resource_type_collection + @parser = Puppet::Parser::Parser.new "development" + @parser.stubs(:resource_type_collection).returns @resource_type_collection end it "should just return false if the classname is empty" do |