Please review pull request #19: Feature/master/add more spec tests opened by (hunner)

Description:

This branch adds spec coverage for hiera_hash() and adds a few fixes for said tests.

  • Opened: Tue Jan 24 22:03:23 UTC 2012
  • Based on: puppetlabs:master (06e70f3768a7cfd0ceaa9135d7e0054f3f0a34fa)
  • Requested merge: hunner:feature/master/add_more_spec_tests (bbde2af8c6166ad4201358772ecf873da7e7a667)

Diff follows:

diff --git a/lib/puppet/parser/functions/hiera_array.rb b/lib/puppet/parser/functions/hiera_array.rb
index c0c4a87..466e010 100644
--- a/lib/puppet/parser/functions/hiera_array.rb
+++ b/lib/puppet/parser/functions/hiera_array.rb
@@ -29,7 +29,7 @@ module Puppet::Parser::Functions
 
         answer = hiera.lookup(key, default, hiera_scope, override, :array)
 
-        raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
+        raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.nil?
 
         answer
     end
diff --git a/lib/puppet/parser/functions/hiera_hash.rb b/lib/puppet/parser/functions/hiera_hash.rb
index 5d49d59..19bce62 100644
--- a/lib/puppet/parser/functions/hiera_hash.rb
+++ b/lib/puppet/parser/functions/hiera_hash.rb
@@ -4,6 +4,9 @@ module Puppet::Parser::Functions
             args = args[0]
         end
 
+        raise(Puppet::ParseError, "Expected a parameter to look up (got none)") if args.empty?
+        raise(Puppet::ParseError, "Expected hash default value (got #{args[1].class})") if args.length > 1 and ! args[1].is_a? Hash
+
         key = args[0]
         default = args[1]
         override = args[2]
@@ -29,7 +32,7 @@ module Puppet::Parser::Functions
 
         answer = hiera.lookup(key, default, hiera_scope, override, :hash)
 
-        raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
+        raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.nil?
 
         answer
     end
diff --git a/lib/puppet/parser/functions/hiera_include.rb b/lib/puppet/parser/functions/hiera_include.rb
index 12e42f6..d141006 100644
--- a/lib/puppet/parser/functions/hiera_include.rb
+++ b/lib/puppet/parser/functions/hiera_include.rb
@@ -29,7 +29,7 @@ module Puppet::Parser::Functions
 
         answer = hiera.lookup(key, default, hiera_scope, override, :array)
 
-        raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
+        raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.nil?
 
         method = Puppet::Parser::Functions.function(:include)
         send(method, answer)
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 60f83a7..fa0b627 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -10,11 +10,3 @@
 RSpec.configure do |config|
     config.mock_with :mocha
 end
-
-class Puppet
-    class Parser
-        class Functions
-        end
-    end
-end
-
diff --git a/spec/unit/parser/functions/hiera_array_spec.rb b/spec/unit/parser/functions/hiera_array_spec.rb
new file mode 100644
index 0000000..e69de29
diff --git a/spec/unit/parser/functions/hiera_hash_spec.rb b/spec/unit/parser/functions/hiera_hash_spec.rb
new file mode 100644
index 0000000..350a798
--- /dev/null
+++ b/spec/unit/parser/functions/hiera_hash_spec.rb
@@ -0,0 +1,42 @@
+require 'puppet'
+require 'hiera'
+require 'spec_helper'
+
+describe 'Puppet::Parser::Functions' do
+  describe "#hiera_hash" do
+    before :each do
+      @scope = Puppet::Parser::Scope.new
+      configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
+      Hiera.any_instance.stubs(:lookup).returns({}) # We don't test what Hiera returns
+      File.stubs(:exist?).with(configfile).returns(true)
+      File.stubs(:exist?).with("/var/lib/hiera/common.yaml").returns(true)
+      YAML.stubs("load_file").returns( {:backends => ["yaml"], :hierarchy => "common", :logger => "console"})
+    end
+    it 'should exist' do
+      Puppet::Parser::Functions.function(:hiera_hash).should == 'function_hiera_hash'
+    end
+    describe 'when accepting arguments it' do
+      before :each do
+        Puppet::Parser::Functions.function(:hiera_hash)
+      end
+      it 'should require a key argument' do
+        expect { @scope.function_hiera_hash([]) }.should raise_error(Puppet::ParseError)
+      end
+      it 'should optionally take a default hash value' do
+        @scope.function_hiera_hash(['key', {'key' => 'default'}])
+      end
+      it 'should accept an empty hash default value' do
+        @scope.function_hiera_hash(['key', {}])
+      end
+      it 'should deny a non-hash default value' do
+        expect { @scope.function_hiera_hash(['key', 'default']) }.should raise_error(Puppet::ParseError)
+      end
+      it 'should optionally take a default value and override level' do
+        @scope.function_hiera_hash(['key', {'key' => 'default'}, 'common'])
+      end
+      it 'should accept an empty override level for empty variables' do
+        @scope.function_hiera_hash(['key', {'key' => 'default'}, ''])
+      end
+    end
+  end
+end
diff --git a/spec/unit/parser/functions/hiera_include_spec.rb b/spec/unit/parser/functions/hiera_include_spec.rb
new file mode 100644
index 0000000..e69de29
diff --git a/spec/unit/parser/functions/hiera_spec.rb b/spec/unit/parser/functions/hiera_spec.rb
new file mode 100644
index 0000000..e69de29
diff --git a/spec/unit/puppet_backend_spec.rb b/spec/unit/puppet_backend_spec.rb
index 61f94cf..0ed7683 100644
--- a/spec/unit/puppet_backend_spec.rb
+++ b/spec/unit/puppet_backend_spec.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../spec_helper'
+require 'spec_helper'
 
 class Hiera
     module Backend
diff --git a/spec/unit/scope_spec.rb b/spec/unit/scope_spec.rb
index 18e0f13..e0883ab 100644
--- a/spec/unit/scope_spec.rb
+++ b/spec/unit/scope_spec.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../spec_helper'
+require 'spec_helper'
 
 class Hiera
     describe Scope do

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to