diff options
| author | Luke Kanies <luke@reductivelabs.com> | 2010-01-29 20:57:21 -0600 |
|---|---|---|
| committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
| commit | 7089446697ad550c22012bc2b5572030727d67e1 (patch) | |
| tree | 40d160f11839fe6e20311186ded4e621d23e1242 /spec/unit/parser/ast | |
| parent | 4871c909cd28c82b64d0b62d8a27e62737d8733d (diff) | |
| download | puppet-7089446697ad550c22012bc2b5572030727d67e1.tar.gz puppet-7089446697ad550c22012bc2b5572030727d67e1.tar.xz puppet-7089446697ad550c22012bc2b5572030727d67e1.zip | |
Removing Resource::Reference classes
This commit is hopefully less messy than it
first appears, but it's certainly cross-cutting.
The reason for all of this is that we previously only
looked up builtin resource types from outside the parser,
but now that the defined resource types are available globally
via environments, we can push that lookup code to Resource.
Once we do that, however, we have to have environment and
namespace information in every resource.
Here I remove the Resource::Reference classes (except
the AST class), and use Resource instances instead. I
did this because the shared code between the two classes
got incredibly complicated, such that they should have had
a hierarchical relationship disallowed by their constants.
This complexity convinced me just to get rid of References
entirely.
I also make Puppet::Parser::Resource a subclass
of Puppet::Resource.
There are still broken tests in test/, but this was a big
enough commit I wanted to get it in.
Signed-off-by: Luke Kanies <luke@reductivelabs.com>
Diffstat (limited to 'spec/unit/parser/ast')
| -rwxr-xr-x | spec/unit/parser/ast/resource.rb | 29 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/resource_reference.rb | 51 |
2 files changed, 28 insertions, 52 deletions
diff --git a/spec/unit/parser/ast/resource.rb b/spec/unit/parser/ast/resource.rb index b257cb116..391f4c770 100755 --- a/spec/unit/parser/ast/resource.rb +++ b/spec/unit/parser/ast/resource.rb @@ -9,16 +9,15 @@ describe Puppet::Parser::AST::Resource do @title = stub_everything 'title' @compiler = stub_everything 'compiler' @scope = Puppet::Parser::Scope.new(:compiler => @compiler) - @param1 = stub_everything 'parameter', :is_a? => true @scope.stubs(:resource).returns(stub_everything) - @params = ast::ASTArray.new( :children => [@param1]) - @resource = ast::Resource.new(:title => @title, :type => "Resource", :params => @params ) + @resource = ast::Resource.new(:title => @title, :type => "Resource", :params => ast::ASTArray.new(:children => []) ) @resource.stubs(:qualified_type).returns("Resource") - Puppet::Parser::Resource.stubs(:new).returns(stub_everything) end it "should evaluate all its parameters" do - @param1.expects(:safeevaluate).with(@scope) + param = stub 'param' + param.expects(:safeevaluate).with(@scope).returns Puppet::Parser::Resource::Param.new(:name => "myparam", :value => "myvalue", :source => stub("source")) + @resource.stubs(:params).returns [param] @resource.evaluate(@scope) end @@ -49,10 +48,10 @@ describe Puppet::Parser::AST::Resource do title_array.stubs(:flatten).returns([@title]) titles.stubs(:safeevaluate).with(@scope).returns(title_array) - Puppet::Parser::Resource.expects(:new).with { |hash| hash[:title] == @title } - @resource.title = titles - @resource.evaluate(@scope) + result = @resource.evaluate(@scope) + result[0].should be_instance_of(Puppet::Parser::Resource) + result[0].title.should == @title end it "should handover resources to the compiler" do @@ -77,18 +76,18 @@ describe Puppet::Parser::AST::Resource do title_array.stubs(:flatten).returns([@title]) titles.stubs(:safeevaluate).with(@scope).returns(title_array) - Puppet::Parser::Resource.stubs(:new).returns(resource) - @compiler.stubs(:add_resource).with(resource) + @compiler.stubs(:add_resource) @resource.title = titles - @resource.evaluate(@scope).should == [resource] + @resource.evaluate(@scope)[0].should be_instance_of(Puppet::Parser::Resource) end it "should generate virtual resources if it is virtual" do @resource.virtual = true - Puppet::Parser::Resource.expects(:new).with { |hash| hash[:virtual] == true } + result = @resource.evaluate(@scope) + result[0].should be_virtual @resource.evaluate(@scope) end @@ -96,8 +95,8 @@ describe Puppet::Parser::AST::Resource do it "should generate virtual and exported resources if it is exported" do @resource.exported = true - Puppet::Parser::Resource.expects(:new).with { |hash| hash[:virtual] == true and hash[:exported] == true } - - @resource.evaluate(@scope) + result = @resource.evaluate(@scope) + result[0].should be_virtual + result[0].should be_exported end end diff --git a/spec/unit/parser/ast/resource_reference.rb b/spec/unit/parser/ast/resource_reference.rb index 10d9678c3..ee42694b9 100755 --- a/spec/unit/parser/ast/resource_reference.rb +++ b/spec/unit/parser/ast/resource_reference.rb @@ -10,54 +10,31 @@ describe Puppet::Parser::AST::ResourceReference do @scope = Puppet::Parser::Scope.new() end - def newref(title, type) + def newref(type, title) title = stub 'title', :safeevaluate => title ref = Puppet::Parser::AST::ResourceReference.new(:type => type, :title => title) end - it "should evaluate correctly reference to builtin types" do - newref("/tmp/yay", "File").evaluate(@scope).to_s.should == "File[/tmp/yay]" + it "should correctly produce reference strings" do + newref("File", "/tmp/yay").evaluate(@scope).to_s.should == "File[/tmp/yay]" end - %{ "one::two" "one-two"}.each do |type| - it "should evaluate correctly reference to define" do - klass = stub 'klass', :title => "three", :name => type - @scope.stubs(:find_definition).returns(klass) - - newref("three", type).evaluate(@scope).to_ref.should == Puppet::Parser::Resource::Reference.new( :type => type, :title => "three" ).to_ref - end + it "should produce a single resource when the title evaluates to a string" do + newref("File", "/tmp/yay").evaluate(@scope).should == Puppet::Resource.new("file", "/tmp/yay") end - it "should be able to call qualified_class" do - klass = stub 'klass', :title => "three", :name => "one" - @scope.expects(:find_hostclass).with("one").returns(klass) - newref("three","class").qualified_class(@scope,"one").should == "one" - end - - it "should be able to find qualified classes when evaluating" do - klass = stub 'klass', :title => "one", :name => "one" - @scope.stubs(:find_hostclass).returns(klass) - - evaled = newref("one", "class").evaluate(@scope) - evaled.type.should == "Class" - evaled.title.should == "one" - end - - it "should return an array of reference if given an array of titles" do + it "should return an array of resources if given an array of titles" do titles = mock 'titles', :safeevaluate => ["title1","title2"] - ref = ast::ResourceReference.new( :title => titles, :type => "Resource" ) - ref.stubs(:qualified_type).with(@scope).returns("Resource") - - ref.evaluate(@scope).should have(2).elements + ref = ast::ResourceReference.new( :title => titles, :type => "File" ) + ref.evaluate(@scope).should == [ + Puppet::Resource.new("file", "title1"), + Puppet::Resource.new("file", "title2") + ] end - it "should qualify class of all titles for Class resource references" do - titles = mock 'titles', :safeevaluate => ["title1","title2"] - ref = ast::ResourceReference.new( :title => titles, :type => "Class" ) - ref.expects(:qualified_class).with(@scope,"title1").returns("class") - ref.expects(:qualified_class).with(@scope,"title2").returns("class") - - ref.evaluate(@scope) + it "should pass its scope's namespaces to all created resource references" do + @scope.add_namespace "foo" + newref("File", "/tmp/yay").evaluate(@scope).namespaces.should == ["foo"] end it "should return a correct representation when converting to string" do |
