Skip to content

Commit

Permalink
Merge pull request #716 from rodjek/100_coverage
Browse files Browse the repository at this point in the history
Improve self test coverage
  • Loading branch information
DavidS authored Nov 16, 2020
2 parents f2e05f4 + 249d24d commit b8b1412
Show file tree
Hide file tree
Showing 14 changed files with 456 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/tmp/
/spec/fixtures/modules/augeas_core/
/spec/fixtures/modules/stdlib/
/coverage/
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ else
gem 'sync' if (RUBY_VERSION >= '2.7.0')
end

if ENV['COVERAGE'] == 'yes'
if ENV['COVERAGE']
gem 'coveralls', :require => false
gem 'simplecov', :require => false
end

gem 'win32-taskscheduler', :platforms => [:mingw, :x64_mingw, :mswin]
Expand Down
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace :test do
end
end

RSpec::Core::RakeTask.new(:spec_unit) do |t|
t.pattern = 'spec/unit/**/*_spec.rb'
end

task :setup do
puppet_version = Gem::Version.new(Puppet.version)

Expand Down Expand Up @@ -64,6 +68,17 @@ namespace :test do
end
end
end

task :unit do
begin
Rake::Task['test:setup'].invoke
ENV['COVERAGE'] = 'local'
Rake::Task['test:spec_unit'].invoke
ensure
ENV.delete('COVERAGE')
Rake::Task['test:teardown'].invoke
end
end
end

task :test do
Expand Down
8 changes: 5 additions & 3 deletions lib/rspec-puppet/matchers/count_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ class CountGeneric
'Stage[main]',
].freeze

attr_reader :resource_type

def initialize(type, count, *method)
if type.nil?
@type = method[0].to_s.gsub(/^have_(.+)_resource_count$/, '\1')
else
@type = type
end
@referenced_type = referenced_type(@type)
@resource_type = referenced_type(@type)
@expected_number = count.to_i
end

Expand All @@ -30,7 +32,7 @@ def matches?(catalogue)
end
else
resources.count do |res|
res.type == @referenced_type
res.type == @resource_type
end
end

Expand All @@ -45,7 +47,7 @@ def description
desc << "#{@expected_number == 1 ? "class" : "classes" }"
else
unless @type == "resource"
desc << "#{@referenced_type}"
desc << "#{@resource_type}"
end
desc << "#{@expected_number == 1 ? "resource" : "resources" }"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec-puppet/matchers/include_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ManifestMatchers

matcher :include_class do |expected_class|
match do |catalogue|
RSpec.deprecate(:include_class, :replacement => :contain_class)
RSpec.deprecate('include_class()', :replacement => 'contain_class()')
catalogue.call.classes.include?(expected_class)
end

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 {
expect(RSpec).to receive(:deprecate).with(:include_class, {:replacement => :contain_class})
expect(RSpec).to receive(:deprecate).with('include_class()', {:replacement => 'contain_class()'})
should include_class('test::bare_class')
}
it {
expect(RSpec).to receive(:deprecate).with(:include_class, {:replacement => :contain_class})
expect(RSpec).to receive(:deprecate).with('include_class()', {:replacement => 'contain_class()'})
should include_class('test::parameterised_class')
}

Expand Down
20 changes: 17 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
if ENV['COVERAGE'] == 'yes'
if ENV['COVERAGE']
require 'simplecov'
require 'coveralls'

SimpleCov.formatter = Coveralls::SimpleCov::Formatter
if ENV['COVERAGE'] == 'yes'
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
end

SimpleCov.start do
add_filter(/^\/spec\//)
add_filter %r{^/spec/}
add_filter %r{^/vendor/}
end
end

Expand All @@ -21,6 +25,13 @@ def sensitive?
defined?(::Puppet::Pops::Types::PSensitiveType)
end

module Helpers
def rspec2?
RSpec::Version::STRING < '3'
end
module_function :rspec2?
end

RSpec.configure do |c|
c.module_path = File.join(File.dirname(File.expand_path(__FILE__)), 'fixtures', 'modules')
c.manifest_dir = File.join(File.dirname(File.expand_path(__FILE__)), 'fixtures', 'manifests')
Expand All @@ -31,4 +42,7 @@ def sensitive?
c.after(:suite) do
RSpec::Puppet::Coverage.report!(0)
end

c.include Helpers
c.extend Helpers
end
49 changes: 49 additions & 0 deletions spec/spec_helper_unit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
if ENV['COVERAGE']
require 'coveralls'
require 'simplecov'

if ENV['COVERAGE'] == 'yes'
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
end

SimpleCov.start do
add_filter %r{^/spec/}
add_filter %r{^/vendor/}
end
end

require 'rspec-puppet'

module Helpers
def rspec2?
RSpec::Version::STRING < '3'
end
module_function :rspec2?

def test_double(type, *args)
if rspec2?
double(type.to_s, *args)
else
instance_double(type, *args)
end
end
end

RSpec.configure do |c|
c.include Helpers
c.extend Helpers

if Helpers.rspec2?
RSpec::Matchers.define :be_truthy do
match do |actual|
!!actual == true
end
end

RSpec::Matchers.define :be_falsey do
match do |actual|
!!actual == false
end
end
end
end
Loading

0 comments on commit b8b1412

Please sign in to comment.