Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rspec 3 and 2 compatibility #225

Merged
merged 21 commits into from
Oct 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
359d361
lower constraints on rspec version
Sep 24, 2014
e97e03e
lower constraints on rspec version
Sep 24, 2014
0a9966d
Merge branch 'rspec3_test' of github.com:tuxmea/rspec-puppet into rsp…
Sep 24, 2014
34b9cab
Merge branch 'rspec3_test' of github.com:tuxmea/rspec-puppet into rsp…
Sep 24, 2014
4549b3a
Merge branch 'rspec3_test' of github.com:tuxmea/rspec-puppet into rsp…
Sep 24, 2014
4a6d93d
lower constraints in gemspec file too
Sep 24, 2014
a8e99d8
readd rspec dependency without version
Sep 24, 2014
7d337da
Fix deprecation warning on :example_group
Sep 24, 2014
5b3d103
fix failure_message deprecation warnings
Sep 24, 2014
30897a8
limit RSpec 2 compatibility to Version 2 only
Sep 24, 2014
77d4ba8
limit RSpec 2 compatibility to Version 2 only
Sep 24, 2014
9242fdb
Merge branch 'rspec3_test' of github.com:tuxmea/rspec-puppet into rsp…
Sep 24, 2014
364cce0
Merge branch 'rspec3_test' of github.com:tuxmea/rspec-puppet into rsp…
Sep 24, 2014
799933d
Merge branch 'rspec3_test' of github.com:tuxmea/rspec-puppet into rsp…
Sep 24, 2014
3537308
example_groups - enable rspec 2 and 3
Sep 24, 2014
3beae4d
Keep env rspec_version in place to allow rspec version switch
Sep 24, 2014
dd8273d
allow usage of older rspec versions also for deprecated matchers
Sep 24, 2014
fe0b344
code deduplication
Sep 25, 2014
091d06f
travis improvements
Sep 27, 2014
4ef6377
travis: use latest puppet 3.4 version (3.4.3 instead of 3.4.1)
Sep 27, 2014
65735fa
remove rspec deprecation warnings from spec tests
Sep 27, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ rvm:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.2
notifications:
email:
- [email protected]
Expand All @@ -13,7 +14,9 @@ env:
- PUPPET_VERSION=3.1.1
- PUPPET_VERSION=3.2.4
- PUPPET_VERSION=3.3.2
- PUPPET_VERSION=3.4.1
- PUPPET_VERSION=3.4.3
- PUPPET_VERSION=3.5.1
- RSPEC_VERSION=2.14
matrix:
exclude:
- rvm: 1.9.2
Expand All @@ -28,3 +31,15 @@ matrix:
env: PUPPET_VERSION=3.0.2
- rvm: 2.0.0
env: PUPPET_VERSION=3.1.1
- rvm: 2.1.2
env: PUPPET_VERSION=2.6.18
- rvm: 2.1.2
env: PUPPET_VERSION=2.7.23
- rvm: 2.1.2
env: PUPPET_VERSION=3.0.2
- rvm: 2.1.2
env: PUPPET_VERSION=3.1.1
- rvm: 2.1.2
env: PUPPET_VERSION=3.2.4
- rvm: 2.1.2
env: PUPPET_VERSION=3.3.2
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 2.7']
rspecversion = ENV.key?('RSPEC_VERSION') ? "= #{ENV['RSPEC_VERSION']}" : ['>= 2.9 ', '< 3.0.0']
rspecversion = ENV.key?('RSPEC_VERSION') ? "= #{ENV['RSPEC_VERSION']}" : ['>= 2.9']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than removing this option entirely, we should just remove the < 3.0.0 requirement so that travis-ci can be configured to run the tests against 2.x and 3.x and ensure we don't break 2.x compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


gem 'rake'
gem 'rspec', rspecversion
Expand Down
34 changes: 19 additions & 15 deletions lib/rspec-puppet/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ def c.escaped_path(*parts)
Regexp.compile(parts.join('[\\\/]'))
end

c.include RSpec::Puppet::DefineExampleGroup, :type => :define, :example_group => {
:file_path => c.escaped_path(%w[spec defines])
}

