Module: SimpleCov::Configuration

Defined in:
lib/simplecov/configuration.rb

Overview

rubocop:disable ModuleLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filtersObject

Returns the list of configured filters. Add filters using SimpleCov.add_filter.



68
69
70
# File 'lib/simplecov/configuration.rb', line 68

def filters
  @filters ||= []
end

#formatter(formatter = nil) ⇒ Object

Gets or sets the configured formatter.

Configure with: SimpleCov.formatter(SimpleCov::Formatter::SimpleFormatter)



92
93
94
95
96
97
# File 'lib/simplecov/configuration.rb', line 92

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

#groupsObject

Returns the configured groups. Add groups using SimpleCov.add_group



133
134
135
# File 'lib/simplecov/configuration.rb', line 133

def groups
  @groups ||= {}
end

Instance Method Details

#adaptersObject



144
145
146
147
# File 'lib/simplecov/configuration.rb', line 144

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



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

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).



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

def add_group(group_name, filter_argument = nil, &filter_proc)
  groups[group_name] = parse_filter(filter_argument, &filter_proc)
end

#at_exit(&block) ⇒ Object

Gets or sets the behavior to process coverage results.

By default, it will call SimpleCov.result.format!

Configure with:

SimpleCov.at_exit do
  puts "Coverage done"
  SimpleCov.result.format!
end


177
178
179
180
181
# File 'lib/simplecov/configuration.rb', line 177

def at_exit(&block)
  return proc {} unless running || block_given?
  @at_exit = block if block_given?
  @at_exit ||= proc { SimpleCov.result.format! }
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



81
82
83
84
85
# File 'lib/simplecov/configuration.rb', line 81

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.



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

def configure(&block)
  return false unless SimpleCov.usable?
  Docile.dsl_eval(self, &block)
end

#coverage_dir(dir = nil) ⇒ Object

The name of the output and cache directory. Defaults to ‘coverage’

Configure with SimpleCov.coverage_dir(‘cov’)



29
30
31
32
33
# File 'lib/simplecov/configuration.rb', line 29

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_pathObject

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



40
41
42
43
44
45
46
# File 'lib/simplecov/configuration.rb', line 40

def coverage_path
  @coverage_path ||= begin
    coverage_path = File.expand_path(coverage_dir, root)
    FileUtils.mkdir_p coverage_path
    coverage_path
  end
end

#formattersObject

Gets the configured formatters.



109
110
111
112
113
114
115
# File 'lib/simplecov/configuration.rb', line 109

def formatters
  if @formatter.is_a?(SimpleCov::Formatter::MultiFormatter)
    @formatter.formatters
  else
    Array(formatter)
  end
end

#formatters=(formatters) ⇒ Object

Sets the configured formatters.



102
103
104
# File 'lib/simplecov/configuration.rb', line 102

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)



235
236
237
# File 'lib/simplecov/configuration.rb', line 235

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



214
215
216
217
# File 'lib/simplecov/configuration.rb', line 214

def merge_timeout(seconds = nil)
  @merge_timeout = seconds if seconds.is_a?(Integer)
  @merge_timeout ||= 600
end

#minimum_coverage(coverage = nil) ⇒ Object

Defines the minimum overall coverage required for the testsuite to pass. SimpleCov will return non-zero if the current coverage is below this threshold.

Default is 0% (disabled)



225
226
227
# File 'lib/simplecov/configuration.rb', line 225

def minimum_coverage(coverage = nil)
  @minimum_coverage ||= (coverage || 0).to_f.round(2)
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)



246
247
248
# File 'lib/simplecov/configuration.rb', line 246

def minimum_coverage_by_file(coverage = nil)
  @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’)



124
125
126
127
# File 'lib/simplecov/configuration.rb', line 124

def nocov_token(nocov_token = nil)
  return @nocov_token if defined?(@nocov_token) && nocov_token.nil?
  @nocov_token = (nocov_token || "nocov")
end

#profilesObject

Returns the hash of available profiles



140
141
142
# File 'lib/simplecov/configuration.rb', line 140

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.



187
188
189
190
191
# File 'lib/simplecov/configuration.rb', line 187

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_dropObject

Refuses any coverage drop. That is, coverage is only allowed to increase. SimpleCov will return non-zero if the coverage decreases.



254
255
256
# File 'lib/simplecov/configuration.rb', line 254

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’)



19
20
21
22
# File 'lib/simplecov/configuration.rb', line 19

def root(root = nil)
  return @root if defined?(@root) && root.nil?
  @root = File.expand_path(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.



53
54
55
# File 'lib/simplecov/configuration.rb', line 53

def track_files(glob)
  @tracked_files = glob
end

#tracked_filesObject

Returns the glob that will be used to include files that were not explicitly required.



61
62
63
# File 'lib/simplecov/configuration.rb', line 61

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



197
198
199
200
# File 'lib/simplecov/configuration.rb', line 197

def use_merging(use = nil)
  @use_merging = use unless use.nil?
  @use_merging = true unless defined?(@use_merging) && @use_merging == false
end