Skip to content

Commit f3e79dd

Browse files
committed
Convert tests to use plain rspec-puppet
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
1 parent b62dff0 commit f3e79dd

File tree

115 files changed

+2432
-4139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+2432
-4139
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ group :development, :unit_tests do
1515
gem 'rspec', '~> 3.1.0', :require => false
1616
gem 'rspec-puppet', :require => false
1717
gem 'mocha', :require => false
18+
# keep for its rake task for now
1819
gem 'puppetlabs_spec_helper', :require => false
1920
gem 'puppet-lint', :require => false
2021
gem 'metadata-json-lint', :require => false

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rubygems'
2+
# keep for compatibility for now
23
require 'puppetlabs_spec_helper/rake_tasks'
34
require 'puppet-lint/tasks/puppet-lint'
45
PuppetLint.configuration.send('disable_80chars')

lib/puppet/parser/functions/member.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module Puppet::Parser::Functions
4444
end
4545

4646
if arguments[1].is_a? String or arguments[1].is_a? Fixnum
47-
item = Array(arguments[1])
47+
item = [arguments[1]]
4848
else
4949
item = arguments[1]
5050
end

spec/acceptance/anchor_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'anchor type', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should effect proper chaining of resources' do
6+
pp = <<-EOS
7+
class anchored {
8+
anchor { 'anchored::begin': }
9+
~> anchor { 'anchored::end': }
10+
}
11+
12+
class anchorrefresh {
13+
notify { 'first': }
14+
~> class { 'anchored': }
15+
~> anchor { 'final': }
16+
}
17+
18+
include anchorrefresh
19+
EOS
20+
21+
apply_manifest(pp, :catch_failures => true) do |r|
22+
expect(r.stdout).to match(/Anchor\[final\]: Triggered 'refresh'/)
23+
end
24+
end
25+
end
26+
end

spec/classes/anchor_spec.rb

-30
This file was deleted.

spec/functions/abs_spec.rb

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
#! /usr/bin/env ruby -S rspec
2-
31
require 'spec_helper'
42

5-
describe "the abs function" do
6-
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7-
8-
it "should exist" do
9-
expect(Puppet::Parser::Functions.function("abs")).to eq("function_abs")
10-
end
3+
describe 'abs' do
4+
it { is_expected.not_to eq(nil) }
115

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

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

21-
it "should do nothing with a positive number" do
22-
result = scope.function_abs(["5678"])
23-
expect(result).to(eq(5678))
24-
end
22+
it { is_expected.to run.with_params(-34).and_return(34) }
23+
it { is_expected.to run.with_params("-34").and_return(34) }
24+
it { is_expected.to run.with_params(34).and_return(34) }
25+
it { is_expected.to run.with_params("34").and_return(34) }
26+
it { is_expected.to run.with_params(-34.5).and_return(34.5) }
27+
it { is_expected.to run.with_params("-34.5").and_return(34.5) }
28+
it { is_expected.to run.with_params(34.5).and_return(34.5) }
29+
it { is_expected.to run.with_params("34.5").and_return(34.5) }
2530
end

spec/functions/any2array_spec.rb

+12-52
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
1-
#! /usr/bin/env ruby -S rspec
21
require 'spec_helper'
32