c.include RSpec::Puppet::ClassExampleGroup, :type => :class, :example_group => {
:file_path => c.escaped_path(%w[spec classes])
}

c.include RSpec::Puppet::FunctionExampleGroup, :type => :puppet_function, :example_group => {
:file_path => c.escaped_path(%w[spec functions])
}

c.include RSpec::Puppet::HostExampleGroup, :type => :host, :example_group => {
:file_path => c.escaped_path(%w[spec hosts])
}
if RSpec::Version::STRING < '3'
c.include RSpec::Puppet::DefineExampleGroup, :type => :define, :example_group => {
:file_path => c.escaped_path(%w[spec defines])
}
c.include RSpec::Puppet::ClassExampleGroup, :type => :class, :example_group => {
:file_path => c.escaped_path(%w[spec classes])
}
c.include RSpec::Puppet::FunctionExampleGroup, :type => :puppet_function, :example_group => {
:file_path => c.escaped_path(%w[spec functions])
}
c.include RSpec::Puppet::HostExampleGroup, :type => :host, :example_group => {
:file_path => c.escaped_path(%w[spec hosts])
}
else
c.include RSpec::Puppet::DefineExampleGroup, :type => :define, :file_path => c.escaped_path(%w[spec defines])
c.include RSpec::Puppet::ClassExampleGroup, :type => :class, :file_path => c.escaped_path(%w[spec classes])
c.include RSpec::Puppet::FunctionExampleGroup, :type => :puppet_function, :file_path => c.escaped_path(%w[spec functions])
c.include RSpec::Puppet::HostExampleGroup, :type => :host, :file_path => c.escaped_path(%w[spec hosts])
end
end
4 changes: 2 additions & 2 deletions lib/rspec-puppet/matchers/compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def description
end
end

def failure_message_for_should
def failure_message
unless @cycles.empty?
"dependency cycles found: #{@cycles.join('; ')}"
else
Expand All @@ -63,7 +63,7 @@ def failure_message_for_should
end
end

def failure_message_for_should_not
def failure_message_when_negated
"expected that the catalogue would not compile but it does"
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rspec-puppet/matchers/count_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ def description
desc.join(" ")
end

def failure_message_for_should
def failure_message
"expected that the catalogue would " + description + " but it contains #{@actual_number}"
end

def failure_message_for_should_not
def failure_message_when_negated
"expected that the catalogue would not " + description + " but it does"
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rspec-puppet/matchers/create_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ def matches?(catalogue)
end
end

def failure_message_for_should
def failure_message
"expected that the catalogue would contain #{@referenced_type}[#{@title}]#{errors}"
end

def failure_message_for_should_not
def failure_message_when_negated
"expected that the catalogue would not contain #{@referenced_type}[#{@title}]#{errors}"
end

Expand Down
12 changes: 10 additions & 2 deletions lib/rspec-puppet/matchers/include_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ module ManifestMatchers
"include Class[#{expected_class}]"
end

failure_message_for_should do |actual|
"expected that the catalogue would include Class[#{expected_class}]"
if RSpec::Version::STRING < '3'
failure_message_for_should do |actual|
"expected that the catalogue would include Class[#{expected_class}]"
end
else
failure_message do |actual|
"expected that the catalogue would include Class[#{expected_class}]"
end
end

end

end
end
4 changes: 2 additions & 2 deletions lib/rspec-puppet/matchers/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def and_raise_error(error_or_message, message=nil)
self
end

def failure_message_for_should
def failure_message
failure_message_generic(:should, @func_obj)
end

def failure_message_for_should_not
def failure_message_when_negated
failure_message_generic(:should_not, @func_obj)
end

Expand Down
8 changes: 8 additions & 0 deletions lib/rspec-puppet/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,13 @@ def build_node(name, opts = {})
opts.merge!({:environment => node_environment})
Puppet::Node.new(name, opts)
end

def rspec_compatibility
if RSpec::Version::STRING < '3'
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
end
end
end
end
2 changes: 1 addition & 1 deletion rspec-puppet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |s|

