Skip to content

Commit

Permalink
Load SimpleCov before rspec to collect coverage correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Jun 28, 2023
1 parent 2a1579b commit 496f04b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
8 changes: 6 additions & 2 deletions .simplecov_spawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

SimpleCov.command_name 'spawn'
SimpleCov.at_fork.call(Process.pid)
SimpleCov.formatter SimpleCov::Formatter::CoberturaFormatter
SimpleCov.start
SimpleCov.formatter SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::CoberturaFormatter,

Check notice

Code scanning / Rubocop

Checks the indentation of the first element in an array literal. Note

Layout/FirstArrayElementIndentation: Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.
])

Check notice

Code scanning / Rubocop

Checks the indentation of the first element in an array literal. Note

Layout/FirstArrayElementIndentation: Indent the right bracket the same as the first position after the preceding left parenthesis.
SimpleCov.start do |pid|

Check warning

Code scanning / Rubocop

Checks for unused block arguments. Warning

Lint/UnusedBlockArgument: Unused block argument - pid. You can omit the argument if you don't care about it.
add_filter "/spec/"

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
end
end
10 changes: 10 additions & 0 deletions scripts/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby

Check notice

Code scanning / Rubocop

Add the frozen_string_literal comment to the top of files to help transition to frozen string literals by default. Note

Style/FrozenStringLiteralComment: Missing frozen string literal comment.

# (The MIT License)
# Copyright (c) 2012 Chad Humphries, David Chelimsky, Myron Marston
# Copyright (c) 2009 Chad Humphries, David Chelimsky
# Copyright (c) 2006 David Chelimsky, The RSpec Development Team
# Copyright (c) 2005 Steven Baker

require 'rspec/core'
RSpec::Core::Runner.invoke
48 changes: 48 additions & 0 deletions scripts/rspec_with_simplecov
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env ruby

Check notice

Code scanning / Rubocop

Add the frozen_string_literal comment to the top of files to help transition to frozen string literals by default. Note

Style/FrozenStringLiteralComment: Missing frozen string literal comment.

# (The MIT License)
# Copyright (c) 2012 Chad Humphries, David Chelimsky, Myron Marston
# Copyright (c) 2009 Chad Humphries, David Chelimsky
# Copyright (c) 2006 David Chelimsky, The RSpec Development Team
# Copyright (c) 2005 Steven Baker

# Turn on verbose to make sure we not generating any ruby warning
$VERBOSE = true

# So our "did they run the rspec command?" detection logic thinks
# that we run `rspec`.
$0 = "rspec"

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

# This is necessary for when `--standalone` is being used.
$:.unshift File.expand_path '../../bundle', __FILE__

Check notice

Code scanning / Rubocop

Avoid Perl-style global variables. Note

Style/SpecialGlobalVars: Prefer $LOAD\_PATH over $:.

Check notice

Code scanning / Rubocop

Use `expand_path(__dir__)` instead of `expand_path('..', __FILE__)`. Note

Style/ExpandPathArguments: Use expand\_path('../bundle', \_\_dir\_\_) instead of expand\_path('../../bundle', \_\_FILE\_\_).

# For the travis build we put the bundle directory up a directory
# so it can be shared among the repos for faster bundle installs.
$:.unshift File.expand_path '../../../bundle', __FILE__

Check notice

Code scanning / Rubocop

Avoid Perl-style global variables. Note

Style/SpecialGlobalVars: Prefer $LOAD\_PATH over $:.

Check notice

Code scanning / Rubocop

Use `expand_path(__dir__)` instead of `expand_path('..', __FILE__)`. Note

Style/ExpandPathArguments: Use expand\_path('../../bundle', \_\_dir\_\_) instead of expand\_path('../../../bundle', \_\_FILE\_\_).

require 'bundler/setup'

# To use simplecov while running rspec-core's test suite, we must
# load simplecov _before_ loading any of rspec-core's files.
# So, this executable exists purely as a wrapper script that
# first loads simplecov, and then loads rspec.
begin
# Simplecov emits some ruby warnings when loaded, so silence them.
old_verbose, $VERBOSE = $VERBOSE, false

Check notice

Code scanning / Rubocop

Check for simple usages of parallel assignment. It will only warn when the number of variables matches on both sides of the assignment. Note

Style/ParallelAssignment: Do not use parallel assignment.

unless ENV['NO_COVERAGE'] || RUBY_VERSION < '1.9.3'
require 'simplecov'

SimpleCov.start do
add_filter "./bundle/"

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
add_filter "./tmp/"

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
add_filter "./spec/"

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
minimum_coverage(RUBY_PLATFORM == 'java' ? 94 : 97)
end
end
rescue LoadError

Check warning

Code scanning / Rubocop

Don't suppress exceptions. Warning

Lint/SuppressedException: Do not suppress exceptions.
ensure
$VERBOSE = old_verbose
end

load File.expand_path("../rspec", __FILE__)

Check notice

Code scanning / Rubocop

Use `expand_path(__dir__)` instead of `expand_path('..', __FILE__)`. Note

Style/ExpandPathArguments: Use expand\_path('rspec', \_\_dir\_\_) instead of expand\_path('../rspec', \_\_FILE\_\_).

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
7 changes: 5 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def assert_run(*args)
end

def run_tests(*args, command:, openapi: false, output: :yaml)
env = { 'OPENAPI' => ('1' if openapi), 'OPENAPI_OUTPUT' => output.to_s }.compact
env = {
'OPENAPI' => ('1' if openapi),
'OPENAPI_OUTPUT' => output.to_s,
}.compact
Bundler.public_send(Bundler.respond_to?(:with_unbundled_env) ? :with_unbundled_env : :with_clean_env) do
Dir.chdir(repo_root) do
assert_run env, 'bundle', 'exec', command, '-r./.simplecov_spawn', *args
Expand All @@ -23,7 +26,7 @@ def run_tests(*args, command:, openapi: false, output: :yaml)
end

def rspec(*args, openapi: false, output: :yaml)
run_tests(*args, command: 'rspec', openapi: openapi, output: output)
run_tests(*args, command: 'scripts/rspec_with_simplecov', openapi: openapi, output: output)
end

def minitest(*args, openapi: false, output: :yaml)
Expand Down

0 comments on commit 496f04b

Please sign in to comment.