Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow config for rerun location as line number for shared or nested examples #3085

7 changes: 7 additions & 0 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ def pending_failure_output=(mode)
@pending_failure_output = mode
end

# @macro use_line_number_for_failed_spec_location_rerun
# Display the line number (`my_spec.rb:10`), as opposed to the example id (`my_spec.rb[1:1:1]`)
# for shared or nested examples (defaults to `false`).
# return [Boolean]
add_setting :use_line_number_for_failed_spec_location_rerun
JonRowe marked this conversation as resolved.
Show resolved Hide resolved

# Determines which bisect runner implementation gets used to run subsets
# of the suite during a bisection. Your choices are:
#
Expand Down Expand Up @@ -580,6 +586,7 @@ def initialize
@world = World::Null
@shared_context_metadata_behavior = :trigger_inclusion
@pending_failure_output = :full
@use_line_number_for_failed_spec_location_rerun = false

define_built_in_hooks
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/core/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ def fully_formatted(colorizer=::RSpec::Core::Formatters::ConsoleCodes)

def rerun_argument_for(example)
location = example.location_rerun_argument

return location unless duplicate_rerun_locations.include?(location)
return location if RSpec.configuration.use_line_number_for_failed_spec_location_rerun
conditionally_quote(example.id)
end

Expand Down
135 changes: 135 additions & 0 deletions spec/integration/location_rerun_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
require 'support/aruba_support'

RSpec.describe 'Failed spec rerun location' do

kykyi marked this conversation as resolved.
Show resolved Hide resolved
include_context "aruba support"

before do
setup_aruba

# Setup some shared examples and call them in a separate file
# from where they are called to demonstrate how nested example ids work
write_file "some_examples.rb", "
RSpec.shared_examples_for 'a failing spec' do
it 'fails' do
expect(1).to eq(2)
end

context 'when you reverse it' do
it 'still fails' do
expect(2).to eq(1)
end
end
end
"

file = cd('.') { "#{Dir.pwd}/some_examples.rb" }
load file

write_file "non_local_shared_examples_spec.rb", "
RSpec.describe do
context 'the first context' do
it_behaves_like 'a failing spec'
end

context 'the second context' do
it_behaves_like 'a failing spec'
end
end
"
# Setup some shared examples in the same file as where they are called
write_file "local_shared_examples_spec.rb", "
RSpec.describe do
shared_examples_for 'a failing spec' do
it 'fails' do
expect(1).to eq(2)
end

context 'when you reverse it' do
it 'still fails' do
expect(2).to eq(1)
end
end
end

context 'the first context' do
it_behaves_like 'a failing spec'
end

context 'the second context' do
it_behaves_like 'a failing spec'
end
end
"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you convert the files to use the same unident trick, should neatedn them up to as theres a stray miasligned "

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the indents manually @JonRowe and have used write_file_formatted which calls unindent just to use the existing util 👌

https://github.com/kykyi/rspec-core/blob/39f4fdfeea8ff6c70314ba385a47f20fc370cd2c/spec/support/aruba_support.rb#L72


end

context "when config.use_line_number_for_failed_spec_location_rerun is set to false" do
it 'prints the example id of the failed assertion' do
run_command("#{Dir.pwd}/tmp/aruba/local_shared_examples_spec.rb")

expect(last_cmd_stdout).to include(<<-MSG
Failed examples:

rspec './local_shared_examples_spec.rb[1:1:1:1]' # the first context behaves like a failing spec fails
rspec './local_shared_examples_spec.rb[1:1:1:2:1]' # the first context behaves like a failing spec when you reverse it still fails
rspec './local_shared_examples_spec.rb[1:2:1:1]' # the second context behaves like a failing spec fails
rspec './local_shared_examples_spec.rb[1:2:1:2:1]' # the second context behaves like a failing spec when you reverse it still fails
MSG
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax is a bit gross but allows for the whole failure segment to be asserted on which is nice

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use a pattern elswhere where we define a dedent helper and add a "pillar" to mark text content, I've add this as a suggestion but I did it in the browser so excuse any typos.

)
kykyi marked this conversation as resolved.
Show resolved Hide resolved
end
context "and the shared examples are defined in a separate file" do
kykyi marked this conversation as resolved.
Show resolved Hide resolved
it 'prints the example id of the failed assertion' do
run_command("#{Dir.pwd}/tmp/aruba/non_local_shared_examples_spec.rb")

