Last active
June 9, 2023 19:10
-
-
Save arsduo/d34be94b3c7aef7063a2d0728ac260ec to your computer and use it in GitHub Desktop.
SimpleCov Test Coverage for Files Changed in a Branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output = `cat #{ENV["CIRCLE_ARTIFACTS"] || "."}/coverage/index.html | grep Changed -A 2 | grep "[0-9\.]*%"` | |
percentage_match = output.match(/([0-9\.]+)%/) | |
raise "Unable to determine test coverage change" unless percentage_match | |
RED = "\033[0;31m" | |
BOLD = "\033[1m" | |
NO_COLOR = "\033[0m" | |
percentage = percentage_match[0].to_f | |
if percentage < 90 | |
warn "\n" | |
warn "#{RED}#{BOLD}⚠️ 📉 INSUFFICIENT TEST COVERAGE#{NO_COLOR} 📉 ⚠️" | |
warn "\n" | |
warn "New and changed files need a test coverage of >= 90% -- Simplecov only detected #{RED}#{BOLD}#{percentage}#{NO_COLOR} 😱" | |
warn "\n" | |
warn "Changed files:" | |
warn " * #{`git diff --name-only origin/master`.split("\n").join("\n * ")}" | |
warn "\n" | |
exit 1 | |
end | |
puts "Test coverage for changed files: #{percentage}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test: | |
post: | |
- ruby scripts/check_changed_coverage.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SimpleCov.start "rails" do | |
changed_files = `git diff --name-only origin/master`.split("\n") | |
add_group "Changed" do |source_file| | |
changed_files.detect do |filename| | |
source_file.filename.ends_with?(filename) | |
end | |
end | |
end | |
if ENV["CIRCLE_ARTIFACTS"] | |
dir = File.join(ENV["CIRCLE_ARTIFACTS"], "coverage") | |
SimpleCov.coverage_dir(dir) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. Had to make some modifications, but it works as expected.