class Minitest::Heat::Location

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' 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

Attributes

backtrace[R]
test_location[R]

Public Class Methods

new(test_location, backtrace = []) click to toggle source
# File lib/minitest/heat/location.rb, line 19
def initialize(test_location, backtrace = [])
  @test_location = test_location
  @backtrace = Backtrace.new(backtrace)
end

Public Instance Methods

backtrace?() click to toggle source
# File lib/minitest/heat/location.rb, line 168
def backtrace?
  backtrace.parsed_lines.any?
end
broken_test?() click to toggle source

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.

@return [Boolean] true if most relevant file is the same as the test location file

# File lib/minitest/heat/location.rb, line 41
def broken_test?
  !test_file.nil? && test_file == most_relevant_file
end
final_failure_line() click to toggle source

The line number of the `final_file` where the failure originated

@return [Integer] line number

# File lib/minitest/heat/location.rb, line 81
def final_failure_line
  final_location[1]
end
final_file() click to toggle source

The final location of the stacktrace regardless of whether it's from within the project

@return [String] the relative path to the file from the project root

# File lib/minitest/heat/location.rb, line 74
def final_file
  Pathname(final_location[0])
end
final_location() click to toggle source

The line number from within the `test_file` test definition where the failure occurred

@return [Location] the last location from the backtrace or the test location if a backtrace

was not passed to the initializer
# File lib/minitest/heat/location.rb, line 142
def final_location
  backtrace? ? backtrace.final_location : test_location
end
local?() click to toggle source
# File lib/minitest/heat/location.rb, line 32
def local?
  broken_test? || proper_failure?
end
most_relevant_failure_line() click to toggle source

The line number of the `most_relevant_file` where the failure originated

@return [Integer] line number

# File lib/minitest/heat/location.rb, line 67
def most_relevant_failure_line
  most_relevant_location[1]
end
most_relevant_file() click to toggle source

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.

@return [String] the relative path to the file from the project root

# File lib/minitest/heat/location.rb, line 60
def most_relevant_file
  Pathname(most_relevant_location[0])
end
most_relevant_location() click to toggle source

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.

@return [Array] file and line number of the most likely source of the problem

# File lib/minitest/heat/location.rb, line 152
def most_relevant_location
  [
    source_code_location,
    test_location,
    final_location
  ].compact.first
end
project_failure_line() click to toggle source

The line number of the `project_file` where the failure originated

@return [Integer] line number

# File lib/minitest/heat/location.rb, line 95
def project_failure_line
  broken_test? ? test_failure_line || test_definition_line : source_code_failure_line
end
project_file() click to toggle source

The final location of the stacktrace regardless of whether it's from within the project

@return [String] the relative path to the file from the project root

# File lib/minitest/heat/location.rb, line 88
def project_file
  broken_test? ? test_file : source_code_file
end
project_location() click to toggle source
# File lib/minitest/heat/location.rb, line 160
def project_location
  source_code_location || test_location
end
proper_failure?() click to toggle source

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.

@return [Boolean] true if there's a non-test project file in the stacktrace but it's not

a result of a broken test
# File lib/minitest/heat/location.rb, line 50
def proper_failure?
  !source_code_file.nil? && !broken_test?
end
source_code_failure_line() click to toggle source

The line number of the `source_code_file` where the failure originated

@return [Integer] line number

# File lib/minitest/heat/location.rb, line 111
def source_code_failure_line
  return nil unless backtrace.source_code_lines.any?

  backtrace.final_source_code_location.number
end
source_code_file() click to toggle source

The final location from the stacktrace that is within the project directory

@return [String, nil] the relative path to the file from the project root

# File lib/minitest/heat/location.rb, line 102
def source_code_file
  return nil unless backtrace.source_code_lines.any?

  backtrace.final_source_code_location.pathname
end
source_code_location() click to toggle source
# File lib/minitest/heat/location.rb, line 164
def source_code_location
  backtrace.final_source_code_location
end
test_definition_line() click to toggle source

The line number of the `test_file` where the test is defined

@return [Integer] line number

# File lib/minitest/heat/location.rb, line 127
def test_definition_line
  test_location[1].to_s
end
test_failure_line() click to toggle source

The line number from within the `test_file` test definition where the failure occurred

@return [Integer] line number

# File lib/minitest/heat/location.rb, line 134
def test_failure_line
  backtrace.final_test_location&.number || test_definition_line
end
test_file() click to toggle source

The final location from the stacktrace that is within the project's test directory

@return [String, nil] the relative path to the file from the project root

# File lib/minitest/heat/location.rb, line 120
def test_file
  Pathname(test_location[0])
end
to_s() click to toggle source

Prints the pathname and line number of the location most likely to be the source of the

test failure

@return [String] ex. 'path/to/file.rb:12'

# File lib/minitest/heat/location.rb, line 28
def to_s
  "#{most_relevant_file}:#{most_relevant_failure_line}"
end