diff --git a/lib/puppet/parser/functions/getvar.rb b/lib/puppet/parser/functions/getvar.rb deleted file mode 100644 index 4ecd8324e..000000000 --- a/lib/puppet/parser/functions/getvar.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -# -# getvar.rb -# -module Puppet::Parser::Functions - newfunction(:getvar, type: :rvalue, doc: <<-'DOC') do |args| - @summary - Lookup a variable in a given namespace. - - @return - undef - if variable does not exist - - @example Example usage - $foo = getvar('site::data::foo') # Equivalent to $foo = $site::data::foo - - @example Where namespace is stored in a string - $datalocation = 'site::data' - $bar = getvar("${datalocation}::bar") # Equivalent to $bar = $site::data::bar - - > **Note:** from Puppet 6.0.0, the compatible function with the same name in Puppet core - will be used instead of this function. The new function also has support for - digging into a structured value. See the built-in - [`getvar`](https://puppet.com/docs/puppet/latest/function.html#getvar) function - DOC - - unless args.length == 1 - raise Puppet::ParseError, "getvar(): wrong number of arguments (#{args.length}; must be 1)" - end - - begin - result = nil - catch(:undefined_variable) do - result = lookupvar((args[0]).to_s) - end - - # avoid relying on inconsistent behaviour around ruby return values from catch - result - rescue Puppet::ParseError - end - end -end diff --git a/spec/functions/getvar_spec.rb b/spec/functions/getvar_spec.rb deleted file mode 100644 index 94af5187b..000000000 --- a/spec/functions/getvar_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'getvar' do - it { is_expected.not_to eq(nil) } - - 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 { 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 { 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