expect(last_cmd_stdout).to include(<<-MSG
Failed examples:

rspec './non_local_shared_examples_spec.rb[1:1:1:1]' # the first context behaves like a failing spec fails
rspec './non_local_shared_examples_spec.rb[1:1:1:2:1]' # the first context behaves like a failing spec when you reverse it still fails
rspec './non_local_shared_examples_spec.rb[1:2:1:1]' # the second context behaves like a failing spec fails
rspec './non_local_shared_examples_spec.rb[1:2:1:2:1]' # the second context behaves like a failing spec when you reverse it still fails
MSG
)
kykyi marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

context "when config.use_line_number_for_failed_spec_location_rerun is set to true" do
before do
allow(RSpec.configuration).to receive(:use_line_number_for_failed_spec_location_rerun).and_return(true)
end

context "when the shared examples are defined in the same file as the spec" do

it 'prints the line number where the assertion failed in the local file' do
run_command("#{Dir.pwd}/tmp/aruba/local_shared_examples_spec.rb")

expect(last_cmd_stdout).to include(<<-MSG
Failed examples:

rspec ./local_shared_examples_spec.rb:4 # the first context behaves like a failing spec fails
rspec ./local_shared_examples_spec.rb:9 # the first context behaves like a failing spec when you reverse it still fails
rspec ./local_shared_examples_spec.rb:4 # the second context behaves like a failing spec fails
rspec ./local_shared_examples_spec.rb:9 # the second context behaves like a failing spec when you reverse it still fails
MSG
)
kykyi marked this conversation as resolved.
Show resolved Hide resolved
end
end
context "and the shared examples are defined in a separate file" do
kykyi marked this conversation as resolved.
Show resolved Hide resolved
it 'prints the line number where the `it_behaves_like` was called in the local file' do

run_command("#{Dir.pwd}/tmp/aruba/non_local_shared_examples_spec.rb")
expect(last_cmd_stdout).to include(<<-MSG
Failed examples:

rspec ./non_local_shared_examples_spec.rb:4 # the first context behaves like a failing spec fails
rspec ./non_local_shared_examples_spec.rb:4 # the first context behaves like a failing spec when you reverse it still fails
rspec ./non_local_shared_examples_spec.rb:8 # the second context behaves like a failing spec fails
rspec ./non_local_shared_examples_spec.rb:8 # the second context behaves like a failing spec when you reverse it still fails
MSG
)
end
end
end
kykyi marked this conversation as resolved.
Show resolved Hide resolved
end
13 changes: 12 additions & 1 deletion spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'tmpdir'
require 'rspec/support/spec/in_sub_process'

module RSpec::Core
RSpec.describe Configuration do
include RSpec::Support::InSubProcess
Expand Down Expand Up @@ -2979,6 +2978,18 @@ def emulate_not_configured_expectation_framework
end
end

describe "#use_line_number_for_failed_spec_location_rerun" do

kykyi marked this conversation as resolved.
Show resolved Hide resolved
it "defaults to false" do
expect(config.use_line_number_for_failed_spec_location_rerun).to eq false
end

it "is configurable" do
config.use_line_number_for_failed_spec_location_rerun = true
expect(config.use_line_number_for_failed_spec_location_rerun).to eq true
end
end

# assigns files_or_directories_to_run and triggers post-processing
# via `files_to_run`.
def assign_files_or_directories_to_run(*value)
Expand Down
Loading