Class: Nicetest::BacktraceFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/nicetest/backtrace_filter.rb

Constant Summary collapse

BUNDLER_REGEX =
%r{/bundler/gems}
GEMS_DEFAULT_DIR =
Regexp.escape(Gem.default_dir)
GEMS_PATHS =
Gem.path.map { |path| Regexp.new(Regexp.escape(path)) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBacktraceFilter

Returns a new instance of BacktraceFilter.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nicetest/backtrace_filter.rb', line 13

def initialize
  @pastel = Cli::PASTEL
  @silence_pattern = Regexp.union([
    /sorbet-runtime/,
    Regexp.escape(RbConfig::CONFIG["rubylibdir"]),
    %r{/gems/bundler-\d+\.\d+\.\d+/lib/bundler},
    %r{/gems/minitest-\d+\.\d+\.\d+/lib/minitest},
    %r{/gems/minitest-reporters-\d+\.\d+\.\d+/lib/minitest},
    %r{(bin|exe)/bundle:},
    /internal:warning/,
  ])
  @dim_pattern = Regexp.union([
    GEMS_DEFAULT_DIR,
    *GEMS_PATHS,
    BUNDLER_REGEX,
  ])
end

Instance Attribute Details

#pastelObject (readonly)

Returns the value of attribute pastel.



11
12
13
# File 'lib/nicetest/backtrace_filter.rb', line 11

def pastel
  @pastel
end

Instance Method Details

#colorize_line(line) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nicetest/backtrace_filter.rb', line 67

def colorize_line(line)
  match = line.match(/(.*):(\d+):in `(.*)'/)

  if match
    file = match[1]
    line_number = match[2]
    method = match[3]

    colored_file = pastel.cyan(file)
    colored_line_number = pastel.green(line_number)
    colored_method = pastel.yellow(method)

    "#{colored_file}:#{colored_line_number}:in `#{colored_method}'"
  else
    line
  end
end

#dim?(line) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/nicetest/backtrace_filter.rb', line 53

def dim?(line)
  @dim_pattern.match?(line)
end

#filter(backtrace) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nicetest/backtrace_filter.rb', line 31

def filter(backtrace)
  bt = []
  first_line = true
  cwd = Dir.pwd

  backtrace.each do |line|
    silenced = silence?(line)

    next if silenced && !first_line

    first_line = false

    bt << if silenced || (dim?(line) && !line.start_with?(cwd))
      pastel.dim(trim_prefix(line))
    else
      colorize_line(trim_prefix(line))
    end
  end

  bt
end

#silence?(line) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/nicetest/backtrace_filter.rb', line 57

def silence?(line)
  @silence_pattern.match?(line)
end

#trim_prefix(line) ⇒ Object



61
62
63
64
65
# File 'lib/nicetest/backtrace_filter.rb', line 61

def trim_prefix(line)
  line = line.delete_prefix(Dir.pwd + "/")
  line.sub!(ENV["HOME"], "~")
  line
end