Module: SimpleCov::Configuration
- Included in:
- SimpleCov
- Defined in:
- lib/simplecov/configuration.rb
Overview
Bundles the configuration options used for SimpleCov. All methods defined here are usable from SimpleCov directly. Please check out SimpleCov documentation for further info.
Constant Summary collapse
- SUPPORTED_COVERAGE_CRITERIA =
%i[line branch].freeze
- DEFAULT_COVERAGE_CRITERION =
:line
Instance Attribute Summary collapse
-
#filters ⇒ Object
Returns the list of configured filters.
-
#formatter(formatter = nil) ⇒ Object
Gets or sets the configured formatter.
-
#groups ⇒ Object
Returns the configured groups.
-
#print_error_status ⇒ Object
Whether we should print non-success status codes.
Instance Method Summary collapse
- #adapters ⇒ Object
-
#add_filter(filter_argument = nil, &filter_proc) ⇒ Object
Add a filter to the processing chain.
-
#add_group(group_name, filter_argument = nil, &filter_proc) ⇒ Object
Define a group for files.
-
#at_exit(&block) ⇒ Object
Gets or sets the behavior to process coverage results.
-
#at_fork(&block) ⇒ Object
Gets or sets the behavior to start a new forked Process.
- #branch_coverage? ⇒ Boolean
- #clear_coverage_criteria ⇒ Object
-
#command_name(name = nil) ⇒ Object
The name of the command (a.k.a. Test Suite) currently running.
-
#configure(&block) ⇒ Object
Allows you to configure simplecov in a block instead of prepending SimpleCov to all config methods you’re calling.
- #coverage_criteria ⇒ Object
-
#coverage_criterion(criterion = nil) ⇒ Object
Define which coverage criterion should be evaluated.
- #coverage_criterion_enabled?(criterion) ⇒ Boolean
-
#coverage_dir(dir = nil) ⇒ Object
The name of the output and cache directory.
-
#coverage_path ⇒ Object
Returns the full path to the output directory using SimpleCov.root and SimpleCov.coverage_dir, so you can adjust this by configuring those values.
- #coverage_start_arguments_supported? ⇒ Boolean (also: #branch_coverage_supported?)
- #enable_coverage(criterion) ⇒ Object
-
#enable_for_subprocesses(value = nil) ⇒ Object
gets or sets the enabled_for_subprocess configuration when true, this will inject SimpleCov code into Process.fork.
-
#enabled_for_subprocesses? ⇒ Boolean
gets the enabled_for_subprocess configuration.
-
#formatters ⇒ Object
Gets the configured formatters.
-
#formatters=(formatters) ⇒ Object
Sets the configured formatters.
-
#maximum_coverage_drop(coverage_drop = nil) ⇒ Object
Defines the maximum coverage drop at once allowed for the testsuite to pass.
-
#merge_timeout(seconds = nil) ⇒ Object
Defines the maximum age (in seconds) of a resultset to still be included in merged results.
-
#minimum_coverage(coverage = nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
-
#minimum_coverage_by_file(coverage = nil) ⇒ Object
Defines the minimum coverage per file required for the testsuite to pass.
-
#nocov_token(nocov_token = nil) ⇒ Object
(also: #skip_token)
Certain code blocks (i.e. Ruby-implementation specific code) can be excluded from the coverage metrics by wrapping it inside # :nocov: comment blocks.
-
#profiles ⇒ Object
Returns the hash of available profiles.
-
#project_name(new_name = nil) ⇒ Object
Returns the project name - currently assuming the last dirname in the SimpleCov.root is this.
-
#refuse_coverage_drop ⇒ Object
Refuses any coverage drop.
-
#root(root = nil) ⇒ Object
The root for the project.
-
#track_files(glob) ⇒ Object
Coverage results will always include files matched by this glob, whether or not they were explicitly required.
-
#tracked_files ⇒ Object
Returns the glob that will be used to include files that were not explicitly required.
-
#use_merging(use = nil) ⇒ Object
Defines whether to use result merging so all your test suites (test:units, test:functionals, cucumber, …) are joined and combined into a single coverage report.
Instance Attribute Details
#filters ⇒ Object
Returns the list of configured filters. Add filters using SimpleCov.add_filter.
74 75 76 |
# File 'lib/simplecov/configuration.rb', line 74 def filters @filters ||= [] end |
#formatter(formatter = nil) ⇒ Object
Gets or sets the configured formatter.
Configure with: SimpleCov.formatter(SimpleCov::Formatter::SimpleFormatter)
98 99 100 101 102 103 104 105 |
# File 'lib/simplecov/configuration.rb', line 98 def formatter(formatter = nil) return @formatter if defined?(@formatter) && formatter.nil? @formatter = formatter raise "No formatter configured. Please specify a formatter using SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter" unless @formatter @formatter end |
#groups ⇒ Object
Returns the configured groups. Add groups using SimpleCov.add_group
150 151 152 |
# File 'lib/simplecov/configuration.rb', line 150 def groups @groups ||= {} end |
#print_error_status ⇒ Object
Whether we should print non-success status codes. This can be configured with the #print_error_status= method.
129 130 131 |
# File 'lib/simplecov/configuration.rb', line 129 def print_error_status defined?(@print_error_status) ? @print_error_status : true end |
Instance Method Details
#adapters ⇒ Object
161 162 163 164 |
# File 'lib/simplecov/configuration.rb', line 161 def adapters warn "#{Kernel.caller.first}: [DEPRECATION] #adapters is deprecated. Use #profiles instead." profiles end |
#add_filter(filter_argument = nil, &filter_proc) ⇒ Object
Add a filter to the processing chain. There are four ways to define a filter:
-
as a String that will then be matched against all source files’ file paths,
SimpleCov.add_filter 'app/models' # will reject all your models
-
as a block which will be passed the source file in question and should either return a true or false value, depending on whether the file should be removed
SimpleCov.add_filter do |src_file| File.basename(src_file.filename) == 'environment.rb' end # Will exclude environment.rb files from the results
-
as an array of strings that are matched against all sorce files’ file paths and then ignored (basically string filter multiple times)
SimpleCov.add_filter ['app/models', 'app/helpers'] # ignores both dirs
-
as an instance of a subclass of SimpleCov::Filter. See the documentation there on how to define your own filter classes
352 353 354 |
# File 'lib/simplecov/configuration.rb', line 352 def add_filter(filter_argument = nil, &filter_proc) filters << parse_filter(filter_argument, &filter_proc) end |
#add_group(group_name, filter_argument = nil, &filter_proc) ⇒ Object
Define a group for files. Works similar to add_filter, only that the first argument is the desired group name and files PASSING the filter end up in the group (while filters exclude when the filter is applicable).
361 362 363 |
# File 'lib/simplecov/configuration.rb', line 361 def add_group(group_name, filter_argument = nil, &filter_proc) groups[group_name] = parse_filter(filter_argument, &filter_proc) end |
#at_exit(&block) ⇒ Object
193 194 195 196 197 198 |
# File 'lib/simplecov/configuration.rb', line 193 def at_exit(&block) return proc {} unless running || block_given? @at_exit = block if block_given? @at_exit ||= proc { SimpleCov.result.format! } end |
#at_fork(&block) ⇒ Object
Gets or sets the behavior to start a new forked Process.
By default, it will add “ (Process #SimpleCov.pid)” to the command_name, and start SimpleCov in quiet mode
Configure with:
SimpleCov.at_fork do |pid|
SimpleCov.start do
# This needs a unique name so it won't be ovewritten
SimpleCov.command_name "#{SimpleCov.command_name} (subprocess: #{pid})"
# be quiet, the parent process will be in charge of using the regular formatter and checking coverage totals
SimpleCov.print_error_status = false
SimpleCov.formatter SimpleCov::Formatter::SimpleFormatter
SimpleCov.minimum_coverage 0
# start
SimpleCov.start
end
end
233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/simplecov/configuration.rb', line 233 def at_fork(&block) @at_fork = block if block_given? @at_fork ||= lambda { |pid| # This needs a unique name so it won't be ovewritten SimpleCov.command_name "#{SimpleCov.command_name} (subprocess: #{pid})" # be quiet, the parent process will be in charge of using the regular formatter and checking coverage totals SimpleCov.print_error_status = false SimpleCov.formatter SimpleCov::Formatter::SimpleFormatter SimpleCov.minimum_coverage 0 # start SimpleCov.start } end |
#branch_coverage? ⇒ Boolean
404 405 406 |
# File 'lib/simplecov/configuration.rb', line 404 def branch_coverage? branch_coverage_supported? && coverage_criterion_enabled?(:branch) end |
#clear_coverage_criteria ⇒ Object
400 401 402 |
# File 'lib/simplecov/configuration.rb', line 400 def clear_coverage_criteria @coverage_criteria = nil end |
#command_name(name = nil) ⇒ Object
The name of the command (a.k.a. Test Suite) currently running. Used for result merging and caching. It first tries to make a guess based upon the command line arguments the current test suite is running on and should automatically detect unit tests, functional tests, integration tests, rpsec and cucumber and label them properly. If it fails to recognize the current command, the command name is set to the shell command that the current suite is running on.
You can specify it manually with SimpleCov.command_name(“test:units”) - please also check out the corresponding section in README.rdoc
87 88 89 90 91 |
# File 'lib/simplecov/configuration.rb', line 87 def command_name(name = nil) @name = name unless name.nil? @name ||= SimpleCov::CommandGuesser.guess @name end |
#configure(&block) ⇒ Object
Allows you to configure simplecov in a block instead of prepending SimpleCov to all config methods you’re calling.
SimpleCov.configure do
add_filter 'foobar'
end
This is equivalent to SimpleCov.add_filter ‘foobar’ and thus makes it easier to set a bunch of configure options at once.
177 178 179 |
# File 'lib/simplecov/configuration.rb', line 177 def configure(&block) Docile.dsl_eval(self, &block) end |
#coverage_criteria ⇒ Object
392 393 394 |
# File 'lib/simplecov/configuration.rb', line 392 def coverage_criteria @coverage_criteria ||= Set[DEFAULT_COVERAGE_CRITERION] end |
#coverage_criterion(criterion = nil) ⇒ Object
Define which coverage criterion should be evaluated.
Possible coverage criteria:
-
:line - coverage based on lines aka has this line been executed?
-
:branch - coverage based on branches aka has this branch (think conditions) been executed?
If not set the default is ‘:line`
378 379 380 381 382 383 384 |
# File 'lib/simplecov/configuration.rb', line 378 def coverage_criterion(criterion = nil) return @coverage_criterion ||= DEFAULT_COVERAGE_CRITERION unless criterion raise_if_criterion_unsupported(criterion) @coverage_criterion = criterion end |
#coverage_criterion_enabled?(criterion) ⇒ Boolean
396 397 398 |
# File 'lib/simplecov/configuration.rb', line 396 def coverage_criterion_enabled?(criterion) coverage_criteria.member?(criterion) end |
#coverage_dir(dir = nil) ⇒ Object
The name of the output and cache directory. Defaults to ‘coverage’
Configure with SimpleCov.coverage_dir(‘cov’)
34 35 36 37 38 39 |
# File 'lib/simplecov/configuration.rb', line 34 def coverage_dir(dir = nil) return @coverage_dir if defined?(@coverage_dir) && dir.nil? @coverage_path = nil # invalidate cache @coverage_dir = (dir || "coverage") end |
#coverage_path ⇒ Object
Returns the full path to the output directory using SimpleCov.root and SimpleCov.coverage_dir, so you can adjust this by configuring those values. Will create the directory if it’s missing
46 47 48 49 50 51 52 |
# File 'lib/simplecov/configuration.rb', line 46 def coverage_path @coverage_path ||= begin coverage_path = File.(coverage_dir, root) FileUtils.mkdir_p coverage_path coverage_path end end |
#coverage_start_arguments_supported? ⇒ Boolean Also known as: branch_coverage_supported?
408 409 410 411 412 413 414 415 416 417 |
# File 'lib/simplecov/configuration.rb', line 408 def coverage_start_arguments_supported? # safe to cache as within one process this value should never # change return @coverage_start_arguments_supported if defined?(@coverage_start_arguments_supported) @coverage_start_arguments_supported = begin require "coverage" !Coverage.method(:start).arity.zero? end end |
#enable_coverage(criterion) ⇒ Object
386 387 388 389 390 |
# File 'lib/simplecov/configuration.rb', line 386 def enable_coverage(criterion) raise_if_criterion_unsupported(criterion) coverage_criteria << criterion end |
#enable_for_subprocesses(value = nil) ⇒ Object
gets or sets the enabled_for_subprocess configuration when true, this will inject SimpleCov code into Process.fork
202 203 204 205 206 |
# File 'lib/simplecov/configuration.rb', line 202 def enable_for_subprocesses(value = nil) return @enable_for_subprocesses if defined?(@enable_for_subprocesses) && value.nil? @enable_for_subprocesses = value || false end |
#enabled_for_subprocesses? ⇒ Boolean
gets the enabled_for_subprocess configuration
209 210 211 |
# File 'lib/simplecov/configuration.rb', line 209 def enabled_for_subprocesses? enable_for_subprocesses end |
#formatters ⇒ Object
Gets the configured formatters.
117 118 119 120 121 122 123 |
# File 'lib/simplecov/configuration.rb', line 117 def formatters if @formatter.is_a?(SimpleCov::Formatter::MultiFormatter) @formatter.formatters else Array(formatter) end end |
#formatters=(formatters) ⇒ Object
Sets the configured formatters.
110 111 112 |
# File 'lib/simplecov/configuration.rb', line 110 def formatters=(formatters) @formatter = SimpleCov::Formatter::MultiFormatter.new(formatters) end |
#maximum_coverage_drop(coverage_drop = nil) ⇒ Object
Defines the maximum coverage drop at once allowed for the testsuite to pass. SimpleCov will return non-zero if the coverage decreases by more than this threshold.
Default is 100% (disabled)
311 312 313 |
# File 'lib/simplecov/configuration.rb', line 311 def maximum_coverage_drop(coverage_drop = nil) @maximum_coverage_drop ||= (coverage_drop || 100).to_f.round(2) end |
#merge_timeout(seconds = nil) ⇒ Object
Defines the maximum age (in seconds) of a resultset to still be included in merged results. i.e. If you run cucumber features, then later rake test, if the stored cucumber resultset is more seconds ago than specified here, it won’t be taken into account when merging (and is also purged from the resultset cache)
Of course, this only applies when merging is active (e.g. SimpleCov.use_merging is not false!)
Default is 600 seconds (10 minutes)
Configure with SimpleCov.merge_timeout(3600) # 1hr
279 280 281 282 |
# File 'lib/simplecov/configuration.rb', line 279 def merge_timeout(seconds = nil) @merge_timeout = seconds if seconds.is_a?(Integer) @merge_timeout ||= 600 end |
#minimum_coverage(coverage = nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/simplecov/configuration.rb', line 292 def minimum_coverage(coverage = nil) return @minimum_coverage ||= {} unless coverage coverage = {DEFAULT_COVERAGE_CRITERION => coverage} if coverage.is_a?(Numeric) coverage.each_key { |criterion| raise_if_criterion_disabled(criterion) } coverage.each_value do |percent| minimum_possible_coverage_exceeded("minimum_coverage") if percent && percent > 100 end @minimum_coverage = coverage end |
#minimum_coverage_by_file(coverage = nil) ⇒ Object
Defines the minimum coverage per file required for the testsuite to pass. SimpleCov will return non-zero if the current coverage of the least covered file is below this threshold.
Default is 0% (disabled)
322 323 324 325 |
# File 'lib/simplecov/configuration.rb', line 322 def minimum_coverage_by_file(coverage = nil) minimum_possible_coverage_exceeded("minimum_coverage_by_file") if coverage && coverage > 100 @minimum_coverage_by_file ||= (coverage || 0).to_f.round(2) end |
#nocov_token(nocov_token = nil) ⇒ Object Also known as: skip_token
Certain code blocks (i.e. Ruby-implementation specific code) can be excluded from the coverage metrics by wrapping it inside # :nocov: comment blocks. The nocov token can be configured to be any other string using this.
Configure with SimpleCov.nocov_token(‘skip’) or it’s alias SimpleCov.skip_token(‘skip’)
140 141 142 143 144 |
# File 'lib/simplecov/configuration.rb', line 140 def nocov_token(nocov_token = nil) return @nocov_token if defined?(@nocov_token) && nocov_token.nil? @nocov_token = (nocov_token || "nocov") end |
#profiles ⇒ Object
Returns the hash of available profiles
157 158 159 |
# File 'lib/simplecov/configuration.rb', line 157 def profiles @profiles ||= SimpleCov::Profiles.new end |
#project_name(new_name = nil) ⇒ Object
Returns the project name - currently assuming the last dirname in the SimpleCov.root is this.
251 252 253 254 255 256 |
# File 'lib/simplecov/configuration.rb', line 251 def project_name(new_name = nil) return @project_name if defined?(@project_name) && @project_name && new_name.nil? @project_name = new_name if new_name.is_a?(String) @project_name ||= File.basename(root.split("/").last).capitalize.tr("_", " ") end |
#refuse_coverage_drop ⇒ Object
Refuses any coverage drop. That is, coverage is only allowed to increase. SimpleCov will return non-zero if the coverage decreases.
331 332 333 |
# File 'lib/simplecov/configuration.rb', line 331 def refuse_coverage_drop maximum_coverage_drop 0 end |
#root(root = nil) ⇒ Object
The root for the project. This defaults to the current working directory.
Configure with SimpleCov.root(‘/my/project/path’)
22 23 24 25 26 27 |
# File 'lib/simplecov/configuration.rb', line 22 def root(root = nil) return @root if defined?(@root) && root.nil? @coverage_path = nil # invalidate cache @root = File.(root || Dir.getwd) end |
#track_files(glob) ⇒ Object
Coverage results will always include files matched by this glob, whether or not they were explicitly required. Without this, un-required files will not be present in the final report.
59 60 61 |
# File 'lib/simplecov/configuration.rb', line 59 def track_files(glob) @tracked_files = glob end |
#tracked_files ⇒ Object
Returns the glob that will be used to include files that were not explicitly required.
67 68 69 |
# File 'lib/simplecov/configuration.rb', line 67 def tracked_files @tracked_files if defined?(@tracked_files) end |
#use_merging(use = nil) ⇒ Object
Defines whether to use result merging so all your test suites (test:units, test:functionals, cucumber, …) are joined and combined into a single coverage report
262 263 264 265 |
# File 'lib/simplecov/configuration.rb', line 262 def use_merging(use = nil) @use_merging = use unless use.nil? @use_merging = true unless defined?(@use_merging) && @use_merging == false end |