From 89ae51d68f9a1571ad030409a9bfde0eb504f779 Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Wed, 10 Jan 2024 12:00:05 +0000 Subject: [PATCH] (CAT-1618) - Add code coverage back to ci --- .github/workflows/ci.yml | 4 ++++ Gemfile | 5 +++-- Rakefile | 8 ++++++++ spec/spec_helper.rb | 26 ++++++++++++++++++++------ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92293f1d..e9260f9f 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: @@ -19,6 +22,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 }} acceptance: diff --git a/Gemfile b/Gemfile index 0459d781..ef8bda50 100644 --- a/Gemfile +++ b/Gemfile @@ -10,8 +10,9 @@ group :test do gem 'json' gem 'rspec-json_expectations', '~> 1.4' - - gem 'simplecov', :require => false if ENV['COVERAGE'] == 'yes' + gem 'simplecov', :require => false + gem 'simplecov-console', :require => false + gem 'codecov', :require => false end group :acceptance do diff --git a/Rakefile b/Rakefile index ba8f1e23..a9668f35 100644 --- a/Rakefile +++ b/Rakefile @@ -27,6 +27,14 @@ rescue LoadError # Gem not present end +namespace :spec do + desc 'Run RSpec code examples with coverage collection' + task :coverage do + ENV['COVERAGE'] = 'yes' + Rake::Task['spec'].execute + end +end + RSpec::Core::RakeTask.new(:spec) do |t| t.exclude_pattern = 'spec/acceptance/**/*_spec.rb' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 65d4a432..367ac1d4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,12 +1,26 @@ # Disable GitHub Actions reporting since it breaks the test suite ENV.delete('GITHUB_ACTION') -if ENV['COVERAGE'] == 'yes' && RUBY_VERSION.start_with?('2.7.') - require 'simplecov' - SimpleCov.start do - add_filter('/spec/') - add_filter('/vendor/') - add_group('Checks', 'lib/puppet-lint/plugins') +if ENV['COVERAGE'] == 'yes' + 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('/spec/') + add_filter('/vendor/') + add_group('Checks', 'lib/puppet-lint/plugins') + end + rescue LoadError + raise 'Add the simplecov, simplecov-console, codecov gems to Gemfile to enable this task' end end