Make the build fail on certain conditions

Sometimes, one may want to force build to fail on certain conditions, e.g. test coverage is less than 75%.

However, that will require like rcov and counting skipped [due to lack of {redis,mongo,couch} on our worker machine] tests as passed or something similar.

wdyt is going on?

Hi @sobrante,

It’s possible to create a custom rake task or a similar build script. Therefore, it should be feasible to develop a test script that executes the tests, monitors the coverage, and causes the build to fail if the coverage falls below a specified threshold.

Here’s a simple script I wrote for you:

require 'simplecov'

SimpleCov.start do
end

require_relative 'path/to/your/test_file'

MIN_COVERAGE_PERCENTAGE = 80

coverage_result = SimpleCov.result
if coverage_result.covered_percent < MIN_COVERAGE_PERCENTAGE
  puts "Coverage (#{coverage_result.covered_percent}%) is below the acceptable threshold (#{MIN_COVERAGE_PERCENTAGE}%)"
  exit 1 # Exits with error, useful for failing Travis CI builds
else
  puts "Coverage (#{coverage_result.covered_percent}%) is above the acceptable threshold (#{MIN_COVERAGE_PERCENTAGE}%)"
end

Remember to replace the test file paths and the test-running code with your actual test setup. This script is a basic example and may need adjustments to fit into your specific environment and requirements.

1 Like

yep this worked! thanks @Montana

Great to hear I was able to solve your issue @sobrante!

1 Like