Skip to content

Instantly share code, notes, and snippets.

@arsduo
Last active June 9, 2023 19:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsduo/d34be94b3c7aef7063a2d0728ac260ec to your computer and use it in GitHub Desktop.
Save arsduo/d34be94b3c7aef7063a2d0728ac260ec to your computer and use it in GitHub Desktop.
SimpleCov Test Coverage for Files Changed in a Branch
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}"
test:
post:
- ruby scripts/check_changed_coverage.rb
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
@afsangujarati93
Copy link

Thank you. Had to make some modifications, but it works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment