class Minitest::Heat::Output::Backtrace

Builds the collection of tokens for a backtrace when an exception occurs

Constants

DEFAULT_INDENTATION_SPACES
DEFAULT_LINE_COUNT

Attributes

backtrace[RW]
location[RW]

Public Class Methods

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

Public Instance Methods

backtrace_lines() click to toggle source

This should probably be smart about what lines are displayed in a backtrace. Maybe… …it could intelligently display the full back trace? …only the backtrace from the first/last line of project source? …it behaves a little different when it's a broken test vs. a true exception? …it could be smart about subtly flagging the lines that show up in the heat map frequently? …it could be influenced by a “compact” or “robust” reporter super-style? …it's smart about exceptions that were raised outside of the project? …it's smart about highlighting lines of code differently based on whether it's source code, test code, or external code?

# File lib/minitest/heat/output/backtrace.rb, line 58
def backtrace_lines
  project_lines
end
line_count() click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 45
def line_count
  DEFAULT_LINE_COUNT
end
tokens() click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 19
def tokens
  # There could be option to expand and display more than one line of source code for the
  # final backtrace line if it might be relevant/helpful?

  # Iterate over the selected lines from the backtrace
  backtrace_lines.each do |backtrace_line|
    # Get the source code for the line from the backtrace
    source_code = source_code_for(backtrace_line)

    parts = [
      indentation_token,
      path_token(backtrace_line),
      file_and_line_number_token(backtrace_line),
      source_code_line_token(source_code)
    ]

    parts << file_freshness(backtrace_line) if most_recently_modified?(backtrace_line)

    @tokens << parts


  end

  @tokens
end

Private Instance Methods

all_backtrace_lines_from_project?() click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 64
def all_backtrace_lines_from_project?
  backtrace_lines.all? { |line| line.path.include?(project_root_dir) }
end
all_lines() click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 76
def all_lines
  backtrace.parsed_lines.take(line_count)
end
file_and_line_number_token(backtrace_line) click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 105
def file_and_line_number_token(backtrace_line)
  [:default, "#{backtrace_line.file}:#{backtrace_line.number}"]
end
file_freshness(line) click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 113
def file_freshness(line)
  [:bold, " < Most Recently Modified"]
end
indentation() click to toggle source

The number of spaces each line of code should be indented. Currently defaults to 2 in

order to provide visual separation between test failures, but in the future, it could
be configurable in order to save horizontal space and create more compact output. For
example, it could be smart based on line length and total available horizontal terminal
space, or there could be higher-level "display" setting that could have a `:compact`
option that would reduce the space used.

@return [type] [description]

# File lib/minitest/heat/output/backtrace.rb, line 125
def indentation
  DEFAULT_INDENTATION_SPACES
end
indentation_token() click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 91
def indentation_token
  [:default, ' ' * indentation]
end
most_recently_modified?(line) click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 86
def most_recently_modified?(line)
  # If there's more than one line being displayed, and the current line is the freshest
  backtrace_lines.size > 1 && line == backtrace.freshest_project_location
end
path_token(line) click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 95
def path_token(line)
  path = "#{line.path}/"

  # If all of the backtrace lines are from the project, no point in the added redundant
  #  noise of showing the project root directory over and over again
  path = path.delete_prefix(project_root_dir) if all_backtrace_lines_from_project?

  [:muted, path]
end
project_lines() click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 72
def project_lines
  backtrace.project_lines.take(line_count)
end
project_root_dir() click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 68
def project_root_dir
  Dir.pwd
end
source_code_for(line) click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 80
def source_code_for(line)
  filename = "#{line.path}/#{line.file}"

  Minitest::Heat::Source.new(filename, line_number: line.number, max_line_count: 1)
end
source_code_line_token(source_code) click to toggle source
# File lib/minitest/heat/output/backtrace.rb, line 109
def source_code_line_token(source_code)
  [:muted, " `#{source_code.line.strip}`"]
end