aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/exception_wrapper_test.rb
blob: d7408164ba5a775c9a46cd6fb99e409e26936393 (plain) (blame)
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
require 'abstract_unit'

module ActionDispatch
  class ExceptionWrapperTest < ActionDispatch::IntegrationTest
    class TestError < StandardError
      attr_reader :backtrace

      def initialize(*backtrace)
        @backtrace = backtrace.flatten
      end
    end

    class BadlyDefinedError < StandardError
      def backtrace
        nil
      end
    end

    setup do
      Rails.stubs(:root).returns(Pathname.new('.'))

      cleaner = ActiveSupport::BacktraceCleaner.new
      cleaner.add_silencer { |line| line !~ /^lib/ }

      @environment = { 'action_dispatch.backtrace_cleaner' => cleaner }
    end

    test '#source_extracts fetches source fragments for every backtrace entry' do
      exception = TestError.new("lib/file.rb:42:in `index'")
      wrapper = ExceptionWrapper.new({}, exception)

      wrapper.expects(:source_fragment).with('lib/file.rb', 42).returns('foo')

      assert_equal [ code: 'foo', line_number: 42 ], wrapper.source_extracts
    end


    test '#application_trace returns traces only from the application' do
      exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
      wrapper = ExceptionWrapper.new(@environment, exception)

      assert_equal [ "lib/file.rb:42:in `index'" ], wrapper.application_trace
    end

    test '#application_trace cannot be nil' do
      nil_backtrace_wrapper = ExceptionWrapper.new(@environment, BadlyDefinedError.new)
      nil_cleaner_wrapper = ExceptionWrapper.new({}, BadlyDefinedError.new)

      assert_equal [], nil_backtrace_wrapper.application_trace
      assert_equal [], nil_cleaner_wrapper.application_trace
    end

    test '#framework_trace returns traces outside the application' do
      exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
      wrapper = ExceptionWrapper.new(@environment, exception)

      assert_equal caller, wrapper.framework_trace
    end

    test '#framework_trace cannot be nil' do
      nil_backtrace_wrapper = ExceptionWrapper.new(@environment, BadlyDefinedError.new)
      nil_cleaner_wrapper = ExceptionWrapper.new({}, BadlyDefinedError.new)

      assert_equal [], nil_backtrace_wrapper.framework_trace
      assert_equal [], nil_cleaner_wrapper.framework_trace
    end

    test '#full_trace returns application and framework traces' do
      exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
      wrapper = ExceptionWrapper.new(@environment, exception)

      assert_equal exception.backtrace, wrapper.full_trace
    end

    test '#full_trace cannot be nil' do
      nil_backtrace_wrapper = ExceptionWrapper.new(@environment, BadlyDefinedError.new)
      nil_cleaner_wrapper = ExceptionWrapper.new({}, BadlyDefinedError.new)

      assert_equal [], nil_backtrace_wrapper.full_trace
      assert_equal [], nil_cleaner_wrapper.full_trace
    end

    test '#traces returns every trace by category enumerated with an index' do
      exception = TestError.new("lib/file.rb:42:in `index'", "/gems/rack.rb:43:in `index'")
      wrapper = ExceptionWrapper.new(@environment, exception)

      assert_equal({
        'Application Trace' => [ id: 0, trace: "lib/file.rb:42:in `index'" ],
        'Framework Trace' => [ id: 1, trace: "/gems/rack.rb:43:in `index'" ],
        'Full Trace' => [
          { id: 0, trace: "lib/file.rb:42:in `index'" },
          { id: 1, trace: "/gems/rack.rb:43:in `index'" }
        ]
      }, wrapper.traces)
    end
  end
end