Sha256: 0f246c7693df95bc5f031d2944d6e54e43d6b4f8facf50324b7473bed67c9b06

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 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 = []
      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
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
minitest-reporters-1.3.6 lib/minitest/extensible_backtrace_filter.rb
nullifyable-0.1.0 vendor/bundle/gems/minitest-reporters-1.3.5/lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.5 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.5.beta1 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.4 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.3 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.2 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.2.beta2 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.2.beta1 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.1 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.3.1.beta1 lib/minitest/extensible_backtrace_filter.rb