Skip to content

Commit 3461082

Browse files
committed
Add code coverage with SimpleCov and coverage badge to CI
1 parent 9a0f32d commit 3461082

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@ on:
33
pull_request:
44
push:
55
branches: [master]
6+
7+
permissions:
8+
contents: read
9+
pages: write
10+
id-token: write
11+
612
jobs:
713
test:
814
name: Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }} / AA ${{ matrix.activeadmin }}
915
runs-on: ubuntu-latest
1016
strategy:
1117
fail-fast: false
1218
matrix:
13-
ruby: ['3.2.0', '3.3.0', '3.4.0']
14-
rails: ['7.1.0', '7.2.0']
19+
ruby: ['3.2', '3.3', '3.4']
20+
rails: ['7.1.0', '7.2.0', '8.0.0']
1521
activeadmin: ['3.2.0', '3.3.0', '3.4.0', '3.5.0']
22+
exclude:
23+
- rails: '8.0.0'
24+
activeadmin: '3.2.0'
1625
env:
1726
RAILS: ${{ matrix.rails }}
1827
AA: ${{ matrix.activeadmin }}
@@ -24,3 +33,55 @@ jobs:
2433
bundler-cache: true
2534
- name: Run tests
2635
run: bundle exec rspec spec
36+
37+
- name: Generate badge.json
38+
if: matrix.ruby == '3.4' && matrix.rails == '8.0.0' && matrix.activeadmin == '3.5.0'
39+
run: |
40+
LAST_RUN="coverage/.last_run.json"
41+
if [ ! -f "$LAST_RUN" ]; then
42+
mkdir -p badge
43+
echo '{"schemaVersion":1,"label":"coverage","message":"unknown","color":"lightgrey"}' > badge/badge.json
44+
exit 0
45+
fi
46+
PERCENT=$(ruby -rjson -e "puts JSON.parse(File.read('$LAST_RUN')).dig('result','line').round(1)")
47+
PERCENT_NUM=$(ruby -rjson -e "puts JSON.parse(File.read('$LAST_RUN')).dig('result','line')")
48+
if ruby -e "exit(($PERCENT_NUM >= 90) ? 0 : 1)"; then COLOR="brightgreen"
49+
elif ruby -e "exit(($PERCENT_NUM >= 75) ? 0 : 1)"; then COLOR="green"
50+
elif ruby -e "exit(($PERCENT_NUM >= 60) ? 0 : 1)"; then COLOR="yellow"
51+
else COLOR="red"; fi
52+
mkdir -p badge
53+
echo "{\"schemaVersion\":1,\"label\":\"coverage\",\"message\":\"${PERCENT}%\",\"color\":\"${COLOR}\"}" > badge/badge.json
54+
55+
- name: Upload badge artifact
56+
if: matrix.ruby == '3.4' && matrix.rails == '8.0.0' && matrix.activeadmin == '3.5.0'
57+
uses: actions/upload-artifact@v4
58+
with:
59+
name: coverage-badge
60+
path: badge
61+
62+
deploy-coverage:
63+
needs: test
64+
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
65+
runs-on: ubuntu-latest
66+
environment:
67+
name: github-pages
68+
url: ${{ steps.deployment.outputs.page_url }}
69+
70+
steps:
71+
- name: Download coverage badge
72+
uses: actions/download-artifact@v4
73+
with:
74+
name: coverage-badge
75+
path: coverage
76+
77+
- name: Setup Pages
78+
uses: actions/configure-pages@v5
79+
80+
- name: Upload Pages artifact
81+
uses: actions/upload-pages-artifact@v3
82+
with:
83+
path: coverage
84+
85+
- name: Deploy to GitHub Pages
86+
id: deployment
87+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/pkg/
44
/tmp/
55
spec/rails/rails-*
6+
/coverage

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gem 'sprockets-rails'
1010
gem 'sass-rails'
1111

1212
group :test do
13+
gem 'simplecov', require: false
1314
gem 'rspec-rails'
1415
gem 'sqlite3', '~> 2.0'
1516
gem 'database_cleaner'

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
require 'simplecov'
2+
SimpleCov.start do
3+
add_filter '/spec/'
4+
end
5+
16
$LOAD_PATH.unshift(File.dirname(__FILE__))
27
$LOAD_PATH << File.expand_path('../support', __FILE__)
38

0 commit comments

Comments
 (0)