Skip to content

Commit

Permalink
Convert tests to use plain rspec-puppet
Browse files Browse the repository at this point in the history
Tests in the new style produces the following documentation output:

abs
  should not eq nil
  should run abs() and raise an Puppet::ParseError
  should run abs(-34) and return 34
  should run abs("-34") and return 34
  should run abs(34) and return 34
  should run abs("34") and return 34
  • Loading branch information
DavidS committed Jun 1, 2015
1 parent b62dff0 commit f3e79dd
Show file tree
Hide file tree
Showing 115 changed files with 2,432 additions and 4,139 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ group :development, :unit_tests do
gem 'rspec', '~> 3.1.0', :require => false
gem 'rspec-puppet', :require => false
gem 'mocha', :require => false
# keep for its rake task for now
gem 'puppetlabs_spec_helper', :require => false
gem 'puppet-lint', :require => false
gem 'metadata-json-lint', :require => false
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rubygems'
# keep for compatibility for now
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Puppet::Parser::Functions
end

if arguments[1].is_a? String or arguments[1].is_a? Fixnum
item = Array(arguments[1])
item = [arguments[1]]
else
item = arguments[1]
end
Expand Down
26 changes: 26 additions & 0 deletions spec/acceptance/anchor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper_acceptance'

describe 'anchor type', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
describe 'success' do
it 'should effect proper chaining of resources' do
pp = <<-EOS
class anchored {
anchor { 'anchored::begin': }
~> anchor { 'anchored::end': }
}
class anchorrefresh {
notify { 'first': }
~> class { 'anchored': }
~> anchor { 'final': }
}
include anchorrefresh
EOS

apply_manifest(pp, :catch_failures => true) do |r|
expect(r.stdout).to match(/Anchor\[final\]: Triggered 'refresh'/)
end
end
end
end
30 changes: 0 additions & 30 deletions spec/classes/anchor_spec.rb

This file was deleted.

39 changes: 22 additions & 17 deletions spec/functions/abs_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
#! /usr/bin/env ruby -S rspec

require 'spec_helper'

describe "the abs function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
expect(Puppet::Parser::Functions.function("abs")).to eq("function_abs")
end
describe 'abs' do
it { is_expected.not_to eq(nil) }

it "should raise a ParseError if there is less than 1 arguments" do
expect { scope.function_abs([]) }.to( raise_error(Puppet::ParseError))
describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
it {
pending("Current implementation ignores parameters after the first.")
is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
}
end

it "should convert a negative number into a positive" do
result = scope.function_abs(["-34"])
expect(result).to(eq(34))
describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do
it { pending "the puppet 4 implementation"; is_expected.to run.with_params().and_raise_error(ArgumentError) }
it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2).and_raise_error(ArgumentError) }
it { pending "the puppet 4 implementation"; is_expected.to run.with_params([]).and_raise_error(ArgumentError) }
it { pending "the puppet 4 implementation"; is_expected.to run.with_params({}).and_raise_error(ArgumentError) }
it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
end

it "should do nothing with a positive number" do
result = scope.function_abs(["5678"])
expect(result).to(eq(5678))
end
it { is_expected.to run.with_params(-34).and_return(34) }
it { is_expected.to run.with_params("-34").and_return(34) }
it { is_expected.to run.with_params(34).and_return(34) }
it { is_expected.to run.with_params("34").and_return(34) }
it { is_expected.to run.with_params(-34.5).and_return(34.5) }
it { is_expected.to run.with_params("-34.5").and_return(34.5) }
it { is_expected.to run.with_params(34.5).and_return(34.5) }
it { is_expected.to run.with_params("34.5").and_return(34.5) }
end
64 changes: 12 additions & 52 deletions spec/functions/any2array_spec.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe "the any2array function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
expect(Puppet::Parser::Functions.function("any2array")).to eq("function_any2array")
end

it "should return an empty array if there is less than 1 argument" do
result = scope.function_any2array([])
expect(result).to(eq([]))
end

it "should convert boolean true to [ true ] " do
result = scope.function_any2array([true])
expect(result).to(eq([true]))
end

it "should convert one object to [object]" do
result = scope.function_any2array(['one'])
expect(result).to(eq(['one']))
end

it "should convert multiple objects to [objects]" do
result = scope.function_any2array(['one', 'two'])
expect(result).to(eq(['one', 'two']))
end

it "should return empty array it was called with" do
result = scope.function_any2array([[]])
expect(result).to(eq([]))
end

it "should return one-member array it was called with" do
result = scope.function_any2array([['string']])
expect(result).to(eq(['string']))
end

it "should return multi-member array it was called with" do
result = scope.function_any2array([['one', 'two']])
expect(result).to(eq(['one', 'two']))
end

it "should return members of a hash it was called with" do
result = scope.function_any2array([{ 'key' => 'value' }])
expect(result).to(eq(['key', 'value']))
end

it "should return an empty array if it was called with an empty hash" do
result = scope.function_any2array([{ }])
expect(result).to(eq([]))
end
describe "any2array" do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_return([]) }
it { is_expected.to run.with_params(true).and_return([true]) }
it { is_expected.to run.with_params('one').and_return(['one']) }
it { is_expected.to run.with_params('one', 'two').and_return(['one', 'two']) }
it { is_expected.to run.with_params([]).and_return([]) }
it { is_expected.to run.with_params(['one']).and_return(['one']) }
it { is_expected.to run.with_params(['one', 'two']).and_return(['one', 'two']) }
it { is_expected.to run.with_params({}).and_return([]) }
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
end
12 changes: 2 additions & 10 deletions spec/functions/assert_private_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe Puppet::Parser::Functions.function(:assert_private) do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

