Permalink
Browse files

Add test coverage

  • Loading branch information...
1 parent 0544fcc commit 2c7da2d2df999b2a93a089a2cbccdbbf5cb50615 @ripienaar ripienaar committed Jun 4, 2011
Showing with 95 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 Rakefile
  3. +3 −3 lib/hiera/backend/json_backend.rb
  4. +83 −0 spec/json_backend_spec.rb
  5. +1 −0 spec/spec.opts
View
@@ -0,0 +1 @@
+pkg
View
@@ -1,5 +1,6 @@
require 'rubygems'
require 'rake/gempackagetask'
+require 'rspec/core/rake_task'
spec = Gem::Specification.new do |s|
s.name = "hiera-json"
@@ -22,3 +23,9 @@ end
Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_tar = true
end
+
+desc "Run all specs"
+RSpec::Core::RakeTask.new(:test) do |t|
+ t.pattern = 'spec/**/*_spec.rb'
+ t.rspec_opts = File.read("spec/spec.opts").chomp || ""
+end
@@ -7,10 +7,10 @@ def initialize
Hiera.warn("JSON Starting")
end
- def lookup(key, default, scope, order_override=nil)
+ def lookup(key, scope, order_override=nil)
answer = nil
- Hiera.warn("Looking up #{key} with default #{default} in JSON backup")
+ Hiera.warn("Looking up #{key} in JSON backup")
datadir = Backend.datadir(:json, scope)
@@ -42,7 +42,7 @@ def lookup(key, default, scope, order_override=nil)
end
end
- answer || default or raise(NoDataFound, "No match found for '#{key}' in any data file during hiera lookup")
+ answer
end
end
end
@@ -0,0 +1,83 @@
+$:.insert(0, File.join([File.dirname(__FILE__), "..", "lib"]))
+
+require 'rubygems'
+require 'rspec'
+require 'hiera/backend/json_backend'
+require 'rspec/mocks'
+require 'mocha'
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
+
+class Hiera
+ module Backend
+ describe Json_backend do
+ before do
+ Hiera.stubs(:warn)
+ Backend.stubs(:parse_string)
+ Backend.stubs(:datasources).yields([])
+
+ Backend.expects(:datadir).with(:json, {}).returns("/nonexisting")
+ @backend = Json_backend.new
+ end
+
+ it "should use the configured datadir" do
+ File.expects(:directory?).with("/nonexisting").returns(true)
+
+ @backend.lookup("test", {})
+ end
+
+ it "should fail for missing data directories" do
+ File.expects(:directory?).with("/nonexisting").returns(false)
+
+ expect {
+ @backend.lookup("test", {})
+ }.to raise_error("Cannot find data directory /nonexisting")
+ end
+
+ it "should look for data in all data sources" do
+ File.expects(:directory?).with("/nonexisting").returns(true)
+ Backend.expects(:datasources).with({}, nil).multiple_yields(["one"], ["two"])
+ File.expects(:exist?).with("/nonexisting/one.json")
+ File.expects(:exist?).with("/nonexisting/two.json")
+ @backend.lookup("test", {})
+ end
+
+ it "should warn about missing data files and continue" do
+ File.expects(:directory?).with("/nonexisting").returns(true)
+ Backend.expects(:datasources).with({}, nil).multiple_yields(["one"], ["two"])
+ File.expects(:exist?).with("/nonexisting/one.json").returns(false)
+ File.expects(:exist?).with("/nonexisting/two.json").returns(false)
+
+ Hiera.expects(:warn).with("Cannot find datafile /nonexisting/one.json, skipping")
+ Hiera.expects(:warn).with("Cannot find datafile /nonexisting/two.json, skipping")
+
+ @backend.lookup("test", {})
+ end
+
+ it "should parse string data for interprolation" do
+ File.expects(:directory?).with("/nonexisting").returns(true)
+ Backend.expects(:datasources).with({}, nil).yields("one")
+ File.expects(:exist?).with("/nonexisting/one.json").returns(true)
+ File.expects(:read).with("/nonexisting/one.json").returns('{"test":"data"}')
+ Backend.expects(:parse_string).with("data", {})
+
+ @backend.lookup("test", {})
+ end
+
+ it "should return the first answer found" do
+ File.expects(:directory?).with("/nonexisting").returns(true)
+ Backend.expects(:datasources).with({}, nil).multiple_yields(["one"], ["two"])
+ File.expects(:exist?).with("/nonexisting/one.json").returns(true)
+ File.expects(:read).with("/nonexisting/one.json").returns('{"test":"data1"}')
+ Backend.expects(:parse_string).with("data1", {}).returns("data1")
+
+ File.expects(:exist?).with("/nonexisting/two.json").never
+ File.expects(:read).with("/nonexisting/two.json").never
+
+ @backend.lookup("test", {}).should == "data1"
+ end
+ end
+ end
+end
View
@@ -0,0 +1 @@
+--format s --colour --backtrace

0 comments on commit 2c7da2d

Please sign in to comment.