Skip to content

Instantly share code, notes, and snippets.

@ypresto
Last active May 30, 2019 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypresto/249065feea1ef4095d19b4d22f8036dd to your computer and use it in GitHub Desktop.
Save ypresto/249065feea1ef4095d19b4d22f8036dd to your computer and use it in GitHub Desktop.
Prepend specified path to backtrace in rspec output (for rails in subdirectory).
# NOTE: Put this file to spec/support/ .
# Prepend specified path to backtrace and Failure/Error section.
# Useful for multi module repository where rails is placed in the subdirectory.
# Intended for editors with click-to-open-file feature.
#
# For example
# # ./spec/models/your_model_spec.rb:12:in `your_method'
# to
# # ./rails/spec/models/your_model_spec.rb:12:in `your_method'
if ENV['RSPEC_RELATIVE_ROOT']
RSpec.configure do |config|
# https://github.com/rspec/rspec-core/blob/7b6b9c3f2e2878213f97d6fc9e9eb23c323cfe1c/lib/rspec/core/backtrace_formatter.rb#L49
orig_backtrace_line = config.backtrace_formatter.method(:backtrace_line)
config.backtrace_formatter.define_singleton_method(:backtrace_line) do |*args|
orig_backtrace_line.call(*args)&.sub(%r{^\./}, "./#{ENV['RSPEC_RELATIVE_ROOT']}/")
end
end
# https://github.com/rspec/rspec-core/blob/7b6b9c3f2e2878213f97d6fc9e9eb23c323cfe1c/lib/rspec/core/example.rb#L96
RSpec::Core::Example.class_eval do
alias_method :location_rerun_argument_without_relative_root, :location_rerun_argument
def location_rerun_argument
location_rerun_argument_without_relative_root&.sub(%r{^\./}, "./#{ENV['RSPEC_RELATIVE_ROOT']}/")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment