Skip to content

Commit

Permalink
Merge pull request #3086 from nevinera/nev--full-test-coverage
Browse files Browse the repository at this point in the history
Restore 100% test coverage
  • Loading branch information
pirj authored and JonRowe committed Sep 5, 2024
1 parent 3ce68cd commit 2d160fa
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ env:
RSPEC_CI: true
# This tells rspec-rails what branch to run in ci
RSPEC_VERSION: '~> 3.13.0'
ENFORCE_COVERAGE_AFTER: "3.2"
jobs:
rubocop:
name: Rubocop
Expand Down Expand Up @@ -130,6 +131,7 @@ jobs:
env:
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
LEGACY_CI: true
NO_COVERAGE: true
JRUBY_OPTS: ${{ matrix.container.jruby_opts || '--dev' }}
NO_COVERAGE: true
steps:
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/core/bisect/fork_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def dispatch_specs(run_descriptor)
private

def run_specs(run_descriptor)
# :nocov: - Executed in a forked process, by integration/bisect_spec
$stdout = $stderr = @spec_output
formatter = CaptureFormatter.new(run_descriptor.failed_example_ids)

Expand All @@ -125,6 +126,7 @@ def run_specs(run_descriptor)
else
@channel.send(latest_run_results)
end
# :nocov:
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/core/configuration_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ def options_file_as_erb_string(path)
if RUBY_VERSION >= '2.6'
ERB.new(File.read(path), :trim_mode => '-').result(binding)
else
# :nocov:
ERB.new(File.read(path), nil, '-').result(binding)
# :nocov:
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/rspec/core/did_you_mean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@ def initialize(relative_file_name)

if defined?(::DidYouMean::SpellChecker)
# provide probable suggestions
# :nocov: - not installed on CI
def call
checker = ::DidYouMean::SpellChecker.new(:dictionary => Dir["spec/**/*.rb"])
probables = checker.correct(relative_file_name.sub('./', ''))[0..2]
return '' unless probables.any?

formats probables
end
# :nocov:
else
# return a hint if API for ::DidYouMean::SpellChecker not supported
# :nocov:
def call
"\nHint: Install the `did_you_mean` gem in order to provide suggestions for similarly named files."
end
# :nocov:
end

private

# :nocov:
def formats(probables)
rspec_format = probables.map { |s, _| "rspec ./#{s}" }
red_font(top_and_tail rspec_format)
Expand All @@ -41,6 +46,7 @@ def red_font(mytext)
colorizer = ::RSpec::Core::Formatters::ConsoleCodes
colorizer.wrap mytext, :failure
end
# :nocov:
end
end
end
6 changes: 6 additions & 0 deletions lib/rspec/core/formatters/exception_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,13 @@ def exception_message_string(exception)
"A #{exception.class} for which `exception.message.to_s` raises #{other.class}."
end
else
# :nocov:
def exception_message_string(exception)
exception.message.to_s
rescue Exception => other
"A #{exception.class} for which `exception.message.to_s` raises #{other.class}."
end
# :nocov:
end
# rubocop:enable Lint/RescueException

Expand Down Expand Up @@ -252,7 +254,9 @@ def read_failed_lines
rescue SnippetExtractor::NoSuchLineError
["Unable to find matching line in #{file_path}"]
rescue SecurityError
# :nocov: - SecurityError is no longer produced starting in ruby 2.7
["Unable to read failed line"]
# :nocov:
end

def find_failed_line
Expand Down Expand Up @@ -284,9 +288,11 @@ def encoded_description(description)
encoded_string(description)
end
else # for 1.8.7
# :nocov:
def encoded_description(description)
description
end
# :nocov:
end

def exception_backtrace
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/core/formatters/html_snippet_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def lines_around(file, line)
"# Couldn't get snippet for #{file}"
end
rescue SecurityError
# :nocov: - SecurityError is no longer produced starting in ruby 2.7
"# Couldn't get snippet for #{file}"
# :nocov:
end

# @api private
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.relative_path(line)
return nil if line == '-e:1'.freeze
line
rescue SecurityError
# :nocov:
# :nocov: - SecurityError is no longer produced starting in ruby 2.7
nil
# :nocov:
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/core/rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class RakeTask < ::Rake::TaskLib

