simplecov 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +76 -1
- data/CODE_OF_CONDUCT.md +76 -0
- data/README.md +265 -76
- data/doc/alternate-formatters.md +5 -0
- data/lib/simplecov/combine/branches_combiner.rb +32 -0
- data/lib/simplecov/combine/files_combiner.rb +24 -0
- data/lib/simplecov/combine/lines_combiner.rb +43 -0
- data/lib/simplecov/combine/results_combiner.rb +60 -0
- data/lib/simplecov/combine.rb +30 -0
- data/lib/simplecov/command_guesser.rb +6 -3
- data/lib/simplecov/configuration.rb +110 -9
- data/lib/simplecov/coverage_statistics.rb +56 -0
- data/lib/simplecov/defaults.rb +4 -2
- data/lib/simplecov/file_list.rb +66 -13
- data/lib/simplecov/filter.rb +2 -1
- data/lib/simplecov/formatter/multi_formatter.rb +2 -2
- data/lib/simplecov/formatter/simple_formatter.rb +4 -4
- data/lib/simplecov/last_run.rb +3 -1
- data/lib/simplecov/lines_classifier.rb +2 -2
- data/lib/simplecov/profiles.rb +9 -7
- data/lib/simplecov/result.rb +39 -6
- data/lib/simplecov/result_adapter.rb +30 -0
- data/lib/simplecov/result_merger.rb +18 -11
- data/lib/simplecov/simulate_coverage.rb +29 -0
- data/lib/simplecov/source_file/branch.rb +84 -0
- data/lib/simplecov/source_file/line.rb +72 -0
- data/lib/simplecov/source_file.rb +222 -125
- data/lib/simplecov/useless_results_remover.rb +16 -0
- data/lib/simplecov/version.rb +1 -1
- data/lib/simplecov.rb +227 -62
- metadata +32 -166
- data/lib/simplecov/jruby_fix.rb +0 -44
- data/lib/simplecov/railtie.rb +0 -9
- data/lib/simplecov/railties/tasks.rake +0 -13
- data/lib/simplecov/raw_coverage.rb +0 -41
data/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
SimpleCov [](https://badge.fury.io/rb/simplecov) [][Continuous Integration] [](https://codeclimate.com/github/colszowka/simplecov) [](http://inch-ci.org/github/colszowka/simplecov)
|
|
2
2
|
=========
|
|
3
|
+
|
|
3
4
|
**Code coverage for Ruby**
|
|
4
5
|
|
|
5
6
|
* [Source Code]
|
|
@@ -8,13 +9,13 @@ SimpleCov [][Conti
|
|
|
8
9
|
* [Rubygem]
|
|
9
10
|
* [Continuous Integration]
|
|
10
11
|
|
|
11
|
-
[Coverage]:
|
|
12
|
+
[Coverage]: https://ruby-doc.org/stdlib/libdoc/coverage/rdoc/Coverage.html "API doc for Ruby's Coverage library"
|
|
12
13
|
[Source Code]: https://github.com/colszowka/simplecov "Source Code @ GitHub"
|
|
13
14
|
[API documentation]: http://rubydoc.info/gems/simplecov/frames "RDoc API Documentation at Rubydoc.info"
|
|
14
15
|
[Configuration]: http://rubydoc.info/gems/simplecov/SimpleCov/Configuration "Configuration options API documentation"
|
|
15
16
|
[Changelog]: https://github.com/colszowka/simplecov/blob/master/CHANGELOG.md "Project Changelog"
|
|
16
17
|
[Rubygem]: http://rubygems.org/gems/simplecov "SimpleCov @ rubygems.org"
|
|
17
|
-
[Continuous Integration]:
|
|
18
|
+
[Continuous Integration]: https://github.com/colszowka/simplecov/actions?query=workflow%3Astable "SimpleCov is built around the clock by github.com"
|
|
18
19
|
[Dependencies]: https://gemnasium.com/colszowka/simplecov "SimpleCov dependencies on Gemnasium"
|
|
19
20
|
[simplecov-html]: https://github.com/colszowka/simplecov-html "SimpleCov HTML Formatter Source Code @ GitHub"
|
|
20
21
|
|
|
@@ -22,13 +23,14 @@ SimpleCov is a code coverage analysis tool for Ruby. It uses [Ruby's built-in Co
|
|
|
22
23
|
coverage data, but makes processing its results much easier by providing a clean API to filter, group, merge, format,
|
|
23
24
|
and display those results, giving you a complete code coverage suite that can be set up with just a couple lines of
|
|
24
25
|
code.
|
|
26
|
+
SimpleCov/Coverage track covered ruby code, gathering coverage for common templating solutions like erb, slim and haml is not supported.
|
|
25
27
|
|
|
26
28
|
In most cases, you'll want overall coverage results for your projects, including all types of tests, Cucumber features,
|
|
27
29
|
etc. SimpleCov automatically takes care of this by caching and merging results when generating reports, so your
|
|
28
30
|
report actually includes coverage across your test suites and thereby gives you a better picture of blank spots.
|
|
29
31
|
|
|
30
|
-
The official formatter of SimpleCov is packaged as a separate gem called [simplecov-html], but will be installed and
|
|
31
|
-
automatically when you launch SimpleCov. If you're curious, you can find it [on GitHub, too][simplecov-html].
|
|
32
|
+
The official formatter of SimpleCov is packaged as a separate gem called [simplecov-html], but will be installed and
|
|
33
|
+
configured automatically when you launch SimpleCov. If you're curious, you can find it [on GitHub, too][simplecov-html].
|
|
32
34
|
|
|
33
35
|
|
|
34
36
|
## Contact
|
|
@@ -36,11 +38,13 @@ automatically when you launch SimpleCov. If you're curious, you can find it [on
|
|
|
36
38
|
*Code and Bug Reports*
|
|
37
39
|
|
|
38
40
|
* [Issue Tracker](https://github.com/colszowka/simplecov/issues)
|
|
39
|
-
* See [CONTRIBUTING](https://github.com/colszowka/simplecov/blob/master/CONTRIBUTING.md) for how to contribute along
|
|
41
|
+
* See [CONTRIBUTING](https://github.com/colszowka/simplecov/blob/master/CONTRIBUTING.md) for how to contribute along
|
|
42
|
+
with some common problems to check out before creating an issue.
|
|
40
43
|
|
|
41
44
|
*Questions, Problems, Suggestions, etc.*
|
|
42
45
|
|
|
43
|
-
* [Mailing List](https://groups.google.com/forum/#!forum/simplecov) "Open mailing list for discussion and announcements
|
|
46
|
+
* [Mailing List](https://groups.google.com/forum/#!forum/simplecov) "Open mailing list for discussion and announcements
|
|
47
|
+
on Google Groups"
|
|
44
48
|
|
|
45
49
|
Getting started
|
|
46
50
|
---------------
|
|
@@ -70,8 +74,8 @@ Getting started
|
|
|
70
74
|
endpoint) via a separate test process (e.g. when using Selenium) where you
|
|
71
75
|
want to see all code executed by the `rails server`, and not just code
|
|
72
76
|
executed in your actual test files, you'll want to add something like this
|
|
73
|
-
to the top of `
|
|
74
|
-
|
|
77
|
+
to the top of `bin/rails`, but below the "shebang" line (`#! /usr/bin/env
|
|
78
|
+
ruby`):
|
|
75
79
|
|
|
76
80
|
```ruby
|
|
77
81
|
if ENV['RAILS_ENV'] == 'test'
|
|
@@ -81,27 +85,41 @@ Getting started
|
|
|
81
85
|
end
|
|
82
86
|
```
|
|
83
87
|
|
|
84
|
-
3. Run your
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
3. Run your full test suite to see the percent coverage that your application has.
|
|
89
|
+
4. After running your tests, open `coverage/index.html` in the browser of your choice. For example, in a Mac Terminal,
|
|
90
|
+
run the following command from your application's root directory:
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
open coverage/index.html
|
|
94
|
+
```
|
|
95
|
+
in a debian/ubuntu Terminal,
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
xdg-open coverage/index.html
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Note:** [This guide](https://dwheeler.com/essays/open-files-urls.html) can help if you're unsure which command your particular
|
|
102
|
+
operating system requires.
|
|
103
|
+
|
|
104
|
+
5. Add the following to your `.gitignore` file to ensure that coverage results
|
|
87
105
|
are not tracked by Git (optional):
|
|
88
106
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
107
|
+
```
|
|
108
|
+
echo "coverage" >> .gitignore
|
|
109
|
+
```
|
|
110
|
+
Or if you use Windows:
|
|
111
|
+
```
|
|
112
|
+
echo coverage >> .gitignore
|
|
113
|
+
```
|
|
96
114
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
115
|
+
If you're making a Rails application, SimpleCov comes with built-in configurations (see below for information on
|
|
116
|
+
profiles) that will get you started with groups for your Controllers, Models and Helpers. To use it, the
|
|
117
|
+
first two lines of your test_helper should be like this:
|
|
100
118
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
```ruby
|
|
120
|
+
require 'simplecov'
|
|
121
|
+
SimpleCov.start 'rails'
|
|
122
|
+
```
|
|
105
123
|
|
|
106
124
|
## Example output
|
|
107
125
|
|
|
@@ -127,8 +145,9 @@ require 'simplecov'
|
|
|
127
145
|
SimpleCov.start 'rails'
|
|
128
146
|
```
|
|
129
147
|
|
|
130
|
-
You could even track what kind of code your UI testers are touching if you want to go overboard with things. SimpleCov
|
|
131
|
-
care what kind of framework it is running in; it just looks at what code is being executed and generates a
|
|
148
|
+
You could even track what kind of code your UI testers are touching if you want to go overboard with things. SimpleCov
|
|
149
|
+
does not care what kind of framework it is running in; it just looks at what code is being executed and generates a
|
|
150
|
+
report about it.
|
|
132
151
|
|
|
133
152
|
### Notes on specific frameworks and test utilities
|
|
134
153
|
|
|
@@ -152,6 +171,17 @@ to use SimpleCov with them. Here's an overview of the known ones:
|
|
|
152
171
|
<a href="https://github.com/colszowka/simplecov/pull/185">#185</a>
|
|
153
172
|
</td>
|
|
154
173
|
</tr>
|
|
174
|
+
<tr>
|
|
175
|
+
<th>
|
|
176
|
+
knapsack_pro
|
|
177
|
+
</th>
|
|
178
|
+
<td>
|
|
179
|
+
To make SimpleCov work with Knapsack Pro Queue Mode to split tests in parallel on CI jobs you need to provide CI node index number to the <code>SimpleCov.command_name</code> in <code>KnapsackPro::Hooks::Queue.before_queue</code> hook.
|
|
180
|
+
</td>
|
|
181
|
+
<td>
|
|
182
|
+
<a href="https://knapsackpro.com/faq/question/how-to-use-simplecov-in-queue-mode">Tip</a>
|
|
183
|
+
</td>
|
|
184
|
+
</tr>
|
|
155
185
|
<tr>
|
|
156
186
|
<th>
|
|
157
187
|
RubyMine
|
|
@@ -223,7 +253,8 @@ to use SimpleCov with them. Here's an overview of the known ones:
|
|
|
223
253
|
```ruby
|
|
224
254
|
SimpleCov.some_config_option 'foo'
|
|
225
255
|
```
|
|
226
|
-
* If you do not want to start coverage immediately after launch or want to add additional configuration later on in a
|
|
256
|
+
* If you do not want to start coverage immediately after launch or want to add additional configuration later on in a
|
|
257
|
+
concise way, use:
|
|
227
258
|
|
|
228
259
|
```ruby
|
|
229
260
|
SimpleCov.configure do
|
|
@@ -235,11 +266,12 @@ Please check out the [Configuration] API documentation to find out what you can
|
|
|
235
266
|
|
|
236
267
|
## Using .simplecov for centralized config
|
|
237
268
|
|
|
238
|
-
If you use SimpleCov to merge multiple test suite results (e.g. Test/Unit and Cucumber) into a single report, you'd
|
|
239
|
-
set up all your config options twice, once in `test_helper.rb` and once in `env.rb`.
|
|
269
|
+
If you use SimpleCov to merge multiple test suite results (e.g. Test/Unit and Cucumber) into a single report, you'd
|
|
270
|
+
normally have to set up all your config options twice, once in `test_helper.rb` and once in `env.rb`.
|
|
240
271
|
|
|
241
|
-
To avoid this, you can place a file called `.simplecov` in your project root. You can then just leave the
|
|
242
|
-
test setup helper (**at the top**) and move the `SimpleCov.start` code with all your
|
|
272
|
+
To avoid this, you can place a file called `.simplecov` in your project root. You can then just leave the
|
|
273
|
+
`require 'simplecov'` in each test setup helper (**at the top**) and move the `SimpleCov.start` code with all your
|
|
274
|
+
custom config options into `.simplecov`:
|
|
243
275
|
|
|
244
276
|
```ruby
|
|
245
277
|
# test/test_helper.rb
|
|
@@ -254,21 +286,73 @@ SimpleCov.start 'rails' do
|
|
|
254
286
|
end
|
|
255
287
|
```
|
|
256
288
|
|
|
257
|
-
Using `.simplecov` rather than separately requiring SimpleCov multiple times is recommended if you are merging multiple
|
|
289
|
+
Using `.simplecov` rather than separately requiring SimpleCov multiple times is recommended if you are merging multiple
|
|
290
|
+
test frameworks like Cucumber and RSpec that rely on each other, as invoking SimpleCov multiple times can cause coverage
|
|
291
|
+
information to be lost.
|
|
292
|
+
|
|
293
|
+
## Branch coverage (ruby "~> 2.5")
|
|
294
|
+
Add branch coverage measurement statistics to your results. Supported in CRuby versions 2.5+.
|
|
295
|
+
|
|
296
|
+
```ruby
|
|
297
|
+
# or in configure or just SimpleCov.enable_coverage :branch
|
|
298
|
+
SimpleCov.start do
|
|
299
|
+
enable_coverage :branch
|
|
300
|
+
end
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Branch coverage is a feature introduced in Ruby 2.5 concerning itself with whether a
|
|
304
|
+
particular branch of a condition had been executed. Line coverage on the other hand
|
|
305
|
+
is only interested in whether a line of code has been executed.
|
|
306
|
+
|
|
307
|
+
This comes in handy for instance for one line conditionals:
|
|
308
|
+
|
|
309
|
+
```ruby
|
|
310
|
+
number.odd? ? "odd" : "even"
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
In line coverage this line would always be marked as executed but you'd never know if both
|
|
314
|
+
conditions were met. Guard clauses have a similar story:
|
|
315
|
+
|
|
316
|
+
```ruby
|
|
317
|
+
return if number.odd?
|
|
318
|
+
|
|
319
|
+
# more code
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
If all the code in that method was covered you'd never know if the guard clause was ever
|
|
323
|
+
triggered! With line coverage as just evaluating the condition marks it as covered.
|
|
324
|
+
|
|
325
|
+
In the HTML report the lines of code will be annotated like `branch_type: hit_count`:
|
|
326
|
+
|
|
327
|
+
* `then: 2` - the then branch (of an `if`) was executed twice
|
|
328
|
+
* `else: 0` - the else branch (of an `if` or `case`) was never executed
|
|
329
|
+
|
|
330
|
+
Not that even if you don't declare an `else` branch it will still show up in the coverage
|
|
331
|
+
reports meaning that the condition of the `if` was not hit or that no `when` of `case`
|
|
332
|
+
was hit during the test runs.
|
|
333
|
+
|
|
334
|
+
**Is branch coverage strictly better?** No. Branch coverage really only concerns itself with
|
|
335
|
+
conditionals - meaning coverage of sequential code is of no interest to it. A file without
|
|
336
|
+
conditional logic will have no branch coverage data and SimpleCov will report 0 of 0
|
|
337
|
+
branches covered as 100% (as everything that can be covered was covered).
|
|
338
|
+
|
|
339
|
+
Hence, we recommend looking at both metrics together. Branch coverage might also be a good
|
|
340
|
+
overall metric to look at - while you might be missing only 10% of your lines that might
|
|
341
|
+
account for 50% of your branches for instance.
|
|
258
342
|
|
|
259
343
|
## Filters
|
|
260
344
|
|
|
261
|
-
Filters can be used to remove selected files from your coverage data. By default, a filter is applied that removes all
|
|
262
|
-
OUTSIDE of your project's root directory - otherwise you'd end up with billions of coverage reports for source
|
|
263
|
-
gems you are using.
|
|
345
|
+
Filters can be used to remove selected files from your coverage data. By default, a filter is applied that removes all
|
|
346
|
+
files OUTSIDE of your project's root directory - otherwise you'd end up with billions of coverage reports for source
|
|
347
|
+
files in the gems you are using.
|
|
264
348
|
|
|
265
349
|
You can define your own to remove things like configuration files, tests or whatever you don't need in your coverage
|
|
266
350
|
report.
|
|
267
351
|
|
|
268
352
|
### Defining custom filters
|
|
269
353
|
|
|
270
|
-
You can currently define a filter using either a String or Regexp (that will then be Regexp-matched against each source
|
|
271
|
-
a block or by passing in your own Filter class.
|
|
354
|
+
You can currently define a filter using either a String or Regexp (that will then be Regexp-matched against each source
|
|
355
|
+
file's path), a block or by passing in your own Filter class.
|
|
272
356
|
|
|
273
357
|
#### String filter
|
|
274
358
|
|
|
@@ -300,9 +384,10 @@ SimpleCov.start do
|
|
|
300
384
|
end
|
|
301
385
|
```
|
|
302
386
|
|
|
303
|
-
Block filters receive a SimpleCov::SourceFile instance and expect your block to return either true (if the file is to be
|
|
304
|
-
from the result) or false (if the result should be kept). Please check out the RDoc for SimpleCov::SourceFile to
|
|
305
|
-
methods available to you. In the above example, the filter will remove all files that have less than 5
|
|
387
|
+
Block filters receive a SimpleCov::SourceFile instance and expect your block to return either true (if the file is to be
|
|
388
|
+
removed from the result) or false (if the result should be kept). Please check out the RDoc for SimpleCov::SourceFile to
|
|
389
|
+
learn about the methods available to you. In the above example, the filter will remove all files that have less than 5
|
|
390
|
+
lines of code.
|
|
306
391
|
|
|
307
392
|
#### Custom filter class
|
|
308
393
|
|
|
@@ -316,9 +401,10 @@ end
|
|
|
316
401
|
SimpleCov.add_filter LineFilter.new(5)
|
|
317
402
|
```
|
|
318
403
|
|
|
319
|
-
Defining your own filters is pretty easy: Just inherit from SimpleCov::Filter and define a method
|
|
320
|
-
the filter, a true return value from this method will result in the removal of the
|
|
321
|
-
is being set in the SimpleCov::Filter initialize method and thus is set to
|
|
404
|
+
Defining your own filters is pretty easy: Just inherit from SimpleCov::Filter and define a method
|
|
405
|
+
'matches?(source_file)'. When running the filter, a true return value from this method will result in the removal of the
|
|
406
|
+
given source_file. The filter_argument method is being set in the SimpleCov::Filter initialize method and thus is set to
|
|
407
|
+
5 in this example.
|
|
322
408
|
|
|
323
409
|
#### Array filter
|
|
324
410
|
|
|
@@ -345,7 +431,9 @@ end
|
|
|
345
431
|
|
|
346
432
|
The name of the token can be changed to your liking. [Learn more about the nocov feature.]( https://github.com/colszowka/simplecov/blob/master/features/config_nocov_token.feature)
|
|
347
433
|
|
|
348
|
-
**Note:** You shouldn't have to use the nocov token to skip private methods that are being included in your coverage. If
|
|
434
|
+
**Note:** You shouldn't have to use the nocov token to skip private methods that are being included in your coverage. If
|
|
435
|
+
you appropriately test the public interface of your classes and objects you should automatically get full coverage of
|
|
436
|
+
your private methods.
|
|
349
437
|
|
|
350
438
|
## Default root filter and coverage for things outside of it
|
|
351
439
|
|
|
@@ -368,8 +456,8 @@ end
|
|
|
368
456
|
|
|
369
457
|
You can separate your source files into groups. For example, in a Rails app, you'll want to have separate listings for
|
|
370
458
|
Models, Controllers, Helpers, and Libs. Group definition works similarly to Filters (and also accepts custom
|
|
371
|
-
filter classes), but source files end up in a group when the filter passes (returns true), as opposed to filtering
|
|
372
|
-
which exclude files from results when the filter results in a true value.
|
|
459
|
+
filter classes), but source files end up in a group when the filter passes (returns true), as opposed to filtering
|
|
460
|
+
results, which exclude files from results when the filter results in a true value.
|
|
373
461
|
|
|
374
462
|
Add your groups with:
|
|
375
463
|
|
|
@@ -389,12 +477,11 @@ end
|
|
|
389
477
|
|
|
390
478
|
You normally want to have your coverage analyzed across ALL of your test suites, right?
|
|
391
479
|
|
|
392
|
-
Simplecov automatically caches coverage results in your
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
There are two things to note here though:
|
|
480
|
+
Simplecov automatically caches coverage results in your
|
|
481
|
+
(coverage_path)/.resultset.json, and will merge or override those with
|
|
482
|
+
subsequent runs, depending on whether simplecov considers those subsequent runs
|
|
483
|
+
as different test suites or as the same test suite as the cached results. To
|
|
484
|
+
make this distinction, simplecov has the concept of "test suite names".
|
|
398
485
|
|
|
399
486
|
### Test suite names
|
|
400
487
|
|
|
@@ -448,24 +535,96 @@ SimpleCov.command_name "features" + (ENV['TEST_ENV_NUMBER'] || '')
|
|
|
448
535
|
|
|
449
536
|
[simplecov-html] prints the used test suites in the footer of the generated coverage report.
|
|
450
537
|
|
|
451
|
-
### Timeout for merge
|
|
452
538
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
539
|
+
### Merging test runs under the same execution environment
|
|
540
|
+
|
|
541
|
+
Test results are automatically merged with previous runs in the same execution
|
|
542
|
+
environment when generating the result, so when coverage is set up properly for
|
|
543
|
+
Cucumber and your unit / functional / integration tests, all of those test
|
|
544
|
+
suites will be taken into account when building the coverage report.
|
|
545
|
+
|
|
546
|
+
#### Timeout for merge
|
|
457
547
|
|
|
458
|
-
|
|
548
|
+
Of course, your cached coverage data is likely to become invalid at some point. Thus, when automatically merging
|
|
549
|
+
subsequent test runs, result sets that are older than `SimpleCov.merge_timeout` will not be used any more. By default,
|
|
550
|
+
the timeout is 600 seconds (10 minutes), and you can raise (or lower) it by specifying `SimpleCov.merge_timeout 3600`
|
|
551
|
+
(1 hour), or, inside a configure/start block, with just `merge_timeout 3600`.
|
|
552
|
+
|
|
553
|
+
You can deactivate this automatic merging altogether with `SimpleCov.use_merging false`.
|
|
554
|
+
|
|
555
|
+
### Merging test runs under different execution environments
|
|
556
|
+
|
|
557
|
+
If your tests are done in parallel across multiple build machines, you can fetch them all and merge them into a single
|
|
558
|
+
result set using the `SimpleCov.collate` method. This can be added to a Rakefile or script file, having downloaded a set of
|
|
559
|
+
`.resultset.json` files from each parallel test run.
|
|
560
|
+
|
|
561
|
+
```ruby
|
|
562
|
+
# lib/tasks/coverage_report.rake
|
|
563
|
+
namespace :coverage do
|
|
564
|
+
desc "Collates all result sets generated by the different test runners"
|
|
565
|
+
task :report do
|
|
566
|
+
require 'simplecov'
|
|
567
|
+
|
|
568
|
+
SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"]
|
|
569
|
+
end
|
|
570
|
+
end
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
`SimpleCov.collate` also takes an optional simplecov profile and an optional
|
|
574
|
+
block for configuration, just the same as `SimpleCov.start` or
|
|
575
|
+
`SimpleCov.configure`. This means you can configure a separate formatter for
|
|
576
|
+
the collated output. For instance, you can make the formatter in
|
|
577
|
+
`SimpleCov.start` the `SimpleCov::Formatter::SimpleFormatter`, and only use more
|
|
578
|
+
complex formatters in the final `SimpleCov.collate` run.
|
|
579
|
+
|
|
580
|
+
```ruby
|
|
581
|
+
# spec/spec_helper.rb
|
|
582
|
+
require 'simplecov'
|
|
583
|
+
|
|
584
|
+
SimpleCov.start 'rails' do
|
|
585
|
+
# Disambiguates individual test runs
|
|
586
|
+
command_name "Job #{ENV["TEST_ENV_NUMBER"]}" if ENV["TEST_ENV_NUMBER"]
|
|
587
|
+
|
|
588
|
+
if ENV['CI']
|
|
589
|
+
formatter SimpleCov::Formatter::SimpleFormatter
|
|
590
|
+
else
|
|
591
|
+
formatter SimpleCov::Formatter::MultiFormatter.new([
|
|
592
|
+
SimpleCov::Formatter::SimpleFormatter,
|
|
593
|
+
SimpleCov::Formatter::HTMLFormatter
|
|
594
|
+
])
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
track_files "**/*.rb"
|
|
598
|
+
end
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
```ruby
|
|
602
|
+
# lib/tasks/coverage_report.rake
|
|
603
|
+
namespace :coverage do
|
|
604
|
+
task :report do
|
|
605
|
+
require 'simplecov'
|
|
606
|
+
|
|
607
|
+
SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"], 'rails' do
|
|
608
|
+
formatter SimpleCov::Formatter::MultiFormatter.new([
|
|
609
|
+
SimpleCov::Formatter::SimpleFormatter,
|
|
610
|
+
SimpleCov::Formatter::HTMLFormatter
|
|
611
|
+
])
|
|
612
|
+
end
|
|
613
|
+
end
|
|
614
|
+
end
|
|
615
|
+
```
|
|
459
616
|
|
|
460
617
|
## Running coverage only on demand
|
|
461
618
|
|
|
462
|
-
The Ruby STDLIB Coverage library that SimpleCov builds upon is *very* fast (on a ~10 min Rails test suite, the speed
|
|
463
|
-
only a couple seconds for me), and therefore it's SimpleCov's policy to just generate coverage every time you
|
|
464
|
-
it doesn't do your test speed any harm and you're always equipped with the latest and greatest
|
|
619
|
+
The Ruby STDLIB Coverage library that SimpleCov builds upon is *very* fast (on a ~10 min Rails test suite, the speed
|
|
620
|
+
drop was only a couple seconds for me), and therefore it's SimpleCov's policy to just generate coverage every time you
|
|
621
|
+
run your tests because it doesn't do your test speed any harm and you're always equipped with the latest and greatest
|
|
622
|
+
coverage results.
|
|
465
623
|
|
|
466
624
|
Because of this, SimpleCov has no explicit built-in mechanism to run coverage only on demand.
|
|
467
625
|
|
|
468
|
-
However, you can still accomplish this very easily by introducing an ENV variable conditional into your SimpleCov setup
|
|
626
|
+
However, you can still accomplish this very easily by introducing an ENV variable conditional into your SimpleCov setup
|
|
627
|
+
block, like this:
|
|
469
628
|
|
|
470
629
|
```ruby
|
|
471
630
|
SimpleCov.start if ENV["COVERAGE"]
|
|
@@ -477,6 +636,21 @@ Then, SimpleCov will only run if you execute your tests like this:
|
|
|
477
636
|
COVERAGE=true rake test
|
|
478
637
|
```
|
|
479
638
|
|
|
639
|
+
## Errors and exit statuses
|
|
640
|
+
|
|
641
|
+
To aid in debugging issues, if an error is raised, SimpleCov will print a message to `STDERR`
|
|
642
|
+
with the exit status of the error, like:
|
|
643
|
+
|
|
644
|
+
```
|
|
645
|
+
SimpleCov failed with exit 1
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
This `STDERR` message can be disabled with:
|
|
649
|
+
|
|
650
|
+
```
|
|
651
|
+
SimpleCov.print_error_status = false
|
|
652
|
+
```
|
|
653
|
+
|
|
480
654
|
## Profiles
|
|
481
655
|
|
|
482
656
|
By default, SimpleCov's only config assumption is that you only want coverage reports for files inside your project
|
|
@@ -514,8 +688,8 @@ end
|
|
|
514
688
|
|
|
515
689
|
### Custom profiles
|
|
516
690
|
|
|
517
|
-
You can load additional profiles with the SimpleCov.load_profile('xyz') method. This allows you to build upon an
|
|
518
|
-
profile and customize it so you can reuse it in unit tests and Cucumber features. For example:
|
|
691
|
+
You can load additional profiles with the SimpleCov.load_profile('xyz') method. This allows you to build upon an
|
|
692
|
+
existing profile and customize it so you can reuse it in unit tests and Cucumber features. For example:
|
|
519
693
|
|
|
520
694
|
```ruby
|
|
521
695
|
# lib/simplecov_custom_profile.rb
|
|
@@ -556,7 +730,8 @@ SimpleCov.minimum_coverage 90
|
|
|
556
730
|
|
|
557
731
|
### Minimum coverage by file
|
|
558
732
|
|
|
559
|
-
You can define the minimum coverage by file percentage expected. SimpleCov will return non-zero if unmet. This is useful
|
|
733
|
+
You can define the minimum coverage by file percentage expected. SimpleCov will return non-zero if unmet. This is useful
|
|
734
|
+
to help ensure coverage is relatively consistent, rather than being skewed by particularly good or bad areas of the code.
|
|
560
735
|
|
|
561
736
|
```ruby
|
|
562
737
|
SimpleCov.minimum_coverage_by_file 80
|
|
@@ -586,8 +761,8 @@ You can use your own formatter with:
|
|
|
586
761
|
SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter
|
|
587
762
|
```
|
|
588
763
|
|
|
589
|
-
When calling SimpleCov.result.format!, it will be invoked with SimpleCov::Formatter::YourFormatter.new.format(result),
|
|
590
|
-
being an instance of SimpleCov::Result. Do whatever your wish with that!
|
|
764
|
+
When calling SimpleCov.result.format!, it will be invoked with SimpleCov::Formatter::YourFormatter.new.format(result),
|
|
765
|
+
"result" being an instance of SimpleCov::Result. Do whatever your wish with that!
|
|
591
766
|
|
|
592
767
|
|
|
593
768
|
## Using multiple formatters
|
|
@@ -609,11 +784,9 @@ SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
|
|
|
609
784
|
|
|
610
785
|
## Ruby version compatibility
|
|
611
786
|
|
|
612
|
-
|
|
613
|
-
SimpleCov is also built against Ruby 1.8 in [Continuous Integration], but this happens only to ensure that SimpleCov
|
|
614
|
-
does not make your test suite crash right now.
|
|
787
|
+
SimpleCov is built in [Continuous Integration] on Ruby 2.4+ as well as JRuby 9.2+.
|
|
615
788
|
|
|
616
|
-
|
|
789
|
+
Note for JRuby => You need to pass JRUBY_OPTS="--debug" or create .jrubyrc and add debug.fullTrace=true
|
|
617
790
|
|
|
618
791
|
## Want to find dead code in production?
|
|
619
792
|
|
|
@@ -621,7 +794,9 @@ Try [Coverband](https://github.com/danmayer/coverband).
|
|
|
621
794
|
|
|
622
795
|
## Want to use Spring with SimpleCov?
|
|
623
796
|
|
|
624
|
-
If you're using [Spring](https://github.com/rails/spring) to speed up test suite runs and want to run SimpleCov along
|
|
797
|
+
If you're using [Spring](https://github.com/rails/spring) to speed up test suite runs and want to run SimpleCov along
|
|
798
|
+
with them, you'll find that it often misreports coverage with the default config due to some sort of eager loading
|
|
799
|
+
issue. Don't despair!
|
|
625
800
|
|
|
626
801
|
One solution is to [explicitly call eager
|
|
627
802
|
load](https://github.com/colszowka/simplecov/issues/381#issuecomment-347651728)
|
|
@@ -633,13 +808,22 @@ SimpleCov.start 'rails'
|
|
|
633
808
|
Rails.application.eager_load!
|
|
634
809
|
```
|
|
635
810
|
|
|
811
|
+
Alternatively, you could disable Spring while running SimpleCov:
|
|
812
|
+
|
|
813
|
+
```
|
|
814
|
+
DISABLE_SPRING=1 rake test
|
|
815
|
+
```
|
|
816
|
+
|
|
636
817
|
Or you could remove `gem 'spring'` from your `Gemfile`.
|
|
637
818
|
|
|
638
819
|
## Troubleshooting
|
|
639
820
|
|
|
640
|
-
The **most common problem is that simplecov isn't required and started before everything else**. In order to track
|
|
821
|
+
The **most common problem is that simplecov isn't required and started before everything else**. In order to track
|
|
822
|
+
coverage for your whole application **simplecov needs to be the first one** so that it (and the underlying coverage
|
|
823
|
+
library) can subsequently track loaded files and their usage.
|
|
641
824
|
|
|
642
|
-
If you are missing coverage for some code a simple trick is to put a puts statement in there and right after
|
|
825
|
+
If you are missing coverage for some code a simple trick is to put a puts statement in there and right after
|
|
826
|
+
`SimpleCov.start` so you can see if the file really was loaded after simplecov was started.
|
|
643
827
|
|
|
644
828
|
```ruby
|
|
645
829
|
# my_code.rb
|
|
@@ -667,6 +851,11 @@ MyCode is being loaded!
|
|
|
667
851
|
|
|
668
852
|
then it's good otherwise you likely have a problem :)
|
|
669
853
|
|
|
854
|
+
## Code of Conduct
|
|
855
|
+
|
|
856
|
+
Everyone participating in this project's development, issue trackers and other channels is expected to follow our
|
|
857
|
+
[Code of Conduct](./CODE_OF_CONDUCT.md)
|
|
858
|
+
|
|
670
859
|
## Contributing
|
|
671
860
|
|
|
672
861
|
See the [contributing guide](https://github.com/colszowka/simplecov/blob/master/CONTRIBUTING.md).
|
data/doc/alternate-formatters.md
CHANGED
|
@@ -54,3 +54,8 @@ A formatter that prints the coverage of the file under test when you run a singl
|
|
|
54
54
|
*by [Yosuke Kabuto](https://github.com/ysksn)*
|
|
55
55
|
|
|
56
56
|
t_wada AA formatter for SimpleCov
|
|
57
|
+
|
|
58
|
+
#### [simplecov-material(https://github.com/chiefpansancolt/simplecov-material)
|
|
59
|
+
*by [Chiefpansancolt](https://github.com/chiefpansancolt)*
|
|
60
|
+
|
|
61
|
+
A Material Designed HTML formatter with clean and easy search of files with a tabular left Navigation.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleCov
|
|
4
|
+
module Combine
|
|
5
|
+
#
|
|
6
|
+
# Combine different branch coverage results on single file.
|
|
7
|
+
#
|
|
8
|
+
# Should be called through `SimpleCov.combine`.
|
|
9
|
+
module BranchesCombiner
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
# Return merged branches or the existed branche if other is missing.
|
|
14
|
+
#
|
|
15
|
+
# Branches inside files are always same if they exists, the difference only in coverage count.
|
|
16
|
+
# Branch coverage report for any conditional case is built from hash, it's key is a condition and
|
|
17
|
+
# it's body is a hash << keys from condition and value is coverage rate >>.
|
|
18
|
+
# ex: branches =>{ [:if, 3, 8, 6, 8, 36] => {[:then, 4, 8, 6, 8, 12] => 1, [:else, 5, 8, 6, 8, 36]=>2}, other conditions...}
|
|
19
|
+
# We create copy of result and update it values depending on the combined branches coverage values.
|
|
20
|
+
#
|
|
21
|
+
# @return [Hash]
|
|
22
|
+
#
|
|
23
|
+
def combine(coverage_a, coverage_b)
|
|
24
|
+
coverage_a.merge(coverage_b) do |_condition, branches_inside_a, branches_inside_b|
|
|
25
|
+
branches_inside_a.merge(branches_inside_b) do |_branch, a_count, b_count|
|
|
26
|
+
a_count + b_count
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleCov
|
|
4
|
+
module Combine
|
|
5
|
+
#
|
|
6
|
+
# Handle combining two coverage results for same file
|
|
7
|
+
#
|
|
8
|
+
# Should be called through `SimpleCov.combine`.
|
|
9
|
+
module FilesCombiner
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
# Combines the results for 2 coverages of a file.
|
|
14
|
+
#
|
|
15
|
+
# @return [Hash]
|
|
16
|
+
#
|
|
17
|
+
def combine(coverage_a, coverage_b)
|
|
18
|
+
combination = {"lines" => Combine.combine(LinesCombiner, coverage_a["lines"], coverage_b["lines"])}
|
|
19
|
+
combination["branches"] = Combine.combine(BranchesCombiner, coverage_a["branches"], coverage_b["branches"]) if SimpleCov.branch_coverage?
|
|
20
|
+
combination
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleCov
|
|
4
|
+
module Combine
|
|
5
|
+
#
|
|
6
|
+
# Combine two different lines coverage results on same file
|
|
7
|
+
#
|
|
8
|
+
# Should be called through `SimpleCov.combine`.
|
|
9
|
+
module LinesCombiner
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def combine(coverage_a, coverage_b)
|
|
13
|
+
coverage_a
|
|
14
|
+
.zip(coverage_b)
|
|
15
|
+
.map do |coverage_a_val, coverage_b_val|
|
|
16
|
+
merge_line_coverage(coverage_a_val, coverage_b_val)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Return depends on coverage in a specific line
|
|
21
|
+
#
|
|
22
|
+
# @param [Integer || nil] first_val
|
|
23
|
+
# @param [Integer || nil] second_val
|
|
24
|
+
#
|
|
25
|
+
# Logic:
|
|
26
|
+
#
|
|
27
|
+
# => nil + 0 = nil
|
|
28
|
+
# => nil + nil = nil
|
|
29
|
+
# => int + int = int
|
|
30
|
+
#
|
|
31
|
+
# @return [Integer || nil]
|
|
32
|
+
def merge_line_coverage(first_val, second_val)
|
|
33
|
+
sum = first_val.to_i + second_val.to_i
|
|
34
|
+
|
|
35
|
+
if sum.zero? && (first_val.nil? || second_val.nil?)
|
|
36
|
+
nil
|
|
37
|
+
else
|
|
38
|
+
sum
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|