summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser
diff options
context:
space:
mode:
authorLuke Kanies <luke@reductivelabs.com>2010-01-06 17:42:42 -0800
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commitb82b4ef04282ca0006931562f60459a1591b6268 (patch)
tree31b77674c07a382a46af8773a81826a6e52c20b3 /spec/unit/parser
parent644ad7e41880fe8c9e73a099e9d6644e19828f46 (diff)
downloadpuppet-b82b4ef04282ca0006931562f60459a1591b6268.tar.gz
puppet-b82b4ef04282ca0006931562f60459a1591b6268.tar.xz
puppet-b82b4ef04282ca0006931562f60459a1591b6268.zip
All non-transient parser references are gone
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 <luke@reductivelabs.com>
Diffstat (limited to 'spec/unit/parser')
-rwxr-xr-xspec/unit/parser/compiler.rb54
-rwxr-xr-xspec/unit/parser/interpreter.rb14
-rwxr-xr-xspec/unit/parser/resource.rb31
-rwxr-xr-xspec/unit/parser/resource/reference.rb45
-rwxr-xr-xspec/unit/parser/resource_type.rb26
-rwxr-xr-xspec/unit/parser/scope.rb26
-rwxr-xr-xspec/unit/parser/templatewrapper.rb20
7 files changed, 142 insertions, 74 deletions
diff --git a/spec/unit/parser/compiler.rb b/spec/unit/parser/compiler.rb
index a2b2e4d51..cf73f4d3b 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 return a class list containing all added classes" do
@@ -53,28 +62,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")
@@ -157,11 +161,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)
@@ -462,7 +472,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)
@@ -472,16 +482,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
@@ -490,7 +500,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)
@@ -500,7 +510,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)
@@ -510,7 +520,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)
@@ -524,7 +534,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 56ad71a4a..99a0a4be8 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 2b57f7898..eb2cd490c 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
@@ -126,9 +139,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
@@ -153,8 +166,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 a09c436c2..1034dbdda 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
@@ -59,16 +69,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
@@ -90,20 +113,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 8a8bfb5de..816f86176 100755
--- a/spec/unit/parser/resource_type.rb
+++ b/spec/unit/parser/resource_type.rb
@@ -327,24 +327,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)
@@ -352,7 +361,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
@@ -360,11 +368,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 ba5f80670..b48bd1246 100755
--- a/spec/unit/parser/scope.rb
+++ b/spec/unit/parser/scope.rb
@@ -46,6 +46,17 @@ describe Puppet::Parser::Scope do
end
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 == ""
@@ -82,14 +93,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 @scope.class_scope(klass)
@@ -126,7 +140,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
@@ -143,7 +157,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 a72595279..b1f9d2ad9 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