Skip to content

Instantly share code, notes, and snippets.

@vinnybad
Created June 3, 2016 19:16
Show Gist options
  • Save vinnybad/efe32d7594ac7c1fe07bacffdb68b4ed to your computer and use it in GitHub Desktop.
Save vinnybad/efe32d7594ac7c1fe07bacffdb68b4ed to your computer and use it in GitHub Desktop.
Shows one way to fail a build in fastlane when code coverage does not meet a minimum threshold.
#
# Shows one way to fail a build in fastlane when code
# coverage does not meet a minimum threshold.
#
# there are other ways to check the coverage of the commit,
# but I couldn't find an existing tool that makes this easier
# that will run on bitrise. I would have normally used diff-cover
# to do this because you can compare branches and only check the diffs.
# Unfortunately that means you have to find a way to install it on bitrise
# at the time of writing and that can be tricky.
#
# Still, this is a pretty good approach for most cases.
#
# somewhere towards the top
require 'open3'
#
# where app constants live
# minimum coverage % for the project as a whole
#
coverage_limit = 80
# wherever you want, below that require
desc "Runs the CI job"
lane :ci do
cocoapods # will run a pod install automatically
unit # assumes you have a lane called unit that runs unit tests for you. I used fastlane's scan tool to do this.
stdout, stderr, status = Open3.capture3('slather coverage --ignore="Pods/*" --ignore="../*" --workspace ../MyWorkspace.xcworkspace --scheme MyScheme ../MyProject.xcodeproj')
matches = /Test Coverage: (\d+(\.\d+)?)/.match(stdout)
full_coverage = (matches && matches.captures.length > 0) ? matches.captures[0] : nil
if full_coverage != nil && full_coverage.length > 0 && full_coverage.to_i < coverage_limit
puts stderr
puts red("ERROR: You are under the code coverage limit (is: #{full_coverage}%, should be at least: #{coverage_limit}%). Please raise code coverage and try again.")
abort()
end
end
@gitrubs
Copy link

gitrubs commented Apr 15, 2020

Thanks a lot!!!

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