Skip to content

Instantly share code, notes, and snippets.

@mattpolzin
Last active October 13, 2019 20:59
Show Gist options
  • Save mattpolzin/4e620c0e3e0a89cb8725ffa6f6f64ce5 to your computer and use it in GitHub Desktop.
Save mattpolzin/4e620c0e3e0a89cb8725ffa6f6f64ce5 to your computer and use it in GitHub Desktop.
A bitrise bash script to take codecov from a previous `swift test --enable-code-coverage` step and generate some basic human readable stats.
#!/usr/bin/env bash
# fail if any commands fails
set -e
##
## INPUTS
## - $CODECOV_JSON - The location of the JSON file produced by
## swift test --enable-code-coverage
## - $PRINT_STDOUT - 'true' by default, but if 'false' then will not
## output the whole codecov table to stdout.
##
## OUTPUTS
## - $CODECOV - Overally code coverage percent.
## - codecov.txt - Code coverage in a file.
## - shield - Code coverge in a format shield.io recognizes
##
SCRIPT_VERSION="0.4.0"
# Set default location for JSON
CODECOV_JSON=${CODECOV_JSON:-.build/debug/codecov/*.json}
# Clone if needed
if [ ! -d "swift-test-codecov" ]; then
echo "Cloning swift-test-codecov"
git clone https://github.com/mattpolzin/swift-test-codecov.git
fi
# build Codecov
pushd swift-test-codecov
git fetch
git checkout "$SCRIPT_VERSION"
swift package resolve
swift build
popd
# Run Codecov for overall coverage
COV=`swift-test-codecov/.build/debug/swift-test-codecov $CODECOV_JSON`
# Run Codecov for full table
FULL_COV_TABLE=`swift-test-codecov/.build/debug/swift-test-codecov $CODECOV_JSON --table`
# Dump to shields.io file
echo "{\"schemaVersion\": 1,\"label\": \"hello\",\"message\": \"${COV}\",\"color\": \"orange\"}" > "${BITRISE_DEPLOY_DIR}/shield"
# Dump to txt file
echo "$FULL_COV_TABLE" > "${BITRISE_DEPLOY_DIR}/codecov.txt"
# Export env var
envman add --key CODECOV --value "$COV"
# Print to stdout
if [ "${PRINT_STDOUT:-true}" = 'true' ]; then
echo "$FULL_COV_TABLE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment