Module: SimpleCov

Extended by:
Configuration
Defined in:
lib/simplecov.rb,
lib/simplecov/filter.rb,
lib/simplecov/result.rb,
lib/simplecov/combine.rb,
lib/simplecov/version.rb,
lib/simplecov/last_run.rb,
lib/simplecov/profiles.rb,
lib/simplecov/file_list.rb,
lib/simplecov/formatter.rb,
lib/simplecov/exit_codes.rb,
lib/simplecov/source_file.rb,
lib/simplecov/configuration.rb,
lib/simplecov/result_merger.rb,
lib/simplecov/result_adapter.rb,
lib/simplecov/command_guesser.rb,
lib/simplecov/lines_classifier.rb,
lib/simplecov/source_file/line.rb,
lib/simplecov/simulate_coverage.rb,
lib/simplecov/source_file/branch.rb,
lib/simplecov/coverage_statistics.rb,
lib/simplecov/combine/files_combiner.rb,
lib/simplecov/combine/lines_combiner.rb,
lib/simplecov/useless_results_remover.rb,
lib/simplecov/combine/results_combiner.rb,
lib/simplecov/combine/branches_combiner.rb,
lib/simplecov/formatter/multi_formatter.rb,
lib/simplecov/formatter/simple_formatter.rb

Overview

Code coverage for ruby. Please check out README for a full introduction.

Defined Under Namespace

Modules: Combine, CommandGuesser, Configuration, ExitCodes, Formatter, LastRun, ResultMerger, SimulateCoverage, UselessResultsRemover Classes: ArrayFilter, BlockFilter, CoverageStatistics, FileList, Filter, LinesClassifier, Profiles, RegexFilter, Result, ResultAdapter, SourceFile, StringFilter

Constant Summary collapse

VERSION =
"0.18.2"

Constants included from Configuration

Configuration::DEFAULT_COVERAGE_CRITERION, Configuration::SUPPORTED_COVERAGE_CRITERIA

Class Attribute Summary collapse

Attributes included from Configuration

#filters, #formatter, #groups, #print_error_status

Class Method Summary collapse

Methods included from Configuration

adapters, add_filter, add_group, at_exit, branch_coverage?, clear_coverage_criteria, command_name, configure, coverage_criteria, coverage_criterion, coverage_criterion_enabled?, coverage_dir, coverage_path, coverage_start_arguments_supported?, enable_coverage, formatters, formatters=, maximum_coverage_drop, merge_timeout, minimum_coverage, minimum_coverage_by_file, nocov_token, profiles, project_name, refuse_coverage_drop, root, track_files, tracked_files, use_merging

Class Attribute Details

.exit_exceptionObject (readonly)

Returns the value of attribute exit_exception.



28
29
30
# File 'lib/simplecov.rb', line 28

def exit_exception
  @exit_exception
end

.pidObject

Returns the value of attribute pid.



27
28
29
# File 'lib/simplecov.rb', line 27

def pid
  @pid
end

.runningObject

Returns the value of attribute running.



26
27
28
# File 'lib/simplecov.rb', line 26

def running
  @running
end

Class Method Details

.clear_resultObject

Clear out the previously cached .result. Primarily useful in testing



167
168
169
# File 'lib/simplecov.rb', line 167

def clear_result
  @result = nil
end

.collate(result_filenames, profile = nil, &block) ⇒ Object

Collate a series of SimpleCov result files into a single SimpleCov output. You can optionally specify configuration with a block:

SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"]
 OR
SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"], 'rails' # using rails profile
 OR
SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"] do
  add_filter 'test'
end
 OR
SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"], 'rails' do
  add_filter 'test'
end

Please check out the RDoc for SimpleCov::Configuration to find about available config options, or checkout the README for more in-depth information about coverage collation



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/simplecov.rb', line 75

