From d54fa786db9b53e781210e6a13f4ff64d39de41f Mon Sep 17 00:00:00 2001 From: Garrett Honeycutt Date: Tue, 31 Jan 2012 15:36:37 -0800 Subject: [PATCH 1/5] (#12345) hiera enabled function for stdlib adds ability to test if hiera is enabled in your environment --- lib/puppet/feature/hiera.rb | 2 ++ lib/puppet/parser/functions/hiera_enabled.rb | 16 ++++++++++ .../parser/functions/hiera_enabled_spec.rb | 32 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 lib/puppet/feature/hiera.rb create mode 100644 lib/puppet/parser/functions/hiera_enabled.rb create mode 100644 spec/unit/puppet/parser/functions/hiera_enabled_spec.rb diff --git a/lib/puppet/feature/hiera.rb b/lib/puppet/feature/hiera.rb new file mode 100644 index 000000000..7f38685fa --- /dev/null +++ b/lib/puppet/feature/hiera.rb @@ -0,0 +1,2 @@ +require 'puppet/util/feature' +Puppet.features.add(:hiera, :libs => %{hiera}) diff --git a/lib/puppet/parser/functions/hiera_enabled.rb b/lib/puppet/parser/functions/hiera_enabled.rb new file mode 100644 index 000000000..168bf3934 --- /dev/null +++ b/lib/puppet/parser/functions/hiera_enabled.rb @@ -0,0 +1,16 @@ +module Puppet::Parser::Functions + newfunction(:hiera_enabled, :type => :rvalue ) do + # begin + # require 'hiera' + # true if Puppet::Parser::Functions.function 'hiera' + # rescue LoadError => e + # false + # end + #true if Puppet.features.hiera? + if Puppet.features.hiera? and Puppet::Parser::Functions.function(:hiera) + true + else + false + end + end +end diff --git a/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb b/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb new file mode 100644 index 000000000..16119d635 --- /dev/null +++ b/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb @@ -0,0 +1,32 @@ +require 'puppet' +require 'mocha' + +RSpec.configure do |config| + config.mock_with :mocha +end + +describe Puppet::Parser::Functions.function(:hiera_enabled) do + before :each do + @scope = Puppet::Parser::Scope.new + end + it "should fail if hiera is not installed" do + Puppet.features.stubs(:hiera?).returns false + Puppet::Parser::Functions.stubs(:function).with(:hiera).returns false + @scope.function_hiera_enabled([]).should be_false + end + it "should fail if hiera is not installed but the function, hiera(), is available" do + Puppet.features.stubs(:hiera?).returns false + Puppet::Parser::Functions.stubs(:function).with(:hiera).returns true + @scope.function_hiera_enabled([]).should be_false + end + it "should fail if hiera is installed but the function, hiera(), is not available" do + Puppet.features.stubs(:hiera?).returns true + Puppet::Parser::Functions.stubs(:function).with(:hiera).returns false + @scope.function_hiera_enabled([]).should be_false + end + it "should succeed is hiera is installed and the function, hiera(), is available" do + Puppet.features.stubs(:hiera?).returns true + Puppet::Parser::Functions.stubs(:function).with(:hiera).returns true + @scope.function_hiera_enabled([]).should be_true + end +end From a78cdf21b1789122481561012b008d5b701baaa3 Mon Sep 17 00:00:00 2001 From: Garrett Honeycutt Date: Tue, 31 Jan 2012 15:46:33 -0800 Subject: [PATCH 2/5] (#12345) hiera enabled function for stdlib removing extraneous code that was commented out --- lib/puppet/parser/functions/hiera_enabled.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/puppet/parser/functions/hiera_enabled.rb b/lib/puppet/parser/functions/hiera_enabled.rb index 168bf3934..99efe6b68 100644 --- a/lib/puppet/parser/functions/hiera_enabled.rb +++ b/lib/puppet/parser/functions/hiera_enabled.rb @@ -1,12 +1,5 @@ module Puppet::Parser::Functions newfunction(:hiera_enabled, :type => :rvalue ) do - # begin - # require 'hiera' - # true if Puppet::Parser::Functions.function 'hiera' - # rescue LoadError => e - # false - # end - #true if Puppet.features.hiera? if Puppet.features.hiera? and Puppet::Parser::Functions.function(:hiera) true else From 15c7137f7ba8932f85794e362fdab2911b1f7de2 Mon Sep 17 00:00:00 2001 From: Garrett Honeycutt Date: Tue, 31 Jan 2012 17:21:04 -0800 Subject: [PATCH 3/5] (#12345) fix spelling typo in hiera_enabled spec test --- spec/unit/puppet/parser/functions/hiera_enabled_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb b/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb index 16119d635..a6f23a934 100644 --- a/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb +++ b/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb @@ -24,7 +24,7 @@ Puppet::Parser::Functions.stubs(:function).with(:hiera).returns false @scope.function_hiera_enabled([]).should be_false end - it "should succeed is hiera is installed and the function, hiera(), is available" do + it "should succeed if hiera is installed and the function, hiera(), is available" do Puppet.features.stubs(:hiera?).returns true Puppet::Parser::Functions.stubs(:function).with(:hiera).returns true @scope.function_hiera_enabled([]).should be_true From 773cb47b3ab9f07302c1d9f057e3f7937cae17a6 Mon Sep 17 00:00:00 2001 From: Garrett Honeycutt Date: Tue, 31 Jan 2012 17:27:01 -0800 Subject: [PATCH 4/5] (#12345) added inline doc to hiera_enabled function --- lib/puppet/parser/functions/hiera_enabled.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/hiera_enabled.rb b/lib/puppet/parser/functions/hiera_enabled.rb index 99efe6b68..411f8299b 100644 --- a/lib/puppet/parser/functions/hiera_enabled.rb +++ b/lib/puppet/parser/functions/hiera_enabled.rb @@ -1,5 +1,8 @@ module Puppet::Parser::Functions - newfunction(:hiera_enabled, :type => :rvalue ) do + newfunction(:hiera_enabled, :type => :rvalue, :doc => <<-EOS + Returns true or false based on hiera being enabled. + EOS + ) do if Puppet.features.hiera? and Puppet::Parser::Functions.function(:hiera) true else From 85ea966d6eca8fe755dfd09b9ec1b465b0825453 Mon Sep 17 00:00:00 2001 From: Garrett Honeycutt Date: Tue, 31 Jan 2012 17:40:18 -0800 Subject: [PATCH 5/5] (#12345) using spec_helper --- spec/spec_helper.rb | 4 ++++ spec/unit/puppet/parser/functions/hiera_enabled_spec.rb | 7 +------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 87aac3407..ec7f75aba 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,6 +27,10 @@ module PuppetSpec require 'monkey_patches/alias_should_to_must' require 'monkey_patches/publicize_methods' +RSpec.configure do |config| + config.mock_with :mocha +end + # JJM Hack to make the stdlib tests run in Puppet 2.6 (See puppet commit cf183534) if not Puppet.constants.include? "Test" then module Puppet::Test diff --git a/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb b/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb index a6f23a934..958a39924 100644 --- a/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb +++ b/spec/unit/puppet/parser/functions/hiera_enabled_spec.rb @@ -1,9 +1,4 @@ -require 'puppet' -require 'mocha' - -RSpec.configure do |config| - config.mock_with :mocha -end +require 'spec_helper' describe Puppet::Parser::Functions.function(:hiera_enabled) do before :each do