simplecov 0.18.0.beta1 → 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.
data/lib/simplecov.rb CHANGED
@@ -46,13 +46,48 @@ module SimpleCov
46
46
  #
47
47
  def start(profile = nil, &block)
48
48
  require "coverage"
49
- load_profile(profile) if profile
50
- configure(&block) if block_given?
49
+ initial_setup(profile, &block)
51
50
  @result = nil
52
- self.running = true
53
51
  self.pid = Process.pid
54
52
 
55
- start_coverage_measurment
53
+ start_coverage_measurement
54
+ end
55
+
56
+ #
57
+ # Collate a series of SimpleCov result files into a single SimpleCov output.
58
+ # You can optionally specify configuration with a block:
59
+ # SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"]
60
+ # OR
61
+ # SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"], 'rails' # using rails profile
62
+ # OR
63
+ # SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"] do
64
+ # add_filter 'test'
65
+ # end
66
+ # OR
67
+ # SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"], 'rails' do
68
+ # add_filter 'test'
69
+ # end
70
+ #
71
+ # Please check out the RDoc for SimpleCov::Configuration to find about
72
+ # available config options, or checkout the README for more in-depth
73
+ # information about coverage collation
74
+ #
75
+ def collate(result_filenames, profile = nil, &block)
76
+ raise "There's no reports to be merged" if result_filenames.empty?
77
+
78
+ initial_setup(profile, &block)
79
+
80
+ results = result_filenames.flat_map do |filename|
81
+ # Re-create each included instance of SimpleCov::Result from the stored run data.
82
+ (JSON.parse(File.read(filename)) || {}).map do |command_name, coverage|
83
+ SimpleCov::Result.from_hash(command_name => coverage)
84
+ end
85
+ end
86
+
87
+ # Use the ResultMerger to produce a single, merged result, ready to use.
88
+ @result = SimpleCov::ResultMerger.merge_and_store(*results)
89
+
90
+ run_exit_tasks!
56
91
  end
57
92
 
58
93
  #
@@ -159,6 +194,8 @@ module SimpleCov
159
194
  # Called from at_exit block
160
195
  #
161
196
  def run_exit_tasks!
197
+ set_exit_exception
198
+
162
199
  exit_status = SimpleCov.exit_status_from_exception
163
200
 
164
201
  SimpleCov.at_exit.call
@@ -170,7 +207,7 @@ module SimpleCov
170
207
  # Force exit with stored status (see github issue #5)
171
208
  # unless it's nil or 0 (see github issue #281)
172
209
  if exit_status&.positive?
173
- $stderr.printf("SimpleCov failed with exit %<exit_status>d\n", :exit_status => exit_status) if print_error_status
210
+ $stderr.printf("SimpleCov failed with exit %<exit_status>d\n", exit_status: exit_status) if print_error_status
174
211
  Kernel.exit exit_status
175
212
  end
176
213
  end
@@ -194,28 +231,24 @@ module SimpleCov
194
231
  # rubocop:disable Metrics/MethodLength
195
232
  def result_exit_status(result, covered_percent)
196
233
  covered_percentages = result.covered_percentages.map { |percentage| percentage.floor(2) }
197
- if covered_percent < SimpleCov.minimum_coverage
198
- $stderr.printf(
199
- "Coverage (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%).\n",
200
- :covered => covered_percent,
201
- :minimum_coverage => SimpleCov.minimum_coverage
202
- )
234
+ if (minimum_violations = minimum_coverage_violated(result)).any?
235
+ report_minimum_violated(minimum_violations)
203
236
  SimpleCov::ExitCodes::MINIMUM_COVERAGE
204
237
  elsif covered_percentages.any? { |p| p < SimpleCov.minimum_coverage_by_file }
205
238
  $stderr.printf(
206
239
  "File (%<file>s) is only (%<least_covered_percentage>.2f%%) covered. This is below the expected minimum coverage per file of (%<min_coverage>.2f%%).\n",
207
- :file => result.least_covered_file,
208
- :least_covered_percentage => covered_percentages.min,
209
- :min_coverage => SimpleCov.minimum_coverage_by_file
240
+ file: result.least_covered_file,
241
+ least_covered_percentage: covered_percentages.min,
242
+ min_coverage: SimpleCov.minimum_coverage_by_file
210
243
  )
211
244
  SimpleCov::ExitCodes::MINIMUM_COVERAGE
212
245
  elsif (last_run = SimpleCov::LastRun.read)
213
- coverage_diff = last_run["result"]["covered_percent"] - covered_percent
246
+ coverage_diff = last_run[:result][:covered_percent] - covered_percent
214
247
  if coverage_diff > SimpleCov.maximum_coverage_drop
215
248
  $stderr.printf(
216
249
  "Coverage has dropped by %<drop_percent>.2f%% since the last time (maximum allowed: %<max_drop>.2f%%).\n",
217
- :drop_percent => coverage_diff,
218
- :max_drop => SimpleCov.maximum_coverage_drop
250
+ drop_percent: coverage_diff,
251
+ max_drop: SimpleCov.maximum_coverage_drop
219
252
  )
220
253
  SimpleCov::ExitCodes::MAXIMUM_COVERAGE_DROP
221
254
  else
@@ -248,18 +281,24 @@ module SimpleCov
248
281
  # @api private
249
282
  #
250
283
  def write_last_run(covered_percent)
251
- SimpleCov::LastRun.write(:result => {:covered_percent => covered_percent})
284
+ SimpleCov::LastRun.write(result: {covered_percent: covered_percent})
252
285
  end
253
286
 
254
287
  private
