Sha256: 64bc69ec1afdd16edc1c07dbd527628f12607ecfaf575919abaaef93495b0b5a

Contents?: true

Size: 1.83 KB

Versions: 149

Compression:

Stored size: 1.83 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

149 entries across 107 versions & 11 rubygems

Version Path
minitest-reporters-1.7.1 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.7.0 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.6.1 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.6.0 lib/minitest/extensible_backtrace_filter.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/minitest-reporters-1.5.0/lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.5.0 lib/minitest/extensible_backtrace_filter.rb
minitest-reporters-1.4.3 lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/paynow_sdk-1.0.9/vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/paynow_sdk-1.0.9/vendor/bundle/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/paynow_sdk-1.0.9/vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.1.0 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.1.0 vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.0.9 vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.0.9 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.0.9 vendor/bundle/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.0.8 vendor/bundle/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.0.8 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.0.8 vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb
paynow_sdk-1.0.7 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/minitest-reporters-1.4.2/lib/minitest/extensible_backtrace_filter.rb