Class: Minitest::Heat::Locations
- Inherits:
-
Object
- Object
- Minitest::Heat::Locations
- Defined in:
- lib/minitest/heat/locations.rb
Overview
Convenience methods for determining the file and line number where the problem occurred.
There are several layers of specificity to help make it easy to communicate the relative
location of the failure:
- 'final' represents the final line of the backtrace regardless of where it is
- 'test_definition' represents where the test is defined
- 'test_failure' represents the last line from the project's tests. It is further differentiated by
the line where the test is defined and the actual line of code in the test that geneated
the failure or exception
- 'source_code' represents the last line from the project's source code
- 'project' represents the last source line, but falls back to the last test line
- 'most_relevant' represents the most specific file to investigate starting with the source
code and then looking to the test code with final line of the backtrace as a fallback
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
readonly
Returns the value of attribute backtrace.
-
#test_definition ⇒ Object
readonly
Returns the value of attribute test_definition.
Instance Method Summary collapse
-
#broken_test? ⇒ Boolean
Knows if the failure is contained within the test.
-
#final ⇒ Location
The line number from within the ‘test_file` test definition where the failure occurred.
- #freshest ⇒ Object
-
#initialize(test_definition_location, backtrace = []) ⇒ Locations
constructor
A new instance of Locations.
-
#most_relevant ⇒ Array
The file most likely to be the source of the underlying problem.
-
#project ⇒ Location
Returns the final project location based on the backtrace if present.
-
#proper_failure? ⇒ Boolean
Knows if the failure occurred in the actual project source code—as opposed to the test or an external piece of code like a gem.
-
#source_code ⇒ Location
Returns the final source code location based on the backtrace.
-
#test_failure ⇒ Location
Returns the final test location based on the backtrace if present.
-
#to_s ⇒ String
Prints the pathname and line number of the location most likely to be the source of the test failure.
Constructor Details
#initialize(test_definition_location, backtrace = []) ⇒ Locations
Returns a new instance of Locations.
20 21 22 23 24 25 |
# File 'lib/minitest/heat/locations.rb', line 20 def initialize(test_definition_location, backtrace = []) test_definition_pathname, test_definition_line_number = test_definition_location @test_definition = ::Minitest::Heat::Location.new(pathname: test_definition_pathname, line_number: test_definition_line_number) @backtrace = Backtrace.new(backtrace) end |
Instance Attribute Details
#backtrace ⇒ Object (readonly)
Returns the value of attribute backtrace.
18 19 20 |
# File 'lib/minitest/heat/locations.rb', line 18 def backtrace @backtrace end |
#test_definition ⇒ Object (readonly)
Returns the value of attribute test_definition.
18 19 20 |
# File 'lib/minitest/heat/locations.rb', line 18 def test_definition @test_definition end |
Instance Method Details
#broken_test? ⇒ Boolean
Knows if the failure is contained within the test. For example, if there’s bad code in a
test, and it raises an exception, then it's really a broken test rather than a proper
faiure.
40 41 42 |
# File 'lib/minitest/heat/locations.rb', line 40 def broken_test? !test_failure.nil? && test_failure == final end |
#final ⇒ Location
The line number from within the ‘test_file` test definition where the failure occurred
100 101 102 |
# File 'lib/minitest/heat/locations.rb', line 100 def final backtrace.locations.any? ? backtrace.locations.first : test_definition end |
#freshest ⇒ Object
67 68 69 |
# File 'lib/minitest/heat/locations.rb', line 67 def freshest backtrace.recently_modified_locations.first end |
#most_relevant ⇒ Array
The file most likely to be the source of the underlying problem. Often, the most recent
backtrace files will be a gem or external library that's failing indirectly as a result
of a problem with local source code (not always, but frequently). In that case, the best
first place to focus is on the code you control.
59 60 61 62 63 64 65 |
# File 'lib/minitest/heat/locations.rb', line 59 def most_relevant [ source_code, test_failure, final ].compact.first end |
#project ⇒ Location
Returns the final project location based on the backtrace if present. Otherwise falls back
to the test location which represents the test definition.
92 93 94 |
# File 'lib/minitest/heat/locations.rb', line 92 def project backtrace.project_locations.any? ? backtrace.project_locations.first : test_definition end |
#proper_failure? ⇒ Boolean
Knows if the failure occurred in the actual project source code—as opposed to the test or
an external piece of code like a gem.
49 50 51 |
# File 'lib/minitest/heat/locations.rb', line 49 def proper_failure? !source_code.nil? && !broken_test? end |
#source_code ⇒ Location
Returns the final source code location based on the backtrace
84 85 86 |
# File 'lib/minitest/heat/locations.rb', line 84 def source_code backtrace.source_code_locations.first end |
#test_failure ⇒ Location
Returns the final test location based on the backtrace if present. Otherwise falls back to
the test location which represents the test definition. The `test_definition` attribute
provides the location of where the test is defined. `test_failure` represents the actual
line from within the test where the problem occurred
77 78 79 |
# File 'lib/minitest/heat/locations.rb', line 77 def test_failure backtrace.test_locations.any? ? backtrace.test_locations.first : test_definition end |
#to_s ⇒ String
Prints the pathname and line number of the location most likely to be the source of the
test failure
31 32 33 |
# File 'lib/minitest/heat/locations.rb', line 31 def to_s "#{most_relevant.absolute_filename}:#{most_relevant.line_number}" end |