-#! /usr/bin/env ruby -S rspec
require 'spec_helper'
-describe Puppet::Parser::Functions.function(:getvar) do
- let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- describe 'when calling getvar from puppet' do
+describe 'getvar' do
+ it { is_expected.not_to eq(nil) }
- it "should not compile when no arguments are passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = '$foo = getvar()'
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
- end
+ describe 'before Puppet 6.0.0', :if => Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') < 0 do
+ it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
+ it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
+ end
+
+ describe 'from Puppet 6.0.0', :if => Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') >= 0 do
+ it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects between 1 and 2 arguments, got none}i) }
+ it { is_expected.to run.with_params('one', 'two').and_return('two') }
+ it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(ArgumentError, %r{expects between 1 and 2 arguments, got 3}i) }
+ end
- it "should not compile when too many arguments are passed" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = '$foo = getvar("foo::bar", "baz")'
- expect {
- scope.compiler.compile
- }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+ it { is_expected.to run.with_params('::foo').and_return(nil) }
+
+ context 'with given variables in namespaces' do
+ let(:pre_condition) do
+ <<-PUPPETCODE
+ class site::data { $foo = 'baz' }
+ include site::data
+ PUPPETCODE
end
- it "should lookup variables in other namespaces" do
- skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
- Puppet[:code] = <<-'ENDofPUPPETcode'
- class site::data { $foo = 'baz' }
- include site::data
- $foo = getvar("site::data::foo")
- if $foo != 'baz' {
- fail('getvar did not return what we expect')
- }
- ENDofPUPPETcode
- scope.compiler.compile
+ it { is_expected.to run.with_params('site::data::foo').and_return('baz') }
+ it { is_expected.to run.with_params('::site::data::foo').and_return('baz') }
+ it { is_expected.to run.with_params('::site::data::bar').and_return(nil) }
+ end
+
+ context 'with given variables in namespaces' do
+ let(:pre_condition) do
+ <<-PUPPETCODE
+ class site::info { $lock = 'ŧҺîš íš ắ śţřĭŋĝ' }
+ class site::new { $item = '万Ü€‰' }
+ include site::info
+ include site::new
+ PUPPETCODE
end
+
+ it { is_expected.to run.with_params('site::info::lock').and_return('ŧҺîš íš ắ śţřĭŋĝ') }
+ it { is_expected.to run.with_params('::site::new::item').and_return('万Ü€‰') }
end
end