diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8423d4ec5..68570f65f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,6 +6,9 @@ on: - "main" workflow_dispatch: +env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + jobs: spec: strategy: @@ -26,6 +29,7 @@ jobs: uses: "puppetlabs/cat-github-actions/.github/workflows/gem_ci.yml@main" secrets: "inherit" with: + rake_task: "spec:coverage" ruby_version: ${{ matrix.ruby_version }} puppet_gem_version: ${{ matrix.puppet_gem_version }} runs_on: ${{ matrix.runs_on }} diff --git a/Rakefile b/Rakefile index 22d4fd938..a99a212a7 100644 --- a/Rakefile +++ b/Rakefile @@ -94,3 +94,11 @@ end RuboCop::RakeTask.new(:rubocop) do |task| task.options = %w[-D -S -E] end + +namespace :spec do + desc 'Run RSpec code examples with coverage collection' + task :coverage do + ENV['COVERAGE'] = 'yes' + Rake::Task['test'].invoke + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3cc73c581..a77a24f23 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,14 +1,29 @@ # frozen_string_literal: true if ENV['COVERAGE'] - require 'simplecov' - require 'coveralls' - - SimpleCov.formatter = Coveralls::SimpleCov::Formatter if ENV['COVERAGE'] == 'yes' - - SimpleCov.start do - add_filter %r{^/spec/} - add_filter %r{^/vendor/} + begin + require 'simplecov' + require 'simplecov-console' + + SimpleCov.formatters = [ + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::Console + ] + + if ENV['CI'] == 'true' + require 'codecov' + SimpleCov.formatters << SimpleCov::Formatter::Codecov + end + + SimpleCov.start do + add_filter %r{^/spec/} + add_filter %r{^/vendor/} + + add_filter '/docs' + add_filter 'lib/rspec-puppet/version.rb' + end + rescue LoadError + raise 'Add the simplecov, simplecov-console, codecov gems to Gemfile to enable this task' end end diff --git a/spec/spec_helper_unit.rb b/spec/spec_helper_unit.rb index 2be7d12e2..9a1ba3b80 100644 --- a/spec/spec_helper_unit.rb +++ b/spec/spec_helper_unit.rb @@ -1,14 +1,29 @@ # frozen_string_literal: true if ENV['COVERAGE'] - require 'coveralls' - require 'simplecov' + begin + require 'simplecov' + require 'simplecov-console' - SimpleCov.formatter = Coveralls::SimpleCov::Formatter if ENV['COVERAGE'] == 'yes' + SimpleCov.formatters = [ + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::Console + ] - SimpleCov.start do - add_filter %r{^/spec/} - add_filter %r{^/vendor/} + if ENV['CI'] == 'true' + require 'codecov' + SimpleCov.formatters << SimpleCov::Formatter::Codecov + end + + SimpleCov.start do + add_filter %r{^/spec/} + add_filter %r{^/vendor/} + + add_filter '/docs' + add_filter 'lib/rspec-puppet/version.rb' + end + rescue LoadError + raise 'Add the simplecov, simplecov-console, codecov gems to Gemfile to enable this task' end end