Skip to content

Commit fe288b9

Browse files
Copilotkwerle
andauthored
Add test coverage badge to GitHub page with self-contained solution (#122)
* Initial plan * Add coverage reporting with Codecov integration and badge Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Explicitly set CI env variable for coverage runs Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Address code review feedback: add explicit dependency and optimize workflow Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Add setup documentation for Codecov integration Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Address review feedback: conditional formatter loading and add rubocop target Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Replace Codecov with self-contained coverage badge solution Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Add permissions and error handling to coverage workflow Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Use sed instead of grep -P for better portability Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Update rubocop target to use docker run to avoid interactive mode * And coverage * Fix badge URL and improve coverage parsing regex Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> * Fix regex to handle single-digit coverage percentages Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com> Co-authored-by: Kurt Werle <kurt@CircleW.org>
1 parent 714d3cc commit fe288b9

6 files changed

Lines changed: 84 additions & 4 deletions

File tree

.github/badges/coverage.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"schemaVersion": 1,
3+
"label": "coverage",
4+
"message": "unknown",
5+
"color": "lightgrey"
6+
}

.github/workflows/test.yml

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,72 @@ on:
1616
jobs:
1717
test:
1818
runs-on: ubuntu-latest
19+
permissions:
20+
contents: write
1921
steps:
2022
- uses: actions/checkout@v2
21-
- name: Run tests
22-
run: make test
23+
- name: Run tests with coverage
24+
run: make coverage
25+
- name: Run rubocop
26+
run: make rubocop
27+
- name: Extract and update coverage badge
28+
run: |
29+
# Extract coverage percentage from SimpleCov's .last_run.json
30+
if [ ! -f coverage/.last_run.json ]; then
31+
echo "Error: coverage/.last_run.json not found"
32+
exit 1
33+
fi
34+
35+
# Use sed for better portability - account for optional spaces after colon
36+
COVERAGE=$(sed -n 's/.*"coverage": *\([0-9.]*\).*/\1/p' coverage/.last_run.json | head -1)
37+
if [ -z "$COVERAGE" ]; then
38+
echo "Error: Could not extract coverage percentage"
39+
echo "Contents of coverage/.last_run.json:"
40+
cat coverage/.last_run.json
41+
exit 1
42+
fi
43+
44+
COVERAGE_INT=$(printf "%.0f" "$COVERAGE")
45+
echo "Coverage: $COVERAGE_INT%"
46+
47+
# Determine badge color based on coverage
48+
if [ "$COVERAGE_INT" -ge 90 ]; then
49+
COLOR="brightgreen"
50+
elif [ "$COVERAGE_INT" -ge 80 ]; then
51+
COLOR="green"
52+
elif [ "$COVERAGE_INT" -ge 70 ]; then
53+
COLOR="yellowgreen"
54+
elif [ "$COVERAGE_INT" -ge 60 ]; then
55+
COLOR="yellow"
56+
else
57+
COLOR="red"
58+
fi
59+
60+
# Create badge JSON for shields.io endpoint
61+
mkdir -p .github/badges
62+
cat > .github/badges/coverage.json << EOF
63+
{
64+
"schemaVersion": 1,
65+
"label": "coverage",
66+
"message": "${COVERAGE_INT}%",
67+
"color": "$COLOR"
68+
}
69+
EOF
70+
71+
# Configure git
72+
git config user.name "github-actions[bot]"
73+
git config user.email "github-actions[bot]@users.noreply.github.com"
74+
75+
# Commit and push if changed
76+
git add .github/badges/coverage.json
77+
if git diff --staged --quiet; then
78+
echo "No changes to coverage badge"
79+
else
80+
git commit -m "Update coverage badge: ${COVERAGE_INT}%"
81+
# Pull any changes before pushing
82+
git pull --rebase origin ${{ github.ref_name }} || true
83+
git push || {
84+
echo "Warning: Failed to push badge update"
85+
exit 0
86+
}
87+
fi

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: image guard continuous_development console test shell run_in_shell server gem gem_release publish_cross_platform_image
1+
.PHONY: image guard continuous_development console test coverage rubocop shell run_in_shell server gem gem_release publish_cross_platform_image
22
PROJECT_NAME=ruby_language_server
33
LOCAL_LINK=-v $(PWD):/tmp/src -w /tmp/src
44

@@ -30,7 +30,10 @@ test: image
3030
docker run --rm $(LOCAL_LINK) $(PROJECT_NAME) sh -c "bundle exec rake test && bundle exec rubocop"
3131

3232
coverage: image
33-
./bin/run_in_shell "COVERAGE=true bundle exec rake test"
33+
docker run --rm $(LOCAL_LINK) $(PROJECT_NAME) sh -c "COVERAGE=true bundle exec rake test"
34+
35+
rubocop: image
36+
docker run --rm $(LOCAL_LINK) $(PROJECT_NAME) sh -c "bundle exec rubocop"
3437

3538
shell: image
3639
./bin/run_in_shell sh

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
![Build Status](https://github.com/kwerle/ruby_language_server/actions/workflows/test.yml/badge.svg)
2+
![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/kwerle/ruby_language_server/copilot/add-test-coverage-badge/.github/badges/coverage.json)
23
# Overview
34

45
https://github.com/kwerle/ruby_language_server

ruby_language_server.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ Gem::Specification.new do |spec|
6060
spec.add_development_dependency 'rubocop-rake'
6161
spec.add_development_dependency 'rubocop-rspec' # Linter - no longer needed - use additional gems?
6262
spec.add_development_dependency 'simplecov'
63+
spec.add_development_dependency 'simplecov_json_formatter'
6364
spec.metadata['rubygems_mfa_required'] = 'true'
6465
end

spec/test_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
if ENV['COVERAGE']
44
require 'simplecov'
5+
if ENV['CI']
6+
require 'simplecov_json_formatter'
7+
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
8+
end
59
SimpleCov.start do
610
add_filter '/spec/'
711
add_filter '/vendor/'

0 commit comments

Comments
 (0)