summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
authorJesse Wolfe <jes5199@gmail.com>2010-11-03 19:58:38 -0700
committerJesse Wolfe <jes5199@gmail.com>2010-11-03 19:59:37 -0700
commit9e2a0e41dfb253a19180aeea6b66f65ca8d63133 (patch)
treef102b396669c074b59eab5c2e59c5145b5bae6ab /spec/unit
parentb1ef091d0209a59ac747568f83416e992db93ea8 (diff)
parent85543a41978924a42490d0c3f1f5437c95b7c869 (diff)
downloadpuppet-9e2a0e41dfb253a19180aeea6b66f65ca8d63133.tar.gz
puppet-9e2a0e41dfb253a19180aeea6b66f65ca8d63133.tar.xz
puppet-9e2a0e41dfb253a19180aeea6b66f65ca8d63133.zip
Merge commit '85543a4'
This updates `master` to the pre-agile-iteration state.
Diffstat (limited to 'spec/unit')
-rwxr-xr-xspec/unit/application/apply_spec.rb11
-rw-r--r--spec/unit/application/master_spec.rb9
-rwxr-xr-xspec/unit/dsl/resource_type_api_spec.rb56
-rwxr-xr-xspec/unit/node/environment_spec.rb59
-rwxr-xr-xspec/unit/parser/ast/astarray_spec.rb49
-rw-r--r--spec/unit/parser/ast/definition_spec.rb22
-rw-r--r--spec/unit/parser/ast/hostclass_spec.rb73
-rw-r--r--spec/unit/parser/ast/node_spec.rb31
-rwxr-xr-xspec/unit/parser/ast/resource_spec.rb29
-rwxr-xr-xspec/unit/parser/parser_spec.rb97
-rwxr-xr-xspec/unit/parser/scope_spec.rb3
-rw-r--r--spec/unit/parser/type_loader_spec.rb96
-rw-r--r--spec/unit/resource/type_collection_spec.rb144
-rwxr-xr-xspec/unit/resource/type_spec.rb5
-rwxr-xr-xspec/unit/util/rdoc/parser_spec.rb22
-rwxr-xr-xspec/unit/util/zaml_spec.rb25
16 files changed, 426 insertions, 305 deletions
diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb
index 85098f490..edb41b5c3 100755
--- a/spec/unit/application/apply_spec.rb
+++ b/spec/unit/application/apply_spec.rb
@@ -142,17 +142,16 @@ describe Puppet::Application::Apply do
describe "the parseonly command" do
before :each do
- Puppet.stubs(:[]).with(:environment)
+ @environment = Puppet::Node::Environment.new("env")
+ Puppet.stubs(:[]).with(:environment).returns(@environment)
Puppet.stubs(:[]).with(:manifest).returns("site.pp")
Puppet.stubs(:err)
@apply.stubs(:exit)
@apply.options.stubs(:[]).with(:code).returns "some code"
- @collection = stub_everything
- Puppet::Resource::TypeCollection.stubs(:new).returns(@collection)
end
- it "should use a Puppet Resource Type Collection to parse the file" do
- @collection.expects(:perform_initial_import)
+ it "should use the environment to parse the file" do
+ @environment.stubs(:perform_initial_import)
@apply.parseonly
end
@@ -162,7 +161,7 @@ describe Puppet::Application::Apply do
end
it "should exit with exit code 1 if error" do
- @collection.stubs(:perform_initial_import).raises(Puppet::ParseError)
+ @environment.stubs(:perform_initial_import).raises(Puppet::ParseError)
@apply.expects(:exit).with(1)
@apply.parseonly
end
diff --git a/spec/unit/application/master_spec.rb b/spec/unit/application/master_spec.rb
index 216c7dc90..e657445a4 100644
--- a/spec/unit/application/master_spec.rb
+++ b/spec/unit/application/master_spec.rb
@@ -257,16 +257,15 @@ describe Puppet::Application::Master do
describe "the parseonly command" do
before :each do
- Puppet.stubs(:[]).with(:environment)
+ @environment = Puppet::Node::Environment.new("env")
+ Puppet.stubs(:[]).with(:environment).returns(@environment)
Puppet.stubs(:[]).with(:manifest).returns("site.pp")
Puppet.stubs(:err)
@master.stubs(:exit)
- @collection = stub_everything
- Puppet::Resource::TypeCollection.stubs(:new).returns(@collection)
end
it "should use a Puppet Resource Type Collection to parse the file" do
- @collection.expects(:perform_initial_import)
+ @environment.expects(:perform_initial_import)
@master.parseonly
end
@@ -276,7 +275,7 @@ describe Puppet::Application::Master do
end
it "should exit with exit code 1 if error" do
- @collection.stubs(:perform_initial_import).raises(Puppet::ParseError)
+ @environment.stubs(:perform_initial_import).raises(Puppet::ParseError)
@master.expects(:exit).with(1)
@master.parseonly
end
diff --git a/spec/unit/dsl/resource_type_api_spec.rb b/spec/unit/dsl/resource_type_api_spec.rb
index 4f4eb7e01..c9a5d272f 100755
--- a/spec/unit/dsl/resource_type_api_spec.rb
+++ b/spec/unit/dsl/resource_type_api_spec.rb
@@ -5,62 +5,50 @@ require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/dsl/resource_type_api'
describe Puppet::DSL::ResourceTypeAPI do
- # Run the given block in the context of a new ResourceTypeAPI
- # object.
+ # Verify that the block creates a single AST node through the API,
+ # instantiate that AST node into a types, and return that type.
def test_api_call(&block)
- Thread.current[:known_resource_types] = Puppet::Resource::TypeCollection.new(:env)
- Puppet::DSL::ResourceTypeAPI.new.instance_eval(&block)
+ main_object = Puppet::DSL::ResourceTypeAPI.new
+ main_object.instance_eval(&block)
+ created_ast_objects = main_object.instance_eval { @__created_ast_objects__ }
+ created_ast_objects.length.should == 1
+ new_types = created_ast_objects[0].instantiate('')
+ new_types.length.should == 1
+ new_types[0]
ensure
- Thread.current[:known_resource_types] = nil
+ Thread.current[:ruby_file_parse_result] = nil
end
[:definition, :node, :hostclass].each do |type|
method = type == :definition ? "define" : type
it "should be able to create a #{type}" do
- newtype = Puppet::Resource::Type.new(:hostclass, "foo")
- Puppet::Resource::Type.expects(:new).with { |t, n, args| t == type }.returns newtype
- test_api_call { send(method, "myname") }
+ newtype = test_api_call { send(method, "myname").should == nil }
+ newtype.should be_a(Puppet::Resource::Type)
+ newtype.type.should == type
end
it "should use the provided name when creating a #{type}" do
- type = Puppet::Resource::Type.new(:hostclass, "foo")
- Puppet::Resource::Type.expects(:new).with { |t, n, args| n == "myname" }.returns type
- test_api_call { send(method, "myname") }
+ newtype = test_api_call { send(method, "myname") }
+ newtype.name.should == "myname"
end
unless type == :definition
- it "should pass in any provided options" do
- type = Puppet::Resource::Type.new(:hostclass, "foo")
- Puppet::Resource::Type.expects(:new).with { |t, n, args| args == {:myarg => :myvalue} }.returns type
- test_api_call { send(method, "myname", :myarg => :myvalue) }
+ it "should pass in any provided options when creating a #{type}" do
+ newtype = test_api_call { send(method, "myname", :line => 200) }
+ newtype.line.should == 200
end
end
it "should set any provided block as the type's ruby code" do
- Puppet::Resource::Type.any_instance.expects(:ruby_code=).with { |blk| blk.call == 'foo' }
- test_api_call { send(method, "myname") { 'foo' } }
- end
-
- it "should add the type to the current environment's known resource types" do
- begin
- newtype = Puppet::Resource::Type.new(:hostclass, "foo")
- Puppet::Resource::Type.expects(:new).returns newtype
- known_resource_types = Puppet::Resource::TypeCollection.new(:env)
- Thread.current[:known_resource_types] = known_resource_types
- known_resource_types.expects(:add).with(newtype)
- Puppet::DSL::ResourceTypeAPI.new.instance_eval { hostclass "myname" }
- ensure
- Thread.current[:known_resource_types] = nil
- end
+ newtype = test_api_call { send(method, "myname") { 'method_result' } }
+ newtype.ruby_code.call.should == 'method_result'
end
end
describe "when creating a definition" do
it "should use the provided options to define valid arguments for the resource type" do
- newtype = Puppet::Resource::Type.new(:definition, "foo")
- Puppet::Resource::Type.expects(:new).returns newtype
- test_api_call { define("myname", :arg1, :arg2) }
- newtype.instance_eval { @arguments }.should == { 'arg1' => nil, 'arg2' => nil }
+ newtype = test_api_call { define("myname", :arg1, :arg2) }
+ newtype.arguments.should == { 'arg1' => nil, 'arg2' => nil }
end
end
end
diff --git a/spec/unit/node/environment_spec.rb b/spec/unit/node/environment_spec.rb
index 6edcce56c..be2980879 100755
--- a/spec/unit/node/environment_spec.rb
+++ b/spec/unit/node/environment_spec.rb
@@ -52,7 +52,7 @@ describe Puppet::Node::Environment do
before do
@env = Puppet::Node::Environment.new("dev")
@collection = Puppet::Resource::TypeCollection.new(@env)
- @collection.stubs(:perform_initial_import)
+ @env.stubs(:perform_initial_import).returns(Puppet::Parser::AST::Hostclass.new(''))
Thread.current[:known_resource_types] = nil
end
@@ -66,9 +66,8 @@ describe Puppet::Node::Environment do
end
it "should perform the initial import when creating a new collection" do
- @collection.expects(:perform_initial_import)
- Puppet::Resource::TypeCollection.expects(:new).returns @collection
-
+ @env = Puppet::Node::Environment.new("dev")
+ @env.expects(:perform_initial_import).returns(Puppet::Parser::AST::Hostclass.new(''))
@env.known_resource_types
end
@@ -274,4 +273,56 @@ describe Puppet::Node::Environment do
@helper.environment.name.should == :foo
end
end
+
+ describe "when performing initial import" do
+ before do
+ @parser = stub 'parser', :file= => nil, :string => nil, :parse => nil
+ Puppet::Parser::Parser.stubs(:new).returns @parser
+ @env = Puppet::Node::Environment.new("env")
+ end
+
+ it "should create a new parser instance" do
+ Puppet::Parser::Parser.expects(:new).returns @parser
+ @env.instance_eval { perform_initial_import }
+ end
+
+ it "should set the parser's string to the 'code' setting and parse if code is available" do
+ Puppet.settings[:code] = "my code"
+ @parser.expects(:string=).with "my code"
+ @parser.expects(:parse)
+ @env.instance_eval { perform_initial_import }
+ end
+
+ it "should set the parser's file to the 'manifest' setting and parse if no code is available and the manifest is available" do
+ File.stubs(:expand_path).with("/my/file").returns "/my/file"
+ File.expects(:exist?).with("/my/file").returns true
+ Puppet.settings[:manifest] = "/my/file"
+ @parser.expects(:file=).with "/my/file"
+ @parser.expects(:parse)
+ @env.instance_eval { perform_initial_import }
+ end
+
+ it "should not attempt to load a manifest if none is present" do
+ File.stubs(:expand_path).with("/my/file").returns "/my/file"
+ File.expects(:exist?).with("/my/file").returns false
+ Puppet.settings[:manifest] = "/my/file"
+ @parser.expects(:file=).never
+ @parser.expects(:parse).never
+ @env.instance_eval { perform_initial_import }
+ end
+
+ it "should fail helpfully if there is an error importing" do
+ File.stubs(:exist?).returns true
+ @parser.expects(:parse).raises ArgumentError
+ lambda { @env.instance_eval { perform_initial_import } }.should raise_error(Puppet::Error)
+ end
+
+ it "should not do anything if the ignore_import settings is set" do
+ Puppet.settings[:ignoreimport] = true
+ @parser.expects(:string=).never
+ @parser.expects(:file=).never
+ @parser.expects(:parse).never
+ @env.instance_eval { perform_initial_import }
+ end
+ end
end
diff --git a/spec/unit/parser/ast/astarray_spec.rb b/spec/unit/parser/ast/astarray_spec.rb
index f79d6c533..8794848b6 100755
--- a/spec/unit/parser/ast/astarray_spec.rb
+++ b/spec/unit/parser/ast/astarray_spec.rb
@@ -23,43 +23,26 @@ describe Puppet::Parser::AST::ASTArray do
operator.evaluate(@scope)
end
- it "should evaluate childrens of type ASTArray" do
- item1 = stub "item1", :is_a? => true
- item2 = stub "item2"
- item2.stubs(:is_a?).with(Puppet::Parser::AST).returns(true)
- item2.stubs(:instance_of?).with(Puppet::Parser::AST::ASTArray).returns(true)
- item2.stubs(:each).yields(item1)
-
- item1.expects(:safeevaluate).with(@scope).returns(123)
-
- operator = Puppet::Parser::AST::ASTArray.new :children => [item2]
- operator.evaluate(@scope).should == [123]
- end
-
- it "should flatten children coming from children ASTArray" do
- item1 = stub "item1", :is_a? => true
- item2 = stub "item2"
- item2.stubs(:is_a?).with(Puppet::Parser::AST).returns(true)
- item2.stubs(:instance_of?).with(Puppet::Parser::AST::ASTArray).returns(true)
- item2.stubs(:each).yields([item1])
-
- item1.expects(:safeevaluate).with(@scope).returns(123)
-
- operator = Puppet::Parser::AST::ASTArray.new :children => [item2]
- operator.evaluate(@scope).should == [123]
+ it "should not flatten children coming from children ASTArray" do
+ item = Puppet::Parser::AST::String.new :value => 'foo'
+ inner_array = Puppet::Parser::AST::ASTArray.new :children => [item, item]
+ operator = Puppet::Parser::AST::ASTArray.new :children => [inner_array, inner_array]
+ operator.evaluate(@scope).should == [['foo', 'foo'], ['foo', 'foo']]
end
it "should not flatten the results of children evaluation" do
- item1 = stub "item1", :is_a? => true
- item2 = stub "item2"
- item2.stubs(:is_a?).with(Puppet::Parser::AST).returns(true)
- item2.stubs(:instance_of?).with(Puppet::Parser::AST::ASTArray).returns(true)
- item2.stubs(:each).yields([item1])
-
- item1.expects(:safeevaluate).with(@scope).returns([123])
+ item = Puppet::Parser::AST::String.new :value => 'foo'
+ item.stubs(:evaluate).returns(['foo'])
+ operator = Puppet::Parser::AST::ASTArray.new :children => [item, item]
+ operator.evaluate(@scope).should == [['foo'], ['foo']]
+ end
- operator = Puppet::Parser::AST::ASTArray.new :children => [item2]
- operator.evaluate(@scope).should == [[123]]
+ it "should discard nil results from children evaluation" do
+ item1 = Puppet::Parser::AST::String.new :value => 'foo'
+ item2 = Puppet::Parser::AST::String.new :value => 'foo'
+ item2.stubs(:evaluate).returns(nil)
+ operator = Puppet::Parser::AST::ASTArray.new :children => [item1, item2]
+ operator.evaluate(@scope).should == ['foo']
end
it "should return a valid string with to_s" do
diff --git a/spec/unit/parser/ast/definition_spec.rb b/spec/unit/parser/ast/definition_spec.rb
new file mode 100644
index 000000000..b7b2c851c
--- /dev/null
+++ b/spec/unit/parser/ast/definition_spec.rb
@@ -0,0 +1,22 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::Definition do
+ it "should make its context available through an accessor" do
+ definition = Puppet::Parser::AST::Definition.new('foo', :line => 5)
+ definition.context.should == {:line => 5}
+ end
+
+ describe "when instantiated" do
+ it "should create a definition with the proper type, name, context, and module name" do
+ definition = Puppet::Parser::AST::Definition.new('foo', :line => 5)
+ instantiated_definitions = definition.instantiate('modname')
+ instantiated_definitions.length.should == 1
+ instantiated_definitions[0].type.should == :definition
+ instantiated_definitions[0].name.should == 'foo'
+ instantiated_definitions[0].line.should == 5
+ instantiated_definitions[0].module_name.should == 'modname'
+ end
+ end
+end
diff --git a/spec/unit/parser/ast/hostclass_spec.rb b/spec/unit/parser/ast/hostclass_spec.rb
new file mode 100644
index 000000000..b22eba98b
--- /dev/null
+++ b/spec/unit/parser/ast/hostclass_spec.rb
@@ -0,0 +1,73 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::Hostclass do
+ def ast
+ Puppet::Parser::AST
+ end
+
+ def newarray(*elems)
+ ast::ASTArray.new({}).push(*elems)
+ end
+
+ it "should make its name and context available through accessors" do
+ hostclass = ast::Hostclass.new('foo', :line => 5)
+ hostclass.name.should == 'foo'
+ hostclass.context.should == {:line => 5}
+ end
+
+ it "should make its code available through an accessor" do
+ code = newarray
+ hostclass = ast::Hostclass.new('foo', :code => code)
+ hostclass.code.should be_equal(code)
+ end
+
+ describe "when instantiated" do
+ it "should create a class with the proper type, code, name, context, and module name" do
+ code = newarray
+ hostclass = ast::Hostclass.new('foo', :code => code, :line => 5)
+ instantiated_class = hostclass.instantiate('modname')[0]
+ instantiated_class.type.should == :hostclass
+ instantiated_class.name.should == 'foo'
+ instantiated_class.code.should be_equal(code)
+ instantiated_class.line.should == 5
+ instantiated_class.module_name.should == 'modname'
+ end
+
+ it "should instantiate all nested classes, defines, and nodes with the same module name." do
+ nested_objects = newarray(ast::Hostclass.new('foo::child1'),
+ ast::Definition.new('foo::child2'),
+ ast::Definition.new('child3'))
+ hostclass = ast::Hostclass.new('foo', :code => nested_objects)
+ instantiated_classes = hostclass.instantiate('modname')
+ instantiated_classes.length.should == 4
+ instantiated_classes[0].name.should == 'foo'
+ instantiated_classes[1].name.should == 'foo::child1'
+ instantiated_classes[2].name.should == 'foo::child2'
+ instantiated_classes[3].name.should == 'child3'
+ instantiated_classes.each { |cls| cls.module_name.should == 'modname' }
+ end
+
+ it "should handle a nested class that contains its own nested classes." do
+ foo_bar_baz = ast::Hostclass.new('foo::bar::baz')
+ foo_bar = ast::Hostclass.new('foo::bar', :code => newarray(foo_bar_baz))
+ foo = ast::Hostclass.new('foo', :code => newarray(foo_bar))
+ instantiated_classes = foo.instantiate('')
+ instantiated_classes.length.should == 3
+ instantiated_classes[0].name.should == 'foo'
+ instantiated_classes[1].name.should == 'foo::bar'
+ instantiated_classes[2].name.should == 'foo::bar::baz'
+ end
+
+ it "should skip nested elements that are not classes, definitions, or nodes." do
+ func = ast::Function.new(:name => 'biz', :arguments => newarray(ast::Name.new(:value => 'baz')))
+ foo = ast::Hostclass.new('foo', :code => newarray(func))
+ instantiated_classes = foo.instantiate('')
+ instantiated_classes.length.should == 1
+ instantiated_classes[0].should be_a(Puppet::Resource::Type)
+ instantiated_classes[0].name.should == 'foo'
+ end
+ end
+end
+
diff --git a/spec/unit/parser/ast/node_spec.rb b/spec/unit/parser/ast/node_spec.rb
new file mode 100644
index 000000000..3e8017de0
--- /dev/null
+++ b/spec/unit/parser/ast/node_spec.rb
@@ -0,0 +1,31 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::Node do
+ describe "when instantiated" do
+ it "should make its names and context available through accessors" do
+ node = Puppet::Parser::AST::Node.new(['foo', 'bar'], :line => 5)
+ node.names.should == ['foo', 'bar']
+ node.context.should == {:line => 5}
+ end
+
+ it "should create a node with the proper type, name, context, and module name" do
+ node = Puppet::Parser::AST::Node.new(['foo'], :line => 5)
+ instantiated_nodes = node.instantiate('modname')
+ instantiated_nodes.length.should == 1
+ instantiated_nodes[0].type.should == :node
+ instantiated_nodes[0].name.should == 'foo'
+ instantiated_nodes[0].line.should == 5
+ instantiated_nodes[0].module_name.should == 'modname'
+ end
+
+ it "should handle multiple names" do
+ node = Puppet::Parser::AST::Node.new(['foo', 'bar'])
+ instantiated_nodes = node.instantiate('modname')
+ instantiated_nodes.length.should == 2
+ instantiated_nodes[0].name.should == 'foo'
+ instantiated_nodes[1].name.should == 'bar'
+ end
+ end
+end
diff --git a/spec/unit/parser/ast/resource_spec.rb b/spec/unit/parser/ast/resource_spec.rb
index 5c94ac0e9..a8e783256 100755
--- a/spec/unit/parser/ast/resource_spec.rb
+++ b/spec/unit/parser/ast/resource_spec.rb
@@ -10,14 +10,15 @@ describe Puppet::Parser::AST::Resource do
@compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("mynode"))
@scope = Puppet::Parser::Scope.new(:compiler => @compiler)
@scope.stubs(:resource).returns(stub_everything)
- @resource = ast::Resource.new(:title => @title, :type => "file", :parameters => ast::ASTArray.new(:children => []) )
+ @instance = ast::ResourceInstance.new(:title => @title, :parameters => ast::ASTArray.new(:children => []))
+ @resource = ast::Resource.new(:type => "file", :instances => ast::ASTArray.new(:children => [@instance]))
@resource.stubs(:qualified_type).returns("Resource")
end
it "should evaluate all its parameters" do
param = stub 'param'
param.expects(:safeevaluate).with(@scope).returns Puppet::Parser::Resource::Param.new(:name => "myparam", :value => "myvalue", :source => stub("source"))
- @resource.stubs(:parameters).returns [param]
+ @instance.stubs(:parameters).returns [param]
@resource.evaluate(@scope)
end
@@ -34,7 +35,7 @@ describe Puppet::Parser::AST::Resource do
array = Puppet::Parser::AST::ASTArray.new(:children => titles)
- @resource.title = array
+ @instance.title = array
result = @resource.evaluate(@scope).collect { |r| r.title }
result.should be_include("one")
result.should be_include("two")
@@ -48,12 +49,19 @@ describe Puppet::Parser::AST::Resource do
array = Puppet::Parser::AST::ASTArray.new(:children => titles)
- @resource.title = array
+ @instance.title = array
result = @resource.evaluate(@scope).collect { |r| r.title }
result.should be_include("one")
result.should be_include("two")
end
+ it "should implicitly iterate over instances" do
+ new_title = Puppet::Parser::AST::String.new(:value => "other_title")
+ new_instance = ast::ResourceInstance.new(:title => new_title, :parameters => ast::ASTArray.new(:children => []))
+ @resource.instances.push(new_instance)
+ @resource.evaluate(@scope).collect { |r| r.title }.should == ["mytitle", "other_title"]
+ end
+
it "should handover resources to the compiler" do
titles = []
%w{one two}.each do |title|
@@ -62,7 +70,7 @@ describe Puppet::Parser::AST::Resource do
array = Puppet::Parser::AST::ASTArray.new(:children => titles)
- @resource.title = array
+ @instance.title = array
result = @resource.evaluate(@scope)
result.each do |res|
@@ -89,16 +97,19 @@ describe Puppet::Parser::AST::Resource do
before do
@scope = Puppet::Parser::Scope.new :compiler => Puppet::Parser::Compiler.new(Puppet::Node.new("mynode"))
@parser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new)
- @parser.newdefine "one"
- @parser.newdefine "one::two"
- @parser.newdefine "three"
+ ["one", "one::two", "three"].each do |name|
+ @parser.environment.known_resource_types.add(Puppet::Resource::Type.new(:definition, name, {}))
+ end
@twoscope = @scope.newscope(:namespace => "one")
@twoscope.resource = @scope.resource
end
def resource(type, params = nil)
params ||= Puppet::Parser::AST::ASTArray.new(:children => [])
- Puppet::Parser::AST::Resource.new(:type => type, :title => Puppet::Parser::AST::String.new(:value => "myresource"), :parameters => params)
+ instance = Puppet::Parser::AST::ResourceInstance.new(
+ :title => Puppet::Parser::AST::String.new(:value => "myresource"), :parameters => params)
+ Puppet::Parser::AST::Resource.new(:type => type,
+ :instances => Puppet::Parser::AST::ASTArray.new(:children => [instance]))
end
it "should be able to generate resources with fully qualified type information" do
diff --git a/spec/unit/parser/parser_spec.rb b/spec/unit/parser/parser_spec.rb
index f73e07a5c..b4e1518c4 100755
--- a/spec/unit/parser/parser_spec.rb
+++ b/spec/unit/parser/parser_spec.rb
@@ -64,14 +64,16 @@ describe Puppet::Parser do
lambda { @parser.parse("$var += ") }.should raise_error
end
- it "should call ast::VarDef with append=true" do
- ast::VarDef.expects(:new).with { |h| h[:append] == true }
- @parser.parse("$var += 2")
+ it "should create ast::VarDef with append=true" do
+ vardef = @parser.parse("$var += 2").code[0]
+ vardef.should be_a(Puppet::Parser::AST::VarDef)
+ vardef.append.should == true
end
it "should work with arrays too" do
- ast::VarDef.expects(:new).with { |h| h[:append] == true }
- @parser.parse("$var += ['test']")
+ vardef = @parser.parse("$var += ['test']").code[0]
+ vardef.should be_a(Puppet::Parser::AST::VarDef)
+ vardef.append.should == true
end
end
@@ -132,7 +134,6 @@ describe Puppet::Parser do
end
it "should create an ast::ResourceReference" do
- ast::Resource.stubs(:new)
ast::ResourceReference.expects(:new).with { |arg|
arg[:line]==1 and arg[:type]=="File" and arg[:title].is_a?(ast::ASTArray)
}
@@ -151,10 +152,14 @@ describe Puppet::Parser do
end
it "should create an ast::ResourceOverride" do
- ast::ResourceOverride.expects(:new).with { |arg|
- arg[:line]==1 and arg[:object].is_a?(ast::ResourceReference) and arg[:parameters].is_a?(ast::ResourceParam)
- }
- @parser.parse('Resource["title1","title2"] { param => value }')
+ #ast::ResourceOverride.expects(:new).with { |arg|
+ # arg[:line]==1 and arg[:object].is_a?(ast::ResourceReference) and arg[:parameters].is_a?(ast::ResourceParam)
+ #}
+ ro = @parser.parse('Resource["title1","title2"] { param => value }').code[0]
+ ro.should be_a(ast::ResourceOverride)
+ ro.line.should == 1
+ ro.object.should be_a(ast::ResourceReference)
+ ro.parameters[0].should be_a(ast::ResourceParam)
end
end
@@ -281,24 +286,6 @@ describe Puppet::Parser do
end
end
- describe "when creating a node" do
- before :each do
- @lexer = stub 'lexer'
- @lexer.stubs(:getcomment)
- @parser.stubs(:lexer).returns(@lexer)
- @node = stub_everything 'node'
- @parser.stubs(:ast_context).returns({})
- @parser.stubs(:node).returns(nil)
-
- @nodename = stub 'nodename', :is_a? => false, :value => "foo"
- @nodename.stubs(:is_a?).with(Puppet::Parser::AST::HostName).returns(true)
- end
-
- it "should return an array of nodes" do
- @parser.newnode(@nodename).should be_instance_of(Array)
- end
- end
-
describe "when retrieving a specific node" do
it "should delegate to the known_resource_types node" do
@known_resource_types.expects(:node).with("node")
@@ -351,30 +338,28 @@ describe Puppet::Parser do
@parser.stubs(:known_resource_types).returns @krt
end
- it "should create new classes" do
- @parser.parse("class foobar {}")
- @krt.hostclass("foobar").should be_instance_of(Puppet::Resource::Type)
+ it "should not create new classes" do
+ @parser.parse("class foobar {}").code[0].should be_a(Puppet::Parser::AST::Hostclass)
+ @krt.hostclass("foobar").should be_nil
end
it "should correctly set the parent class when one is provided" do
- @parser.parse("class foobar inherits yayness {}")
- @krt.hostclass("foobar").parent.should == "yayness"
+ @parser.parse("class foobar inherits yayness {}").code[0].instantiate('')[0].parent.should == "yayness"
end
it "should correctly set the parent class for multiple classes at a time" do
- @parser.parse("class foobar inherits yayness {}\nclass boo inherits bar {}")
- @krt.hostclass("foobar").parent.should == "yayness"
- @krt.hostclass("boo").parent.should == "bar"
+ statements = @parser.parse("class foobar inherits yayness {}\nclass boo inherits bar {}").code
+ statements[0].instantiate('')[0].parent.should == "yayness"
+ statements[1].instantiate('')[0].parent.should == "bar"
end
it "should define the code when some is provided" do
- @parser.parse("class foobar { $var = val }")
- @krt.hostclass("foobar").code.should_not be_nil
+ @parser.parse("class foobar { $var = val }").code[0].code.should_not be_nil
end
it "should define parameters when provided" do
- @parser.parse("class foobar($biz,$baz) {}")
- @krt.hostclass("foobar").arguments.should == {"biz" => nil, "baz" => nil}
+ foobar = @parser.parse("class foobar($biz,$baz) {}").code[0].instantiate('')[0]
+ foobar.arguments.should == {"biz" => nil, "baz" => nil}
end
end
@@ -391,13 +376,37 @@ describe Puppet::Parser do
end
it "should correctly mark exported resources as exported" do
- @parser.parse("@@file { '/file': }")
- @krt.hostclass("").code[0].exported.should be_true
+ @parser.parse("@@file { '/file': }").code[0].exported.should be_true
end
it "should correctly mark virtual resources as virtual" do
- @parser.parse("@file { '/file': }")
- @krt.hostclass("").code[0].virtual.should be_true
+ @parser.parse("@file { '/file': }").code[0].virtual.should be_true
+ end
+ end
+
+ describe "when parsing nodes" do
+ it "should be able to parse a node with a single name" do
+ node = @parser.parse("node foo { }").code[0]
+ node.should be_a Puppet::Parser::AST::Node
+ node.names.length.should == 1
+ node.names[0].value.should == "foo"
+ end
+
+ it "should be able to parse a node with two names" do
+ node = @parser.parse("node foo, bar { }").code[0]
+ node.should be_a Puppet::Parser::AST::Node
+ node.names.length.should == 2
+ node.names[0].value.should == "foo"
+ node.names[1].value.should == "bar"
+ end
+
+ it "should be able to parse a node with three names" do
+ node = @parser.parse("node foo, bar, baz { }").code[0]
+ node.should be_a Puppet::Parser::AST::Node
+ node.names.length.should == 3
+ node.names[0].value.should == "foo"
+ node.names[1].value.should == "bar"
+ node.names[2].value.should == "baz"
end
end
end
diff --git a/spec/unit/parser/scope_spec.rb b/spec/unit/parser/scope_spec.rb
index 9895f446b..2e390a53b 100755
--- a/spec/unit/parser/scope_spec.rb
+++ b/spec/unit/parser/scope_spec.rb
@@ -29,8 +29,7 @@ describe Puppet::Parser::Scope do
end
it "should be able to retrieve its parent module name from the source of its parent type" do
- @topscope.source = Puppet::Resource::Type.new(:hostclass, :foo)
- @topscope.source.module_name = "foo"
+ @topscope.source = Puppet::Resource::Type.new(:hostclass, :foo, :module_name => "foo")
@scope.parent_module_name.should == "foo"
end
diff --git a/spec/unit/parser/type_loader_spec.rb b/spec/unit/parser/type_loader_spec.rb
index 02d543b02..cfa68871d 100644
--- a/spec/unit/parser/type_loader_spec.rb
+++ b/spec/unit/parser/type_loader_spec.rb
@@ -28,85 +28,20 @@ describe Puppet::Parser::TypeLoader do
describe "when loading names from namespaces" do
it "should do nothing if the name to import is an empty string" do
@loader.expects(:name2files).never
- @loader.load_until(["foo"], "") { |f| false }.should be_nil
- end
-
- it "should turn the provided namespaces and name into a list of files" do
- @loader.expects(:name2files).with(["foo"], "bar").returns []
- @loader.load_until(["foo"], "bar") { |f| false }
+ @loader.try_load_fqname(:hostclass, "") { |filename, modname| raise :should_not_occur }.should be_nil
end
it "should attempt to import each generated name" do
- @loader.expects(:name2files).returns %w{foo bar}
- @loader.expects(:import).with("foo",nil)
- @loader.expects(:import).with("bar",nil)
- @loader.load_until(["foo"], "bar") { |f| false }
- end
-
- it "should yield after each import" do
- yielded = []
- @loader.expects(:name2files).returns %w{foo bar}
- @loader.expects(:import).with("foo",nil)
- @loader.expects(:import).with("bar",nil)
- @loader.load_until(["foo"], "bar") { |f| yielded << f; false }
- yielded.should == %w{foo bar}
- end
-
- it "should stop importing when the yielded block returns true" do
- yielded = []
- @loader.expects(:name2files).returns %w{foo bar baz}
- @loader.expects(:import).with("foo",nil)
- @loader.expects(:import).with("bar",nil)
- @loader.expects(:import).with("baz",nil).never
- @loader.load_until(["foo"], "bar") { |f| true if f == "bar" }
- end
-
- it "should return the result of the block" do
- yielded = []
- @loader.expects(:name2files).returns %w{foo bar baz}
- @loader.expects(:import).with("foo",nil)
- @loader.expects(:import).with("bar",nil)
- @loader.expects(:import).with("baz",nil).never
- @loader.load_until(["foo"], "bar") { |f| 10 if f == "bar" }.should == 10
- end
-
- it "should return nil if the block never returns true" do
- @loader.expects(:name2files).returns %w{foo bar}
- @loader.expects(:import).with("foo",nil)
- @loader.expects(:import).with("bar",nil)
- @loader.load_until(["foo"], "bar") { |f| false }.should be_nil
- end
-
- it "should set the module name on any created resource types" do
- type = Puppet::Resource::Type.new(:hostclass, "mytype")
-
- Puppet::Parser::Files.expects(:find_manifests).returns ["modname", %w{one}]
- @loader.stubs(:parse_file)
- @loader.load_until(["foo"], "one") { |f| type }
-
- type.module_name.should == "modname"
- end
- end
-
- describe "when mapping names to files" do
- {
- [["foo"], "::bar::baz"] => %w{bar/baz},
- [[""], "foo::bar"] => %w{foo foo/bar},
- [%w{foo}, "bar"] => %w{foo foo/bar bar},
- [%w{a b}, "bar"] => %w{a a/bar b b/bar bar},
- [%w{a::b::c}, "bar"] => %w{a a/b/c/bar bar},
- [%w{a::b}, "foo::bar"] => %w{a a/b/foo/bar foo/bar}
- }.each do |inputs, outputs|
- it "should produce #{outputs.inspect} from the #{inputs[0].inspect} namespace and #{inputs[1]} name" do
- @loader.name2files(*inputs).should == outputs
- end
+ @loader.expects(:import).with("foo/bar",nil).returns([])
+ @loader.expects(:import).with("foo",nil).returns([])
+ @loader.try_load_fqname(:hostclass, "foo::bar") { |f| false }
end
end
describe "when importing" do
before do
Puppet::Parser::Files.stubs(:find_manifests).returns ["modname", %w{file}]
- Puppet::Parser::Parser.any_instance.stubs(:parse)
+ Puppet::Parser::Parser.any_instance.stubs(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
Puppet::Parser::Parser.any_instance.stubs(:file=)
end
@@ -138,19 +73,19 @@ describe Puppet::Parser::TypeLoader do
it "should parse each found file" do
Puppet::Parser::Files.expects(:find_manifests).returns ["modname", %w{/one}]
- @loader.expects(:parse_file).with("/one")
+ @loader.expects(:parse_file).with("/one").returns(Puppet::Parser::AST::Hostclass.new(''))
@loader.import("myfile")
end
it "should make each file qualified before attempting to parse it" do
Puppet::Parser::Files.expects(:find_manifests).returns ["modname", %w{one}]
- @loader.expects(:parse_file).with("/current/one")
+ @loader.expects(:parse_file).with("/current/one").returns(Puppet::Parser::AST::Hostclass.new(''))
@loader.import("myfile", "/current/file")
end
it "should not attempt to import files that have already been imported" do
Puppet::Parser::Files.expects(:find_manifests).returns ["modname", %w{/one}]
- Puppet::Parser::Parser.any_instance.expects(:parse).once
+ Puppet::Parser::Parser.any_instance.expects(:parse).once.returns(Puppet::Parser::AST::Hostclass.new(''))
@loader.import("myfile")
# This will fail if it tries to reimport the file.
@@ -161,7 +96,7 @@ describe Puppet::Parser::TypeLoader do
describe "when parsing a file" do
before do
@parser = Puppet::Parser::Parser.new(@loader.environment)
- @parser.stubs(:parse)
+ @parser.stubs(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
@parser.stubs(:file=)
Puppet::Parser::Parser.stubs(:new).with(@loader.environment).returns @parser
end
@@ -173,7 +108,7 @@ describe Puppet::Parser::TypeLoader do
it "should assign the parser its file and parse" do
@parser.expects(:file=).with("/my/file")
- @parser.expects(:parse)
+ @parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
@loader.parse_file("/my/file")
end
end
@@ -185,4 +120,15 @@ describe Puppet::Parser::TypeLoader do
@loader.known_resource_types.hostclass("foo").should be_instance_of(Puppet::Resource::Type)
end
+
+ describe "when deciding where to look for files" do
+ { 'foo' => ['foo'],
+ 'foo::bar' => ['foo/bar', 'foo'],
+ 'foo::bar::baz' => ['foo/bar/baz', 'foo/bar', 'foo']
+ }.each do |fqname, expected_paths|
+ it "should look for #{fqname.inspect} in #{expected_paths.inspect}" do
+ @loader.instance_eval { name2files(fqname) }.should == expected_paths
+ end
+ end
+ end
end
diff --git a/spec/unit/resource/type_collection_spec.rb b/spec/unit/resource/type_collection_spec.rb
index 577aea42b..b8da3cf58 100644
--- a/spec/unit/resource/type_collection_spec.rb
+++ b/spec/unit/resource/type_collection_spec.rb
@@ -89,6 +89,34 @@ describe Puppet::Resource::TypeCollection do
loader.node("node").should be_nil
end
+ describe "when resolving namespaces" do
+ [ ['', '::foo', ['foo']],
+ ['a', '::foo', ['foo']],
+ ['a::b', '::foo', ['foo']],
+ [['a::b'], '::foo', ['foo']],
+ [['a::b', 'c'], '::foo', ['foo']],
+ [['A::B', 'C'], '::Foo', ['foo']],
+ ['', '', ['']],
+ ['a', '', ['']],
+ ['a::b', '', ['']],
+ [['a::b'], '', ['']],
+ [['a::b', 'c'], '', ['']],
+ [['A::B', 'C'], '', ['']],
+ ['', 'foo', ['foo']],
+ ['a', 'foo', ['a::foo', 'foo']],
+ ['a::b', 'foo', ['a::b::foo', 'a::foo', 'foo']],
+ ['A::B', 'Foo', ['a::b::foo', 'a::foo', 'foo']],
+ [['a::b'], 'foo', ['a::b::foo', 'a::foo', 'foo']],
+ [['a', 'b'], 'foo', ['a::foo', 'foo', 'b::foo']],
+ [['a::b', 'c::d'], 'foo', ['a::b::foo', 'a::foo', 'foo', 'c::d::foo', 'c::foo']],
+ [['a::b', 'a::c'], 'foo', ['a::b::foo', 'a::foo', 'foo', 'a::c::foo']],
+ ].each do |namespaces, name, expected_result|
+ it "should resolve #{name.inspect} in namespaces #{namespaces.inspect} correctly" do
+ @code.instance_eval { resolve_namespaces(namespaces, name) }.should == expected_result
+ end
+ end
+ end
+
describe "when looking up names" do
before do
@type = Puppet::Resource::Type.new(:hostclass, "ns::klass")
@@ -107,29 +135,32 @@ describe Puppet::Resource::TypeCollection do
describe "that need to be loaded" do
it "should use the loader to load the files" do
- @code.loader.expects(:load_until).with(["ns"], "klass")
- @code.find_or_load(["ns"], "klass", :hostclass)
+ @code.loader.expects(:try_load_fqname).with(:hostclass, "ns::klass")
+ @code.loader.expects(:try_load_fqname).with(:hostclass, "klass")
+ @code.find_hostclass(["ns"], "klass")
end
it "should downcase the name and downcase and array-fy the namespaces before passing to the loader" do
- @code.loader.expects(:load_until).with(["ns"], "klass")
- @code.find_or_load("Ns", "Klass", :hostclass)
+ @code.loader.expects(:try_load_fqname).with(:hostclass, "ns::klass")
+ @code.loader.expects(:try_load_fqname).with(:hostclass, "klass")
+ @code.find_hostclass("Ns", "Klass")
end
- it "should attempt to find the type when the loader yields" do
- @code.loader.expects(:load_until).yields
- @code.expects(:find).with(["ns"], "klass", :hostclass).times(2).returns(false).then.returns(true)
- @code.find_or_load("ns", "klass", :hostclass)
+ it "should use the class returned by the loader" do
+ @code.loader.expects(:try_load_fqname).returns(:klass)
+ @code.expects(:hostclass).with("ns::klass").returns(false)
+ @code.find_hostclass("ns", "klass").should == :klass
end
- it "should return the result of 'load_until'" do
- @code.loader.expects(:load_until).returns "foo"
- @code.find_or_load("Ns", "Klass", :hostclass).should == "foo"
+ it "should return nil if the name isn't found" do
+ @code.stubs(:try_load_fqname).returns(nil)
+ @code.find_hostclass("Ns", "Klass").should be_nil
end
- it "should return nil if the name isn't found" do
- @code.stubs(:load_until).returns(nil)
- @code.find_or_load("Ns", "Klass", :hostclass).should be_nil
+ it "already-loaded names at broader scopes should not shadow autoloaded names" do
+ @code.add Puppet::Resource::Type.new(:hostclass, "bar")
+ @code.loader.expects(:try_load_fqname).with(:hostclass, "foo::bar").returns(:foobar)
+ @code.find_hostclass("foo", "bar").should == :foobar
end
end
end
@@ -195,68 +226,68 @@ describe Puppet::Resource::TypeCollection do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar")
loader.add instance
- loader.find("namespace", "::foo::bar", :hostclass).should equal(instance)
+ loader.find_hostclass("namespace", "::foo::bar").should equal(instance)
end
it "should return nil if the instance name is fully qualified and no such instance exists" do
loader = Puppet::Resource::TypeCollection.new("env")
- loader.find("namespace", "::foo::bar", :hostclass).should be_nil
+ loader.find_hostclass("namespace", "::foo::bar").should be_nil
end
it "should be able to find classes in the base namespace" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo")
loader.add instance
- loader.find("", "foo", :hostclass).should equal(instance)
+ loader.find_hostclass("", "foo").should equal(instance)
end
it "should return the partially qualified object if it exists in a provided namespace" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz")
loader.add instance
- loader.find("foo", "bar::baz", :hostclass).should equal(instance)
+ loader.find_hostclass("foo", "bar::baz").should equal(instance)
end
it "should be able to find partially qualified objects in any of the provided namespaces" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz")
loader.add instance
- loader.find(["nons", "foo", "otherns"], "bar::baz", :hostclass).should equal(instance)
+ loader.find_hostclass(["nons", "foo", "otherns"], "bar::baz").should equal(instance)
end
it "should return the unqualified object if it exists in a provided namespace" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar")
loader.add instance
- loader.find("foo", "bar", :hostclass).should equal(instance)
+ loader.find_hostclass("foo", "bar").should equal(instance)
end
it "should return the unqualified object if it exists in the parent namespace" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar")
loader.add instance
- loader.find("foo::bar::baz", "bar", :hostclass).should equal(instance)
+ loader.find_hostclass("foo::bar::baz", "bar").should equal(instance)
end
it "should should return the partially qualified object if it exists in the parent namespace" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz")
loader.add instance
- loader.find("foo::bar", "bar::baz", :hostclass).should equal(instance)
+ loader.find_hostclass("foo::bar", "bar::baz").should equal(instance)
end
it "should return the qualified object if it exists in the root namespace" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz")
loader.add instance
- loader.find("foo::bar", "foo::bar::baz", :hostclass).should equal(instance)
+ loader.find_hostclass("foo::bar", "foo::bar::baz").should equal(instance)
end
it "should return nil if the object cannot be found" do
loader = Puppet::Resource::TypeCollection.new("env")
instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz")
loader.add instance
- loader.find("foo::bar", "eh", :hostclass).should be_nil
+ loader.find_hostclass("foo::bar", "eh").should be_nil
end
describe "when topscope has a class that has the same name as a local class" do
@@ -268,11 +299,11 @@ describe Puppet::Resource::TypeCollection do
end
it "should favor the local class, if the name is unqualified" do
- @loader.find("foo", "bar", :hostclass).name.should == 'foo::bar'
+ @loader.find_hostclass("foo", "bar").name.should == 'foo::bar'
end
it "should only look in the topclass, if the name is qualified" do
- @loader.find("foo", "::bar", :hostclass).name.should == 'bar'
+ @loader.find_hostclass("foo", "::bar").name.should == 'bar'
end
end
@@ -281,15 +312,16 @@ describe Puppet::Resource::TypeCollection do
@loader = Puppet::Resource::TypeCollection.new("env")
@loader.add Puppet::Resource::Type.new(:hostclass, "foo::bar")
- @loader.find("foo", "::bar", :hostclass).should == nil
+ @loader.find_hostclass("foo", "::bar").should == nil
end
end
- it "should use the generic 'find' method with an empty namespace to find nodes" do
+ it "should be able to find nodes" do
+ node = Puppet::Resource::Type.new(:node, "bar")
loader = Puppet::Resource::TypeCollection.new("env")
- loader.expects(:find).with("", "bar", :node)
- loader.find_node(stub("ignored"), "bar")
+ loader.add(node)
+ loader.find_node(stub("ignored"), "bar").should == node
end
it "should use the 'find_or_load' method to find hostclasses" do
@@ -384,58 +416,6 @@ describe Puppet::Resource::TypeCollection do
end
end
- describe "when performing initial import" do
- before do
- @parser = stub 'parser', :file= => nil, :string => nil, :parse => nil
- Puppet::Parser::Parser.stubs(:new).returns @parser
- @code = Puppet::Resource::TypeCollection.new("env")
- end
-
- it "should create a new parser instance" do
- Puppet::Parser::Parser.expects(:new).returns @parser
- @code.perform_initial_import
- end
-
- it "should set the parser's string to the 'code' setting and parse if code is available" do
- Puppet.settings[:code] = "my code"
- @parser.expects(:string=).with "my code"
- @parser.expects(:parse)
- @code.perform_initial_import
- end
-
- it "should set the parser's file to the 'manifest' setting and parse if no code is available and the manifest is available" do
- File.stubs(:expand_path).with("/my/file").returns "/my/file"
- File.expects(:exist?).with("/my/file").returns true
- Puppet.settings[:manifest] = "/my/file"
- @parser.expects(:file=).with "/my/file"
- @parser.expects(:parse)
- @code.perform_initial_import
- end
-
- it "should not attempt to load a manifest if none is present" do
- File.stubs(:expand_path).with("/my/file").returns "/my/file"
- File.expects(:exist?).with("/my/file").returns false
- Puppet.settings[:manifest] = "/my/file"
- @parser.expects(:file=).never
- @parser.expects(:parse).never
- @code.perform_initial_import
- end
-
- it "should fail helpfully if there is an error importing" do
- File.stubs(:exist?).returns true
- @parser.expects(:parse).raises ArgumentError
- lambda { @code.perform_initial_import }.should raise_error(Puppet::Error)
- end
-
- it "should not do anything if the ignore_import settings is set" do
- Puppet.settings[:ignoreimport] = true
- @parser.expects(:string=).never
- @parser.expects(:file=).never
- @parser.expects(:parse).never
- @code.perform_initial_import
- end
- end
-
describe "when determining the configuration version" do
before do
@code = Puppet::Resource::TypeCollection.new("env")
diff --git a/spec/unit/resource/type_spec.rb b/spec/unit/resource/type_spec.rb
index f58092ec5..7701e55d5 100755
--- a/spec/unit/resource/type_spec.rb
+++ b/spec/unit/resource/type_spec.rb
@@ -322,7 +322,7 @@ describe Puppet::Resource::Type do
end
it "should set its module name in the scope if available" do
- @type.module_name = "mymod"
+ @type.instance_eval { @module_name = "mymod" }
@type.set_resource_parameters(@resource, @scope)
@@ -531,8 +531,7 @@ describe Puppet::Resource::Type do
@compiler.add_resource @scope, @parent_resource
@type.resource_type_collection = @scope.known_resource_types
- @type.resource_type_collection.stubs(:node).with("parent").returns(@parent_type)
- @type.resource_type_collection.stubs(:node).with("Parent").returns(@parent_type)
+ @type.resource_type_collection.add(@parent_type)
end
it "should evaluate the parent's resource" do
diff --git a/spec/unit/util/rdoc/parser_spec.rb b/spec/unit/util/rdoc/parser_spec.rb
index 28c33c295..04713f293 100755
--- a/spec/unit/util/rdoc/parser_spec.rb
+++ b/spec/unit/util/rdoc/parser_spec.rb
@@ -20,7 +20,7 @@ describe RDoc::Parser do
@parser.stubs(:scan_top_level)
parser = stub 'parser'
Puppet::Parser::Parser.stubs(:new).returns(parser)
- parser.expects(:parse)
+ parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
parser.expects(:file=).with("module/manifests/init.pp")
@parser.scan
@@ -29,6 +29,7 @@ describe RDoc::Parser do
it "should scan the ast for Puppet files" do
parser = stub_everything 'parser'
Puppet::Parser::Parser.stubs(:new).returns(parser)
+ parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
@parser.expects(:scan_top_level)
@@ -38,6 +39,7 @@ describe RDoc::Parser do
it "should return a PuppetTopLevel to RDoc" do
parser = stub_everything 'parser'
Puppet::Parser::Parser.stubs(:new).returns(parser)
+ parser.expects(:parse).returns(Puppet::Parser::AST::Hostclass.new(''))
@parser.expects(:scan_top_level)
@@ -47,8 +49,8 @@ describe RDoc::Parser do
describe "when scanning top level entities" do
before :each do
- @resource_type_collection = stub_everything 'resource_type_collection'
- @parser.ast = @resource_type_collection
+ @resource_type_collection = resource_type_collection = stub_everything('resource_type_collection')
+ @parser.instance_eval { @known_resource_types = resource_type_collection }
@parser.stubs(:split_module).returns("module")
@topcontainer = stub_everything 'topcontainer'
@@ -141,8 +143,8 @@ describe RDoc::Parser do
@definition = stub_everything 'definition', :file => "module/manifests/init.pp", :type => :definition, :name => "mydef"
@node = stub_everything 'node', :file => "module/manifests/init.pp", :type => :node, :name => "mynode"
- @resource_type_collection = Puppet::Resource::TypeCollection.new("env")
- @parser.ast = @resource_type_collection
+ @resource_type_collection = resource_type_collection = Puppet::Resource::TypeCollection.new("env")
+ @parser.instance_eval { @known_resource_types = resource_type_collection }
@container = stub_everything 'container'
end
@@ -437,9 +439,13 @@ describe RDoc::Parser do
@class = stub_everything 'class'
@stmt = Puppet::Parser::AST::Resource.new(
:type => "File",
- :title => "myfile",
- :doc => 'mydoc',
- :parameters => Puppet::Parser::AST::ASTArray.new(:children => [])
+ :instances => Puppet::Parser::AST::ASTArray.new(:children => [
+ Puppet::Parser::AST::ResourceInstance.new(
+ :title => Puppet::Parser::AST::Name.new(:value => "myfile"),
+ :parameters => Puppet::Parser::AST::ASTArray.new(:children => [])
+ )
+ ]),
+ :doc => 'mydoc'
)
@code = stub_everything 'code'
diff --git a/spec/unit/util/zaml_spec.rb b/spec/unit/util/zaml_spec.rb
index f2bcefe01..b223f89d4 100755
--- a/spec/unit/util/zaml_spec.rb
+++ b/spec/unit/util/zaml_spec.rb
@@ -35,5 +35,30 @@ describe "Pure ruby yaml implementation" do
lambda { YAML.load(o.to_yaml) }.should_not raise_error
end
}
+
+ it "should emit proper labels and backreferences for common objects" do
+ # Note: this test makes assumptions about the names ZAML chooses
+ # for labels.
+ x = [1, 2]
+ y = [3, 4]
+ z = [x, y, x, y]
+ z.to_yaml.should == "--- \n - &id001\n - 1\n - 2\n - &id002\n - 3\n - 4\n - *id001\n - *id002"
+ z2 = YAML.load(z.to_yaml)
+ z2.should == z
+ z2[0].should equal(z2[2])
+ z2[1].should equal(z2[3])
+ end
+
+ it "should emit proper labels and backreferences for recursive objects" do
+ x = [1, 2]
+ x << x
+ x.to_yaml.should == "--- &id001\n \n - 1\n - 2\n - *id001"
+ x2 = YAML.load(x.to_yaml)
+ x2.should be_a(Array)
+ x2.length.should == 3
+ x2[0].should == 1
+ x2[1].should == 2
+ x2[2].should equal(x2)
+ end
end