4-
describe "the any2array function" do
5-
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6-
7-
it "should exist" do
8-
expect(Puppet::Parser::Functions.function("any2array")).to eq("function_any2array")
9-
end
10-
11-
it "should return an empty array if there is less than 1 argument" do
12-
result = scope.function_any2array([])
13-
expect(result).to(eq([]))
14-
end
15-
16-
it "should convert boolean true to [ true ] " do
17-
result = scope.function_any2array([true])
18-
expect(result).to(eq([true]))
19-
end
20-
21-
it "should convert one object to [object]" do
22-
result = scope.function_any2array(['one'])
23-
expect(result).to(eq(['one']))
24-
end
25-
26-
it "should convert multiple objects to [objects]" do
27-
result = scope.function_any2array(['one', 'two'])
28-
expect(result).to(eq(['one', 'two']))
29-
end
30-
31-
it "should return empty array it was called with" do
32-
result = scope.function_any2array([[]])
33-
expect(result).to(eq([]))
34-
end
35-
36-
it "should return one-member array it was called with" do
37-
result = scope.function_any2array([['string']])
38-
expect(result).to(eq(['string']))
39-
end
40-
41-
it "should return multi-member array it was called with" do
42-
result = scope.function_any2array([['one', 'two']])
43-
expect(result).to(eq(['one', 'two']))
44-
end
45-
46-
it "should return members of a hash it was called with" do
47-
result = scope.function_any2array([{ 'key' => 'value' }])
48-
expect(result).to(eq(['key', 'value']))
49-
end
50-
51-
it "should return an empty array if it was called with an empty hash" do
52-
result = scope.function_any2array([{ }])
53-
expect(result).to(eq([]))
54-
end
3+
describe "any2array" do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_return([]) }
6+
it { is_expected.to run.with_params(true).and_return([true]) }
7+
it { is_expected.to run.with_params('one').and_return(['one']) }
8+
it { is_expected.to run.with_params('one', 'two').and_return(['one', 'two']) }
9+
it { is_expected.to run.with_params([]).and_return([]) }
10+
it { is_expected.to run.with_params(['one']).and_return(['one']) }
11+
it { is_expected.to run.with_params(['one', 'two']).and_return(['one', 'two']) }
12+
it { is_expected.to run.with_params({}).and_return([]) }
13+
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
14+
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
5515
end

spec/functions/assert_private_spec.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
#! /usr/bin/env ruby -S rspec
21
require 'spec_helper'
32

4-
describe Puppet::Parser::Functions.function(:assert_private) do
5-
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6-
7-
subject do
8-
function_name = Puppet::Parser::Functions.function(:assert_private)
9-
scope.method(function_name)
10-
end
11-
12-
context "when called from inside module" do
3+
describe 'assert_private' do
4+
context 'when called from inside module' do
135
it "should not fail" do
146
scope.expects(:lookupvar).with('module_name').returns('foo')
157
scope.expects(:lookupvar).with('caller_module_name').returns('foo')

spec/functions/base64_spec.rb

+12-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
#! /usr/bin/env ruby -S rspec
2-
31
require 'spec_helper'
42