s.files = Dir['LICENSE.md', 'README.md', 'lib/**/*', 'bin/**/*']

s.add_dependency 'rspec', '< 3.0.0'
s.add_dependency 'rspec'

s.authors = ['Tim Sharpe']
s.email = '[email protected]'
Expand Down
18 changes: 9 additions & 9 deletions spec/classes/sysctl_common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@
describe 'when using with to specify a hash of parameters' do
it 'should fail if the parameter is not contained in the resource' do
expect do
subject.should contain_exec('sysctl/reload').with('foo' => 'bar')
expect(subject).to contain_exec('sysctl/reload').with('foo' => 'bar')
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
it 'should pass if the parameters are contained in the resource' do
subject.should contain_exec('sysctl/reload').with(
expect(subject).to contain_exec('sysctl/reload').with(
'refreshonly' => 'true',
'returns' => [0, 2]
)
end
end
describe 'when using without to specify parameter name(s)' do
it 'should pass if the parameter name is not contained in the resource' do
subject.should contain_exec('sysctl/reload').without('foo')
expect(subject).to contain_exec('sysctl/reload').without('foo')
end
it 'should pass if the parameter names are not contained in the resource' do
subject.should contain_exec('sysctl/reload').without(['foo', 'bar'])
expect(subject).to contain_exec('sysctl/reload').without(['foo', 'bar'])
end
it 'should fail if any of the parameter names are contained in the resource' do
expect do
subject.should contain_exec('sysctl/reload').without(['foo', 'returns'])
expect(subject).to contain_exec('sysctl/reload').without(['foo', 'returns'])
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
end
describe 'when using without to specify parameter value(s)' do
it 'should pass if the parameter value is not contained in the resource' do
subject.should contain_exec('sysctl/reload').without_refreshonly('false')
expect(subject).to contain_exec('sysctl/reload').without_refreshonly('false')
end
it 'should fail if the parameter value is contained in the resource' do
expect do
subject.should contain_exec('sysctl/reload').without_refreshonly('true')
expect(subject).to contain_exec('sysctl/reload').without_refreshonly('true')
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
end
Expand Down Expand Up @@ -65,15 +65,15 @@
}
it 'should fail if not enough parameters are contained in the resource' do
expect do
subject.should contain_exec('sysctl/reload').only_with(
expect(subject).to contain_exec('sysctl/reload').only_with(
:command => '/sbin/sysctl -p /etc/sysctl.conf',
:returns => [0, 2]
)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
it 'should fail if different parameters are contained in the resource' do
expect do
subject.should contain_exec('sysctl/reload').only_with(
expect(subject).to contain_exec('sysctl/reload').only_with(
:command => '/sbin/sysctl -p /etc/sysctl.conf',
:refreshonly => true,
:creates => '/tmp/bla'
Expand Down
4 changes: 2 additions & 2 deletions spec/classes/test_classes_used_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe 'test::classes_used' do
it {
RSpec.should receive(:deprecate).with(:include_class, :contain_class)
expect(RSpec).to receive(:deprecate).with(:include_class, :contain_class)
should include_class('test::bare_class')
}
it {
RSpec.should receive(:deprecate).with(:include_class, :contain_class)
expect(RSpec).to receive(:deprecate).with(:include_class, :contain_class)
should include_class('test::parameterised_class')
}

Expand Down
6 changes: 3 additions & 3 deletions spec/functions/catalogue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
describe 'split' do
describe 'rspec group' do
it 'should have a catalogue method' do
catalogue.should be_a(Puppet::Resource::Catalog)
expect(catalogue).to be_a(Puppet::Resource::Catalog)
end

it 'catalogue should not change after subject is called' do
catalogue.should be_a(Puppet::Resource::Catalog)
expect(catalogue).to be_a(Puppet::Resource::Catalog)
pre_id = catalogue.object_id

should run.with_params('aoeu', 'o').and_return(['a', 'eu'])

post_id = catalogue.object_id

pre_id.should eq post_id
expect(pre_id).to eq post_id
end
end
end