Sha256: 2178b1a0075603802e01c2e1aa4aee24931ddd337f799c0d7f7fc18ef3a6095f

Contents?: true

Size: 1.79 KB

Versions: 26

Compression:

Stored size: 1.79 KB

Contents

module MiniTest
  # Filters backtraces of exceptions that may arise when running tests.
  class ExtensibleBacktraceFilter
    # Returns the default filter.
    #
    # The default filter will filter out all MiniTest and minitest-reporters
    # lines.
    #
    # @return [MiniTest::ExtensibleBacktraceFilter]
    def self.default_filter
      unless defined? @default_filter
        filter = self.new
        filter.add_filter(/lib\/minitest/)
        @default_filter = filter
      end

      @default_filter
    end

    # Creates a new backtrace filter.
    def initialize
      @filters = []
    end

    # Adds a filter.
    #
    # @param [Regex] regex the filter
    def add_filter(regex)
      @filters << regex
    end

    # Determines if the string would be filtered.
    #
    # @param [String] str
    # @return [Boolean]
    def filters?(str)
      @filters.any? { |filter| str =~ filter }
    end

    # 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.
    #
    # @param [Array] backtrace the backtrace to filter
    # @return [Array] the filtered backtrace
    # @note This logic is based off of MiniTest's #filter_backtrace.
    def filter(backtrace)
      result = []

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

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
minitest-reporters-0.14.20 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.19 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.18 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.17 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.16 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.15 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.14 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.13 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.12 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.11 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.10 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.9 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.8 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.7 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.6 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.5 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.4 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.3 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.2 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-0.14.1 lib/minitest/extensible_backtrace_filter.rb