if RUBY_VERSION < "1.9.0" || Support::Ruby.jruby?
# Run RSpec with a clean (empty) environment is not supported
# :nocov:
def with_clean_environment=(_value)
raise ArgumentError, "Running in a clean environment is not supported on Ruby versions before 1.9.0"
end
Expand All @@ -55,6 +56,7 @@ def with_clean_environment=(_value)
def with_clean_environment
false
end
# :nocov:
else
# Run RSpec with a clean (empty) environment.
attr_accessor :with_clean_environment
Expand Down
2 changes: 1 addition & 1 deletion script/rspec_with_simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ begin
add_filter %r{/bundle/}
add_filter %r{/tmp/}
add_filter %r{/spec/}
minimum_coverage(99)
minimum_coverage(100) if RUBY_VERSION.to_f >= ENV['ENFORCE_COVERAGE_AFTER'].to_f
end
end
rescue LoadError # rubocop:disable Lint/SuppressedException
Expand Down
17 changes: 17 additions & 0 deletions spec/integration/spec_file_load_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@
end
end

it 'nicely handles load errors from --require files' do
run_command "--require ./helper_with_load_error"
expect(last_cmd_exit_status).to eq(error_exit_code)
output = normalize_durations(last_cmd_stdout)
expect(output).to include("An error occurred while loading ./helper_with_load_error.")
expect(output).to include("LoadError:")
end

it 'nicely handles syntax errors from --require files' do
write_file_formatted "helper_with_syntax_error.rb", "3 = hello"

run_command "--require ./helper_with_syntax_error"
expect(last_cmd_exit_status).to eq(error_exit_code)
output = normalize_durations(last_cmd_stdout)
expect(output).to include("While loading ./helper_with_syntax_error a `raise SyntaxError` occurred, RSpec will now quit.")
end

it 'nicely handles load-time errors from --require files' do
write_file_formatted "helper_with_error.rb", "raise 'boom'"

Expand Down
4 changes: 4 additions & 0 deletions spec/rspec/core/bisect/shell_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module RSpec::Core
let(:shell_command) { Bisect::ShellCommand.new(original_cli_args) }
let(:runner) { described_class.new(server, shell_command) }

it "has the expected name" do
expect(described_class.name).to eq(:shell)
end

describe "#run" do
let(:original_cli_args) { %w[ spec/1_spec.rb ] }
let(:target_specs) { %w[ spec/1_spec.rb[1:1] spec/1_spec.rb[1:2] ] }
Expand Down
4 changes: 4 additions & 0 deletions spec/rspec/core/drb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def drb_filter_manager_for(args)
expect(drb_argv_for(%w[--failure-exit-code 2])).to include("--failure-exit-code", "2")
end

it "includes --error-exit-code" do
expect(drb_argv_for(%w[--error-exit-code 5])).to include("--error-exit-code", "5")
end

it "includes --options" do
expect(drb_argv_for(%w[--options custom.opts])).to include("--options", "custom.opts")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/core/formatters/exception_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def read_failed_lines
end
end

context "when backtrace will generate a security error" do
context "when backtrace will generate a security error", :skip => !RSpec::Support::RubyFeatures.supports_taint? do
let(:exception) { instance_double(Exception, :backtrace => [ "#{__FILE__}:#{__LINE__}"]) }

it "is handled gracefully" do
Expand Down
22 changes: 22 additions & 0 deletions spec/rspec/core/memoized_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,28 @@ def supports_value_expectations?
end
end

RSpec.describe 'custom matcher that does not specify if it supports value expectations' do
let(:value_matcher_class) do
Class.new do
def matches?(_actual); true; end
def failure_message_when_negated; "oops"; end
end
end
let(:value_matcher) { value_matcher_class.new }

before { expect(value_matcher).not_to respond_to(:supports_value_expectations?) }

it '`should` does not print a deprecation warning when given a value' do
expect_no_deprecations
expect { should value_matcher }.not_to raise_error
end

it '`should_not` does not print a deprecation warning when given a value' do
expect_no_deprecations
expect { should_not value_matcher }.to raise_error(Exception)
end
end

RSpec.describe 'Module#define_method' do
it 'retains its normal private visibility on Ruby versions where it is normally private', :if => RUBY_VERSION < '2.5' do
a_module = Module.new
Expand Down
3 changes: 2 additions & 1 deletion spec/rspec/core/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ module Core
it "returns nil if passed an unparseable file:line combo" do
expect(Metadata.relative_path("-e:1")).to be_nil
end

# I have no idea what line = line.sub(/\A([^:]+:\d+)$/, '\\1') is supposed to do
it "gracefully returns nil if run in a secure thread" do
it "gracefully returns nil if run in a secure thread", :skip => !RSpec::Support::RubyFeatures.supports_taint? do
# Ensure our call to `File.expand_path` is not cached as that is the insecure operation.
Metadata.instance_eval { @relative_path_regex = nil }

Expand Down

0 comments on commit 2d160fa

Please sign in to comment.