Class: SimpleCov::RSpec
- Inherits:
-
Object
- Object
- SimpleCov::RSpec
- Defined in:
- lib/simplecov/rspec.rb
Overview
Configure SimpleCov to fail RSpec if the test coverage falls below a given threshold
Configures RSpec to:
-
Fail (and exit with with a non-zero exitcode) if the test coverage is below the configured threshold and
-
(optionally) list the lines of code not covered by tests.
Simply add the line ‘SimpleCov::RSpec.start` in place of `SimpleCov::Start` in the project’s ‘spec_helper.rb`. This line must appear before the project is required.
Constant Summary collapse
- COVERAGE_THRESHOLD =
Environment variable to override coverage_threshold
'COVERAGE_THRESHOLD'
- FAIL_ON_LOW_COVERAGE =
Environment variable to override fail_on_low_coverage
'FAIL_ON_LOW_COVERAGE'
- LIST_UNCOVERED_LINES =
Environment variable to override list_uncovered_lines
'LIST_UNCOVERED_LINES'
- DEFAULT_TEST_COVERAGE_THRESHOLD =
Default value for coverage_threshold
100
- DEFAULT_FAIL_ON_LOW_COVERAGEERAGE =
Default value for fail_on_low_coverage
true
- DEFAULT_LIST_UNCOVERED_LINES =
Default value for list_uncovered_lines
false
Instance Attribute Summary collapse
-
#env ⇒ Hash
readonly
private
Command line environment variables (default: ENV).
-
#simplecov_module ⇒ Module
readonly
private
The SimpleCov module (default: ::SimpleCov).
-
#start_config_block ⇒ Proc
readonly
private
A configuration block to pass to ‘SimpleCov.start`.
Class Method Summary collapse
-
.start(coverage_threshold: 100, fail_on_low_coverage: true, list_uncovered_lines: false, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, &start_config_block) ⇒ Void
Configure and start SimpleCov for RSpec.
Instance Method Summary collapse
-
#coverage_threshold ⇒ Integer
private
The coverage threshold.
-
#fail_on_low_coverage? ⇒ Boolean
private
Whether to fail if the coverage is below the threshold.
-
#list_uncovered_lines? ⇒ Boolean
private
Whether to list the lines not covered by tests.
-
#rspec_dry_run? ⇒ Boolean
private
Whether the rspec run is a dry run.
Instance Attribute Details
#env ⇒ Hash (readonly)
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.
Command line environment variables (default: ENV)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/simplecov/rspec.rb', line 48 class RSpec # rubocop:disable Layout/LineLength # Configure and start SimpleCov for RSpec # # @example Initialize SimpleCov with defaults # SimpleCov::RSpec.start # # @return [Void] # # @api public # # @overload start(coverage_threshold: 100, fail_on_low_coverage: true, list_uncovered_lines: false, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, &start_config_block) # # @param coverage_threshold [Integer] the test coverage threshold (default: 100) # # Coverage below this threshold will cause the rspec to fail if # fail_on_low_coverage is true. # # @param fail_on_low_coverage [Boolean] whether to fail if the coverage is below the threshold (default: true) # # This setting will be read from the environment variable FAIL_ON_LOW_COVERAGE if # it is NOT given in the `.start` method. Setting that environment variable # to 'true', 'yes', 'on', or '1' will cause this setting to be `false` # (the logic is inverted). Any other value will cause this seetting to # be `true`. # # @param list_uncovered_lines [Boolean] whether to list the lines not covered by tests (default: false) # # All lines not covered by tests will be listed if the coverage is below the threshold. # Probably only makes sense to use if the threshold is 100%. # # This setting will be read from the environment variable LIST_UNCOVERED_LINES # if it is NOT given in the `.start` method. Setting that environment variable # to 'true', 'yes', 'on', or '1' will cause this setting to be `true`. Any # other value will cause this setting to be `false`. # # @param start_config_block [Proc] a configuration block to pass to `SimpleCov.start` (default: nil) # # @param rspec_dry_run [Boolean] whether the rspec run is a dry run # # Typically not set by the user. Used for this gem's unit testing. # # If RSpec is being run in dry run mode, test coverage under the threshold will not fail the build. # # The purpose of this is to allow the test coverage to be run in a dry run by an IDE # so it can report failed tests and coverage without reporting that the entire RSpec # run has failed. # # @param simplecov_module [Module] the SimpleCov module (default: ::SimpleCov) # # Typically not set by the user. Used for this gem's unit testing. # # Allows the SimpleCov module to be mocked. # # @param env [Hash] the environment variables (default: ENV) # # Typically not set by the user. Used for this gem's unit testing. # # @example Initialize SimpleCov with a test coverage threshold other than 100% # SimpleCov::RSpec.start(coverage_threshold: 90) # # @example Initialize SimpleCov to not fail the test run if the coverage is below the threshold # SimpleCov::RSpec.start(fail_on_low_coverage: false) # # # OR use an environment variable to override the default # ENV['FAIL_ON_LOW_COVERAGE'] = 'true' # SimpleCov::RSpec.start # # # OR use an environment variable to override the default from the rspec command line # FAIL_ON_LOW_COVERAGE=TRUE rspec # # @example Initialize SimpleCov to list the lines not covered by tests # SimpleCov::RSpec.start(list_uncovered_lines: true) # # # OR use an environment variable to override the default # ENV['LIST_UNCOVERED_LINES'] = 'true' # SimpleCov::RSpec.start # # # OR use an environment variable to override the default from the rspec command line # LIST_UNCOVERED_LINES=TRUE rspec # def self.start(...) = new(...).send(:start) # rubocop:enable Layout/LineLength # Environment variable to override coverage_threshold COVERAGE_THRESHOLD = 'COVERAGE_THRESHOLD' # Environment variable to override fail_on_low_coverage FAIL_ON_LOW_COVERAGE = 'FAIL_ON_LOW_COVERAGE' # Environment variable to override list_uncovered_lines LIST_UNCOVERED_LINES = 'LIST_UNCOVERED_LINES' # Default value for coverage_threshold DEFAULT_TEST_COVERAGE_THRESHOLD = 100 # Default value for fail_on_low_coverage DEFAULT_FAIL_ON_LOW_COVERAGEERAGE = true # Default value for list_uncovered_lines DEFAULT_LIST_UNCOVERED_LINES = false attr_reader :env, :simplecov_module, :start_config_block # The coverage threshold # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Integer] # # @api private # def coverage_threshold return env.fetch(COVERAGE_THRESHOLD).to_i if env.key?(COVERAGE_THRESHOLD) return @coverage_threshold.to_i unless @coverage_threshold.nil? DEFAULT_TEST_COVERAGE_THRESHOLD end # Whether to fail if the coverage is below the threshold # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Boolean] # # @api private # def fail_on_low_coverage? return false if rspec_dry_run? return env_true?(FAIL_ON_LOW_COVERAGE) if env.key?(FAIL_ON_LOW_COVERAGE) return @fail_on_low_coverage unless @fail_on_low_coverage.nil? DEFAULT_FAIL_ON_LOW_COVERAGEERAGE end # Whether to list the lines not covered by tests # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Boolean] # # @api private # def list_uncovered_lines? return env_true?(LIST_UNCOVERED_LINES) if env.key?(LIST_UNCOVERED_LINES) return @list_uncovered_lines unless @list_uncovered_lines.nil? DEFAULT_LIST_UNCOVERED_LINES end # Whether the rspec run is a dry run # # @return [Boolean] # # @api private # def rspec_dry_run? = @rspec_dry_run private # rubocop:disable Metrics/ParameterLists # Create a new SimpleCov::RSpec instance # @see SimpleCov::RSpec.start # @api private def initialize( coverage_threshold: nil, fail_on_low_coverage: nil, list_uncovered_lines: nil, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, simplecov_module: ::SimpleCov, &start_config_block ) @coverage_threshold = coverage_threshold @fail_on_low_coverage = fail_on_low_coverage @list_uncovered_lines = list_uncovered_lines @start_config_block = start_config_block @rspec_dry_run = rspec_dry_run @env = env @simplecov_module = simplecov_module end # rubocop:enable Metrics/ParameterLists # Set the at_exit hook and then configure and start SimpleCov # @return [Void] # @api private def start simplecov_module.at_exit(&at_exit_hook) simplecov_module.start(&start_config_block) end # Called by SimpleCov.at_exit # @return [Proc] # @api private def at_exit_hook lambda do simplecov_module.result.format! output_at_exit_report exit 1 if coverage_below_threshold? && fail_on_low_coverage? end end # Output the at_exit report # @return [Void] # @api private def output_at_exit_report low_coverage_report if show_low_coverage_report? lines_not_covered_report if show_lines_not_covered_report? $stderr.puts if show_low_coverage_report? || show_lines_not_covered_report? end # Whether to show the low coverage report # @return [Boolean] # @api private def show_low_coverage_report? = coverage_below_threshold? # Whether the test coverage is below the threshold # @return [Boolean] # @api private def coverage_below_threshold? = simplecov_module.result.covered_percent < coverage_threshold # Output the low coverage part of the at_exit report # @return [Void] # @api private def low_coverage_report $stderr.puts $stderr.print 'FAIL: ' if fail_on_low_coverage? $stderr.puts "Test coverage is below the low coverage threshold of #{coverage_threshold}%" end # Whether there are uncovered lines # @return [Boolean] # @api private def uncovered_lines_found? = simplecov_module.result.files.any? { |source_file| source_file.missed_lines.any? } # Whether to show lines not covered report # @return [Boolean] # @api private def show_lines_not_covered_report? = list_uncovered_lines? && uncovered_lines_found? # Output the lines not covered part of the at_exit report # @return [Void] # @api private def lines_not_covered_report $stderr.puts $stderr.puts "The following lines were not covered by tests:\n" simplecov_module.result.files.each do |source_file| # SimpleCov::SourceFile source_file.missed_lines.each do |line| # SimpleCov::SourceFile::Line $stderr.puts " ./#{source_file.project_filename}:#{line.number}" end end end # Return `true` if the environment variable is set to a truthy value # # @example # env_true?('LIST_UNCOVERED_LINES') # # @param name [String] the name of the environment variable # @return [Boolean] # @api private # def env_true?(name) value = env.fetch(name, '').downcase %w[yes on true 1].include?(value) end end |
#simplecov_module ⇒ Module (readonly)
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.
The SimpleCov module (default: ::SimpleCov)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/simplecov/rspec.rb', line 48 class RSpec # rubocop:disable Layout/LineLength # Configure and start SimpleCov for RSpec # # @example Initialize SimpleCov with defaults # SimpleCov::RSpec.start # # @return [Void] # # @api public # # @overload start(coverage_threshold: 100, fail_on_low_coverage: true, list_uncovered_lines: false, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, &start_config_block) # # @param coverage_threshold [Integer] the test coverage threshold (default: 100) # # Coverage below this threshold will cause the rspec to fail if # fail_on_low_coverage is true. # # @param fail_on_low_coverage [Boolean] whether to fail if the coverage is below the threshold (default: true) # # This setting will be read from the environment variable FAIL_ON_LOW_COVERAGE if # it is NOT given in the `.start` method. Setting that environment variable # to 'true', 'yes', 'on', or '1' will cause this setting to be `false` # (the logic is inverted). Any other value will cause this seetting to # be `true`. # # @param list_uncovered_lines [Boolean] whether to list the lines not covered by tests (default: false) # # All lines not covered by tests will be listed if the coverage is below the threshold. # Probably only makes sense to use if the threshold is 100%. # # This setting will be read from the environment variable LIST_UNCOVERED_LINES # if it is NOT given in the `.start` method. Setting that environment variable # to 'true', 'yes', 'on', or '1' will cause this setting to be `true`. Any # other value will cause this setting to be `false`. # # @param start_config_block [Proc] a configuration block to pass to `SimpleCov.start` (default: nil) # # @param rspec_dry_run [Boolean] whether the rspec run is a dry run # # Typically not set by the user. Used for this gem's unit testing. # # If RSpec is being run in dry run mode, test coverage under the threshold will not fail the build. # # The purpose of this is to allow the test coverage to be run in a dry run by an IDE # so it can report failed tests and coverage without reporting that the entire RSpec # run has failed. # # @param simplecov_module [Module] the SimpleCov module (default: ::SimpleCov) # # Typically not set by the user. Used for this gem's unit testing. # # Allows the SimpleCov module to be mocked. # # @param env [Hash] the environment variables (default: ENV) # # Typically not set by the user. Used for this gem's unit testing. # # @example Initialize SimpleCov with a test coverage threshold other than 100% # SimpleCov::RSpec.start(coverage_threshold: 90) # # @example Initialize SimpleCov to not fail the test run if the coverage is below the threshold # SimpleCov::RSpec.start(fail_on_low_coverage: false) # # # OR use an environment variable to override the default # ENV['FAIL_ON_LOW_COVERAGE'] = 'true' # SimpleCov::RSpec.start # # # OR use an environment variable to override the default from the rspec command line # FAIL_ON_LOW_COVERAGE=TRUE rspec # # @example Initialize SimpleCov to list the lines not covered by tests # SimpleCov::RSpec.start(list_uncovered_lines: true) # # # OR use an environment variable to override the default # ENV['LIST_UNCOVERED_LINES'] = 'true' # SimpleCov::RSpec.start # # # OR use an environment variable to override the default from the rspec command line # LIST_UNCOVERED_LINES=TRUE rspec # def self.start(...) = new(...).send(:start) # rubocop:enable Layout/LineLength # Environment variable to override coverage_threshold COVERAGE_THRESHOLD = 'COVERAGE_THRESHOLD' # Environment variable to override fail_on_low_coverage FAIL_ON_LOW_COVERAGE = 'FAIL_ON_LOW_COVERAGE' # Environment variable to override list_uncovered_lines LIST_UNCOVERED_LINES = 'LIST_UNCOVERED_LINES' # Default value for coverage_threshold DEFAULT_TEST_COVERAGE_THRESHOLD = 100 # Default value for fail_on_low_coverage DEFAULT_FAIL_ON_LOW_COVERAGEERAGE = true # Default value for list_uncovered_lines DEFAULT_LIST_UNCOVERED_LINES = false attr_reader :env, :simplecov_module, :start_config_block # The coverage threshold # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Integer] # # @api private # def coverage_threshold return env.fetch(COVERAGE_THRESHOLD).to_i if env.key?(COVERAGE_THRESHOLD) return @coverage_threshold.to_i unless @coverage_threshold.nil? DEFAULT_TEST_COVERAGE_THRESHOLD end # Whether to fail if the coverage is below the threshold # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Boolean] # # @api private # def fail_on_low_coverage? return false if rspec_dry_run? return env_true?(FAIL_ON_LOW_COVERAGE) if env.key?(FAIL_ON_LOW_COVERAGE) return @fail_on_low_coverage unless @fail_on_low_coverage.nil? DEFAULT_FAIL_ON_LOW_COVERAGEERAGE end # Whether to list the lines not covered by tests # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Boolean] # # @api private # def list_uncovered_lines? return env_true?(LIST_UNCOVERED_LINES) if env.key?(LIST_UNCOVERED_LINES) return @list_uncovered_lines unless @list_uncovered_lines.nil? DEFAULT_LIST_UNCOVERED_LINES end # Whether the rspec run is a dry run # # @return [Boolean] # # @api private # def rspec_dry_run? = @rspec_dry_run private # rubocop:disable Metrics/ParameterLists # Create a new SimpleCov::RSpec instance # @see SimpleCov::RSpec.start # @api private def initialize( coverage_threshold: nil, fail_on_low_coverage: nil, list_uncovered_lines: nil, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, simplecov_module: ::SimpleCov, &start_config_block ) @coverage_threshold = coverage_threshold @fail_on_low_coverage = fail_on_low_coverage @list_uncovered_lines = list_uncovered_lines @start_config_block = start_config_block @rspec_dry_run = rspec_dry_run @env = env @simplecov_module = simplecov_module end # rubocop:enable Metrics/ParameterLists # Set the at_exit hook and then configure and start SimpleCov # @return [Void] # @api private def start simplecov_module.at_exit(&at_exit_hook) simplecov_module.start(&start_config_block) end # Called by SimpleCov.at_exit # @return [Proc] # @api private def at_exit_hook lambda do simplecov_module.result.format! output_at_exit_report exit 1 if coverage_below_threshold? && fail_on_low_coverage? end end # Output the at_exit report # @return [Void] # @api private def output_at_exit_report low_coverage_report if show_low_coverage_report? lines_not_covered_report if show_lines_not_covered_report? $stderr.puts if show_low_coverage_report? || show_lines_not_covered_report? end # Whether to show the low coverage report # @return [Boolean] # @api private def show_low_coverage_report? = coverage_below_threshold? # Whether the test coverage is below the threshold # @return [Boolean] # @api private def coverage_below_threshold? = simplecov_module.result.covered_percent < coverage_threshold # Output the low coverage part of the at_exit report # @return [Void] # @api private def low_coverage_report $stderr.puts $stderr.print 'FAIL: ' if fail_on_low_coverage? $stderr.puts "Test coverage is below the low coverage threshold of #{coverage_threshold}%" end # Whether there are uncovered lines # @return [Boolean] # @api private def uncovered_lines_found? = simplecov_module.result.files.any? { |source_file| source_file.missed_lines.any? } # Whether to show lines not covered report # @return [Boolean] # @api private def show_lines_not_covered_report? = list_uncovered_lines? && uncovered_lines_found? # Output the lines not covered part of the at_exit report # @return [Void] # @api private def lines_not_covered_report $stderr.puts $stderr.puts "The following lines were not covered by tests:\n" simplecov_module.result.files.each do |source_file| # SimpleCov::SourceFile source_file.missed_lines.each do |line| # SimpleCov::SourceFile::Line $stderr.puts " ./#{source_file.project_filename}:#{line.number}" end end end # Return `true` if the environment variable is set to a truthy value # # @example # env_true?('LIST_UNCOVERED_LINES') # # @param name [String] the name of the environment variable # @return [Boolean] # @api private # def env_true?(name) value = env.fetch(name, '').downcase %w[yes on true 1].include?(value) end end |
#start_config_block ⇒ Proc (readonly)
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.
A configuration block to pass to ‘SimpleCov.start`
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/simplecov/rspec.rb', line 48 class RSpec # rubocop:disable Layout/LineLength # Configure and start SimpleCov for RSpec # # @example Initialize SimpleCov with defaults # SimpleCov::RSpec.start # # @return [Void] # # @api public # # @overload start(coverage_threshold: 100, fail_on_low_coverage: true, list_uncovered_lines: false, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, &start_config_block) # # @param coverage_threshold [Integer] the test coverage threshold (default: 100) # # Coverage below this threshold will cause the rspec to fail if # fail_on_low_coverage is true. # # @param fail_on_low_coverage [Boolean] whether to fail if the coverage is below the threshold (default: true) # # This setting will be read from the environment variable FAIL_ON_LOW_COVERAGE if # it is NOT given in the `.start` method. Setting that environment variable # to 'true', 'yes', 'on', or '1' will cause this setting to be `false` # (the logic is inverted). Any other value will cause this seetting to # be `true`. # # @param list_uncovered_lines [Boolean] whether to list the lines not covered by tests (default: false) # # All lines not covered by tests will be listed if the coverage is below the threshold. # Probably only makes sense to use if the threshold is 100%. # # This setting will be read from the environment variable LIST_UNCOVERED_LINES # if it is NOT given in the `.start` method. Setting that environment variable # to 'true', 'yes', 'on', or '1' will cause this setting to be `true`. Any # other value will cause this setting to be `false`. # # @param start_config_block [Proc] a configuration block to pass to `SimpleCov.start` (default: nil) # # @param rspec_dry_run [Boolean] whether the rspec run is a dry run # # Typically not set by the user. Used for this gem's unit testing. # # If RSpec is being run in dry run mode, test coverage under the threshold will not fail the build. # # The purpose of this is to allow the test coverage to be run in a dry run by an IDE # so it can report failed tests and coverage without reporting that the entire RSpec # run has failed. # # @param simplecov_module [Module] the SimpleCov module (default: ::SimpleCov) # # Typically not set by the user. Used for this gem's unit testing. # # Allows the SimpleCov module to be mocked. # # @param env [Hash] the environment variables (default: ENV) # # Typically not set by the user. Used for this gem's unit testing. # # @example Initialize SimpleCov with a test coverage threshold other than 100% # SimpleCov::RSpec.start(coverage_threshold: 90) # # @example Initialize SimpleCov to not fail the test run if the coverage is below the threshold # SimpleCov::RSpec.start(fail_on_low_coverage: false) # # # OR use an environment variable to override the default # ENV['FAIL_ON_LOW_COVERAGE'] = 'true' # SimpleCov::RSpec.start # # # OR use an environment variable to override the default from the rspec command line # FAIL_ON_LOW_COVERAGE=TRUE rspec # # @example Initialize SimpleCov to list the lines not covered by tests # SimpleCov::RSpec.start(list_uncovered_lines: true) # # # OR use an environment variable to override the default # ENV['LIST_UNCOVERED_LINES'] = 'true' # SimpleCov::RSpec.start # # # OR use an environment variable to override the default from the rspec command line # LIST_UNCOVERED_LINES=TRUE rspec # def self.start(...) = new(...).send(:start) # rubocop:enable Layout/LineLength # Environment variable to override coverage_threshold COVERAGE_THRESHOLD = 'COVERAGE_THRESHOLD' # Environment variable to override fail_on_low_coverage FAIL_ON_LOW_COVERAGE = 'FAIL_ON_LOW_COVERAGE' # Environment variable to override list_uncovered_lines LIST_UNCOVERED_LINES = 'LIST_UNCOVERED_LINES' # Default value for coverage_threshold DEFAULT_TEST_COVERAGE_THRESHOLD = 100 # Default value for fail_on_low_coverage DEFAULT_FAIL_ON_LOW_COVERAGEERAGE = true # Default value for list_uncovered_lines DEFAULT_LIST_UNCOVERED_LINES = false attr_reader :env, :simplecov_module, :start_config_block # The coverage threshold # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Integer] # # @api private # def coverage_threshold return env.fetch(COVERAGE_THRESHOLD).to_i if env.key?(COVERAGE_THRESHOLD) return @coverage_threshold.to_i unless @coverage_threshold.nil? DEFAULT_TEST_COVERAGE_THRESHOLD end # Whether to fail if the coverage is below the threshold # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Boolean] # # @api private # def fail_on_low_coverage? return false if rspec_dry_run? return env_true?(FAIL_ON_LOW_COVERAGE) if env.key?(FAIL_ON_LOW_COVERAGE) return @fail_on_low_coverage unless @fail_on_low_coverage.nil? DEFAULT_FAIL_ON_LOW_COVERAGEERAGE end # Whether to list the lines not covered by tests # # Searches the ENV, the value given in the `.start` method, and the default value # and returns the first value found. # # @return [Boolean] # # @api private # def list_uncovered_lines? return env_true?(LIST_UNCOVERED_LINES) if env.key?(LIST_UNCOVERED_LINES) return @list_uncovered_lines unless @list_uncovered_lines.nil? DEFAULT_LIST_UNCOVERED_LINES end # Whether the rspec run is a dry run # # @return [Boolean] # # @api private # def rspec_dry_run? = @rspec_dry_run private # rubocop:disable Metrics/ParameterLists # Create a new SimpleCov::RSpec instance # @see SimpleCov::RSpec.start # @api private def initialize( coverage_threshold: nil, fail_on_low_coverage: nil, list_uncovered_lines: nil, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, simplecov_module: ::SimpleCov, &start_config_block ) @coverage_threshold = coverage_threshold @fail_on_low_coverage = fail_on_low_coverage @list_uncovered_lines = list_uncovered_lines @start_config_block = start_config_block @rspec_dry_run = rspec_dry_run @env = env @simplecov_module = simplecov_module end # rubocop:enable Metrics/ParameterLists # Set the at_exit hook and then configure and start SimpleCov # @return [Void] # @api private def start simplecov_module.at_exit(&at_exit_hook) simplecov_module.start(&start_config_block) end # Called by SimpleCov.at_exit # @return [Proc] # @api private def at_exit_hook lambda do simplecov_module.result.format! output_at_exit_report exit 1 if coverage_below_threshold? && fail_on_low_coverage? end end # Output the at_exit report # @return [Void] # @api private def output_at_exit_report low_coverage_report if show_low_coverage_report? lines_not_covered_report if show_lines_not_covered_report? $stderr.puts if show_low_coverage_report? || show_lines_not_covered_report? end # Whether to show the low coverage report # @return [Boolean] # @api private def show_low_coverage_report? = coverage_below_threshold? # Whether the test coverage is below the threshold # @return [Boolean] # @api private def coverage_below_threshold? = simplecov_module.result.covered_percent < coverage_threshold # Output the low coverage part of the at_exit report # @return [Void] # @api private def low_coverage_report $stderr.puts $stderr.print 'FAIL: ' if fail_on_low_coverage? $stderr.puts "Test coverage is below the low coverage threshold of #{coverage_threshold}%" end # Whether there are uncovered lines # @return [Boolean] # @api private def uncovered_lines_found? = simplecov_module.result.files.any? { |source_file| source_file.missed_lines.any? } # Whether to show lines not covered report # @return [Boolean] # @api private def show_lines_not_covered_report? = list_uncovered_lines? && uncovered_lines_found? # Output the lines not covered part of the at_exit report # @return [Void] # @api private def lines_not_covered_report $stderr.puts $stderr.puts "The following lines were not covered by tests:\n" simplecov_module.result.files.each do |source_file| # SimpleCov::SourceFile source_file.missed_lines.each do |line| # SimpleCov::SourceFile::Line $stderr.puts " ./#{source_file.project_filename}:#{line.number}" end end end # Return `true` if the environment variable is set to a truthy value # # @example # env_true?('LIST_UNCOVERED_LINES') # # @param name [String] the name of the environment variable # @return [Boolean] # @api private # def env_true?(name) value = env.fetch(name, '').downcase %w[yes on true 1].include?(value) end end |
Class Method Details
.start(coverage_threshold: 100, fail_on_low_coverage: true, list_uncovered_lines: false, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, &start_config_block) ⇒ Void
Configure and start SimpleCov for RSpec
130 |
# File 'lib/simplecov/rspec.rb', line 130 def self.start(...) = new(...).send(:start) |
Instance Method Details
#coverage_threshold ⇒ Integer
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.
The coverage threshold
Searches the ENV, the value given in the ‘.start` method, and the default value and returns the first value found.
163 164 165 166 167 168 169 |
# File 'lib/simplecov/rspec.rb', line 163 def coverage_threshold return env.fetch(COVERAGE_THRESHOLD).to_i if env.key?(COVERAGE_THRESHOLD) return @coverage_threshold.to_i unless @coverage_threshold.nil? DEFAULT_TEST_COVERAGE_THRESHOLD end |
#fail_on_low_coverage? ⇒ 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.
Whether to fail if the coverage is below the threshold
Searches the ENV, the value given in the ‘.start` method, and the default value and returns the first value found.
180 181 182 183 184 185 186 187 188 |
# File 'lib/simplecov/rspec.rb', line 180 def fail_on_low_coverage? return false if rspec_dry_run? return env_true?(FAIL_ON_LOW_COVERAGE) if env.key?(FAIL_ON_LOW_COVERAGE) return @fail_on_low_coverage unless @fail_on_low_coverage.nil? DEFAULT_FAIL_ON_LOW_COVERAGEERAGE end |
#list_uncovered_lines? ⇒ 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.
Whether to list the lines not covered by tests
Searches the ENV, the value given in the ‘.start` method, and the default value and returns the first value found.
199 200 201 202 203 204 205 |
# File 'lib/simplecov/rspec.rb', line 199 def list_uncovered_lines? return env_true?(LIST_UNCOVERED_LINES) if env.key?(LIST_UNCOVERED_LINES) return @list_uncovered_lines unless @list_uncovered_lines.nil? DEFAULT_LIST_UNCOVERED_LINES end |
#rspec_dry_run? ⇒ 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.
Whether the rspec run is a dry run
213 |
# File 'lib/simplecov/rspec.rb', line 213 def rspec_dry_run? = @rspec_dry_run |