Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract BacktraceFormatters into a module.

This will be used by rspec/rspec-expectations#59.
  • Loading branch information...
commit 39857f30ebc8f19374ece61e3c59cd12b5986f7d 1 parent f2d7105
@myronmarston myronmarston authored
View
19 lib/rspec/core/formatters/base_formatter.rb
@@ -115,31 +115,12 @@ def close
restore_sync_output
end
- def format_backtrace(backtrace, example)
- return "" unless backtrace
- return backtrace if example.metadata[:full_backtrace] == true
-
- if at_exit_index = backtrace.index(RSpec::Core::Runner::AT_EXIT_HOOK_BACKTRACE_LINE)
- backtrace = backtrace[0, at_exit_index]
- end
-
- cleansed = backtrace.map { |line| backtrace_line(line) }.compact
- cleansed.empty? ? backtrace : cleansed
- end
-
protected
def configuration
RSpec.configuration
end
- def backtrace_line(line)
- return nil if configuration.cleaned_from_backtrace?(line)
- RSpec::Core::Metadata::relative_path(line)
- rescue SecurityError
- nil
- end
-
def read_failed_line(exception, example)
unless matching_line = find_failed_line(exception.backtrace, example.file_path)
return "Unable to find matching line from backtrace"
View
2  lib/rspec/core/formatters/base_text_formatter.rb
@@ -156,7 +156,7 @@ def format_caller(caller_info)
end
def dump_backtrace(example)
- format_backtrace(example.execution_result[:exception].backtrace, example).each do |backtrace_info|
+ format_backtrace(example.execution_result[:exception].backtrace, example.metadata).each do |backtrace_info|
output.puts cyan("#{long_padding}# #{backtrace_info}")
end
end
View
29 lib/rspec/core/formatters/helpers.rb
@@ -1,8 +1,34 @@
module RSpec
module Core
- module Formatters
+ module BacktraceFormatter
+ extend self
+
+ def format_backtrace(backtrace, options = {})
+ return "" unless backtrace
+ return backtrace if options[:full_backtrace] == true
+
+ if at_exit_index = backtrace.index(RSpec::Core::Runner::AT_EXIT_HOOK_BACKTRACE_LINE)
+ backtrace = backtrace[0, at_exit_index]
+ end
+
+ cleansed = backtrace.map { |line| backtrace_line(line) }.compact
+ cleansed.empty? ? backtrace : cleansed
+ end
+
+ protected
+ def backtrace_line(line)
+ return nil if RSpec.configuration.cleaned_from_backtrace?(line)
+ RSpec::Core::Metadata::relative_path(line)
+ rescue SecurityError
+ nil
+ end
+ end
+
+ module Formatters
module Helpers
+ include BacktraceFormatter
+
SUB_SECOND_PRECISION = 5
DEFAULT_PRECISION = 2
@@ -31,7 +57,6 @@ def strip_trailing_zeroes(string)
def pluralize(count, string)
"#{count} #{string}#{'s' unless count.to_f == 1}"
end
-
end
end
View
2  lib/rspec/core/formatters/html_formatter.rb
@@ -86,7 +86,7 @@ def example_failed(example)
exception_details = if exception
{
:message => exception.message,
- :backtrace => format_backtrace(exception.backtrace, example).join("\n")
+ :backtrace => format_backtrace(exception.backtrace, example.metadata).join("\n")
}
else
false
View
2  spec/rspec/core/formatters/base_formatter_spec.rb
@@ -91,7 +91,7 @@
end
it "removes lines from rspec and lines that come before the invocation of the at_exit autorun hook" do
- formatter.format_backtrace(backtrace, stub.as_null_object).should eq(["./my_spec.rb:5"])
+ formatter.format_backtrace(backtrace).should eq(["./my_spec.rb:5"])
end
end

4 comments on commit 39857f3

@DouweM

FYI, changing the signature for #format_backtrace broke https://github.com/dgvncsz0f/rspec_formatters. I'm not sure if you follow semver, but if you do: #format_backtrace is a public API, so in a minor version update the signature should not have changed.

@myronmarston

@DouweM -- thanks for pointing this out. This was a completely unintentional API change. We try to follow SemVer, although there are places where we haven't documented very well if an API is public or not, so we're not 100% there.

I just pushed a fix to master to restore this API:

f06254c

I also plan to cut a 2.11.2 release (2.11.1 plus this fix) and a 2.12.1 release (2.12.0 plus this fix) to get the fixes out right away. I won't have the time tonight, though, but hopefully later this weekend.

@DouweM

Perfect, thanks for taking this seriously!

@myronmarston

@DouweM - I just released 2.12.1 with this fix (plus the other bug fixes that were already in master).

It turns out no 2.11.2 release was needed....2.11.1 didn't include the broken API.

Please sign in to comment.
Something went wrong with that request. Please try again.