From 4fa24bb4be7fd73af2a25884185f91b42f73d785 Mon Sep 17 00:00:00 2001 From: Brice Figureau Date: Sat, 30 Oct 2010 15:29:19 +0200 Subject: Fix #5127 - error when accessing array elements Accesing an array with an integer index (ie $array[1]) is producing a ruby error: can't convert String into Integer This is because the array index is not properly converted to an number before the array element lookup is performed. Signed-off-by: Brice Figureau --- spec/unit/parser/ast/leaf_spec.rb | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'spec/unit/parser') diff --git a/spec/unit/parser/ast/leaf_spec.rb b/spec/unit/parser/ast/leaf_spec.rb index eb71f0d37..384ee08eb 100755 --- a/spec/unit/parser/ast/leaf_spec.rb +++ b/spec/unit/parser/ast/leaf_spec.rb @@ -136,6 +136,22 @@ describe Puppet::Parser::AST::HashOrArrayAccess do access.evaluate(@scope).should == "val2" end + it "should be able to return an array member when index is a stringified number" do + @scope.stubs(:lookupvar).with("a").returns(["val1", "val2", "val3"]) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "1" ) + + access.evaluate(@scope).should == "val2" + end + + it "should raise an error when accessing an array with a key" do + @scope.stubs(:lookupvar).with("a").returns(["val1", "val2", "val3"]) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "get_me_the_second_element_please" ) + + lambda { access.evaluate(@scope) }.should raise_error + end + it "should be able to return an hash value" do @scope.stubs(:lookupvar).with("a").returns({ "key1" => "val1", "key2" => "val2", "key3" => "val3" }) @@ -144,6 +160,14 @@ describe Puppet::Parser::AST::HashOrArrayAccess do access.evaluate(@scope).should == "val2" end + it "should be able to return an hash value with a numerical key" do + @scope.stubs(:lookupvar).with("a").returns({ "key1" => "val1", "key2" => "val2", "45" => "45", "key3" => "val3" }) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "45" ) + + access.evaluate(@scope).should == "45" + end + it "should raise an error if the variable lookup didn't return an hash or an array" do @scope.stubs(:lookupvar).with("a").returns("I'm a string") @@ -195,6 +219,24 @@ describe Puppet::Parser::AST::HashOrArrayAccess do scope.lookupvar("a").should be_include("b") end + it "should raise an error when assigning an array element with a key" do + @scope.stubs(:lookupvar).with("a").returns([]) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "get_me_the_second_element_please" ) + + lambda { access.assign(@scope, "test") }.should raise_error + end + + it "should be able to return an array member when index is a stringified number" do + scope = Puppet::Parser::Scope.new + scope.setvar("a", []) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "0" ) + + access.assign(scope, "val2") + scope.lookupvar("a").should == ["val2"] + end + it "should raise an error when trying to overwrite an hash value" do @scope.stubs(:lookupvar).with("a").returns({ "key" => [ "a" , "b" ]}) access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key") -- cgit From 9604f1c4cd5a368da08c6f3219b44908a9b9921c Mon Sep 17 00:00:00 2001 From: Brice Figureau Date: Thu, 11 Nov 2010 15:51:21 +0100 Subject: Fix #5252 - line number mis-attribution during parsing It is a resurgence of #2366 that appeared because of the commit 8971d8. Before this commit, for associating documentation comments, we were preferring line numbers coming from the parser currently reducing rule, instead of the current lexer line number (which can be in advance of several tokens due to the nature of LALR parsers). We now merge the ast line number before fetching the comment from the lexer. Signed-off-by: Brice Figureau --- spec/unit/parser/parser_spec.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'spec/unit/parser') diff --git a/spec/unit/parser/parser_spec.rb b/spec/unit/parser/parser_spec.rb index f73e07a5c..07e2d220b 100755 --- a/spec/unit/parser/parser_spec.rb +++ b/spec/unit/parser/parser_spec.rb @@ -269,16 +269,23 @@ describe Puppet::Parser do it "should prefer provided options over AST context" do @class.expects(:new).with { |opts| opts[:file] == "/bar" } - @parser.expects(:ast_context).returns :file => "/foo" + @lexer.expects(:file).returns "/foo" @parser.ast(@class, :file => "/bar") end it "should include docs when the AST class uses them" do @class.expects(:use_docs).returns true @class.stubs(:new) - @parser.expects(:ast_context).with(true).returns({}) + @parser.expects(:ast_context).with{ |a| a[0] == true }.returns({}) @parser.ast(@class, :file => "/bar") end + + it "should get docs from lexer using the correct AST line number" do + @class.expects(:use_docs).returns true + @class.stubs(:new).with{ |a| a[:doc] == "doc" } + @lexer.expects(:getcomment).with(12).returns "doc" + @parser.ast(@class, :file => "/bar", :line => 12) + end end describe "when creating a node" do -- cgit From c8b6fb5bed057bf25a25c9e1b3f55bedf64ba775 Mon Sep 17 00:00:00 2001 From: James Turnbull Date: Mon, 15 Nov 2010 19:41:43 +1100 Subject: Fixed #5296 - test warnings messages --- spec/unit/parser/ast/leaf_spec.rb | 2 +- spec/unit/parser/ast/resource_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/unit/parser') diff --git a/spec/unit/parser/ast/leaf_spec.rb b/spec/unit/parser/ast/leaf_spec.rb index 384ee08eb..a19c24115 100755 --- a/spec/unit/parser/ast/leaf_spec.rb +++ b/spec/unit/parser/ast/leaf_spec.rb @@ -49,7 +49,7 @@ describe Puppet::Parser::AST::String do end it "should return a dup of its value" do value = "" - Puppet::Parser::AST::String.new( :value => value ).evaluate(stub 'scope').should_not be_equal(value) + Puppet::Parser::AST::String.new( :value => value ).evaluate(stub('scope')).should_not be_equal(value) end end end diff --git a/spec/unit/parser/ast/resource_spec.rb b/spec/unit/parser/ast/resource_spec.rb index e1f73e161..721c31a8d 100755 --- a/spec/unit/parser/ast/resource_spec.rb +++ b/spec/unit/parser/ast/resource_spec.rb @@ -152,7 +152,7 @@ describe Puppet::Parser::AST::Resource do result = @resource.evaluate(@scope) result.length.should == 1 result.first.ref.should == "Class[Classname]" - @compiler.catalog.resource("Class[Classname]").should equal result.first + @compiler.catalog.resource("Class[Classname]").should equal(result.first) end it "should cause its parent to be evaluated" do @@ -163,7 +163,7 @@ describe Puppet::Parser::AST::Resource do result = @resource.evaluate(@scope) result.length.should == 1 result.first.ref.should == "Class[Classname]" - @compiler.catalog.resource("Class[Classname]").should equal result.first + @compiler.catalog.resource("Class[Classname]").should equal(result.first) @compiler.catalog.resource("Class[Parentname]").should be_instance_of(Puppet::Parser::Resource) end -- cgit From b2ff6a50202a05f2925fa5046a39ef7f163983f1 Mon Sep 17 00:00:00 2001 From: Markus Roberts Date: Mon, 15 Nov 2010 19:19:02 -0800 Subject: Fix for #5298 -- Collections need to do type lookup When the responsibility for type-name resolution was moved to the AST nodes in commit 449315a2c705df2396852462a1d1e14774b9f117, at least one instance was missed: the space ship operator Myclass <<| tag == foo |>> fails unless Myclass has been previously loaded. This commit adds the lookup to AST::Collection nodes in the same way it was added to the other node types. Note that I haven't audited the other note types for similar cases. --- spec/unit/parser/ast/collection_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spec/unit/parser') diff --git a/spec/unit/parser/ast/collection_spec.rb b/spec/unit/parser/ast/collection_spec.rb index 3f7878a99..392a2c0f0 100755 --- a/spec/unit/parser/ast/collection_spec.rb +++ b/spec/unit/parser/ast/collection_spec.rb @@ -5,6 +5,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe Puppet::Parser::AST::Collection do before :each do @scope = stub_everything 'scope' + @mytype = stub_everything('mytype') + @scope.stubs(:find_resource_type).returns @mytype @compiler = stub_everything 'compile' @scope.stubs(:compiler).returns(@compiler) @@ -24,6 +26,8 @@ describe Puppet::Parser::AST::Collection do it "should instantiate a Collector for this type" do collection = Puppet::Parser::AST::Collection.new :form => :virtual, :type => "test" + @test_type = stub 'type', :name => 'test' + @scope.expects(:find_resource_type).with('test').returns @test_type Puppet::Parser::Collector.expects(:new).with(@scope, "test", nil, nil, :virtual) -- cgit From cd8126f4422afd452564e37fae501f9c356a751d Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Wed, 17 Nov 2010 16:52:21 -0800 Subject: maint: Fix intermittent parser spec failures The variable 'ast' was being used as shorthand for Puppet::Parser::AST, but a test was also trying to use it as a local variable, causing problems. --- spec/unit/parser/parser_spec.rb | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'spec/unit/parser') diff --git a/spec/unit/parser/parser_spec.rb b/spec/unit/parser/parser_spec.rb index 75f505e66..ab43194e9 100755 --- a/spec/unit/parser/parser_spec.rb +++ b/spec/unit/parser/parser_spec.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe Puppet::Parser do - ast = Puppet::Parser::AST + Puppet::Parser::AST before :each do @known_resource_types = Puppet::Resource::TypeCollection.new("development") @@ -80,21 +80,21 @@ describe Puppet::Parser do describe "when parsing 'if'" do it "not, it should create the correct ast objects" do - ast::Not.expects(:new).with { |h| h[:value].is_a?(ast::Boolean) } + Puppet::Parser::AST::Not.expects(:new).with { |h| h[:value].is_a?(Puppet::Parser::AST::Boolean) } @parser.parse("if ! true { $var = 1 }") end it "boolean operation, it should create the correct ast objects" do - ast::BooleanOperator.expects(:new).with { - |h| h[:rval].is_a?(ast::Boolean) and h[:lval].is_a?(ast::Boolean) and h[:operator]=="or" + Puppet::Parser::AST::BooleanOperator.expects(:new).with { + |h| h[:rval].is_a?(Puppet::Parser::AST::Boolean) and h[:lval].is_a?(Puppet::Parser::AST::Boolean) and h[:operator]=="or" } @parser.parse("if true or true { $var = 1 }") end it "comparison operation, it should create the correct ast objects" do - ast::ComparisonOperator.expects(:new).with { - |h| h[:lval].is_a?(ast::Name) and h[:rval].is_a?(ast::Name) and h[:operator]=="<" + Puppet::Parser::AST::ComparisonOperator.expects(:new).with { + |h| h[:lval].is_a?(Puppet::Parser::AST::Name) and h[:rval].is_a?(Puppet::Parser::AST::Name) and h[:operator]=="<" } @parser.parse("if 1 < 2 { $var = 1 }") @@ -105,13 +105,13 @@ describe Puppet::Parser do describe "when parsing if complex expressions" do it "should create a correct ast tree" do aststub = stub_everything 'ast' - ast::ComparisonOperator.expects(:new).with { - |h| h[:rval].is_a?(ast::Name) and h[:lval].is_a?(ast::Name) and h[:operator]==">" + Puppet::Parser::AST::ComparisonOperator.expects(:new).with { + |h| h[:rval].is_a?(Puppet::Parser::AST::Name) and h[:lval].is_a?(Puppet::Parser::AST::Name) and h[:operator]==">" }.returns(aststub) - ast::ComparisonOperator.expects(:new).with { - |h| h[:rval].is_a?(ast::Name) and h[:lval].is_a?(ast::Name) and h[:operator]=="==" + Puppet::Parser::AST::ComparisonOperator.expects(:new).with { + |h| h[:rval].is_a?(Puppet::Parser::AST::Name) and h[:lval].is_a?(Puppet::Parser::AST::Name) and h[:operator]=="==" }.returns(aststub) - ast::BooleanOperator.expects(:new).with { + Puppet::Parser::AST::BooleanOperator.expects(:new).with { |h| h[:rval]==aststub and h[:lval]==aststub and h[:operator]=="and" } @parser.parse("if (1 > 2) and (1 == 2) { $var = 1 }") @@ -134,8 +134,8 @@ describe Puppet::Parser do end it "should create an ast::ResourceReference" do - ast::ResourceReference.expects(:new).with { |arg| - arg[:line]==1 and arg[:type]=="File" and arg[:title].is_a?(ast::ASTArray) + Puppet::Parser::AST::ResourceReference.expects(:new).with { |arg| + arg[:line]==1 and arg[:type]=="File" and arg[:title].is_a?(Puppet::Parser::AST::ASTArray) } @parser.parse('exec { test: command => File["a","b"] }') end @@ -152,14 +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) + #Puppet::Parser::AST::ResourceOverride.expects(:new).with { |arg| + # arg[:line]==1 and arg[:object].is_a?(Puppet::Parser::AST::ResourceReference) and arg[:parameters].is_a?(Puppet::Parser::AST::ResourceParam) #} ro = @parser.parse('Resource["title1","title2"] { param => value }').code[0] - ro.should be_a(ast::ResourceOverride) + ro.should be_a(Puppet::Parser::AST::ResourceOverride) ro.line.should == 1 - ro.object.should be_a(ast::ResourceReference) - ro.parameters[0].should be_a(ast::ResourceParam) + ro.object.should be_a(Puppet::Parser::AST::ResourceReference) + ro.parameters[0].should be_a(Puppet::Parser::AST::ResourceParam) end end @@ -179,17 +179,17 @@ describe Puppet::Parser do end it "should create a nop node for empty branch" do - ast::Nop.expects(:new) + Puppet::Parser::AST::Nop.expects(:new) @parser.parse("if true { }") end it "should create a nop node for empty else branch" do - ast::Nop.expects(:new) + Puppet::Parser::AST::Nop.expects(:new) @parser.parse("if true { notice('test') } else { }") end it "should build a chain of 'ifs' if there's an 'elsif'" do - ast = @parser.parse(<<-PP) + lambda { @parser.parse(<<-PP) }.should_not raise_error if true { notice('test') } elsif true {} else { } PP end -- cgit From 4a2bbbcf197760ce2eb3c257742293794d6fb571 Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Mon, 22 Nov 2010 14:56:49 -0800 Subject: maint: Fix tests that don't run on their own From the spec directory I found all the specs that fail when run on their own. for TEST in `find . -name "*.rb" -type f`; do spec $TEST > /dev/null 2>&1 if [[ $? != 0 ]]; then echo $TEST fi done All of them were cases of missing requires. Paired-with: Nick Lewis --- spec/unit/parser/collector_spec.rb | 1 + spec/unit/parser/templatewrapper_spec.rb | 1 + 2 files changed, 2 insertions(+) (limited to 'spec/unit/parser') diff --git a/spec/unit/parser/collector_spec.rb b/spec/unit/parser/collector_spec.rb index 15808d6ff..908cda63a 100755 --- a/spec/unit/parser/collector_spec.rb +++ b/spec/unit/parser/collector_spec.rb @@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' +require 'puppet/rails' require 'puppet/parser/collector' describe Puppet::Parser::Collector, "when initializing" do diff --git a/spec/unit/parser/templatewrapper_spec.rb b/spec/unit/parser/templatewrapper_spec.rb index d4d1d1b8e..68d90a1cc 100755 --- a/spec/unit/parser/templatewrapper_spec.rb +++ b/spec/unit/parser/templatewrapper_spec.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../../spec_helper' +require 'puppet/parser/templatewrapper' describe Puppet::Parser::TemplateWrapper do before(:each) do -- cgit