simplecov 0.17.1 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -427
  3. data/README.md +388 -94
  4. data/doc/alternate-formatters.md +16 -1
  5. data/doc/commercial-services.md +5 -0
  6. data/lib/minitest/simplecov_plugin.rb +15 -0
  7. data/lib/simplecov/combine/branches_combiner.rb +32 -0
  8. data/lib/simplecov/combine/files_combiner.rb +24 -0
  9. data/lib/simplecov/combine/lines_combiner.rb +43 -0
  10. data/lib/simplecov/combine/results_combiner.rb +60 -0
  11. data/lib/simplecov/combine.rb +30 -0
  12. data/lib/simplecov/command_guesser.rb +6 -3
  13. data/lib/simplecov/configuration.rb +210 -15
  14. data/lib/simplecov/coverage_statistics.rb +56 -0
  15. data/lib/simplecov/default_formatter.rb +20 -0
  16. data/lib/simplecov/defaults.rb +14 -13
  17. data/lib/simplecov/exit_codes/exit_code_handling.rb +29 -0
  18. data/lib/simplecov/exit_codes/maximum_coverage_drop_check.rb +83 -0
  19. data/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb +54 -0
  20. data/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb +53 -0
  21. data/lib/simplecov/exit_codes.rb +5 -0
  22. data/lib/simplecov/file_list.rb +72 -13
  23. data/lib/simplecov/filter.rb +9 -6
  24. data/lib/simplecov/formatter/multi_formatter.rb +5 -7
  25. data/lib/simplecov/formatter/simple_formatter.rb +4 -4
  26. data/lib/simplecov/formatter.rb +2 -2
  27. data/lib/simplecov/last_run.rb +3 -1
  28. data/lib/simplecov/lines_classifier.rb +5 -5
  29. data/lib/simplecov/no_defaults.rb +1 -1
  30. data/lib/simplecov/process.rb +19 -0
  31. data/lib/simplecov/profiles.rb +9 -7
  32. data/lib/simplecov/result.rb +18 -12
  33. data/lib/simplecov/result_adapter.rb +30 -0
  34. data/lib/simplecov/result_merger.rb +130 -59
  35. data/lib/simplecov/simulate_coverage.rb +29 -0
  36. data/lib/simplecov/source_file/branch.rb +84 -0
  37. data/lib/simplecov/source_file/line.rb +72 -0
  38. data/lib/simplecov/source_file.rb +279 -127
  39. data/lib/simplecov/useless_results_remover.rb +18 -0
  40. data/lib/simplecov/version.rb +1 -1
  41. data/lib/simplecov.rb +296 -128
  42. metadata +47 -161
  43. data/CONTRIBUTING.md +0 -51
  44. data/ISSUE_TEMPLATE.md +0 -23
  45. data/lib/simplecov/jruby_fix.rb +0 -44
  46. data/lib/simplecov/railtie.rb +0 -9
  47. data/lib/simplecov/railties/tasks.rake +0 -13
  48. data/lib/simplecov/raw_coverage.rb +0 -41
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
- SimpleCov [![Build Status](https://travis-ci.org/colszowka/simplecov.svg)][Continuous Integration] [![Code Climate](https://codeclimate.com/github/colszowka/simplecov.svg)](https://codeclimate.com/github/colszowka/simplecov) [![Inline docs](http://inch-ci.org/github/colszowka/simplecov.svg)](http://inch-ci.org/github/colszowka/simplecov)
1
+ SimpleCov [![Gem Version](https://badge.fury.io/rb/simplecov.svg)](https://badge.fury.io/rb/simplecov) [![Build Status](https://github.com/simplecov-ruby/simplecov/workflows/stable/badge.svg?branch=main)][Continuous Integration] [![Maintainability](https://api.codeclimate.com/v1/badges/c071d197d61953a7e482/maintainability)](https://codeclimate.com/github/simplecov-ruby/simplecov/maintainability) [![Inline docs](http://inch-ci.org/github/simplecov-ruby/simplecov.svg?branch=main)](http://inch-ci.org/github/simplecov-ruby/simplecov)
2
2
  =========
3
+
3
4
  **Code coverage for Ruby**
4
5
 
5
6
  * [Source Code]
@@ -8,39 +9,42 @@ SimpleCov [![Build Status](https://travis-ci.org/colszowka/simplecov.svg)][Conti
8
9
  * [Rubygem]
9
10
  * [Continuous Integration]
10
11
 
11
- [Coverage]: http://www.ruby-doc.org/stdlib-2.1.0/libdoc/coverage/rdoc/Coverage.html "API doc for Ruby's Coverage library"
12
- [Source Code]: https://github.com/colszowka/simplecov "Source Code @ GitHub"
12
+ [Coverage]: https://ruby-doc.org/stdlib/libdoc/coverage/rdoc/Coverage.html "API doc for Ruby's Coverage library"
13
+ [Source Code]: https://github.com/simplecov-ruby/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
- [Changelog]: https://github.com/colszowka/simplecov/blob/master/CHANGELOG.md "Project Changelog"
16
+ [Changelog]: https://github.com/simplecov-ruby/simplecov/blob/main/CHANGELOG.md "Project Changelog"
16
17
  [Rubygem]: http://rubygems.org/gems/simplecov "SimpleCov @ rubygems.org"
17
- [Continuous Integration]: http://travis-ci.org/colszowka/simplecov "SimpleCov is built around the clock by travis-ci.org"
18
- [Dependencies]: https://gemnasium.com/colszowka/simplecov "SimpleCov dependencies on Gemnasium"
19
- [simplecov-html]: https://github.com/colszowka/simplecov-html "SimpleCov HTML Formatter Source Code @ GitHub"
18
+ [Continuous Integration]: https://github.com/simplecov-ruby/simplecov/actions?query=workflow%3Astable "SimpleCov is built around the clock by github.com"
19
+ [Dependencies]: https://gemnasium.com/simplecov-ruby/simplecov "SimpleCov dependencies on Gemnasium"
20
+ [simplecov-html]: https://github.com/simplecov-ruby/simplecov-html "SimpleCov HTML Formatter Source Code @ GitHub"
20
21
 
21
22
  SimpleCov is a code coverage analysis tool for Ruby. It uses [Ruby's built-in Coverage][Coverage] library to gather code
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 configured
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
35
37
 
36
38
  *Code and Bug Reports*
37
39
 
38
- * [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 with some common problems to check out before creating an issue.
40
+ * [Issue Tracker](https://github.com/simplecov-ruby/simplecov/issues)
41
+ * See [CONTRIBUTING](https://github.com/simplecov-ruby/simplecov/blob/main/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 on Google Groups"
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
  ---------------
@@ -65,13 +69,16 @@ Getting started
65
69
  The `SimpleCov.start` **must** be issued **before any of your application
66
70
  code is required!**
67
71
 
72
+ This is especially true if you use anything that keeps your tests application loaded like spring, check out the **[spring section](#want-to-use-spring-with-simplecov)**.
73
+
68
74
  SimpleCov must be running in the process that you want the code coverage
69
75
  analysis to happen on. When testing a server process (e.g. a JSON API
70
76
  endpoint) via a separate test process (e.g. when using Selenium) where you
71
77
  want to see all code executed by the `rails server`, and not just code
72
- executed in your actual test files, you'll want to add something like this
73
- to the top of `script/rails` (or `bin/rails` for Rails 4), but below the
74
- "shebang" line (`#! /usr/bin/env ruby`):
78
+ executed in your actual test files, you need to require SimpleCov in the
79
+ server process. For rails for instance, you'll want to add something like this
80
+ to the top of `bin/rails`, but below the "shebang" line (`#! /usr/bin/env
81
+ ruby`) and after config/boot is required:
75
82
 
76
83
  ```ruby
77
84
  if ENV['RAILS_ENV'] == 'test'
@@ -81,27 +88,37 @@ Getting started
81
88
  end
82
89
  ```
83
90
 
84
- 3. Run your tests, open up `coverage/index.html` in your browser and check out
85
- what you've missed so far.
86
- 4. Add the following to your `.gitignore` file to ensure that coverage results
91
+ 3. Run your full test suite to see the percent coverage that your application has.
92
+ 4. After running your tests, open `coverage/index.html` in the browser of your choice. For example, in a Mac Terminal,
93
+ run the following command from your application's root directory:
94
+
95
+ ```
96
+ open coverage/index.html
97
+ ```
98
+ in a debian/ubuntu Terminal,
99
+
100
+ ```
101
+ xdg-open coverage/index.html
102
+ ```
103
+
104
+ **Note:** [This guide](https://dwheeler.com/essays/open-files-urls.html) can help if you're unsure which command your particular
105
+ operating system requires.
106
+
107
+ 5. Add the following to your `.gitignore` file to ensure that coverage results
87
108
  are not tracked by Git (optional):
88
109
 
89
- ```
90
- echo "coverage" >> .gitignore
91
- ```
92
- Or if you use Windows:
93
- ```
94
- echo coverage >> .gitignore
95
- ```
110
+ ```
111
+ echo coverage >> .gitignore
112
+ ```
96
113
 
97
- If you're making a Rails application, SimpleCov comes with built-in configurations (see below for information on profiles)
98
- that will get you started with groups for your Controllers, Views, Models and Helpers. To use it, the first two lines of
99
- your test_helper should be like this:
114
+ If you're making a Rails application, SimpleCov comes with built-in configurations (see below for information on
115
+ profiles) that will get you started with groups for your Controllers, Models and Helpers. To use it, the
116
+ first two lines of your test_helper should be like this:
100
117
 
101
- ```ruby
102
- require 'simplecov'
103
- SimpleCov.start 'rails'
104
- ```
118
+ ```ruby
119
+ require 'simplecov'
120
+ SimpleCov.start 'rails'
121
+ ```
105
122
 
106
123
  ## Example output
107
124
 
@@ -127,8 +144,9 @@ require 'simplecov'
127
144
  SimpleCov.start 'rails'
128
145
  ```
129
146
 
130
- You could even track what kind of code your UI testers are touching if you want to go overboard with things. SimpleCov does not
131
- care what kind of framework it is running in; it just looks at what code is being executed and generates a report about it.
147
+ You could even track what kind of code your UI testers are touching if you want to go overboard with things. SimpleCov
148
+ does not care what kind of framework it is running in; it just looks at what code is being executed and generates a
149
+ report about it.
132
150
 
133
151
  ### Notes on specific frameworks and test utilities
134
152
 
@@ -148,8 +166,19 @@ to use SimpleCov with them. Here's an overview of the known ones:
148
166
  race conditions occur when results are merged.
149
167
  </td>
150
168
  <td>
151
- <a href="https://github.com/colszowka/simplecov/issues/64">#64</a> &amp;
152
- <a href="https://github.com/colszowka/simplecov/pull/185">#185</a>
169
+ <a href="https://github.com/simplecov-ruby/simplecov/issues/64">#64</a> &amp;
170
+ <a href="https://github.com/simplecov-ruby/simplecov/pull/185">#185</a>
171
+ </td>
172
+ </tr>
173
+ <tr>
174
+ <th>
175
+ knapsack_pro
176
+ </th>
177
+ <td>
178
+ 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.
179
+ </td>
180
+ <td>
181
+ <a href="https://knapsackpro.com/faq/question/how-to-use-simplecov-in-queue-mode">Tip</a>
153
182
  </td>
154
183
  </tr>
155
184
  <tr>
@@ -162,7 +191,7 @@ to use SimpleCov with them. Here's an overview of the known ones:
162
191
  to explicitly set the output root using `SimpleCov.root('foo/bar/baz')`
163
192
  </td>
164
193
  <td>
165
- <a href="https://github.com/colszowka/simplecov/issues/95">#95</a>
194
+ <a href="https://github.com/simplecov-ruby/simplecov/issues/95">#95</a>
166
195
  </td>
167
196
  </tr>
168
197
  <tr>
@@ -173,11 +202,11 @@ to use SimpleCov with them. Here's an overview of the known ones:
173
202
  Because of how Spork works internally (using preforking), there used to
174
203
  be trouble when using SimpleCov with it, but that has apparently been
175
204
  resolved with a specific configuration strategy. See <a
176
- href="https://github.com/colszowka/simplecov/issues/42#issuecomment-4440284">this</a>
205
+ href="https://github.com/simplecov-ruby/simplecov/issues/42#issuecomment-4440284">this</a>
177
206
  comment.
178
207
  </td>
179
208
  <td>
180
- <a href="https://github.com/colszowka/simplecov/issues/42#issuecomment-4440284">#42</a>
209
+ <a href="https://github.com/simplecov-ruby/simplecov/issues/42#issuecomment-4440284">#42</a>
181
210
  </td>
182
211
  </tr>
183
212
  <tr>
@@ -188,7 +217,7 @@ to use SimpleCov with them. Here's an overview of the known ones:
188
217
  <a href="#want-to-use-spring-with-simplecov">See section below.</a>
189
218
  </td>
190
219
  <td>
191
- <a href="https://github.com/colszowka/simplecov/issues/381">#381</a>
220
+ <a href="https://github.com/simplecov-ruby/simplecov/issues/381">#381</a>
192
221
  </td>
193
222
  </tr>
194
223
  <tr>
@@ -201,7 +230,7 @@ to use SimpleCov with them. Here's an overview of the known ones:
201
230
  (Dec 11th, 2011) should have this problem resolved.
202
231
  </td>
203
232
  <td>
204
- <a href="https://github.com/colszowka/simplecov/issues/45">#45</a> &amp;
233
+ <a href="https://github.com/simplecov-ruby/simplecov/issues/45">#45</a> &amp;
205
234
  <a href="https://github.com/test-unit/test-unit/pull/12">test-unit/test-unit#12</a>
206
235
  </td>
207
236
  </tr>
@@ -223,7 +252,8 @@ to use SimpleCov with them. Here's an overview of the known ones:
223
252
  ```ruby
224
253
  SimpleCov.some_config_option 'foo'
225
254
  ```
226
- * If you do not want to start coverage immediately after launch or want to add additional configuration later on in a concise way, use:
255
+ * If you do not want to start coverage immediately after launch or want to add additional configuration later on in a
256
+ concise way, use:
227
257
 
228
258
  ```ruby
229
259
  SimpleCov.configure do
@@ -235,11 +265,12 @@ Please check out the [Configuration] API documentation to find out what you can
235
265
 
236
266
  ## Using .simplecov for centralized config
237
267
 
238
- If you use SimpleCov to merge multiple test suite results (e.g. Test/Unit and Cucumber) into a single report, you'd normally have to
239
- set up all your config options twice, once in `test_helper.rb` and once in `env.rb`.
268
+ If you use SimpleCov to merge multiple test suite results (e.g. Test/Unit and Cucumber) into a single report, you'd
269
+ normally have to set up all your config options twice, once in `test_helper.rb` and once in `env.rb`.
240
270
 
241
- To avoid this, you can place a file called `.simplecov` in your project root. You can then just leave the `require 'simplecov'` in each
242
- test setup helper (**at the top**) and move the `SimpleCov.start` code with all your custom config options into `.simplecov`:
271
+ To avoid this, you can place a file called `.simplecov` in your project root. You can then just leave the
272
+ `require 'simplecov'` in each test setup helper (**at the top**) and move the `SimpleCov.start` code with all your
273
+ custom config options into `.simplecov`:
243
274
 
244
275
  ```ruby
245
276
  # test/test_helper.rb
@@ -254,21 +285,100 @@ SimpleCov.start 'rails' do
254
285
  end
255
286
  ```
256
287
 
257
- Using `.simplecov` rather than separately requiring SimpleCov multiple times is recommended if you are merging multiple test frameworks like Cucumber and RSpec that rely on each other, as invoking SimpleCov multiple times can cause coverage information to be lost.
288
+ Using `.simplecov` rather than separately requiring SimpleCov multiple times is recommended if you are merging multiple
289
+ test frameworks like Cucumber and RSpec that rely on each other, as invoking SimpleCov multiple times can cause coverage
290
+ information to be lost.
291
+
292
+ ## Branch coverage (ruby "~> 2.5")
293
+ Add branch coverage measurement statistics to your results. Supported in CRuby versions 2.5+.
294
+
295
+ ```ruby
296
+ SimpleCov.start do
297
+ enable_coverage :branch
298
+ end
299
+ ```
300
+
301
+ Branch coverage is a feature introduced in Ruby 2.5 concerning itself with whether a
302
+ particular branch of a condition had been executed. Line coverage on the other hand
303
+ is only interested in whether a line of code has been executed.
304
+
305
+ This comes in handy for instance for one line conditionals:
306
+
307
+ ```ruby
308
+ number.odd? ? "odd" : "even"
309
+ ```
310
+
311
+ In line coverage this line would always be marked as executed but you'd never know if both
312
+ conditions were met. Guard clauses have a similar story:
313
+
314
+ ```ruby
315
+ return if number.odd?
316
+
317
+ # more code
318
+ ```
319
+
320
+ If all the code in that method was covered you'd never know if the guard clause was ever
321
+ triggered! With line coverage as just evaluating the condition marks it as covered.
322
+
323
+ In the HTML report the lines of code will be annotated like `branch_type: hit_count`:
324
+
325
+ * `then: 2` - the then branch (of an `if`) was executed twice
326
+ * `else: 0` - the else branch (of an `if` or `case`) was never executed
327
+
328
+ Not that even if you don't declare an `else` branch it will still show up in the coverage
329
+ reports meaning that the condition of the `if` was not hit or that no `when` of `case`
330
+ was hit during the test runs.
331
+
332
+ **Is branch coverage strictly better?** No. Branch coverage really only concerns itself with
333
+ conditionals - meaning coverage of sequential code is of no interest to it. A file without
334
+ conditional logic will have no branch coverage data and SimpleCov will report 0 of 0
335
+ branches covered as 100% (as everything that can be covered was covered).
336
+
337
+ Hence, we recommend looking at both metrics together. Branch coverage might also be a good
338
+ overall metric to look at - while you might be missing only 10% of your lines that might
339
+ account for 50% of your branches for instance.
340
+
341
+ ## Primary Coverage
342
+
343
+ By default, the primary coverage type is `line`. To set the primary coverage to something else, use the following:
344
+
345
+ ```ruby
346
+ # or in configure SimpleCov.primary_coverage :branch
347
+ SimpleCov.start do
348
+ enable_coverage :branch
349
+ primary_coverage :branch
350
+ end
351
+ ```
352
+
353
+ Primary coverage determines what will come in first all output, and the type of coverage to check if you don't specify the type of coverage when customizing exit behavior (`SimpleCov.minimum_coverage 90`).
354
+
355
+ Note that coverage must first be enabled for non-default coverage types.
356
+
357
+ ## Coverage for eval
358
+
359
+ You can measure coverage for code that is evaluated by `Kernel#eval`. Supported in CRuby versions 3.2+.
360
+
361
+ ```ruby
362
+ SimpleCov.start do
363
+ enable_coverage_for_eval
364
+ end
365
+ ```
366
+
367
+ This is typically useful for ERB. Set `ERB#filename=` to make it possible for SimpleCov to trace the original .erb source file.
258
368
 
259
369
  ## Filters
260
370
 
261
- Filters can be used to remove selected files from your coverage data. By default, a filter is applied that removes all files
262
- OUTSIDE of your project's root directory - otherwise you'd end up with billions of coverage reports for source files in the
263
- gems you are using.
371
+ Filters can be used to remove selected files from your coverage data. By default, a filter is applied that removes all
372
+ files OUTSIDE of your project's root directory - otherwise you'd end up with billions of coverage reports for source
373
+ files in the gems you are using.
264
374
 
265
375
  You can define your own to remove things like configuration files, tests or whatever you don't need in your coverage
266
376
  report.
267
377
 
268
378
  ### Defining custom filters
269
379
 
270
- You can currently define a filter using either a String or Regexp (that will then be Regexp-matched against each source file's path),
271
- a block or by passing in your own Filter class.
380
+ You can currently define a filter using either a String or Regexp (that will then be Regexp-matched against each source
381
+ file's path), a block or by passing in your own Filter class.
272
382
 
273
383
  #### String filter
274
384
 
@@ -300,9 +410,10 @@ SimpleCov.start do
300
410
  end
301
411
  ```
302
412
 
303
- Block filters receive a SimpleCov::SourceFile instance and expect your block to return either true (if the file is to be removed
304
- from the result) or false (if the result should be kept). Please check out the RDoc for SimpleCov::SourceFile to learn about the
305
- methods available to you. In the above example, the filter will remove all files that have less than 5 lines of code.
413
+ Block filters receive a SimpleCov::SourceFile instance and expect your block to return either true (if the file is to be
414
+ removed from the result) or false (if the result should be kept). Please check out the RDoc for SimpleCov::SourceFile to
415
+ learn about the methods available to you. In the above example, the filter will remove all files that have less than 5
416
+ lines of code.
306
417
 
307
418
  #### Custom filter class
308
419
 
@@ -316,9 +427,10 @@ end
316
427
  SimpleCov.add_filter LineFilter.new(5)
317
428
  ```
318
429
 
319
- Defining your own filters is pretty easy: Just inherit from SimpleCov::Filter and define a method 'matches?(source_file)'. When running
320
- the filter, a true return value from this method will result in the removal of the given source_file. The filter_argument method
321
- is being set in the SimpleCov::Filter initialize method and thus is set to 5 in this example.
430
+ Defining your own filters is pretty easy: Just inherit from SimpleCov::Filter and define a method
431
+ 'matches?(source_file)'. When running the filter, a true return value from this method will result in the removal of the
432
+ given source_file. The filter_argument method is being set in the SimpleCov::Filter initialize method and thus is set to
433
+ 5 in this example.
322
434
 
323
435
  #### Array filter
324
436
 
@@ -343,16 +455,18 @@ end
343
455
  # :nocov:
344
456
  ```
345
457
 
346
- 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)
458
+ The name of the token can be changed to your liking. [Learn more about the nocov feature.]( https://github.com/simplecov-ruby/simplecov/blob/main/features/config_nocov_token.feature)
347
459
 
348
- **Note:** You shouldn't have to use the nocov token to skip private methods that are being included in your coverage. If you appropriately test the public interface of your classes and objects you should automatically get full coverage of your private methods.
460
+ **Note:** You shouldn't have to use the nocov token to skip private methods that are being included in your coverage. If
461
+ you appropriately test the public interface of your classes and objects you should automatically get full coverage of
462
+ your private methods.
349
463
 
350
464
  ## Default root filter and coverage for things outside of it
351
465
 
352
466
  By default, SimpleCov filters everything outside of the `SimpleCov.root` directory. However, sometimes you may want
353
467
  to include coverage reports for things you include as a gem, for example a Rails Engine.
354
468
 
355
- Here's an example by [@lsaffie](https://github.com/lsaffie) from [#221](https://github.com/colszowka/simplecov/issues/221)
469
+ Here's an example by [@lsaffie](https://github.com/lsaffie) from [#221](https://github.com/simplecov-ruby/simplecov/issues/221)
356
470
  that shows how you can achieve just that:
357
471
 
358
472
  ```ruby
@@ -368,8 +482,8 @@ end
368
482
 
369
483
  You can separate your source files into groups. For example, in a Rails app, you'll want to have separate listings for
370
484
  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 results,
372
- which exclude files from results when the filter results in a true value.
485
+ filter classes), but source files end up in a group when the filter passes (returns true), as opposed to filtering
486
+ results, which exclude files from results when the filter results in a true value.
373
487
 
374
488
  Add your groups with:
375
489
 
@@ -389,12 +503,11 @@ end
389
503
 
390
504
  You normally want to have your coverage analyzed across ALL of your test suites, right?
391
505
 
392
- Simplecov automatically caches coverage results in your (coverage_path)/.resultset.json. Those results will then
393
- be automatically merged when generating the result, so when coverage is set up properly for Cucumber and your
394
- unit / functional / integration tests, all of those test suites will be taken into account when building the
395
- coverage report.
396
-
397
- There are two things to note here though:
506
+ Simplecov automatically caches coverage results in your
507
+ (coverage_path)/.resultset.json, and will merge or override those with
508
+ subsequent runs, depending on whether simplecov considers those subsequent runs
509
+ as different test suites or as the same test suite as the cached results. To
510
+ make this distinction, simplecov has the concept of "test suite names".
398
511
 
399
512
  ### Test suite names
400
513
 
@@ -448,24 +561,143 @@ SimpleCov.command_name "features" + (ENV['TEST_ENV_NUMBER'] || '')
448
561
 
449
562
  [simplecov-html] prints the used test suites in the footer of the generated coverage report.
450
563
 
451
- ### Timeout for merge
452
564
 
453
- Of course, your cached coverage data is likely to become invalid at some point. Thus, result sets that are older than
454
- `SimpleCov.merge_timeout` will not be used any more. By default, the timeout is 600 seconds (10 minutes), and you can
455
- raise (or lower) it by specifying `SimpleCov.merge_timeout 3600` (1 hour), or, inside a configure/start block, with
456
- just `merge_timeout 3600`.
565
+ ### Merging test runs under the same execution environment
566
+
567
+ Test results are automatically merged with previous runs in the same execution
568
+ environment when generating the result, so when coverage is set up properly for
569
+ Cucumber and your unit / functional / integration tests, all of those test
570
+ suites will be taken into account when building the coverage report.
571
+
572
+ #### Timeout for merge
573
+
574
+ Of course, your cached coverage data is likely to become invalid at some point. Thus, when automatically merging
575
+ subsequent test runs, result sets that are older than `SimpleCov.merge_timeout` will not be used any more. By default,
576
+ the timeout is 600 seconds (10 minutes), and you can raise (or lower) it by specifying `SimpleCov.merge_timeout 3600`
577
+ (1 hour), or, inside a configure/start block, with just `merge_timeout 3600`.
578
+
579
+ You can deactivate this automatic merging altogether with `SimpleCov.use_merging false`.
580
+
581
+ ### Merging test runs under different execution environments
457
582
 
458
- You can deactivate merging altogether with `SimpleCov.use_merging false`.
583
+ If your tests are done in parallel across multiple build machines, you can fetch them all and merge them into a single
584
+ result set using the `SimpleCov.collate` method. This can be added to a Rakefile or script file, having downloaded a set of
585
+ `.resultset.json` files from each parallel test run.
586
+
587
+ ```ruby
588
+ # lib/tasks/coverage_report.rake
589
+ namespace :coverage do
590
+ desc "Collates all result sets generated by the different test runners"
591
+ task :report do
592
+ require 'simplecov'
593
+
594
+ SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"]
595
+ end
596
+ end
597
+ ```
598
+
599
+ `SimpleCov.collate` also takes an optional simplecov profile and an optional
600
+ block for configuration, just the same as `SimpleCov.start` or
601
+ `SimpleCov.configure`. This means you can configure a separate formatter for
602
+ the collated output. For instance, you can make the formatter in
603
+ `SimpleCov.start` the `SimpleCov::Formatter::SimpleFormatter`, and only use more
604
+ complex formatters in the final `SimpleCov.collate` run.
605
+
606
+ ```ruby
607
+ # spec/spec_helper.rb
608
+ require 'simplecov'
609
+
610
+ SimpleCov.start 'rails' do
611
+ # Disambiguates individual test runs
612
+ command_name "Job #{ENV["TEST_ENV_NUMBER"]}" if ENV["TEST_ENV_NUMBER"]
613
+
614
+ if ENV['CI']
615
+ formatter SimpleCov::Formatter::SimpleFormatter
616
+ else
617
+ formatter SimpleCov::Formatter::MultiFormatter.new([
618
+ SimpleCov::Formatter::SimpleFormatter,
619
+ SimpleCov::Formatter::HTMLFormatter
620
+ ])
621
+ end
622
+
623
+ track_files "**/*.rb"
624
+ end
625
+ ```
626
+
627
+ ```ruby
628
+ # lib/tasks/coverage_report.rake
629
+ namespace :coverage do
630
+ task :report do
631
+ require 'simplecov'
632
+
633
+ SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"], 'rails' do
634
+ formatter SimpleCov::Formatter::MultiFormatter.new([
635
+ SimpleCov::Formatter::SimpleFormatter,
636
+ SimpleCov::Formatter::HTMLFormatter
637
+ ])
638
+ end
639
+ end
640
+ end
641
+ ```
642
+
643
+ ## Running simplecov against subprocesses
644
+
645
+ `SimpleCov.enable_for_subprocesses` will allow SimpleCov to observe subprocesses starting using `Process.fork`.
646
+ This modifies ruby's core Process.fork method so that SimpleCov can see into it, appending `" (subprocess #{pid})"`
647
+ to the `SimpleCov.command_name`, with results that can be merged together using SimpleCov's merging feature.
648
+
649
+ To configure this, use `.at_fork`.
650
+
651
+ ```ruby
652
+ SimpleCov.enable_for_subprocesses true
653
+ SimpleCov.at_fork do |pid|
654
+ # This needs a unique name so it won't be ovewritten
655
+ SimpleCov.command_name "#{SimpleCov.command_name} (subprocess: #{pid})"
656
+ # be quiet, the parent process will be in charge of output and checking coverage totals
657
+ SimpleCov.print_error_status = false
658
+ SimpleCov.formatter SimpleCov::Formatter::SimpleFormatter
659
+ SimpleCov.minimum_coverage 0
660
+ # start
661
+ SimpleCov.start
662
+ end
663
+ ```
664
+
665
+ NOTE: SimpleCov must have already been started before `Process.fork` was called.
666
+
667
+ ### Running simplecov against spawned subprocesses
668
+
669
+ Perhaps you're testing a ruby script with `PTY.spawn` or `Open3.popen`, or `Process.spawn` or etc.
670
+ SimpleCov can cover this too.
671
+
672
+ Add a .simplecov_spawn.rb file to your project root
673
+ ```ruby
674
+ # .simplecov_spawn.rb
675
+ require 'simplecov' # this will also pick up whatever config is in .simplecov
676
+ # so ensure it just contains configuration, and doesn't call SimpleCov.start.
677
+ SimpleCov.command_name 'spawn' # As this is not for a test runner directly, script doesn't have a pre-defined base command_name
678
+ SimpleCov.at_fork.call(Process.pid) # Use the per-process setup described previously
679
+ SimpleCov.start # only now can we start.
680
+ ```
681
+ Then, instead of calling your script directly, like:
682
+ ```ruby
683
+ PTY.spawn('my_script.rb') do # ...
684
+ ```
685
+ Use bin/ruby to require the new .simplecov_spawn file, then your script
686
+ ```ruby
687
+ PTY.spawn('ruby -r./.simplecov_spawn my_script.rb') do # ...
688
+ ```
459
689
 
460
690
  ## Running coverage only on demand
461
691
 
462
- The Ruby STDLIB Coverage library that SimpleCov builds upon is *very* fast (on a ~10 min Rails test suite, the speed drop was
463
- only a couple seconds for me), and therefore it's SimpleCov's policy to just generate coverage every time you run your tests because
464
- it doesn't do your test speed any harm and you're always equipped with the latest and greatest coverage results.
692
+ The Ruby STDLIB Coverage library that SimpleCov builds upon is *very* fast (on a ~10 min Rails test suite, the speed
693
+ drop was only a couple seconds for me), and therefore it's SimpleCov's policy to just generate coverage every time you
694
+ run your tests because it doesn't do your test speed any harm and you're always equipped with the latest and greatest
695
+ coverage results.
465
696
 
466
697
  Because of this, SimpleCov has no explicit built-in mechanism to run coverage only on demand.
467
698
 
468
- However, you can still accomplish this very easily by introducing an ENV variable conditional into your SimpleCov setup block, like this:
699
+ However, you can still accomplish this very easily by introducing an ENV variable conditional into your SimpleCov setup
700
+ block, like this:
469
701
 
470
702
  ```ruby
471
703
  SimpleCov.start if ENV["COVERAGE"]
@@ -477,6 +709,21 @@ Then, SimpleCov will only run if you execute your tests like this:
477
709
  COVERAGE=true rake test
478
710
  ```
479
711
 
712
+ ## Errors and exit statuses
713
+
714
+ To aid in debugging issues, if an error is raised, SimpleCov will print a message to `STDERR`
715
+ with the exit status of the error, like:
716
+
717
+ ```
718
+ SimpleCov failed with exit 1
719
+ ```
720
+
721
+ This `STDERR` message can be disabled with:
722
+
723
+ ```
724
+ SimpleCov.print_error_status = false
725
+ ```
726
+
480
727
  ## Profiles
481
728
 
482
729
  By default, SimpleCov's only config assumption is that you only want coverage reports for files inside your project
@@ -514,8 +761,8 @@ end
514
761
 
515
762
  ### Custom profiles
516
763
 
517
- You can load additional profiles with the SimpleCov.load_profile('xyz') method. This allows you to build upon an existing
518
- profile and customize it so you can reuse it in unit tests and Cucumber features. For example:
764
+ You can load additional profiles with the SimpleCov.load_profile('xyz') method. This allows you to build upon an
765
+ existing profile and customize it so you can reuse it in unit tests and Cucumber features. For example:
519
766
 
520
767
  ```ruby
521
768
  # lib/simplecov_custom_profile.rb
@@ -552,14 +799,23 @@ You can define the minimum coverage percentage expected. SimpleCov will return n
552
799
 
553
800
  ```ruby
554
801
  SimpleCov.minimum_coverage 90
802
+ # same as above (the default is to check line coverage)
803
+ SimpleCov.minimum_coverage line: 90
804
+ # check for a minimum line coverage of 90% and minimum 80% branch coverage
805
+ SimpleCov.minimum_coverage line: 90, branch: 80
555
806
  ```
556
807
 
557
808
  ### Minimum coverage by file
558
809
 
559
- You can define the minimum coverage by file percentage expected. SimpleCov will return non-zero if unmet. This is useful to help ensure coverage is relatively consistent, rather than being skewed by particularly good or bad areas of the code.
810
+ You can define the minimum coverage by file percentage expected. SimpleCov will return non-zero if unmet. This is useful
811
+ to help ensure coverage is relatively consistent, rather than being skewed by particularly good or bad areas of the code.
560
812
 
561
813
  ```ruby
562
814
  SimpleCov.minimum_coverage_by_file 80
815
+ # same as above (the default is to check line coverage by file)
816
+ SimpleCov.minimum_coverage_by_file line: 80
817
+ # check for a minimum line coverage by file of 90% and minimum 80% branch coverage
818
+ SimpleCov.minimum_coverage_by_file line: 90, branch: 80
563
819
  ```
564
820
 
565
821
  ### Maximum coverage drop
@@ -568,6 +824,10 @@ You can define the maximum coverage drop percentage at once. SimpleCov will retu
568
824
 
569
825
  ```ruby
570
826
  SimpleCov.maximum_coverage_drop 5
827
+ # same as above (the default is to check line drop)
828
+ SimpleCov.maximum_coverage_drop line: 5
829
+ # check for a maximum line drop of 5% and maximum 10% branch drop
830
+ SimpleCov.maximum_coverage_drop line: 5, branch: 10
571
831
  ```
572
832
 
573
833
  ### Refuse dropping coverage
@@ -576,6 +836,10 @@ You can also entirely refuse dropping coverage between test runs:
576
836
 
577
837
  ```ruby
578
838
  SimpleCov.refuse_coverage_drop
839
+ # same as above (the default is to only refuse line drop)
840
+ SimpleCov.refuse_coverage_drop :line
841
+ # refuse drop for line and branch
842
+ SimpleCov.refuse_coverage_drop :line, :branch
579
843
  ```
580
844
 
581
845
  ## Using your own formatter
@@ -586,21 +850,37 @@ You can use your own formatter with:
586
850
  SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter
587
851
  ```
588
852
 
589
- When calling SimpleCov.result.format!, it will be invoked with SimpleCov::Formatter::YourFormatter.new.format(result), "result"
590
- being an instance of SimpleCov::Result. Do whatever your wish with that!
853
+ Calling `SimpleCov.result.format!` will be invoked with `SimpleCov::Formatter::YourFormatter.new.format(result)`,
854
+ and `result` is an instance of `SimpleCov::Result`. Do whatever your wish with that!
591
855
 
592
856
 
593
857
  ## Using multiple formatters
594
858
 
595
- As of SimpleCov 0.9, you can specify multiple result formats:
859
+ As of SimpleCov 0.9, you can specify multiple result formats. Formatters besides the default HTML formatter require separate gems, however.
596
860
 
597
861
  ```ruby
862
+ require "simplecov-html"
863
+
598
864
  SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
599
865
  SimpleCov::Formatter::HTMLFormatter,
600
866
  SimpleCov::Formatter::CSVFormatter,
601
867
  ])
602
868
  ```
603
869
 
870
+ ## JSON formatter
871
+
872
+ SimpleCov is packaged with a separate gem called [simplecov_json_formatter](https://github.com/codeclimate-community/simplecov_json_formatter) that provides you with a JSON formatter, this formatter could be useful for different use cases, such as for CI consumption or for reporting to external services.
873
+
874
+ In order to use it you will need to manually load the installed gem like so:
875
+
876
+ ```ruby
877
+ require "simplecov_json_formatter"
878
+ SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
879
+ ```
880
+
881
+ > _Note:_ In case you plan to report your coverage results to CodeClimate services, know that SimpleCov will automatically use the
882
+ > JSON formatter along with the HTML formatter when the `CC_TEST_REPORTER_ID` variable is present in the environment.
883
+
604
884
  ## Available formatters, editor integrations and hosted services
605
885
 
606
886
  * [Open Source formatter and integration plugins for SimpleCov](doc/alternate-formatters.md)
@@ -609,11 +889,9 @@ SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
609
889
 
610
890
  ## Ruby version compatibility
611
891
 
612
- Only Ruby 1.9+ ships with the coverage library that SimpleCov depends upon and that's what SimpleCov supports. Additionally JRuby 9.1+ is supported as well, while JRuby 1.7 and 9.0 should work they're not "officially" supported.
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.
892
+ SimpleCov is built in [Continuous Integration] on Ruby 2.7+ as well as JRuby 9.3+.
615
893
 
616
- SimpleCov is built in [Continuous Integration] on Ruby 1.9.3, 2.0.0, 2.1, 2.2, 2.3, 2.4, 2.5 as well as JRuby 9.1.
894
+ Note for JRuby => You need to pass JRUBY_OPTS="--debug" or create .jrubyrc and add debug.fullTrace=true
617
895
 
618
896
  ## Want to find dead code in production?
619
897
 
@@ -621,10 +899,12 @@ Try [Coverband](https://github.com/danmayer/coverband).
621
899
 
622
900
  ## Want to use Spring with SimpleCov?
623
901
 
624
- If you're using [Spring](https://github.com/rails/spring) to speed up test suite runs and want to run SimpleCov along with them, you'll find that it often misreports coverage with the default config due to some sort of eager loading issue. Don't despair!
902
+ If you're using [Spring](https://github.com/rails/spring) to speed up test suite runs and want to run SimpleCov along
903
+ with them, you'll find that it often misreports coverage with the default config due to some sort of eager loading
904
+ issue. Don't despair!
625
905
 
626
906
  One solution is to [explicitly call eager
627
- load](https://github.com/colszowka/simplecov/issues/381#issuecomment-347651728)
907
+ load](https://github.com/simplecov-ruby/simplecov/issues/381#issuecomment-347651728)
628
908
  in your `test_helper.rb` / `spec_helper.rb` after calling `SimpleCov.start`.
629
909
 
630
910
  ```ruby
@@ -633,13 +913,22 @@ SimpleCov.start 'rails'
633
913
  Rails.application.eager_load!
634
914
  ```
635
915
 
916
+ Alternatively, you could disable Spring while running SimpleCov:
917
+
918
+ ```
919
+ DISABLE_SPRING=1 rake test
920
+ ```
921
+
636
922
  Or you could remove `gem 'spring'` from your `Gemfile`.
637
923
 
638
924
  ## Troubleshooting
639
925
 
640
- The **most common problem is that simplecov isn't required and started before everything else**. In order to track coverage for your whole application **simplecov needs to be the first one** so that it (and the underlying coverage library) can subsequently track loaded files and their usage.
926
+ The **most common problem is that simplecov isn't required and started before everything else**. In order to track
927
+ coverage for your whole application **simplecov needs to be the first one** so that it (and the underlying coverage
928
+ library) can subsequently track loaded files and their usage.
641
929
 
642
- If you are missing coverage for some code a simple trick is to put a puts statement in there and right after `SimpleCov.start` so you can see if the file really was loaded after simplecov was started.
930
+ If you are missing coverage for some code a simple trick is to put a puts statement in there and right after
931
+ `SimpleCov.start` so you can see if the file really was loaded after simplecov was started.
643
932
 
644
933
  ```ruby
645
934
  # my_code.rb
@@ -667,9 +956,14 @@ MyCode is being loaded!
667
956
 
668
957
  then it's good otherwise you likely have a problem :)
669
958
 
959
+ ## Code of Conduct
960
+
961
+ Everyone participating in this project's development, issue trackers and other channels is expected to follow our
962
+ [Code of Conduct](./CODE_OF_CONDUCT.md)
963
+
670
964
  ## Contributing
671
965
 
672
- See the [contributing guide](https://github.com/colszowka/simplecov/blob/master/CONTRIBUTING.md).
966
+ See the [contributing guide](https://github.com/simplecov-ruby/simplecov/blob/main/CONTRIBUTING.md).
673
967
 
674
968
  ## Kudos
675
969