From d2e177de4ae4e420396d0147905e0cbd60e3eae0 Mon Sep 17 00:00:00 2001 From: "Everton J. Carpes" Date: Fri, 15 Nov 2024 11:48:58 -0300 Subject: [PATCH] Make minitest exit with non-zero status if coverage bellow minimum. (#32) The minimum coverage can be set with ENV var MINIMUM_COVERAGE (default 100%). --- lib/covered/minitest.rb | 9 +++++++++ test/covered/minitest.rb | 13 +++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/covered/minitest.rb b/lib/covered/minitest.rb index d8e525a..1a56940 100644 --- a/lib/covered/minitest.rb +++ b/lib/covered/minitest.rb @@ -12,6 +12,9 @@ module Covered module Minitest + MINIMUM_COVERAGE = + Float(ENV.fetch('MINIMUM_COVERAGE', 100.0)) + def run(*) $covered.start @@ -26,5 +29,11 @@ def run(*) Minitest.after_run do $covered.finish $covered.call($stderr) + + stats = Covered::Statistics.new + + $covered.policy.each { stats << _1 } + + stats.validate! Covered::Minitest::MINIMUM_COVERAGE / 100.0 end end diff --git a/test/covered/minitest.rb b/test/covered/minitest.rb index 89ec7cd..a51acab 100644 --- a/test/covered/minitest.rb +++ b/test/covered/minitest.rb @@ -11,11 +11,20 @@ it "can run minitest test suite with coverage" do input, output = IO.pipe - - system({"COVERAGE" => "PartialSummary"}, test_path, out: output, err: output) + + env = { + "COVERAGE" => "PartialSummary", + "MINIMUM_COVERAGE" => "100.1" + } + + system(env, test_path, out: output, err: output) output.close + expect($?.exitstatus).not.to be(:zero?) + buffer = input.read + expect(buffer).to be =~ /(.*?) files checked; (.*?) lines executed; (.*?)% covered/ + expect(buffer).to be =~ /is less than required minimum of 100.1/ end end