subject do
function_name = Puppet::Parser::Functions.function(:assert_private)
scope.method(function_name)
end

context "when called from inside module" do
describe 'assert_private' do
context 'when called from inside module' do
it "should not fail" do
scope.expects(:lookupvar).with('module_name').returns('foo')
scope.expects(:lookupvar).with('caller_module_name').returns('foo')
Expand Down
43 changes: 12 additions & 31 deletions spec/functions/base64_spec.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
#! /usr/bin/env ruby -S rspec

require 'spec_helper'

describe "the base64 function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
expect(Puppet::Parser::Functions.function("base64")).to eq("function_base64")
end

it "should raise a ParseError if there are other than 2 arguments" do
expect { scope.function_base64([]) }.to(raise_error(Puppet::ParseError))
expect { scope.function_base64(["asdf"]) }.to(raise_error(Puppet::ParseError))
expect { scope.function_base64(["asdf","moo","cow"]) }.to(raise_error(Puppet::ParseError))
end

it "should raise a ParseError if argument 1 isn't 'encode' or 'decode'" do
expect { scope.function_base64(["bees","astring"]) }.to(raise_error(Puppet::ParseError, /first argument must be one of/))
end

it "should raise a ParseError if argument 2 isn't a string" do
expect { scope.function_base64(["encode",["2"]]) }.to(raise_error(Puppet::ParseError, /second argument must be a string/))
end

it "should encode a encoded string" do
result = scope.function_base64(["encode",'thestring'])
expect(result).to match(/\AdGhlc3RyaW5n\n\Z/)
end
it "should decode a base64 encoded string" do
result = scope.function_base64(["decode",'dGhlc3RyaW5n'])
expect(result).to eq('thestring')
end
describe 'base64' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params("one").and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /first argument must be one of/) }
it { is_expected.to run.with_params("encode", ["two"]).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
it { is_expected.to run.with_params("encode", 2).and_raise_error(Puppet::ParseError, /second argument must be a string/) }

it { is_expected.to run.with_params("encode", "thestring").and_return("dGhlc3RyaW5n\n") }
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n").and_return("thestring") }
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n").and_return("thestring") }
end
53 changes: 10 additions & 43 deletions spec/functions/basename_spec.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe "the basename function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
Puppet::Parser::Functions.function("basename").should == "function_basename"
end

it "should raise a ParseError if there is less than 1 argument" do
lambda { scope.function_basename([]) }.should( raise_error(Puppet::ParseError))
end

it "should raise a ParseError if there are more than 2 arguments" do
lambda { scope.function_basename(['a', 'b', 'c']) }.should( raise_error(Puppet::ParseError))
end

it "should return basename for an absolute path" do
result = scope.function_basename(['/path/to/a/file.ext'])
result.should(eq('file.ext'))
end

it "should return basename for a relative path" do
result = scope.function_basename(['path/to/a/file.ext'])
result.should(eq('file.ext'))
end

it "should strip extention when extension specified (absolute path)" do
result = scope.function_basename(['/path/to/a/file.ext', '.ext'])
result.should(eq('file'))
end

it "should strip extention when extension specified (relative path)" do
result = scope.function_basename(['path/to/a/file.ext', '.ext'])
result.should(eq('file'))
end

it "should complain about non-string first argument" do
lambda { scope.function_basename([[]]) }.should( raise_error(Puppet::ParseError))
end

it "should complain about non-string second argument" do
lambda { scope.function_basename(['/path/to/a/file.ext', []]) }.should( raise_error(Puppet::ParseError))
end
describe 'basename' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('file.ext') }
it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('file.ext') }
it { is_expected.to run.with_params('/path/to/a/file.ext', '.ext').and_return('file') }
it { is_expected.to run.with_params('relative_path/to/a/file.ext', '.ext').and_return('file') }
end
38 changes: 7 additions & 31 deletions spec/functions/bool2num_spec.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe "the bool2num function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
describe 'bool2num' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }

it "should exist" do
expect(Puppet::Parser::Functions.function("bool2num")).to eq("function_bool2num")
[ true, 'true', AlsoString.new('true') ].each do |truthy|
it { is_expected.to run.with_params(truthy).and_return(1) }
end

it "should raise a ParseError if there is less than 1 arguments" do
expect { scope.function_bool2num([]) }.to( raise_error(Puppet::ParseError))
end

it "should convert true to 1" do
result = scope.function_bool2num([true])
expect(result).to(eq(1))
end

it "should convert 'true' to 1" do
result = scope.function_bool2num(['true'])
result.should(eq(1))
end

it "should convert 'false' to 0" do
result = scope.function_bool2num(['false'])
expect(result).to(eq(0))
end

it "should accept objects which extend String" do
class AlsoString < String
end

value = AlsoString.new('true')
result = scope.function_bool2num([value])
result.should(eq(1))
[ false, 'false', AlsoString.new('false') ].each do |falsey|
it { is_expected.to run.with_params(falsey).and_return(0) }
end
end
Loading

0 comments on commit f3e79dd

Please sign in to comment.