Sha256: 4f886548550ff073891d584d11ee9c9aba86fd3c503c89dc70e4b05ac2d298c4

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module RSpec
  module Core
    # @private
    class BacktraceFormatter
      # @private
      attr_accessor :exclusion_patterns, :inclusion_patterns

      def initialize
        @full_backtrace = false

        patterns = [
          "/lib\d*/ruby/",
          "org/jruby/",
          "bin/",
          "/gems/",
        ].map { |s| Regexp.new(s.gsub("/", File::SEPARATOR)) }

        @system_exclusion_patterns = [Regexp.union(RSpec::CallerFilter::IGNORE_REGEX, *patterns)]
        @exclusion_patterns = [] + @system_exclusion_patterns
        @inclusion_patterns = [Regexp.new(Dir.getwd)]
      end

      def full_backtrace=(full_backtrace)
        @full_backtrace = full_backtrace
      end

      def full_backtrace?
        @full_backtrace || @exclusion_patterns.empty?
      end

      def format_backtrace(backtrace, options = {})
        return backtrace if options[:full_backtrace]

        backtrace.map { |l| backtrace_line(l) }.compact.
          tap do |filtered|
            if filtered.empty?
              filtered.concat backtrace
              filtered << ""
              filtered << "  Showing full backtrace because every line was filtered out."
              filtered << "  See docs for RSpec::Configuration#backtrace_exclusion_patterns and"
              filtered << "  RSpec::Configuration#backtrace_inclusion_patterns for more information."
            end
          end
      end

      def backtrace_line(line)
        Metadata.relative_path(line) unless exclude?(line)
      rescue SecurityError
        nil
      end

      def exclude?(line)
        return false if @full_backtrace
        relative_line = Metadata.relative_path(line)
        return false unless matches?(@exclusion_patterns, relative_line)
        matches?(@system_exclusion_patterns, relative_line) || !matches?(@inclusion_patterns, line)
      end

    private

      def matches?(patterns, line)
        patterns.any? { |p| line =~ p }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-core-3.0.4 lib/rspec/core/backtrace_formatter.rb
rspec-core-3.0.3 lib/rspec/core/backtrace_formatter.rb