Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 163 lines (128 sloc) 5.283 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
require File.dirname(__FILE__) + '/helper'

class BacktraceTest < Test::Unit::TestCase

  should "parse a backtrace into lines" do
    array = [
      "app/models/user.rb:13:in `magic'",
      "app/controllers/users_controller.rb:8:in `index'"
    ]

    backtrace = Airbrake::Backtrace.parse(array)

    line = backtrace.lines.first
    assert_equal '13', line.number
    assert_equal 'app/models/user.rb', line.file
    assert_equal 'magic', line.method

    line = backtrace.lines.last
    assert_equal '8', line.number
    assert_equal 'app/controllers/users_controller.rb', line.file
    assert_equal 'index', line.method
  end
  
  should "parse a windows backtrace into lines" do
    array = [
      "C:/Program Files/Server/app/models/user.rb:13:in `magic'",
      "C:/Program Files/Server/app/controllers/users_controller.rb:8:in `index'"
    ]

    backtrace = Airbrake::Backtrace.parse(array)

    line = backtrace.lines.first
    assert_equal '13', line.number
    assert_equal 'C:/Program Files/Server/app/models/user.rb', line.file
    assert_equal 'magic', line.method

    line = backtrace.lines.last
    assert_equal '8', line.number
    assert_equal 'C:/Program Files/Server/app/controllers/users_controller.rb', line.file
    assert_equal 'index', line.method
  end

  should "be equal with equal lines" do
    one = build_backtrace_array
    two = one.dup

    assert_equal Airbrake::Backtrace.parse(one), Airbrake::Backtrace.parse(two)
  end

  should "parse massive one-line exceptions into multiple lines" do
    original_backtrace = Airbrake::Backtrace.
      parse(["one:1:in `one'\n two:2:in `two'\n three:3:in `three`"])
    expected_backtrace = Airbrake::Backtrace.
      parse(["one:1:in `one'", "two:2:in `two'", "three:3:in `three`"])

    assert_equal expected_backtrace, original_backtrace
  end

  context "with a project root" do
    setup do
      @project_root = '/some/path'
      Airbrake.configure {|config| config.project_root = @project_root }
    end

    teardown do
      reset_config
    end

    should "filter out the project root" do
      backtrace_with_root = Airbrake::Backtrace.parse(
        ["#{@project_root}/app/models/user.rb:7:in `latest'",
         "#{@project_root}/app/controllers/users_controller.rb:13:in `index'",
         "/lib/something.rb:41:in `open'"],
        :filters => default_filters)
      backtrace_without_root = Airbrake::Backtrace.parse(
        ["[PROJECT_ROOT]/app/models/user.rb:7:in `latest'",
         "[PROJECT_ROOT]/app/controllers/users_controller.rb:13:in `index'",
         "/lib/something.rb:41:in `open'"])

      assert_equal backtrace_without_root, backtrace_with_root
    end
  end

  context "with a project root equals to a part of file name" do
    setup do
      # Heroku-like
      @project_root = '/app'
      Airbrake.configure {|config| config.project_root = @project_root }
    end

    teardown do
      reset_config
    end

    should "filter out the project root" do
      backtrace_with_root = Airbrake::Backtrace.parse(
        ["#{@project_root}/app/models/user.rb:7:in `latest'",
         "#{@project_root}/app/controllers/users_controller.rb:13:in `index'",
         "/lib/something.rb:41:in `open'"],
        :filters => default_filters)
      backtrace_without_root = Airbrake::Backtrace.parse(
        ["[PROJECT_ROOT]/app/models/user.rb:7:in `latest'",
         "[PROJECT_ROOT]/app/controllers/users_controller.rb:13:in `index'",
         "/lib/something.rb:41:in `open'"])

      assert_equal backtrace_without_root, backtrace_with_root
    end
  end

  context "with a blank project root" do
    setup do
      Airbrake.configure {|config| config.project_root = '' }
    end

    teardown do
      reset_config
    end

    should "not filter line numbers with respect to any project root" do
      backtrace = ["/app/models/user.rb:7:in `latest'",
                   "/app/controllers/users_controller.rb:13:in `index'",
                   "/lib/something.rb:41:in `open'"]

      backtrace_with_root =
        Airbrake::Backtrace.parse(backtrace, :filters => default_filters)

      backtrace_without_root =
        Airbrake::Backtrace.parse(backtrace)

      assert_equal backtrace_without_root, backtrace_with_root
    end
  end

  should "remove notifier trace" do
    inside_notifier = ['lib/airbrake.rb:13:in `voodoo`']
    outside_notifier = ['users_controller:8:in `index`']

    without_inside = Airbrake::Backtrace.parse(outside_notifier)
    with_inside = Airbrake::Backtrace.parse(inside_notifier + outside_notifier,
                                                      :filters => default_filters)

    assert_equal without_inside, with_inside
  end

  should "run filters on the backtrace" do
    filters = [lambda { |line| line.sub('foo', 'bar') }]
    input = Airbrake::Backtrace.parse(["foo:13:in `one'", "baz:14:in `two'"],
                                             :filters => filters)
    expected = Airbrake::Backtrace.parse(["bar:13:in `one'", "baz:14:in `two'"])
    assert_equal expected, input
  end

  def build_backtrace_array
    ["app/models/user.rb:13:in `magic'",
      "app/controllers/users_controller.rb:8:in `index'"]
  end

  def default_filters
    Airbrake::Configuration::DEFAULT_BACKTRACE_FILTERS
  end

end
Something went wrong with that request. Please try again.