Skip to content

Commit

Permalink
Integrate simplecov-rspec into the project
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouball committed Sep 15, 2024
1 parent be896ab commit d675a2e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 102 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
- ruby: "jruby-9.4"
operating-system: windows-latest

env:
JRUBY_OPTS: --debug

steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -50,6 +53,9 @@ jobs:
needs: [build]
runs-on: ubuntu-latest

env:
JRUBY_OPTS: --debug

steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/experimental_ruby_builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
jobs:
build:
name: Ruby ${{ matrix.ruby }} on ${{ matrix.operating-system }}

runs-on: ${{ matrix.operating-system }}
continue-on-error: true

Expand All @@ -32,6 +32,9 @@ jobs:
- ruby: jruby-head
operating-system: windows-latest

env:
JRUBY_OPTS: --debug

steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions drive_v3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rubocop', '~> 1.66'
spec.add_development_dependency 'simplecov', '~> 0.22'
spec.add_development_dependency 'simplecov-lcov', '~> 0.8'
spec.add_development_dependency 'simplecov-rspec', '~> 0.3'

unless RUBY_PLATFORM == 'java'
spec.add_development_dependency 'redcarpet', '~> 3.6'
Expand Down
111 changes: 10 additions & 101 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,113 +12,22 @@
end
end

# Setup simplecov

# SimpleCov configuration
#
require 'simplecov'
require 'simplecov-lcov'
require 'json'

SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, SimpleCov::Formatter::LcovFormatter]

# Return `true` if the environment variable is set to a truthy value
#
# @example
# env_true?('COV_SHOW_UNCOVERED')
#
# @param name [String] the name of the environment variable
# @return [Boolean]
#
def env_true?(name)
value = ENV.fetch(name, '').downcase
%w[yes on true 1].include?(value)
end

# Return `true` if the environment variable is NOT set to a truthy value
#
# @example
# env_false?('COV_NO_FAIL')
#
# @param name [String] the name of the environment variable
# @return [Boolean]
#
def env_false?(name)
!env_true?(name)
end
require 'simplecov-rspec'

# Return `true` if the the test run should fail if the coverage is below the threshold
#
# @return [Boolean]
#
def fail_on_low_coverage?
!(RSpec.configuration.dry_run? || env_true?('COV_NO_FAIL'))
end
def ci_build? = ENV.fetch('GITHUB_ACTIONS', 'false') == 'true'

# Return `true` if the the test run should show the lines not covered by tests
#
# @return [Boolean]
#
def show_lines_not_covered?
env_true?('COV_SHOW_UNCOVERED')
end

# Report if the test coverage was below the configured threshold
#
# The threshold is configured by setting the `test_coverage_threshold` variable
# in this file.
#
# Example:
#
# ```Ruby
# test_coverage_threshold = 100
# ```
#
# Coverage below the threshold will cause the rspec run to fail unless the
# `COV_NO_FAIL` environment variable is set to TRUE.
#
# ```Shell
# COV_NO_FAIL=TRUE rspec
# ```
#
# Example of running the tests in an infinite loop writing failures to `fail.txt`:
#
# ```Shell
# while true; do COV_NO_FAIL=TRUE rspec >> fail.txt; done
# ````
#
# The lines missing coverage will be displayed if the `COV_SHOW_UNCOVERED`
# environment variable is set to TRUE.
#
# ```Shell
# COV_SHOW_UNCOVERED=TRUE rspec
# ```
#
test_coverage_threshold = 100

SimpleCov.at_exit do
SimpleCov.result.format!
# rubocop:disable Style/StderrPuts
if SimpleCov.result.covered_percent < test_coverage_threshold
$stderr.puts
$stderr.print 'FAIL: ' if fail_on_low_coverage?
$stderr.puts "RSpec Test coverage fell below #{test_coverage_threshold}%"

if show_lines_not_covered?
$stderr.puts "\nThe following lines were not covered by tests:\n"
SimpleCov.result.files.each do |source_file| # SimpleCov::SourceFile
source_file.missed_lines.each do |line| # SimpleCov::SourceFile::Line
$stderr.puts " .#{source_file.project_filename}:#{line.number}"
end
end
end

$stderr.puts

exit 1 if fail_on_low_coverage?
end
# rubocop:enable Style/StderrPuts
if ci_build?
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::LcovFormatter
]
end

SimpleCov.start
SimpleCov::RSpec.start(list_uncovered_lines: ci_build?)

# Make sure to require your project AFTER SimpleCov.start
#
Expand Down

0 comments on commit d675a2e

Please sign in to comment.