def collate(result_filenames, profile = nil, &block)
  raise "There's no reports to be merged" if result_filenames.empty?

  initial_setup(profile, &block)

  results = result_filenames.flat_map do |filename|
    # Re-create each included instance of SimpleCov::Result from the stored run data.
    (JSON.parse(File.read(filename)) || {}).map do |command_name, coverage|
      SimpleCov::Result.from_hash(command_name => coverage)
    end
  end

  # Use the ResultMerger to produce a single, merged result, ready to use.
  @result = SimpleCov::ResultMerger.merge_and_store(*results)

  run_exit_tasks!
end

.exit_status_from_exceptionObject

Returns the exit status from the exit exception



182
183
184
185
186
187
188
189
190
# File 'lib/simplecov.rb', line 182

def exit_status_from_exception
  return SimpleCov::ExitCodes::SUCCESS unless exit_exception

  if exit_exception.is_a?(SystemExit)
    exit_exception.status
  else
    SimpleCov::ExitCodes::EXCEPTION
  end
end

.filtered(files) ⇒ Object

Applies the configured filters to the given array of SimpleCov::SourceFile items



128
129
130
131
132
133
134
# File 'lib/simplecov.rb', line 128

def filtered(files)
  result = files.clone
  filters.each do |filter|
    result = result.reject { |source_file| filter.matches?(source_file) }
  end
  SimpleCov::FileList.new result
end

.final_result_process?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


266
267
268
269
# File 'lib/simplecov.rb', line 266

def final_result_process?
  # checking for ENV["TEST_ENV_NUMBER"] to determine if the tess are being run in parallel
  !defined?(ParallelTests) || !ENV["TEST_ENV_NUMBER"] || ParallelTests.number_of_running_processes <= 1
end

.grouped(files) ⇒ Object

Applies the configured groups to the given array of SimpleCov::SourceFile items



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/simplecov.rb', line 139

def grouped(files)
  grouped = {}
  grouped_files = []
  groups.each do |name, filter|
    grouped[name] = SimpleCov::FileList.new(files.select { |source_file| filter.matches?(source_file) })
    grouped_files += grouped[name]
  end
  if !groups.empty? && !(other_files = files.reject { |source_file| grouped_files.include?(source_file) }).empty?
    grouped["Ungrouped"] = SimpleCov::FileList.new(other_files)
  end
  grouped
end

.load_adapter(name) ⇒ Object



159
160
161
162
# File 'lib/simplecov.rb', line 159

def load_adapter(name)
  warn "#{Kernel.caller.first}: [DEPRECATION] #load_adapter is deprecated. Use #load_profile instead."
  load_profile(name)
end

.load_profile(name) ⇒ Object

Applies the profile of given name on SimpleCov configuration



155
156
157
# File 'lib/simplecov.rb', line 155

def load_profile(name)
  profiles.load(name)
end

.process_result(result, exit_status) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Usage:

exit_status = SimpleCov.process_result(SimpleCov.result, exit_status)


220
221
222
223
224
225
226
227
# File 'lib/simplecov.rb', line 220

def process_result(result, exit_status)
  return exit_status if exit_status != SimpleCov::ExitCodes::SUCCESS # Existing errors

  covered_percent = result.covered_percent.floor(2)
  result_exit_status = result_exit_status(result, covered_percent)
  write_last_run(covered_percent) if result_exit_status == SimpleCov::ExitCodes::SUCCESS # No result errors
  final_result_process? ? result_exit_status : SimpleCov::ExitCodes::SUCCESS
end

.resultObject

Returns the result for the current coverage run, merging it across test suites from cache using SimpleCov::ResultMerger if use_merging is activated (default)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/simplecov.rb', line 97

def result
  return @result if result?

  # Collect our coverage result

  process_coverage_result if running

  # If we're using merging of results, store the current result
  # first (if there is one), then merge the results and return those
  if use_merging
    wait_for_other_processes
    SimpleCov::ResultMerger.store_result(@result) if result?
    @result = SimpleCov::ResultMerger.merged_result
  end

  @result
ensure
  self.running = false
end

