Skip to content

Commit

Permalink
Merge pull request #464 from DavidS/modules-1882-convert-to-rspec
Browse files Browse the repository at this point in the history
(MODULES-1882) convert function tests to rspec-puppet
  • Loading branch information
hunner committed Jun 1, 2015
2 parents 1ae9058 + 18d4c21 commit a383705
Show file tree
Hide file tree
Showing 129 changed files with 2,460 additions and 4,538 deletions.
19 changes: 11 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pkg/
Gemfile.lock
vendor/
spec/fixtures/
.vagrant/
.bundle/
coverage/
.idea/
/pkg/
/Gemfile.lock
/vendor/
/spec/fixtures/manifests/*
/spec/fixtures/modules/*
!/spec/fixtures/modules/stdlib
!/spec/fixtures/modules/stdlib/*
/.vagrant/
/.bundle/
/coverage/
/.idea/
*.iml
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
sudo: false
language: ruby
cache: bundler
bundler_args: --without system_tests
script: "bundle exec rake validate && bundle exec rake lint && bundle exec rake spec SPEC_OPTS='--color --format documentation'"
matrix:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ 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
gem 'pry', :require => false
gem 'simplecov', :require => false
end
Expand Down
13 changes: 1 addition & 12 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
require 'rubygems'
# keep for compatibility for now
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]

desc "Validate manifests, templates, and ruby files in lib."
task :validate do
Dir['manifests/**/*.pp'].each do |manifest|
sh "puppet parser validate --noop #{manifest}"
end
Dir['lib/**/*.rb'].each do |lib_file|
sh "ruby -c #{lib_file}"
end
Dir['templates/**/*.erb'].each do |template|
sh "erb -P -x -T '-' #{template} | ruby -c"
end
end
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.

1 change: 1 addition & 0 deletions spec/fixtures/modules/stdlib/lib
1 change: 1 addition & 0 deletions spec/fixtures/modules/stdlib/manifests
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
Loading

0 comments on commit a383705

Please sign in to comment.