Skip to content

Commit a383705

Browse files
committed
Merge pull request #464 from DavidS/modules-1882-convert-to-rspec
(MODULES-1882) convert function tests to rspec-puppet
2 parents 1ae9058 + 18d4c21 commit a383705

File tree

129 files changed

+2460
-4538
lines changed

Some content is hidden

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

129 files changed

+2460
-4538
lines changed

.gitignore

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
pkg/
2-
Gemfile.lock
3-
vendor/
4-
spec/fixtures/
5-
.vagrant/
6-
.bundle/
7-
coverage/
8-
.idea/
1+
/pkg/
2+
/Gemfile.lock
3+
/vendor/
4+
/spec/fixtures/manifests/*
5+
/spec/fixtures/modules/*
6+
!/spec/fixtures/modules/stdlib
7+
!/spec/fixtures/modules/stdlib/*
8+
/.vagrant/
9+
/.bundle/
10+
/coverage/
11+
/.idea/
912
*.iml

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
sudo: false
33
language: ruby
4+
cache: bundler
45
bundler_args: --without system_tests
56
script: "bundle exec rake validate && bundle exec rake lint && bundle exec rake spec SPEC_OPTS='--color --format documentation'"
67
matrix:

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ 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
21+
gem 'metadata-json-lint', :require => false
2022
gem 'pry', :require => false
2123
gem 'simplecov', :require => false
2224
end

Rakefile

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
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')
56
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
67

7-
desc "Validate manifests, templates, and ruby files in lib."
8-
task :validate do
9-
Dir['manifests/**/*.pp'].each do |manifest|
10-
sh "puppet parser validate --noop #{manifest}"
11-
end
12-
Dir['lib/**/*.rb'].each do |lib_file|
13-
sh "ruby -c #{lib_file}"
14-
end
15-
Dir['templates/**/*.erb'].each do |template|
16-
sh "erb -P -x -T '-' #{template} | ruby -c"
17-
end
18-
end

lib/puppet/parser/functions/member.rb

Lines changed: 1 addition & 1 deletion
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

Lines changed: 26 additions & 0 deletions
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

Lines changed: 0 additions & 30 deletions
This file was deleted.

spec/fixtures/modules/stdlib/lib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../lib/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../manifests/

spec/functions/abs_spec.rb

Lines changed: 22 additions & 17 deletions
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

Lines changed: 12 additions & 52 deletions
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

Lines changed: 2 additions & 10 deletions
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

Lines changed: 12 additions & 31 deletions
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

Lines changed: 10 additions & 43 deletions
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

0 commit comments

Comments
 (0)