.result?Boolean

Returns nil if the result has not been computed Otherwise, returns the result

Returns:

  • (Boolean)


121
122
123
# File 'lib/simplecov.rb', line 121

def result?
  defined?(@result) && @result
end

.result_exit_status(result, covered_percent) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/MethodLength



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/simplecov.rb', line 232

def result_exit_status(result, covered_percent)
  covered_percentages = result.covered_percentages.map { |percentage| percentage.floor(2) }
  if (minimum_violations = minimum_coverage_violated(result)).any?
    report_minimum_violated(minimum_violations)
    SimpleCov::ExitCodes::MINIMUM_COVERAGE
  elsif covered_percentages.any? { |p| p < SimpleCov.minimum_coverage_by_file }
    $stderr.printf(
      "File (%<file>s) is only (%<least_covered_percentage>.2f%%) covered. This is below the expected minimum coverage per file of (%<min_coverage>.2f%%).\n",
      file: result.least_covered_file,
      least_covered_percentage: covered_percentages.min,
      min_coverage: SimpleCov.minimum_coverage_by_file
    )
    SimpleCov::ExitCodes::MINIMUM_COVERAGE
  elsif (last_run = SimpleCov::LastRun.read)
    coverage_diff = last_run[:result][:covered_percent] - covered_percent
    if coverage_diff > SimpleCov.maximum_coverage_drop
      $stderr.printf(
        "Coverage has dropped by %<drop_percent>.2f%% since the last time (maximum allowed: %<max_drop>.2f%%).\n",
        drop_percent: coverage_diff,
        max_drop: SimpleCov.maximum_coverage_drop
      )
      SimpleCov::ExitCodes::MAXIMUM_COVERAGE_DROP
    else
      SimpleCov::ExitCodes::SUCCESS
    end
  else
    SimpleCov::ExitCodes::SUCCESS
  end
end

.run_exit_tasks!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called from at_exit block



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/simplecov.rb', line 196

def run_exit_tasks!
  set_exit_exception

  exit_status = SimpleCov.exit_status_from_exception

  SimpleCov.at_exit.call

  # Don't modify the exit status unless the result has already been
  # computed
  exit_status = SimpleCov.process_result(SimpleCov.result, exit_status) if SimpleCov.result?

  # Force exit with stored status (see github issue #5)
  # unless it's nil or 0 (see github issue #281)
  if exit_status&.positive?
    $stderr.printf("SimpleCov failed with exit %<exit_status>d\n", exit_status: exit_status) if print_error_status
    Kernel.exit exit_status
  end
end

.set_exit_exceptionObject

Capture the current exception if it exists This will get called inside the at_exit block



175
176
177
# File 'lib/simplecov.rb', line 175

def set_exit_exception
  @exit_exception = $ERROR_INFO
end

.start(profile = nil, &block) ⇒ Object

Sets up SimpleCov to run against your project. You can optionally specify a profile to use as well as configuration with a block:

SimpleCov.start
 OR
SimpleCov.start 'rails' # using rails profile
 OR
SimpleCov.start do
  add_filter 'test'
end
  OR
SimpleCov.start 'rails' do
  add_filter 'test'
end

Please check out the RDoc for SimpleCov::Configuration to find about available config options



47
48
49
50
51
52
53
54
# File 'lib/simplecov.rb', line 47

def start(profile = nil, &block)
  require "coverage"
  initial_setup(profile, &block)
  @result = nil
  self.pid = Process.pid

  start_coverage_measurement
end

.wait_for_other_processesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



274
275
276
277
278
# File 'lib/simplecov.rb', line 274

def wait_for_other_processes
  return unless defined?(ParallelTests) && final_result_process?

  ParallelTests.wait_for_other_processes_to_finish
end

.write_last_run(covered_percent) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



283
284
285
# File 'lib/simplecov.rb', line 283

def write_last_run(covered_percent)
  SimpleCov::LastRun.write(result: {covered_percent: covered_percent})
end