Skip to content

Commit a0b4243

Browse files
committed
Add coverband and custom tasks
1 parent 90dd147 commit a0b4243

5 files changed

Lines changed: 68 additions & 15 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ group :test do
6464
gem "capybara"
6565
gem "selenium-webdriver"
6666
gem "simplecov", require: false
67+
gem "coverband"
6768
end
6869

6970
gem "minitest", "~> 5.0"

Gemfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ GEM
101101
xpath (~> 3.2)
102102
concurrent-ruby (1.3.6)
103103
connection_pool (3.0.2)
104+
coverband (6.1.6)
105+
base64
106+
redis (>= 3.0)
104107
crass (1.0.6)
105108
cypress-on-rails (1.20.0)
106109
rack
@@ -267,6 +270,10 @@ GEM
267270
erb
268271
psych (>= 4.0.0)
269272
tsort
273+
redis (5.4.1)
274+
redis-client (>= 0.22.0)
275+
redis-client (0.26.2)
276+
connection_pool
270277
regexp_parser (2.11.3)
271278
reline (0.6.3)
272279
io-console (~> 0.5)
@@ -394,6 +401,7 @@ DEPENDENCIES
394401
brakeman
395402
bundler-audit
396403
capybara
404+
coverband
397405
cypress-on-rails (~> 1.0)
398406
debug
399407
image_processing (~> 1.2)
@@ -443,6 +451,7 @@ CHECKSUMS
443451
capybara (3.40.0) sha256=42dba720578ea1ca65fd7a41d163dd368502c191804558f6e0f71b391054aeef
444452
concurrent-ruby (1.3.6) sha256=6b56837e1e7e5292f9864f34b69c5a2cbc75c0cf5338f1ce9903d10fa762d5ab
445453
connection_pool (3.0.2) sha256=33fff5ba71a12d2aa26cb72b1db8bba2a1a01823559fb01d29eb74c286e62e0a
454+
coverband (6.1.6) sha256=8619896439d3394ca77fa839a67aecb456dc2e06072c88aefea80a9d24e28f09
446455
crass (1.0.6) sha256=dc516022a56e7b3b156099abc81b6d2b08ea1ed12676ac7a5657617f012bd45d
447456
cypress-on-rails (1.20.0) sha256=36e79d77d073f4b83d73a275b4908cb838e103adbad305882a683db0eb2ca7e4
448457
date (3.5.1) sha256=750d06384d7b9c15d562c76291407d89e368dda4d4fff957eb94962d325a0dc0
@@ -518,6 +527,8 @@ CHECKSUMS
518527
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
519528
rake (13.3.1) sha256=8c9e89d09f66a26a01264e7e3480ec0607f0c497a861ef16063604b1b08eb19c
520529
rdoc (7.0.3) sha256=dfe3d0981d19b7bba71d9dbaeb57c9f4e3a7a4103162148a559c4fc687ea81f9
530+
redis (5.4.1) sha256=b5e675b57ad22b15c9bcc765d5ac26f60b675408af916d31527af9bd5a81faae
531+
redis-client (0.26.2) sha256=1336fb5a7202d398b719531853c184b7c9cbdcace1f00f8356062b9dfba6779b
521532
regexp_parser (2.11.3) sha256=ca13f381a173b7a93450e53459075c9b76a10433caadcb2f1180f2c741fc55a4
522533
reline (0.6.3) sha256=1198b04973565b36ec0f11542ab3f5cfeeec34823f4e54cebde90968092b1835
523534
rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
# README
22

3-
This README would normally document whatever steps are necessary to get the
4-
application up and running.
3+
Run `rails db:migrate` to create the DB
54

6-
Things you may want to cover:
5+
Run `rails test` to run the Minitest test suite
76

8-
* Ruby version
7+
Check the coverage report: ~35%
98

10-
* System dependencies
9+
Run `RAILS_ENV=test rails coverband:coverage_server` to start the Coverband server in one terminal
1110

12-
* Configuration
11+
Run `rails cypress:run` to run Cypress tests
1312

14-
* Database creation
13+
Run `rails simplecov_json_report` to extract the code coverage from Coveband into a `cypress_coverage.json` (note that this command will show ~74% coverage, but it's not filtering files we don't want to count for the real coverage)
1514

16-
* Database initialization
15+
Run `rails coverage:merge` to merge both Minitest and Cypress results into one
1716

18-
* How to run the test suite
17+
Check the coverage report: ~52%
1918

20-
* Services (job queues, cache servers, search engines, etc.)
21-
22-
* Deployment instructions
23-
24-
* ...
19+
NOTE: Use `RAILS_ENV=test rails coverband:clear` to clear Coverband's coverage if needed

lib/tasks/json_coverage.rake

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
desc "static HTML formatted report of Coverband code coverage"
2+
task :simplecov_json_report do
3+
require "coverband"
4+
require "coverband/utils/html_formatter"
5+
require "coverband/utils/result"
6+
require "coverband/utils/file_list"
7+
require "coverband/utils/source_file"
8+
require "coverband/utils/lines_classifier"
9+
require "coverband/utils/results"
10+
11+
require "simplecov"
12+
require "simplecov_json_formatter"
13+
14+
`mkdir -p #{SimpleCov.coverage_path}`
15+
16+
# For a fully static HTML that can be copied to artifacts are part of CI
17+
# we generate with inline assets
18+
ENV["SIMPLECOV_INLINE_ASSETS"] = "true"
19+
20+
SimpleCovJSONFormatter::ResultExporter.send(:remove_const, "FILENAME")
21+
SimpleCovJSONFormatter::ResultExporter::FILENAME = "cypress_coverage.json"
22+
23+
coverband_reports = Coverband::Reporters::Base.report(Coverband.configuration.store)
24+
Coverband::Reporters::Base.fix_reports(coverband_reports)
25+
result = Coverband::Utils::Results.new(coverband_reports)
26+
SimpleCov::Formatter::JSONFormatter.new.format(result)
27+
28+
# fix json structure so it can be merged with coverage:merge
29+
generated_json_file = File.join(SimpleCov.coverage_path, SimpleCovJSONFormatter::ResultExporter::FILENAME)
30+
content = { "Cypress" => JSON.parse(File.read(generated_json_file)) }
31+
File.write(generated_json_file, content.to_json)
32+
end
33+
34+
namespace :coverage do
35+
desc "Merge Minitest's and Cypress' code coverage json files into one"
36+
task :merge do
37+
require "simplecov"
38+
39+
# change this if you use different json result names
40+
coverage_files = Dir["#{SimpleCov.coverage_path}/.resultset.json"] + Dir["#{SimpleCov.coverage_path}/cypress_coverage.json"]
41+
42+
# make sure to use the `rails` profile to not add noise with files we won't test
43+
SimpleCov.collate coverage_files, "rails"
44+
end
45+
end

test/test_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ENV["RAILS_ENV"] ||= "test"
2+
23
require "simplecov"
3-
SimpleCov.start
4+
SimpleCov.start "rails"
45

56
require_relative "../config/environment"
67
require "rails/test_help"

0 commit comments

Comments
 (0)