255
288
 
289
+ def initial_setup(profile, &block)
290
+ load_profile(profile) if profile
291
+ configure(&block) if block_given?
292
+ self.running = true
293
+ end
294
+
256
295
  #
257
296
  # Trigger Coverage.start depends on given config coverage_criterion
258
297
  #
259
298
  # With Positive branch it supports all coverage measurement types
260
299
  # With Negative branch it supports only line coverage measurement type
261
300
  #
262
- def start_coverage_measurment
301
+ def start_coverage_measurement
263
302
  # This blog post gives a good run down of the coverage criterias introduced
264
303
  # in Ruby 2.5: https://blog.bigbinary.com/2018/04/11/ruby-2-5-supports-measuring-branch-and-method-coverages.html
265
304
  # There is also a nice writeup of the different coverage criteria made in this
@@ -287,13 +326,29 @@ module SimpleCov
287
326
  # :oneshot_lines - can not be combined with lines
288
327
  # :all - same as lines + branches + methods
289
328
  #
290
- if branch_coverage?
291
- Coverage.start(:all)
329
+ if coverage_start_arguments_supported?
330
+ start_coverage_with_criteria
292
331
  else
293
332
  Coverage.start
294
333
  end
295
334
  end
296
335
 
336
+ def start_coverage_with_criteria
337
+ start_arguments = coverage_criteria.map do |criterion|
338
+ [lookup_corresponding_ruby_coverage_name(criterion), true]
339
+ end.to_h
340
+
341
+ Coverage.start(start_arguments)
342
+ end
343
+
344
+ CRITERION_TO_RUBY_COVERAGE = {
345
+ branch: :branches,
346
+ line: :lines
347
+ }.freeze
348
+ def lookup_corresponding_ruby_coverage_name(criterion)
349
+ CRITERION_TO_RUBY_COVERAGE.fetch(criterion)
350
+ end
351
+
297
352
  #
298
353
  # Finds files that were to be tracked but were not loaded and initializes
299
354
  # the line-by-line coverage to zero (if relevant) or nil (comments / whitespace etc).
@@ -350,13 +405,40 @@ module SimpleCov
350
405
  def result_with_not_loaded_files
351
406
  @result = SimpleCov::Result.new(add_not_loaded_files(@result))
352
407
  end
408
+
409
+ def minimum_coverage_violated(result)
410
+ coverage_achieved = minimum_coverage.map do |criterion, percent|
411
+ {
412
+ criterion: criterion,
413
+ minimum_expected: percent,
414
+ actual: result.coverage_statistics[criterion].percent
415
+ }
416
+ end
417
+
418
+ coverage_achieved.select do |achieved|
419
+ achieved.fetch(:actual) < achieved.fetch(:minimum_expected)
420
+ end
421
+ end
422
+
423
+ def report_minimum_violated(violations)
424
+ violations.each do |violation|
425
+ $stderr.printf(
426
+ "%<criterion>s coverage (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%).\n",
427
+ covered: violation.fetch(:actual).floor(2),
428
+ minimum_coverage: violation.fetch(:minimum_expected),
429
+ criterion: violation.fetch(:criterion).capitalize
430
+ )
431
+ end
432
+ end
353
433
  end
354
434
  end
355
435
 
356
436
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__)))
357
437
  require "set"
438
+ require "forwardable"
358
439
  require "simplecov/configuration"
359
440
  SimpleCov.extend SimpleCov::Configuration
441
+ require "simplecov/coverage_statistics"
360
442
  require "simplecov/exit_codes"
361
443
  require "simplecov/profiles"
362
444
  require "simplecov/source_file/line"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplecov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0.beta1
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Olszowka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-05 00:00:00.000000000 Z
11
+ date: 2020-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.11.0.beta1
33
+ version: 0.11.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.11.0.beta1
40
+ version: 0.11.0
41
41
  description: Code coverage for Ruby with a powerful configuration library and automatic
42
42
  merging of coverage across test suites
43
43
  email:
@@ -63,6 +63,7 @@ files:
63
63
  - lib/simplecov/combine/results_combiner.rb
64
64
  - lib/simplecov/command_guesser.rb
65
65
  - lib/simplecov/configuration.rb
66
+ - lib/simplecov/coverage_statistics.rb
66
67
  - lib/simplecov/defaults.rb
67
68
  - lib/simplecov/exit_codes.rb
68
69
  - lib/simplecov/file_list.rb
@@ -95,9 +96,9 @@ licenses:
95
96
  metadata:
96
97
  bug_tracker_uri: https://github.com/colszowka/simplecov/issues
97
98
  changelog_uri: https://github.com/colszowka/simplecov/blob/master/CHANGELOG.md
98
- documentation_uri: https://www.rubydoc.info/gems/simplecov/0.18.0.beta1
99
+ documentation_uri: https://www.rubydoc.info/gems/simplecov/0.18.0
99
100
  mailing_list_uri: https://groups.google.com/forum/#!forum/simplecov
100
- source_code_uri: https://github.com/colszowka/simplecov/tree/v0.18.0.beta1
101
+ source_code_uri: https://github.com/colszowka/simplecov/tree/v0.18.0
101
102
  post_install_message:
102
103
  rdoc_options: []
103
104
  require_paths:
@@ -109,9 +110,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  version: 2.4.0
110
111
  required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  requirements:
112
- - - ">"
113
+ - - ">="
113
114
  - !ruby/object:Gem::Version
114
- version: 1.3.1
115
+ version: '0'
115
116
  requirements: []
116
117
  rubygems_version: 3.1.2
117
118
  signing_key: