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
Public Class Methods
# File lib/minitest/heat/output/backtrace.rb, line 13 def initialize(location) @location = location @backtrace = location.backtrace @tokens = [] end
Public Instance Methods
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
# File lib/minitest/heat/output/backtrace.rb, line 45 def line_count DEFAULT_LINE_COUNT end
# 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
# 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
# File lib/minitest/heat/output/backtrace.rb, line 76 def all_lines backtrace.parsed_lines.take(line_count) end
# 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 lib/minitest/heat/output/backtrace.rb, line 113 def file_freshness(line) [:bold, " < Most Recently Modified"] end
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
# File lib/minitest/heat/output/backtrace.rb, line 91 def indentation_token [:default, ' ' * indentation] end
# 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
# 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
# File lib/minitest/heat/output/backtrace.rb, line 72 def project_lines backtrace.project_lines.take(line_count) end
# File lib/minitest/heat/output/backtrace.rb, line 68 def project_root_dir Dir.pwd end
# 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
# File lib/minitest/heat/output/backtrace.rb, line 109 def source_code_line_token(source_code) [:muted, " `#{source_code.line.strip}`"] end