Class: Minitest::ExtensibleBacktraceFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/extensible_backtrace_filter.rb

Overview

Filters backtraces of exceptions that may arise when running tests.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExtensibleBacktraceFilter

Creates a new backtrace filter.


21
22
23
# File 'lib/minitest/extensible_backtrace_filter.rb', line 21

def initialize
  @filters = []
end

Class Method Details

.default_filterMinitest::ExtensibleBacktraceFilter

Returns the default filter.

The default filter will filter out all Minitest and minitest-reporters lines.


10
11
12
13
14
15
16
17
18
# File 'lib/minitest/extensible_backtrace_filter.rb', line 10

def self.default_filter
  unless defined? @default_filter
    filter = self.new
    filter.add_filter(/lib\/minitest/)
    @default_filter = filter
  end

  @default_filter
end

Instance Method Details

#add_filter(regex)

Adds a filter.

Parameters:

  • regex (Regex)

    the filter


28
29
30
# File 'lib/minitest/extensible_backtrace_filter.rb', line 28

def add_filter(regex)
  @filters << regex
end

#filter(backtrace) ⇒ Array

Note:

This logic is based off of Minitest's #filter_backtrace.

Filters a backtrace.

This will add new lines to the new backtrace until a filtered line is encountered. If there were lines added to the new backtrace, it returns those lines. However, if the first line in the backtrace was filtered, resulting in an empty backtrace, it returns all lines that would have been unfiltered. If that in turn does not contain any lines, it returns the original backtrace.

Parameters:

  • backtrace (Array)

    the backtrace to filter

Returns:

  • (Array)

    the filtered backtrace


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/minitest/extensible_backtrace_filter.rb', line 52

def filter(backtrace)
  result = []
  return result unless backtrace

  backtrace.each do |line|
    break if filters?(line)
    result << line
  end

  result = backtrace.reject { |line| filters?(line) } if result.empty?
  result = backtrace.dup if result.empty?

  result
end

#filters?(str) ⇒ Boolean

Determines if the string would be filtered.

Parameters:

  • str (String)

Returns:

  • (Boolean)

36
37
38
# File 'lib/minitest/extensible_backtrace_filter.rb', line 36

def filters?(str)
  @filters.any? { |filter| str =~ filter }
end