diff options
Diffstat (limited to 'spec/unit/parser')
23 files changed, 342 insertions, 1316 deletions
diff --git a/spec/unit/parser/compiler_spec.rb b/spec/unit/parser/compiler_spec.rb index d1e855a20..0ca01f521 100755 --- a/spec/unit/parser/compiler_spec.rb +++ b/spec/unit/parser/compiler_spec.rb @@ -97,6 +97,13 @@ describe Puppet::Parser::Compiler do @compiler.environment.should equal(@node.environment) end + it "fails if the node's environment has conflicting manifest settings" do + conflicted_environment = Puppet::Node::Environment.create(:testing, [], '/some/environment.conf/manifest.pp') + conflicted_environment.stubs(:conflicting_manifest_settings?).returns(true) + @node.environment = conflicted_environment + expect { Puppet::Parser::Compiler.compile(@node) }.to raise_error(Puppet::Error, /disable_per_environment_manifest.*true.*environment.conf.*manifest.*conflict/) + end + it "should include the resource type collection helper" do Puppet::Parser::Compiler.ancestors.should be_include(Puppet::Resource::TypeCollectionHelper) end @@ -681,7 +688,7 @@ describe Puppet::Parser::Compiler do it "should skip classes that have already been evaluated" do @compiler.catalog.stubs(:tag) - @scope.stubs(:class_scope).with(@class).returns("something") + @scope.stubs(:class_scope).with(@class).returns(@scope) @compiler.expects(:add_resource).never @@ -694,7 +701,7 @@ describe Puppet::Parser::Compiler do it "should skip classes previously evaluated with different capitalization" do @compiler.catalog.stubs(:tag) @scope.stubs(:find_hostclass).with("MyClass",{:assume_fqname => false}).returns(@class) - @scope.stubs(:class_scope).with(@class).returns("something") + @scope.stubs(:class_scope).with(@class).returns(@scope) @compiler.expects(:add_resource).never @resource.expects(:evaluate).never Puppet::Parser::Resource.expects(:new).never diff --git a/spec/unit/parser/eparser_adapter_spec.rb b/spec/unit/parser/eparser_adapter_spec.rb deleted file mode 100644 index 173cfb783..000000000 --- a/spec/unit/parser/eparser_adapter_spec.rb +++ /dev/null @@ -1,407 +0,0 @@ -#! /usr/bin/env ruby -require 'spec_helper' -require 'puppet/parser/e_parser_adapter' - -describe Puppet::Parser do - - Puppet::Parser::AST - - before :each do - @known_resource_types = Puppet::Resource::TypeCollection.new("development") - @classic_parser = Puppet::Parser::Parser.new "development" - @parser = Puppet::Parser::EParserAdapter.new(@classic_parser) - @classic_parser.stubs(:known_resource_types).returns @known_resource_types - @true_ast = Puppet::Parser::AST::Boolean.new :value => true - end - - it "should require an environment at initialization" do - expect { - Puppet::Parser::EParserAdapter.new - }.to raise_error(ArgumentError, /wrong number of arguments/) - end - - describe "when parsing append operator" do - - it "should not raise syntax errors" do - expect { @parser.parse("$var += something") }.to_not raise_error - end - - it "should raise syntax error on incomplete syntax " do - expect { - @parser.parse("$var += ") - }.to raise_error(Puppet::ParseError, /Syntax error at end of file/) - end - - 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 - vardef = @parser.parse("$var += ['test']").code[0] - vardef.should be_a(Puppet::Parser::AST::VarDef) - vardef.append.should == true - end - - end - - describe "when parsing selector" do - it "should support hash access on the left hand side" do - expect { @parser.parse("$h = { 'a' => 'b' } $a = $h['a'] ? { 'b' => 'd', default => undef }") }.to_not raise_error - end - end - - describe "parsing 'unless'" do - it "should create the correct ast objects" do - Puppet::Parser::AST::Not.expects(:new).with { |h| h[:value].is_a?(Puppet::Parser::AST::Boolean) } - @parser.parse("unless false { $var = 1 }") - end - - it "should not raise an error with empty statements" do - expect { @parser.parse("unless false { }") }.to_not raise_error - end - - #test for bug #13296 - it "should not override 'unless' as a parameter inside resources" do - lambda { @parser.parse("exec {'/bin/echo foo': unless => '/usr/bin/false',}") }.should_not raise_error - end - end - - describe "when parsing parameter names" do - Puppet::Parser::Lexer::KEYWORDS.sort_tokens.each do |keyword| - it "should allow #{keyword} as a keyword" do - lambda { @parser.parse("exec {'/bin/echo foo': #{keyword} => '/usr/bin/false',}") }.should_not raise_error - end - end - end - - describe "when parsing 'if'" do - it "not, it should create the correct ast objects" do - 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 - 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 - 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 }") - - end - - end - - describe "when parsing if complex expressions" do - it "should create a correct ast tree" do - aststub = stub_everything 'ast' - 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) - 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) - 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 }") - end - - it "should raise an error on incorrect expression" do - expect { - @parser.parse("if (1 > 2 > ) or (1 == 2) { $var = 1 }") - }.to raise_error(Puppet::ParseError, /Syntax error at '\)'/) - end - - end - - describe "when parsing resource references" do - - it "should not raise syntax errors" do - expect { @parser.parse('exec { test: param => File["a"] }') }.to_not raise_error - end - - it "should not raise syntax errors with multiple references" do - expect { @parser.parse('exec { test: param => File["a","b"] }') }.to_not raise_error - end - - it "should create an ast::ResourceReference" do - # NOTE: In egrammar, type and name are unified immediately to lower case whereas the regular grammar - # keeps the UC name in some contexts - it gets downcased later as the name of the type is in lower case. - # - Puppet::Parser::AST::ResourceReference.expects(:new).with { |arg| - arg[:line]==1 and arg[:pos] ==25 and arg[:type]=="file" and arg[:title].is_a?(Puppet::Parser::AST::ASTArray) - } - @parser.parse('exec { test: command => File["a","b"] }') - end - end - - describe "when parsing resource overrides" do - - it "should not raise syntax errors" do - expect { @parser.parse('Resource["title"] { param => value }') }.to_not raise_error - end - - it "should not raise syntax errors with multiple overrides" do - expect { @parser.parse('Resource["title1","title2"] { param => value }') }.to_not raise_error - end - - it "should create an ast::ResourceOverride" do - ro = @parser.parse('Resource["title1","title2"] { param => value }').code[0] - ro.should be_a(Puppet::Parser::AST::ResourceOverride) - ro.line.should == 1 - ro.object.should be_a(Puppet::Parser::AST::ResourceReference) - ro.parameters[0].should be_a(Puppet::Parser::AST::ResourceParam) - end - - end - - describe "when parsing if statements" do - - it "should not raise errors with empty if" do - expect { @parser.parse("if true { }") }.to_not raise_error - end - - it "should not raise errors with empty else" do - expect { @parser.parse("if false { notice('if') } else { }") }.to_not raise_error - end - - it "should not raise errors with empty if and else" do - expect { @parser.parse("if false { } else { }") }.to_not raise_error - end - - it "should create a nop node for empty branch" do - Puppet::Parser::AST::Nop.expects(:new).twice - @parser.parse("if true { }") - end - - it "should create a nop node for empty else branch" do - 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 - expect { @parser.parse(<<-PP) }.to_not raise_error - if true { notice('test') } elsif true {} else { } - PP - end - - end - - describe "when parsing function calls" do - it "should not raise errors with no arguments" do - expect { @parser.parse("tag()") }.to_not raise_error - end - - it "should not raise errors with rvalue function with no args" do - expect { @parser.parse("$a = template()") }.to_not raise_error - end - - it "should not raise errors with arguments" do - expect { @parser.parse("notice(1)") }.to_not raise_error - end - - it "should not raise errors with multiple arguments" do - expect { @parser.parse("notice(1,2)") }.to_not raise_error - end - - it "should not raise errors with multiple arguments and a trailing comma" do - expect { @parser.parse("notice(1,2,)") }.to_not raise_error - end - - end - - describe "when parsing arrays" do - it "should parse an array" do - expect { @parser.parse("$a = [1,2]") }.to_not raise_error - end - - it "should not raise errors with a trailing comma" do - expect { @parser.parse("$a = [1,2,]") }.to_not raise_error - end - - it "should accept an empty array" do - expect { @parser.parse("$var = []\n") }.to_not raise_error - end - end - - describe "when parsing classes" do - before :each do - @krt = Puppet::Resource::TypeCollection.new("development") - @classic_parser = Puppet::Parser::Parser.new "development" - @parser = Puppet::Parser::EParserAdapter.new(@classic_parser) - @classic_parser.stubs(:known_resource_types).returns @krt - end - - 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 {}").code[0].instantiate('')[0].parent.should == "yayness" - end - - it "should correctly set the parent class for multiple classes at a time" do - 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 }").code[0].code.should_not be_nil - end - - it "should accept parameters with trailing comma" do - @parser.parse("file { '/example': ensure => file, }").should be - end - - it "should accept parametrized classes with trailing comma" do - @parser.parse("class foobar ($var1 = 0,) { $var = val }").code[0].code.should_not be_nil - end - - it "should define parameters when provided" do - foobar = @parser.parse("class foobar($biz,$baz) {}").code[0].instantiate('')[0] - foobar.arguments.should == {"biz" => nil, "baz" => nil} - end - end - - describe "when parsing resources" do - before :each do - @krt = Puppet::Resource::TypeCollection.new("development") - @classic_parser = Puppet::Parser::Parser.new "development" - @parser = Puppet::Parser::EParserAdapter.new(@classic_parser) - @classic_parser.stubs(:known_resource_types).returns @krt - end - - it "should be able to parse class resources" do - @krt.add(Puppet::Resource::Type.new(:hostclass, "foobar", :arguments => {"biz" => nil})) - expect { @parser.parse("class { foobar: biz => stuff }") }.to_not raise_error - end - - it "should correctly mark exported resources as exported" do - @parser.parse("@@file { '/file': }").code[0].exported.should be_true - end - - it "should correctly mark virtual resources as virtual" do - @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 - - it "should fail if trying to collect defaults" do - expect { - @parser.parse("@Port { protocols => tcp }") - }.to raise_error(Puppet::ParseError, /Defaults are not virtualizable/) - end - - context "when parsing collections" do - it "should parse basic collections" do - @parser.parse("Port <| |>").code. - should be_all {|x| x.is_a? Puppet::Parser::AST::Collection } - end - - it "should parse fully qualified collections" do - @parser.parse("Port::Range <| |>").code. - should be_all {|x| x.is_a? Puppet::Parser::AST::Collection } - end - end - - it "should not assign to a fully qualified variable" do - expect { - @parser.parse("$one::two = yay") - }.to raise_error(Puppet::ParseError, /Cannot assign to variables in other namespaces/) - end - - it "should parse assignment of undef" do - tree = @parser.parse("$var = undef") - tree.code.children[0].should be_an_instance_of Puppet::Parser::AST::VarDef - tree.code.children[0].value.should be_an_instance_of Puppet::Parser::AST::Undef - end - - it "should treat classes as case insensitive" do - @classic_parser.known_resource_types.import_ast(@parser.parse("class yayness {}"), '') - @classic_parser.known_resource_types.hostclass('yayness'). - should == @classic_parser.find_hostclass("", "YayNess") - end - - it "should treat defines as case insensitive" do - @classic_parser.known_resource_types.import_ast(@parser.parse("define funtest {}"), '') - @classic_parser.known_resource_types.hostclass('funtest'). - should == @classic_parser.find_hostclass("", "fUntEst") - end - context "when parsing method calls" do - it "should parse method call with one param lambda" do - expect { @parser.parse("$a.each |$a|{ debug $a }") }.to_not raise_error - end - it "should parse method call with two param lambda" do - expect { @parser.parse("$a.each |$a,$b|{ debug $a }") }.to_not raise_error - end - it "should parse method call with two param lambda and default value" do - expect { @parser.parse("$a.each |$a,$b=1|{ debug $a }") }.to_not raise_error - end - it "should parse method call without lambda (statement)" do - expect { @parser.parse("$a.each") }.to_not raise_error - end - it "should parse method call without lambda (expression)" do - expect { @parser.parse("$x = $a.each + 1") }.to_not raise_error - end - context "a receiver expression of type" do - it "variable should be allowed" do - expect { @parser.parse("$a.each") }.to_not raise_error - end - it "hasharrayaccess should be allowed" do - expect { @parser.parse("$a[0][1].each") }.to_not raise_error - end - it "quoted text should be allowed" do - expect { @parser.parse("\"monkey\".each") }.to_not raise_error - expect { @parser.parse("'monkey'.each") }.to_not raise_error - end - it "selector text should be allowed" do - expect { @parser.parse("$a ? { 'banana'=>[1,2,3]}.each") }.to_not raise_error - end - it "function call should be allowed" do - expect { @parser.parse("duh(1,2,3).each") }.to_not raise_error - end - it "method call should be allowed" do - expect { @parser.parse("$a.foo.bar") }.to_not raise_error - end - it "chained method calls with lambda should be allowed" do - expect { @parser.parse("$a.foo||{}.bar||{}") }.to_not raise_error - end - end - end -end diff --git a/spec/unit/parser/files_spec.rb b/spec/unit/parser/files_spec.rb index 3eb43b276..020ce740b 100755 --- a/spec/unit/parser/files_spec.rb +++ b/spec/unit/parser/files_spec.rb @@ -12,6 +12,25 @@ describe Puppet::Parser::Files do @basepath = make_absolute("/somepath") end + describe "when searching for files" do + it "should return fully-qualified files directly" do + Puppet::Parser::Files.expects(:modulepath).never + Puppet::Parser::Files.find_file(@basepath + "/my/file", environment).should == @basepath + "/my/file" + end + + it "should return the first found file" do + mod = mock 'module' + mod.expects(:file).returns("/one/mymod/files/myfile") + environment.expects(:module).with("mymod").returns mod + + Puppet::Parser::Files.find_file("mymod/myfile", environment).should == "/one/mymod/files/myfile" + end + + it "should return nil if template is not found" do + Puppet::Parser::Files.find_file("foomod/myfile", environment).should be_nil + end + end + describe "when searching for templates" do it "should return fully-qualified templates directly" do Puppet::Parser::Files.expects(:modulepath).never diff --git a/spec/unit/parser/functions/contain_spec.rb b/spec/unit/parser/functions/contain_spec.rb index 3150e0c8e..2a5aa57c7 100644 --- a/spec/unit/parser/functions/contain_spec.rb +++ b/spec/unit/parser/functions/contain_spec.rb @@ -3,11 +3,15 @@ require 'spec_helper' require 'puppet_spec/compiler' require 'puppet/parser/functions' require 'matchers/containment_matchers' +require 'matchers/resource' require 'matchers/include_in_order' +require 'unit/parser/functions/shared' + describe 'The "contain" function' do include PuppetSpec::Compiler include ContainmentMatchers + include Matchers::Resource it "includes the class" do catalog = compile_to_catalog(<<-MANIFEST) @@ -25,6 +29,41 @@ describe 'The "contain" function' do expect(catalog.classes).to include("contained") end + it "includes the class when using a fully qualified anchored name" do + catalog = compile_to_catalog(<<-MANIFEST) + class contained { + notify { "contained": } + } + + class container { + contain ::contained + } + + include container + MANIFEST + + expect(catalog.classes).to include("contained") + end + + it "ensures that the edge is with the correct class" do + catalog = compile_to_catalog(<<-MANIFEST) + class outer { + class named { } + contain named + } + + class named { } + + include named + include outer + MANIFEST + + expect(catalog).to have_resource("Class[Named]") + expect(catalog).to have_resource("Class[Outer]") + expect(catalog).to have_resource("Class[Outer::Named]") + expect(catalog).to contain_class("outer::named").in("outer") + end + it "makes the class contained in the current class" do catalog = compile_to_catalog(<<-MANIFEST) class contained { @@ -182,4 +221,16 @@ describe 'The "contain" function' do ) end end + + describe "When the future parser is in use" do + require 'puppet/pops' + before(:each) do + Puppet[:parser] = 'future' + compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foo")) + @scope = Puppet::Parser::Scope.new(compiler) + end + + it_should_behave_like 'all functions transforming relative to absolute names', :function_contain + it_should_behave_like 'an inclusion function, regardless of the type of class reference,', :contain + end end diff --git a/spec/unit/parser/functions/create_resources_spec.rb b/spec/unit/parser/functions/create_resources_spec.rb index 79ed02f22..3e7bd8015 100755 --- a/spec/unit/parser/functions/create_resources_spec.rb +++ b/spec/unit/parser/functions/create_resources_spec.rb @@ -49,7 +49,7 @@ describe 'function for dynamically creating resources' do end it 'should be able to add exported resources' do - catalog = compile_to_catalog("create_resources('@@file', {'/etc/foo'=>{'ensure'=>'present'}})") + catalog = compile_to_catalog("create_resources('@@file', {'/etc/foo'=>{'ensure'=>'present'}}) realize(File['/etc/foo'])") catalog.resource(:file, "/etc/foo")['ensure'].should == 'present' catalog.resource(:file, "/etc/foo").exported.should == true end @@ -202,5 +202,12 @@ describe 'function for dynamically creating resources' do catalog.resource(:notify, "test")['message'].should == 'two' catalog.resource(:class, "bar").should_not be_nil end + + it 'should fail with a correct error message if the syntax of an imported file is incorrect' do + expect{ + Puppet[:modulepath] = my_fixture_dir + compile_to_catalog('include foo') + }.to raise_error(Puppet::Error, /Syntax error at.*/) + end end end diff --git a/spec/unit/parser/functions/digest_spec.rb b/spec/unit/parser/functions/digest_spec.rb new file mode 100755 index 000000000..e3c0762d4 --- /dev/null +++ b/spec/unit/parser/functions/digest_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the digest function", :uses_checksums => true do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + n = Puppet::Node.new('unnamed') + c = Puppet::Parser::Compiler.new(n) + @scope = Puppet::Parser::Scope.new(c) + end + + it "should exist" do + Puppet::Parser::Functions.function("digest").should == "function_digest" + end + + with_digest_algorithms do + it "should use the proper digest function" do + result = @scope.function_digest([plaintext]) + result.should(eql( checksum )) + end + + it "should only accept one parameter" do + expect do + @scope.function_digest(['foo', 'bar']) + end.to raise_error(ArgumentError) + end + end +end diff --git a/spec/unit/parser/functions/epp_spec.rb b/spec/unit/parser/functions/epp_spec.rb deleted file mode 100644 index b88d3da8f..000000000 --- a/spec/unit/parser/functions/epp_spec.rb +++ /dev/null @@ -1,103 +0,0 @@ - -require 'spec_helper' - -describe "the epp function" do - include PuppetSpec::Files - - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - Puppet[:parser] = 'future' - end - - let :node do Puppet::Node.new('localhost') end - let :compiler do Puppet::Parser::Compiler.new(node) end - let :scope do Puppet::Parser::Scope.new(compiler) end - - context "when accessing scope variables as $ variables" do - it "looks up the value from the scope" do - scope["what"] = "are belong" - eval_template("all your base <%= $what %> to us").should == "all your base are belong to us" - end - - it "get nil accessing a variable that does not exist" do - eval_template("<%= $kryptonite == undef %>").should == "true" - end - - it "get nil accessing a variable that is undef" do - scope['undef_var'] = :undef - eval_template("<%= $undef_var == undef %>").should == "true" - end - - it "gets shadowed variable if args are given" do - scope['phantom'] = 'of the opera' - eval_template_with_args("<%= $phantom == dragos %>", 'phantom' => 'dragos').should == "true" - end - - it "gets shadowed variable if args are given and parameters are specified" do - scope['x'] = 'wrong one' - eval_template_with_args("<%-( $x )-%><%= $x == correct %>", 'x' => 'correct').should == "true" - end - - it "raises an error if required variable is not given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'y' => 'correct') - }.to raise_error(/no value given for required parameters x/) - end - - it "raises an error if too many arguments are given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'x' => 'correct', 'y' => 'surplus') - }.to raise_error(/Too many arguments: 2 for 1/) - end - end - - # although never a problem with epp - it "is not interfered with by having a variable named 'string' (#14093)" do - scope['string'] = "this output should not be seen" - eval_template("some text that is static").should == "some text that is static" - end - - it "has access to a variable named 'string' (#14093)" do - scope['string'] = "the string value" - eval_template("string was: <%= $string %>").should == "string was: the string value" - end - - describe 'when loading from modules' do - include PuppetSpec::Files - it 'an epp template is found' do - modules_dir = dir_containing('modules', { - 'testmodule' => { - 'templates' => { - 'the_x.epp' => 'The x is <%= $x %>' - } - }}) - Puppet.override({:current_environment => (env = Puppet::Node::Environment.create(:testload, [ modules_dir ]))}, "test") do - node.environment = env - expect(scope.function_epp([ 'testmodule/the_x.epp', { 'x' => '3'} ])).to eql("The x is 3") - end - end - end - - def eval_template_with_args(content, args_hash) - file_path = tmpdir('epp_spec_content') - filename = File.join(file_path, "template.epp") - File.open(filename, "w+") { |f| f.write(content) } - - Puppet::Parser::Files.stubs(:find_template).returns(filename) - scope.function_epp(['template', args_hash]) - end - - def eval_template(content) - file_path = tmpdir('epp_spec_content') - filename = File.join(file_path, "template.epp") - File.open(filename, "w+") { |f| f.write(content) } - - Puppet::Parser::Files.stubs(:find_template).returns(filename) - scope.function_epp(['template']) - end -end diff --git a/spec/unit/parser/functions/file_spec.rb b/spec/unit/parser/functions/file_spec.rb index 34d12e2f2..c5f157300 100755 --- a/spec/unit/parser/functions/file_spec.rb +++ b/spec/unit/parser/functions/file_spec.rb @@ -13,10 +13,6 @@ describe "the 'file' function" do let :compiler do Puppet::Parser::Compiler.new(node) end let :scope do Puppet::Parser::Scope.new(compiler) end - it "should exist" do - Puppet::Parser::Functions.function("file").should == "function_file" - end - def with_file_content(content) path = tmpfile('file-function') file = File.new(path, 'w') @@ -31,7 +27,17 @@ describe "the 'file' function" do end end - it "should return the first file if given two files" do + it "should read a file from a module path" do + with_file_content('file content') do |name| + mod = mock 'module' + mod.stubs(:file).with('myfile').returns(name) + compiler.environment.stubs(:module).with('mymod').returns(mod) + + scope.function_file(['mymod/myfile']).should == 'file content' + end + end + + it "should return the first file if given two files with absolute paths" do with_file_content('one') do |one| with_file_content('two') do |two| scope.function_file([one, two]).should == "one" @@ -39,6 +45,43 @@ describe "the 'file' function" do end end + it "should return the first file if given two files with module paths" do + with_file_content('one') do |one| + with_file_content('two') do |two| + mod = mock 'module' + compiler.environment.expects(:module).with('mymod').returns(mod) + mod.expects(:file).with('one').returns(one) + mod.stubs(:file).with('two').returns(two) + + scope.function_file(['mymod/one','mymod/two']).should == 'one' + end + end + end + + it "should return the first file if given two files with mixed paths, absolute first" do + with_file_content('one') do |one| + with_file_content('two') do |two| + mod = mock 'module' + compiler.environment.stubs(:module).with('mymod').returns(mod) + mod.stubs(:file).with('two').returns(two) + + scope.function_file([one,'mymod/two']).should == 'one' + end + end + end + + it "should return the first file if given two files with mixed paths, module first" do + with_file_content('one') do |one| + with_file_content('two') do |two| + mod = mock 'module' + compiler.environment.expects(:module).with('mymod').returns(mod) + mod.stubs(:file).with('two').returns(two) + + scope.function_file(['mymod/two',one]).should == 'two' + end + end + end + it "should not fail when some files are absent" do expect { with_file_content('one') do |one| diff --git a/spec/unit/parser/functions/include_spec.rb b/spec/unit/parser/functions/include_spec.rb index c1a5cbd5c..3fa0da35d 100755 --- a/spec/unit/parser/functions/include_spec.rb +++ b/spec/unit/parser/functions/include_spec.rb @@ -1,5 +1,6 @@ #! /usr/bin/env ruby require 'spec_helper' +require 'unit/parser/functions/shared' describe "the 'include' function" do before :all do @@ -46,6 +47,19 @@ describe "the 'include' function" do it "should raise if the class is not found" do @scope.stubs(:source).returns(true) - expect { @scope.function_include(["nosuchclass"]) }.to raise_error Puppet::Error + expect { @scope.function_include(["nosuchclass"]) }.to raise_error(Puppet::Error) + end + + describe "When the future parser is in use" do + require 'puppet/pops' + require 'puppet_spec/compiler' + include PuppetSpec::Compiler + + before(:each) do + Puppet[:parser] = 'future' + end + + it_should_behave_like 'all functions transforming relative to absolute names', :function_include + it_should_behave_like 'an inclusion function, regardless of the type of class reference,', :include end end diff --git a/spec/unit/parser/functions/inline_epp_spec.rb b/spec/unit/parser/functions/inline_epp_spec.rb deleted file mode 100644 index 44b24528b..000000000 --- a/spec/unit/parser/functions/inline_epp_spec.rb +++ /dev/null @@ -1,82 +0,0 @@ - -require 'spec_helper' - -describe "the inline_epp function" do - include PuppetSpec::Files - - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - Puppet[:parser] = 'future' - end - - let :node do Puppet::Node.new('localhost') end - let :compiler do Puppet::Parser::Compiler.new(node) end - let :scope do Puppet::Parser::Scope.new(compiler) end - - context "when accessing scope variables as $ variables" do - it "looks up the value from the scope" do - scope["what"] = "are belong" - eval_template("all your base <%= $what %> to us").should == "all your base are belong to us" - end - - it "get nil accessing a variable that does not exist" do - eval_template("<%= $kryptonite == undef %>").should == "true" - end - - it "get nil accessing a variable that is undef" do - scope['undef_var'] = :undef - eval_template("<%= $undef_var == undef %>").should == "true" - end - - it "gets shadowed variable if args are given" do - scope['phantom'] = 'of the opera' - eval_template_with_args("<%= $phantom == dragos %>", 'phantom' => 'dragos').should == "true" - end - - it "gets shadowed variable if args are given and parameters are specified" do - scope['x'] = 'wrong one' - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'x' => 'correct').should == "true" - end - - it "raises an error if required variable is not given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'y' => 'correct') - }.to raise_error(/no value given for required parameters x/) - end - - it "raises an error if too many arguments are given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'x' => 'correct', 'y' => 'surplus') - }.to raise_error(/Too many arguments: 2 for 1/) - end - end - - it "renders a block expression" do - eval_template_with_args("<%= {($x) $x + 1} %>", 'x' => 2).should == "3" - end - - # although never a problem with epp - it "is not interfered with by having a variable named 'string' (#14093)" do - scope['string'] = "this output should not be seen" - eval_template("some text that is static").should == "some text that is static" - end - - it "has access to a variable named 'string' (#14093)" do - scope['string'] = "the string value" - eval_template("string was: <%= $string %>").should == "string was: the string value" - end - - - def eval_template_with_args(content, args_hash) - scope.function_inline_epp([content, args_hash]) - end - - def eval_template(content) - scope.function_inline_epp([content]) - end -end diff --git a/spec/unit/parser/functions/realize_spec.rb b/spec/unit/parser/functions/realize_spec.rb index 9f53f5a76..79e5eb155 100755 --- a/spec/unit/parser/functions/realize_spec.rb +++ b/spec/unit/parser/functions/realize_spec.rb @@ -1,53 +1,61 @@ -#! /usr/bin/env ruby require 'spec_helper' +require 'matchers/resource' +require 'puppet_spec/compiler' describe "the realize function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end + include Matchers::Resource + include PuppetSpec::Compiler - before :each do - @collector = stub_everything 'collector' - node = Puppet::Node.new('localhost') - @compiler = Puppet::Parser::Compiler.new(node) - @scope = Puppet::Parser::Scope.new(@compiler) - @compiler.stubs(:add_collection).with(@collector) - end + it "realizes a single, referenced resource" do + catalog = compile_to_catalog(<<-EOM) + @notify { testing: } + realize(Notify[testing]) + EOM - it "should exist" do - Puppet::Parser::Functions.function("realize").should == "function_realize" + expect(catalog).to have_resource("Notify[testing]") end - it "should create a Collector when called" do - - Puppet::Parser::Collector.expects(:new).returns(@collector) + it "realizes multiple resources" do + catalog = compile_to_catalog(<<-EOM) + @notify { testing: } + @notify { other: } + realize(Notify[testing], Notify[other]) + EOM - @scope.function_realize(["test"]) + expect(catalog).to have_resource("Notify[testing]") + expect(catalog).to have_resource("Notify[other]") end - it "should assign the passed-in resources to the collector" do - Puppet::Parser::Collector.stubs(:new).returns(@collector) + it "realizes resources provided in arrays" do + catalog = compile_to_catalog(<<-EOM) + @notify { testing: } + @notify { other: } + realize([Notify[testing], [Notify[other]]]) + EOM - @collector.expects(:resources=).with(["test"]) - - @scope.function_realize(["test"]) + expect(catalog).to have_resource("Notify[testing]") + expect(catalog).to have_resource("Notify[other]") end - it "should flatten the resources assigned to the collector" do - Puppet::Parser::Collector.stubs(:new).returns(@collector) - - @collector.expects(:resources=).with(["test"]) - - @scope.function_realize([["test"]]) + it "fails when the resource does not exist" do + expect do + compile_to_catalog(<<-EOM) + realize(Notify[missing]) + EOM + end.to raise_error(Puppet::Error, /Failed to realize/) end - it "should let the compiler know this collector" do - Puppet::Parser::Collector.stubs(:new).returns(@collector) - @collector.stubs(:resources=).with(["test"]) - - @compiler.expects(:add_collection).with(@collector) - - @scope.function_realize(["test"]) + it "fails when no parameters given" do + expect do + compile_to_catalog(<<-EOM) + realize() + EOM + end.to raise_error(Puppet::Error, /Wrong number of arguments/) end + it "silently does nothing when an empty array of resources is given" do + compile_to_catalog(<<-EOM) + realize([]) + EOM + end end diff --git a/spec/unit/parser/functions/require_spec.rb b/spec/unit/parser/functions/require_spec.rb index 72c3f9f5f..f0b4fcc28 100755 --- a/spec/unit/parser/functions/require_spec.rb +++ b/spec/unit/parser/functions/require_spec.rb @@ -1,5 +1,6 @@ #! /usr/bin/env ruby require 'spec_helper' +require 'unit/parser/functions/shared' describe "the require function" do before :all do @@ -26,13 +27,13 @@ describe "the require function" do end it "should delegate to the 'include' puppet function" do - @scope.expects(:function_include).with(["myclass"]) + @scope.compiler.expects(:evaluate_classes).with(["myclass"], @scope, false) @scope.function_require(["myclass"]) end - it "should set the 'require' prarameter on the resource to a resource reference" do - @scope.stubs(:function_include) + it "should set the 'require' parameter on the resource to a resource reference" do + @scope.compiler.stubs(:evaluate_classes) @scope.function_require(["myclass"]) @resource["require"].should be_instance_of(Array) @@ -40,7 +41,7 @@ describe "the require function" do end it "should lookup the absolute class path" do - @scope.stubs(:function_include) + @scope.compiler.stubs(:evaluate_classes) @scope.expects(:find_hostclass).with("myclass").returns(@klass) @klass.expects(:name).returns("myclass") @@ -49,7 +50,7 @@ describe "the require function" do end it "should append the required class to the require parameter" do - @scope.stubs(:function_include) + @scope.compiler.stubs(:evaluate_classes) one = Puppet::Resource.new(:file, "/one") @resource[:require] = one @@ -58,4 +59,17 @@ describe "the require function" do @resource[:require].should be_include(one) @resource[:require].detect { |r| r.to_s == "Class[Myclass]" }.should be_instance_of(Puppet::Resource) end + + describe "When the future parser is in use" do + require 'puppet/pops' + require 'puppet_spec/compiler' + include PuppetSpec::Compiler + + before(:each) do + Puppet[:parser] = 'future' + end + + it_should_behave_like 'all functions transforming relative to absolute names', :function_require + it_should_behave_like 'an inclusion function, regardless of the type of class reference,', :require + end end diff --git a/spec/unit/parser/functions/search_spec.rb b/spec/unit/parser/functions/search_spec.rb index b2c042b04..54054bd6a 100755 --- a/spec/unit/parser/functions/search_spec.rb +++ b/spec/unit/parser/functions/search_spec.rb @@ -20,4 +20,9 @@ describe "the 'search' function" do scope.expects(:add_namespace).with("who") scope.function_search(["where", "what", "who"]) end + + it "is deprecated" do + Puppet.expects(:deprecation_warning).with("The 'search' function is deprecated. See http://links.puppetlabs.com/search-function-deprecation") + scope.function_search(['wat']) + end end diff --git a/spec/unit/parser/functions/shared.rb b/spec/unit/parser/functions/shared.rb new file mode 100644 index 000000000..f5adcd811 --- /dev/null +++ b/spec/unit/parser/functions/shared.rb @@ -0,0 +1,82 @@ +shared_examples_for 'all functions transforming relative to absolute names' do |func_method| + + it 'transforms relative names to absolute' do + @scope.compiler.expects(:evaluate_classes).with(["::myclass"], @scope, false) + @scope.send(func_method, ["myclass"]) + end + + it 'accepts a Class[name] type' do + @scope.compiler.expects(:evaluate_classes).with(["::myclass"], @scope, false) + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.host_class('myclass')]) + end + + it 'accepts a Resource[class, name] type' do + @scope.compiler.expects(:evaluate_classes).with(["::myclass"], @scope, false) + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource('class', 'myclass')]) + end + + it 'raises and error for unspecific Class' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.host_class()]) + }.to raise_error(ArgumentError, /Cannot use an unspecific Class\[\] Type/) + end + + it 'raises and error for Resource that is not of class type' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource('file')]) + }.to raise_error(ArgumentError, /Cannot use a Resource\[file\] where a Resource\['class', name\] is expected/) + end + + it 'raises and error for Resource that is unspecific' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource()]) + }.to raise_error(ArgumentError, /Cannot use an unspecific Resource\[\] where a Resource\['class', name\] is expected/) + end + + it 'raises and error for Resource[class] that is unspecific' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource('class')]) + }.to raise_error(ArgumentError, /Cannot use an unspecific Resource\['class'\] where a Resource\['class', name\] is expected/) + end + +end + +shared_examples_for 'an inclusion function, regardless of the type of class reference,' do |function| + + it "and #{function} a class absolutely, even when a relative namespaced class of the same name is present" do + catalog = compile_to_catalog(<<-MANIFEST) + class foo { + class bar { } + #{function} bar + } + class bar { } + include foo + MANIFEST + expect(catalog.classes).to include('foo','bar') + end + + it "and #{function} a class absolutely by Class['type'] reference" do + catalog = compile_to_catalog(<<-MANIFEST) + class foo { + class bar { } + #{function} Class['bar'] + } + class bar { } + include foo + MANIFEST + expect(catalog.classes).to include('foo','bar') + end + + it "and #{function} a class absolutely by Resource['type','title'] reference" do + catalog = compile_to_catalog(<<-MANIFEST) + class foo { + class bar { } + #{function} Resource['class','bar'] + } + class bar { } + include foo + MANIFEST + expect(catalog.classes).to include('foo','bar') + end + +end diff --git a/spec/unit/parser/functions_spec.rb b/spec/unit/parser/functions_spec.rb index b3c04d1af..3c6266752 100755 --- a/spec/unit/parser/functions_spec.rb +++ b/spec/unit/parser/functions_spec.rb @@ -38,7 +38,7 @@ describe Puppet::Parser::Functions do it "instruments the function to profile the execution" do messages = [] - Puppet::Util::Profiler.current = Puppet::Util::Profiler::WallClock.new(proc { |msg| messages << msg }, "id") + Puppet::Util::Profiler.add_profiler(Puppet::Util::Profiler::WallClock.new(proc { |msg| messages << msg }, "id")) Puppet::Parser::Functions.newfunction("name", :type => :rvalue) { |args| } callable_functions_from(function_module).function_name([]) @@ -85,10 +85,7 @@ describe Puppet::Parser::Functions do end describe "when calling function to test arity" do - let(:function_module) { Module.new } - before do - Puppet::Parser::Functions.stubs(:environment_module).returns(function_module) - end + let(:function_module) { Puppet::Parser::Functions.environment_module(Puppet.lookup(:current_environment)) } it "should raise an error if the function is called with too many arguments" do Puppet::Parser::Functions.newfunction("name", :arity => 2) { |args| } diff --git a/spec/unit/parser/lexer_spec.rb b/spec/unit/parser/lexer_spec.rb index 62234e214..f0f10e9f3 100755 --- a/spec/unit/parser/lexer_spec.rb +++ b/spec/unit/parser/lexer_spec.rb @@ -279,7 +279,8 @@ describe Puppet::Parser::Lexer::TOKENS[:NAME] do it "should return itself and the value if the matched term is not a keyword" do Puppet::Parser::Lexer::KEYWORDS.expects(:lookup).returns(nil) - @token.convert(stub("lexer"), "myval").should == [Puppet::Parser::Lexer::TOKENS[:NAME], "myval"] + lexer = stub("lexer") + @token.convert(lexer, "myval").should == [Puppet::Parser::Lexer::TOKENS[:NAME], "myval"] end it "should return the keyword token and the value if the matched term is a keyword" do @@ -845,6 +846,14 @@ describe "Puppet::Parser::Lexer in the old tests" do end end +describe 'Puppet::Parser::Lexer handles reserved words' do + ['function', 'private', 'attr', 'type'].each do |reserved_bare_word| + it "by delivering '#{reserved_bare_word}' as a bare word" do + expect(tokens_scanned_from(reserved_bare_word)).to eq([[:NAME, {:value=>reserved_bare_word, :line => 1}]]) + end + end +end + describe "Puppet::Parser::Lexer in the old tests when lexing example files" do my_fixtures('*.pp') do |file| it "should correctly lex #{file}" do diff --git a/spec/unit/parser/methods/each_spec.rb b/spec/unit/parser/methods/each_spec.rb deleted file mode 100644 index 5e9ce4e0c..000000000 --- a/spec/unit/parser/methods/each_spec.rb +++ /dev/null @@ -1,91 +0,0 @@ -require 'puppet' -require 'spec_helper' -require 'puppet_spec/compiler' -require 'rubygems' - -describe 'the each method' do - include PuppetSpec::Compiler - - before :each do - Puppet[:parser] = 'future' - end - - context "should be callable as" do - it 'each on an array selecting each value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1,2,3] - $a.each |$v| { - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'present' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - it 'each on an array selecting each value - function call style' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1,2,3] - each ($a) |$index, $v| { - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'present' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - it 'each on an array with index' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [present, absent, present] - $a.each |$k,$v| { - file { "/file_${$k+1}": ensure => $v } - } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'absent' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - it 'each on a hash selecting entries' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'a'=>'present','b'=>'absent','c'=>'present'} - $a.each |$e| { - file { "/file_${e[0]}": ensure => $e[1] } - } - MANIFEST - - catalog.resource(:file, "/file_a")['ensure'].should == 'present' - catalog.resource(:file, "/file_b")['ensure'].should == 'absent' - catalog.resource(:file, "/file_c")['ensure'].should == 'present' - end - it 'each on a hash selecting key and value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'a'=>present,'b'=>absent,'c'=>present} - $a.each |$k, $v| { - file { "/file_$k": ensure => $v } - } - MANIFEST - - catalog.resource(:file, "/file_a")['ensure'].should == 'present' - catalog.resource(:file, "/file_b")['ensure'].should == 'absent' - catalog.resource(:file, "/file_c")['ensure'].should == 'present' - end - end - context "should produce receiver" do - it 'each checking produced value using single expression' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1, 3, 2] - $b = $a.each |$x| { "unwanted" } - file { "/file_${b[1]}": - ensure => present - } - MANIFEST - - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - end -end diff --git a/spec/unit/parser/methods/filter_spec.rb b/spec/unit/parser/methods/filter_spec.rb deleted file mode 100644 index c9fed31d2..000000000 --- a/spec/unit/parser/methods/filter_spec.rb +++ /dev/null @@ -1,135 +0,0 @@ -require 'puppet' -require 'spec_helper' -require 'puppet_spec/compiler' - -require 'unit/parser/methods/shared' - -describe 'the filter method' do - include PuppetSpec::Compiler - - before :each do - Puppet[:parser] = 'future' - end - - it 'should filter on an array (all berries)' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = ['strawberry','blueberry','orange'] - $a.filter |$x|{ $x =~ /berry$/}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_strawberry")['ensure'].should == 'present' - catalog.resource(:file, "/file_blueberry")['ensure'].should == 'present' - end - - it 'should filter on enumerable type (Integer)' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = Integer[1,10] - $a.filter |$x|{ $x % 3 == 0}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - catalog.resource(:file, "/file_6")['ensure'].should == 'present' - catalog.resource(:file, "/file_9")['ensure'].should == 'present' - end - - it 'should filter on enumerable type (Integer) using two args index/value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = Integer[10,18] - $a.filter |$i, $x|{ $i % 3 == 0}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_10")['ensure'].should == 'present' - catalog.resource(:file, "/file_13")['ensure'].should == 'present' - catalog.resource(:file, "/file_16")['ensure'].should == 'present' - end - - it 'should produce an array when acting on an array' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = ['strawberry','blueberry','orange'] - $b = $a.filter |$x|{ $x =~ /berry$/} - file { "/file_${b[0]}": ensure => present } - file { "/file_${b[1]}": ensure => present } - MANIFEST - - catalog.resource(:file, "/file_strawberry")['ensure'].should == 'present' - catalog.resource(:file, "/file_blueberry")['ensure'].should == 'present' - end - - it 'can filter array using index and value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = ['strawberry','blueberry','orange'] - $b = $a.filter |$index, $x|{ $index == 0 or $index ==2} - file { "/file_${b[0]}": ensure => present } - file { "/file_${b[1]}": ensure => present } - MANIFEST - - catalog.resource(:file, "/file_strawberry")['ensure'].should == 'present' - catalog.resource(:file, "/file_orange")['ensure'].should == 'present' - end - - it 'filters on a hash (all berries) by key' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'strawberry'=>'red','blueberry'=>'blue','orange'=>'orange'} - $a.filter |$x|{ $x[0] =~ /berry$/}.each |$v|{ - file { "/file_${v[0]}": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_strawberry")['ensure'].should == 'present' - catalog.resource(:file, "/file_blueberry")['ensure'].should == 'present' - end - - it 'should produce a hash when acting on a hash' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'strawberry'=>'red','blueberry'=>'blue','orange'=>'orange'} - $b = $a.filter |$x|{ $x[0] =~ /berry$/} - file { "/file_${b['strawberry']}": ensure => present } - file { "/file_${b['blueberry']}": ensure => present } - file { "/file_${b['orange']}": ensure => present } - - MANIFEST - - catalog.resource(:file, "/file_red")['ensure'].should == 'present' - catalog.resource(:file, "/file_blue")['ensure'].should == 'present' - catalog.resource(:file, "/file_")['ensure'].should == 'present' - end - - it 'filters on a hash (all berries) by value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'strawb'=>'red berry','blueb'=>'blue berry','orange'=>'orange fruit'} - $a.filter |$x|{ $x[1] =~ /berry$/}.each |$v|{ - file { "/file_${v[0]}": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_strawb")['ensure'].should == 'present' - catalog.resource(:file, "/file_blueb")['ensure'].should == 'present' - end - - context 'filter checks arguments and' do - it 'raises an error when block has more than 2 argument' do - expect do - compile_to_catalog(<<-MANIFEST) - [1].filter |$indexm, $x, $yikes|{ } - MANIFEST - end.to raise_error(Puppet::Error, /block must define at most two parameters/) - end - - it 'raises an error when block has fewer than 1 argument' do - expect do - compile_to_catalog(<<-MANIFEST) - [1].filter || { } - MANIFEST - end.to raise_error(Puppet::Error, /block must define at least one parameter/) - end - end - - it_should_behave_like 'all iterative functions argument checks', 'filter' - it_should_behave_like 'all iterative functions hash handling', 'filter' -end diff --git a/spec/unit/parser/methods/map_spec.rb b/spec/unit/parser/methods/map_spec.rb deleted file mode 100644 index 7f8e79789..000000000 --- a/spec/unit/parser/methods/map_spec.rb +++ /dev/null @@ -1,184 +0,0 @@ -require 'puppet' -require 'spec_helper' -require 'puppet_spec/compiler' - -require 'unit/parser/methods/shared' - -describe 'the map method' do - include PuppetSpec::Compiler - - before :each do - Puppet[:parser] = "future" - end - - context "using future parser" do - it 'map on an array (multiplying each value by 2)' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1,2,3] - $a.map |$x|{ $x*2}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_2")['ensure'].should == 'present' - catalog.resource(:file, "/file_4")['ensure'].should == 'present' - catalog.resource(:file, "/file_6")['ensure'].should == 'present' - end - - it 'map on an enumerable type (multiplying each value by 2)' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = Integer[1,3] - $a.map |$x|{ $x*2}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_2")['ensure'].should == 'present' - catalog.resource(:file, "/file_4")['ensure'].should == 'present' - catalog.resource(:file, "/file_6")['ensure'].should == 'present' - end - - it 'map on an integer (multiply each by 3)' do - catalog = compile_to_catalog(<<-MANIFEST) - 3.map |$x|{ $x*3}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_0")['ensure'].should == 'present' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - catalog.resource(:file, "/file_6")['ensure'].should == 'present' - end - - it 'map on a string' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {a=>x, b=>y} - "ab".map |$x|{$a[$x]}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_x")['ensure'].should == 'present' - catalog.resource(:file, "/file_y")['ensure'].should == 'present' - end - - it 'map on an array (multiplying value by 10 in even index position)' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1,2,3] - $a.map |$i, $x|{ if $i % 2 == 0 {$x} else {$x*10}}.each |$v|{ - file { "/file_$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_20")['ensure'].should == 'present' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - it 'map on a hash selecting keys' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'a'=>1,'b'=>2,'c'=>3} - $a.map |$x|{ $x[0]}.each |$k|{ - file { "/file_$k": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_a")['ensure'].should == 'present' - catalog.resource(:file, "/file_b")['ensure'].should == 'present' - catalog.resource(:file, "/file_c")['ensure'].should == 'present' - end - - it 'map on a hash selecting keys - using two block parameters' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'a'=>1,'b'=>2,'c'=>3} - $a.map |$k,$v|{ file { "/file_$k": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_a")['ensure'].should == 'present' - catalog.resource(:file, "/file_b")['ensure'].should == 'present' - catalog.resource(:file, "/file_c")['ensure'].should == 'present' - end - - it 'each on a hash selecting value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'a'=>1,'b'=>2,'c'=>3} - $a.map |$x|{ $x[1]}.each |$k|{ file { "/file_$k": ensure => present } } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'present' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - it 'each on a hash selecting value - using two bloc parameters' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {'a'=>1,'b'=>2,'c'=>3} - $a.map |$k,$v|{ file { "/file_$v": ensure => present } } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'present' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - context "handles data type corner cases" do - it "map gets values that are false" do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [false,false] - $a.map |$x| { $x }.each |$i, $v| { - file { "/file_$i.$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_0.false")['ensure'].should == 'present' - catalog.resource(:file, "/file_1.false")['ensure'].should == 'present' - end - - it "map gets values that are nil" do - Puppet::Parser::Functions.newfunction(:nil_array, :type => :rvalue) do |args| - [nil] - end - catalog = compile_to_catalog(<<-MANIFEST) - $a = nil_array() - $a.map |$x| { $x }.each |$i, $v| { - file { "/file_$i.$v": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_0.")['ensure'].should == 'present' - end - - it "map gets values that are undef" do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [$does_not_exist] - $a.map |$x = "something"| { $x }.each |$i, $v| { - file { "/file_$i.$v": ensure => present } - } - MANIFEST - catalog.resource(:file, "/file_0.something")['ensure'].should == 'present' - end - end - - context 'map checks arguments and' do - it 'raises an error when block has more than 2 argument' do - expect do - compile_to_catalog(<<-MANIFEST) - [1].map |$index, $x, $yikes|{ } - MANIFEST - end.to raise_error(Puppet::Error, /block must define at most two parameters/) - end - - it 'raises an error when block has fewer than 1 argument' do - expect do - compile_to_catalog(<<-MANIFEST) - [1].map || { } - MANIFEST - end.to raise_error(Puppet::Error, /block must define at least one parameter/) - end - end - - it_should_behave_like 'all iterative functions argument checks', 'map' - it_should_behave_like 'all iterative functions hash handling', 'map' - end -end diff --git a/spec/unit/parser/methods/reduce_spec.rb b/spec/unit/parser/methods/reduce_spec.rb deleted file mode 100644 index 4f0c14e5e..000000000 --- a/spec/unit/parser/methods/reduce_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'puppet' -require 'spec_helper' -require 'puppet_spec/compiler' - -describe 'the reduce method' do - include PuppetSpec::Compiler - - before :all do - # enable switching back - @saved_parser = Puppet[:parser] - # These tests only work with future parser - end - after :all do - # switch back to original - Puppet[:parser] = @saved_parser - end - - before :each do - node = Puppet::Node.new("floppy", :environment => 'production') - @compiler = Puppet::Parser::Compiler.new(node) - @scope = Puppet::Parser::Scope.new(@compiler) - @topscope = @scope.compiler.topscope - @scope.parent = @topscope - Puppet[:parser] = 'future' - end - - context "should be callable as" do - it 'reduce on an array' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1,2,3] - $b = $a.reduce |$memo, $x| { $memo + $x } - file { "/file_$b": ensure => present } - MANIFEST - - catalog.resource(:file, "/file_6")['ensure'].should == 'present' - end - - it 'reduce on enumerable type' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = Integer[1,3] - $b = $a.reduce |$memo, $x| { $memo + $x } - file { "/file_$b": ensure => present } - MANIFEST - - catalog.resource(:file, "/file_6")['ensure'].should == 'present' - end - - it 'reduce on an array with start value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1,2,3] - $b = $a.reduce(4) |$memo, $x| { $memo + $x } - file { "/file_$b": ensure => present } - MANIFEST - - catalog.resource(:file, "/file_10")['ensure'].should == 'present' - end - it 'reduce on a hash' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {a=>1, b=>2, c=>3} - $start = [ignored, 4] - $b = $a.reduce |$memo, $x| {['sum', $memo[1] + $x[1]] } - file { "/file_${$b[0]}_${$b[1]}": ensure => present } - MANIFEST - - catalog.resource(:file, "/file_sum_6")['ensure'].should == 'present' - end - it 'reduce on a hash with start value' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {a=>1, b=>2, c=>3} - $start = ['ignored', 4] - $b = $a.reduce($start) |$memo, $x| { ['sum', $memo[1] + $x[1]] } - file { "/file_${$b[0]}_${$b[1]}": ensure => present } - MANIFEST - - catalog.resource(:file, "/file_sum_10")['ensure'].should == 'present' - end - end -end diff --git a/spec/unit/parser/methods/shared.rb b/spec/unit/parser/methods/shared.rb deleted file mode 100644 index 42cfd2359..000000000 --- a/spec/unit/parser/methods/shared.rb +++ /dev/null @@ -1,45 +0,0 @@ - -shared_examples_for 'all iterative functions hash handling' do |func| - it 'passes a hash entry as an array of the key and value' do - catalog = compile_to_catalog(<<-MANIFEST) - {a=>1}.#{func} |$v| { notify { "${v[0]} ${v[1]}": } } - MANIFEST - - catalog.resource(:notify, "a 1").should_not be_nil - end -end - -shared_examples_for 'all iterative functions argument checks' do |func| - - it 'raises an error when used against an unsupported type' do - expect do - compile_to_catalog(<<-MANIFEST) - 3.14.#{func} |$v| { } - MANIFEST - end.to raise_error(Puppet::Error, /must be something enumerable/) - end - - it 'raises an error when called with any parameters besides a block' do - expect do - compile_to_catalog(<<-MANIFEST) - [1].#{func}(1) |$v| { } - MANIFEST - end.to raise_error(Puppet::Error, /Wrong number of arguments/) - end - - it 'raises an error when called without a block' do - expect do - compile_to_catalog(<<-MANIFEST) - [1].#{func}() - MANIFEST - end.to raise_error(Puppet::Error, /Wrong number of arguments/) - end - - it 'raises an error when called without a block' do - expect do - compile_to_catalog(<<-MANIFEST) - [1].#{func}(1) - MANIFEST - end.to raise_error(Puppet::Error, /must be a parameterized block/) - end -end diff --git a/spec/unit/parser/methods/slice_spec.rb b/spec/unit/parser/methods/slice_spec.rb deleted file mode 100644 index 1de1dd0f1..000000000 --- a/spec/unit/parser/methods/slice_spec.rb +++ /dev/null @@ -1,135 +0,0 @@ -require 'puppet' -require 'spec_helper' -require 'puppet_spec/compiler' -require 'rubygems' - -describe 'methods' do - include PuppetSpec::Compiler - - before :all do - # enable switching back - @saved_parser = Puppet[:parser] - # These tests only work with future parser - Puppet[:parser] = 'future' - end - after :all do - # switch back to original - Puppet[:parser] = @saved_parser - end - - before :each do - node = Puppet::Node.new("floppy", :environment => 'production') - @compiler = Puppet::Parser::Compiler.new(node) - @scope = Puppet::Parser::Scope.new(@compiler) - @topscope = @scope.compiler.topscope - @scope.parent = @topscope - Puppet[:parser] = 'future' - end - - context "should be callable on array as" do - - it 'slice with explicit parameters' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1, present, 2, absent, 3, present] - $a.slice(2) |$k,$v| { - file { "/file_${$k}": ensure => $v } - } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'absent' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - it 'slice with one parameter' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1, present, 2, absent, 3, present] - $a.slice(2) |$k| { - file { "/file_${$k[0]}": ensure => $k[1] } - } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'absent' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - end - - it 'slice with shorter last slice' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1, present, 2, present, 3, absent] - $a.slice(4) |$a, $b, $c, $d| { - file { "/file_$a.$c": ensure => $b } - } - MANIFEST - - catalog.resource(:file, "/file_1.2")['ensure'].should == 'present' - catalog.resource(:file, "/file_3.")['ensure'].should == 'absent' - end - end - - context "should be callable on hash as" do - it 'slice with explicit parameters, missing are empty' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = {1=>present, 2=>present, 3=>absent} - $a.slice(2) |$a,$b| { - file { "/file_${a[0]}.${b[0]}": ensure => $a[1] } - } - MANIFEST - - catalog.resource(:file, "/file_1.2")['ensure'].should == 'present' - catalog.resource(:file, "/file_3.")['ensure'].should == 'absent' - end - end - - context "should be callable on enumerable types as" do - it 'slice with integer range' do - catalog = compile_to_catalog(<<-MANIFEST) - $a = Integer[1,4] - $a.slice(2) |$a,$b| { - file { "/file_${a}.${b}": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_1.2")['ensure'].should == 'present' - catalog.resource(:file, "/file_3.4")['ensure'].should == 'present' - end - - it 'slice with integer' do - catalog = compile_to_catalog(<<-MANIFEST) - 4.slice(2) |$a,$b| { - file { "/file_${a}.${b}": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_0.1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2.3")['ensure'].should == 'present' - end - - it 'slice with string' do - catalog = compile_to_catalog(<<-MANIFEST) - 'abcd'.slice(2) |$a,$b| { - file { "/file_${a}.${b}": ensure => present } - } - MANIFEST - - catalog.resource(:file, "/file_a.b")['ensure'].should == 'present' - catalog.resource(:file, "/file_c.d")['ensure'].should == 'present' - end - end - - context "when called without a block" do - it "should produce an array with the result" do - catalog = compile_to_catalog(<<-MANIFEST) - $a = [1, present, 2, absent, 3, present] - $a.slice(2).each |$k| { - file { "/file_${$k[0]}": ensure => $k[1] } - } - MANIFEST - - catalog.resource(:file, "/file_1")['ensure'].should == 'present' - catalog.resource(:file, "/file_2")['ensure'].should == 'absent' - catalog.resource(:file, "/file_3")['ensure'].should == 'present' - - end - end -end diff --git a/spec/unit/parser/type_loader_spec.rb b/spec/unit/parser/type_loader_spec.rb index 659ffa942..5454528a7 100755 --- a/spec/unit/parser/type_loader_spec.rb +++ b/spec/unit/parser/type_loader_spec.rb @@ -3,7 +3,6 @@ require 'spec_helper' require 'puppet/parser/type_loader' require 'puppet/parser/parser_factory' -require 'puppet/parser/e_parser_adapter' require 'puppet_spec/modules' require 'puppet_spec/files' |