From 41ee7abffc657e84d01bd917e3ff7e0a9b2ca168 Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Mon, 15 Jan 2024 13:59:01 +0000 Subject: [PATCH] "(CAT-1618) - Add code coverage to ci" --- .github/workflows/ci.yml | 7 ++++++- Gemfile | 1 + Rakefile | 8 ++++++++ spec/spec_helper.rb | 39 ++++++++++++++++++--------------------- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 24e19cdf..47c2d5a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,11 @@ on: - "main" workflow_dispatch: +env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + jobs: + spec: strategy: fail-fast: false @@ -18,4 +22,5 @@ jobs: uses: "puppetlabs/cat-github-actions/.github/workflows/gem_ci.yml@main" secrets: "inherit" with: - ruby_version: ${{ matrix.ruby_version }} + rake_task: "spec:coverage" + ruby_version: ${{ matrix.ruby_version }} \ No newline at end of file diff --git a/Gemfile b/Gemfile index 670d1fe7..af760edb 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ group :test do gem 'codecov' gem 'simplecov' + gem 'simplecov-console' end group :development do diff --git a/Rakefile b/Rakefile index 5bb50ccb..f443aea7 100644 --- a/Rakefile +++ b/Rakefile @@ -7,5 +7,13 @@ require 'yard' RSpec::Core::RakeTask.new(:spec) task :default => :spec +namespace :spec do + desc 'Run RSpec code examples with coverage collection' + task :coverage do + ENV['COVERAGE'] = 'yes' + Rake::Task['spec'].execute + end +end + YARD::Rake::YardocTask.new do |t| end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ca5afaa5..c3132a6e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,32 +5,29 @@ require 'ostruct' if ENV['COVERAGE'] == 'yes' - require 'simplecov' - - if ENV['CI'] == 'true' - require 'codecov' - SimpleCov.formatter = SimpleCov::Formatter::Codecov - else + begin + require 'simplecov' + require 'simplecov-console' SimpleCov.formatters = [ - SimpleCov::Formatter::HTMLFormatter + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::Console ] - end - SimpleCov.start do - track_files 'lib/**/*.rb' - - add_filter '/spec' + if ENV['CI'] == 'true' + require 'codecov' + SimpleCov.formatters << SimpleCov::Formatter::Codecov + end - # do not track vendored files - add_filter '/vendor' - add_filter '/.vendor' + SimpleCov.start do + track_files 'lib/**/*.rb' - # do not track gitignored files - # this adds about 4 seconds to the coverage check - # this could definitely be optimized - add_filter do |f| - # system returns true if exit status is 0, which with git-check-ignore means file is ignored - system("git check-ignore --quiet #{f.filename}") + add_filter '/spec' + add_filter 'lib/puppet_litmus/version.rb' + # do not track vendored files + add_filter '/vendor' + add_filter '/.vendor' end + rescue LoadError + raise 'Add the simplecov, simplecov-console, and codecov gems to Gemfile to enable this task' end end