Skip to content

Commit

Permalink
Integrate simplecov-rspec to ensure code covage in CI builds
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouball committed Sep 9, 2024
1 parent f33707e commit c5ef6b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 65 deletions.
1 change: 1 addition & 0 deletions process_executer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'semverify', '~> 0.3'
spec.add_development_dependency 'simplecov', '~> 0.22'
spec.add_development_dependency 'simplecov-lcov', '~> 0.8'
spec.add_development_dependency 'simplecov-rspec', '~> 0.2'

unless RUBY_PLATFORM == 'java'
spec.add_development_dependency 'redcarpet', '~> 3.6'
Expand Down
12 changes: 9 additions & 3 deletions spec/process_executer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
it { is_expected.to have_attributes(timeout?: false, exitstatus: 1) }
end

def windows?
!!(RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/)
rescue StandardError
# :nocov:
false
# :nocov:
end

context 'for a command that times out' do
let(:command) { %w[sleep 1] }
let(:options) { { timeout: 0.01 } }
Expand All @@ -68,8 +76,7 @@
expect(end_time - start_time).to be < 0.1

# :nocov:
# rubocop:disable Style/RescueModifier
if (WINDOWS = (RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/) rescue false)
if windows?
# On windows, the status of a process killed with SIGKILL will indicate
# that the process exited normally with exitstatus 0.
expect(subject).to have_attributes(exited?: true, exitstatus: 0, timeout?: true)
Expand All @@ -78,7 +85,6 @@
# that the process terminated because of the uncaught signal
expect(subject).to have_attributes(signaled?: true, termsig: 9, timeout?: true)
end
# rubocop:enable Style/RescueModifier
# :nocov:
end
end
Expand Down
70 changes: 8 additions & 62 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,19 @@
end
end

# Setup simplecov

require 'simplecov'
require 'simplecov-lcov'
require 'json'

SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, SimpleCov::Formatter::LcovFormatter]
require 'simplecov-rspec'

# 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 ENV.fetch('GITHUB_ACTIONS', 'false') == 'true'
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::LcovFormatter
]
end

SimpleCov.start
SimpleCov::RSpec.start

# Make sure to require your project AFTER SimpleCov.start
# Make sure to require your project AFTER starting SimpleCov
#
require 'process_executer'

0 comments on commit c5ef6b0

Please sign in to comment.