5-
describe "the base64 function" do
6-
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7-
8-
it "should exist" do
9-
expect(Puppet::Parser::Functions.function("base64")).to eq("function_base64")
10-
end
11-
12-
it "should raise a ParseError if there are other than 2 arguments" do
13-
expect { scope.function_base64([]) }.to(raise_error(Puppet::ParseError))
14-
expect { scope.function_base64(["asdf"]) }.to(raise_error(Puppet::ParseError))
15-
expect { scope.function_base64(["asdf","moo","cow"]) }.to(raise_error(Puppet::ParseError))
16-
end
17-
18-
it "should raise a ParseError if argument 1 isn't 'encode' or 'decode'" do
19-
expect { scope.function_base64(["bees","astring"]) }.to(raise_error(Puppet::ParseError, /first argument must be one of/))
20-
end
21-
22-
it "should raise a ParseError if argument 2 isn't a string" do
23-
expect { scope.function_base64(["encode",["2"]]) }.to(raise_error(Puppet::ParseError, /second argument must be a string/))
24-
end
25-
26-
it "should encode a encoded string" do
27-
result = scope.function_base64(["encode",'thestring'])
28-
expect(result).to match(/\AdGhlc3RyaW5n\n\Z/)
29-
end
30-
it "should decode a base64 encoded string" do
31-
result = scope.function_base64(["decode",'dGhlc3RyaW5n'])
32-
expect(result).to eq('thestring')
33-
end
3+
describe 'base64' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
6+
it { is_expected.to run.with_params("one").and_raise_error(Puppet::ParseError) }
7+
it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError) }
8+
it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /first argument must be one of/) }
9+
it { is_expected.to run.with_params("encode", ["two"]).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
10+
it { is_expected.to run.with_params("encode", 2).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
11+
12+
it { is_expected.to run.with_params("encode", "thestring").and_return("dGhlc3RyaW5n\n") }
13+
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n").and_return("thestring") }
14+
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n").and_return("thestring") }
3415
end

spec/functions/basename_spec.rb

+10-43
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,13 @@
1-
#! /usr/bin/env ruby -S rspec
21
require 'spec_helper'
32

4-
describe "the basename function" do
5-
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6-
7-
it "should exist" do
8-
Puppet::Parser::Functions.function("basename").should == "function_basename"
9-
end
10-
11-
it "should raise a ParseError if there is less than 1 argument" do
12-
lambda { scope.function_basename([]) }.should( raise_error(Puppet::ParseError))
13-
end
14-
15-
it "should raise a ParseError if there are more than 2 arguments" do
16-
lambda { scope.function_basename(['a', 'b', 'c']) }.should( raise_error(Puppet::ParseError))
17-
end
18-
19-
it "should return basename for an absolute path" do
20-
result = scope.function_basename(['/path/to/a/file.ext'])
21-
result.should(eq('file.ext'))
22-
end
23-
24-
it "should return basename for a relative path" do
25-
result = scope.function_basename(['path/to/a/file.ext'])
26-
result.should(eq('file.ext'))
27-
end
28-
29-
it "should strip extention when extension specified (absolute path)" do
30-
result = scope.function_basename(['/path/to/a/file.ext', '.ext'])
31-
result.should(eq('file'))
32-
end
33-
34-
it "should strip extention when extension specified (relative path)" do
35-
result = scope.function_basename(['path/to/a/file.ext', '.ext'])
36-
result.should(eq('file'))
37-
end
38-
39-
it "should complain about non-string first argument" do
40-
lambda { scope.function_basename([[]]) }.should( raise_error(Puppet::ParseError))
41-
end
42-
43-
it "should complain about non-string second argument" do
44-
lambda { scope.function_basename(['/path/to/a/file.ext', []]) }.should( raise_error(Puppet::ParseError))
45-
end
3+
describe 'basename' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
6+
it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
7+
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
8+
it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) }
9+
it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('file.ext') }
10+
it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('file.ext') }
11+
it { is_expected.to run.with_params('/path/to/a/file.ext', '.ext').and_return('file') }
12+
it { is_expected.to run.with_params('relative_path/to/a/file.ext', '.ext').and_return('file') }
4613
end

spec/functions/bool2num_spec.rb

+7-31
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
1-
#! /usr/bin/env ruby -S rspec
21
require 'spec_helper'
32

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

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

11-
it "should raise a ParseError if there is less than 1 arguments" do
12-
expect { scope.function_bool2num([]) }.to( raise_error(Puppet::ParseError))
13-
end
14-
15-
it "should convert true to 1" do
16-
result = scope.function_bool2num([true])
17-
expect(result).to(eq(1))
18-
end
19-
20-
it "should convert 'true' to 1" do
21-
result = scope.function_bool2num(['true'])
22-
result.should(eq(1))
23-
end
24-
25-
it "should convert 'false' to 0" do
26-
result = scope.function_bool2num(['false'])
27-
expect(result).to(eq(0))
28-
end
29-
30-
it "should accept objects which extend String" do
31-
class AlsoString < String
32-
end
33-
34-
value = AlsoString.new('true')
35-
result = scope.function_bool2num([value])
36-
result.should(eq(1))
11+
[ false, 'false', AlsoString.new('false') ].each do |falsey|
12+
it { is_expected.to run.with_params(falsey).and_return(0) }
3713
end
3814
end

0 commit comments

Comments
 (0)