File: failed_line_detection_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (158 lines) | stat: -rw-r--r-- 4,342 bytes parent folder | download | duplicates (3)
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
require 'support/aruba_support'

RSpec.describe 'Failed line detection' do
  include_context "aruba support"
  before { setup_aruba }

  it "finds the source of a failure in a spec file that is defined at the current directory instead of in the normal `spec` subdir" do
    write_file "the_spec.rb", "
      RSpec.describe do
        it 'fails via expect' do
          expect(1).to eq(2)
        end
      end
    "

    run_command "the_spec.rb"
    expect(last_cmd_stdout).to include("expect(1).to eq(2)")
  end

  it "finds the source of a failure in a spec file loaded by running `ruby file` rather than loaded directly by RSpec" do
    write_file "passing_spec.rb", "
      RSpec.describe do
        example { }
      end
    "

    write_file "failing_spec.rb", "
      RSpec.describe do
        it 'fails via expect' do
          expect(1).to eq(2)
        end
      end
    "

    file = cd('.') { "#{Dir.pwd}/failing_spec.rb" }
    load file
    run_command "passing_spec.rb"

    expect(last_cmd_stdout).to include("expect(1).to eq(2)")
  end

  it "finds the direct source of failure in any lib, app or spec file, and allows the user to configure what is considered a project source dir" do
    write_file "lib/lib_mod.rb", "
      module LibMod
        def self.trigger_failure
          raise 'LibMod failure'
        end
      end
    "

    write_file "app/app_mod.rb", "
      module AppMod
        def self.trigger_failure
          raise 'AppMod failure'
        end
      end
    "

    write_file "spec/support/spec_support.rb", "
      module SpecSupport
        def self.trigger_failure
          raise 'SpecSupport failure'
        end
      end
    "

    write_file "spec/default_config_spec.rb", "
      require './lib/lib_mod'
      require './spec/support/spec_support'
      require './app/app_mod'

      RSpec.describe do
        example('1') { LibMod.trigger_failure }
        example('2') { AppMod.trigger_failure }
        example('3') { SpecSupport.trigger_failure }
      end
    "

    run_command "./spec/default_config_spec.rb"

    expect(last_cmd_stdout).to include("raise 'LibMod failure'").
                           and include("raise 'AppMod failure'").
                           and include("raise 'SpecSupport failure'").
                           and exclude("AppMod.trigger_failure")

    write_file "spec/change_config_spec.rb", "
      require './app/app_mod'

      RSpec.configure do |c|
        c.project_source_dirs = %w[ lib spec ]
      end

      RSpec.describe do
        example('1') { AppMod.trigger_failure }
      end
    "

    run_command "./spec/change_config_spec.rb"

    expect(last_cmd_stdout).to include("AppMod.trigger_failure").
                           and exclude("raise 'AppMod failure'")
  end

  it "finds the callsite of a method provided by a gem that fails (rather than the line in the gem)" do
    write_file "vendor/gems/assertions/lib/assertions.rb", "
      module Assertions
        AssertionFailed = Class.new(StandardError)

        def assert(value, msg)
          raise(AssertionFailed, msg) unless value
        end
      end
    "

    write_file "spec/unit/the_spec.rb", "
      require './vendor/gems/assertions/lib/assertions'

      RSpec.describe do
        include Assertions

        it 'fails via assert' do
          assert false, 'failed assertion'
        end

        it 'fails via expect' do
          expect(1).to eq(2)
        end
      end
    "

    run_command ""

    expect(last_cmd_stdout).to include("assert false, 'failed assertion'").
                           and include("expect(1).to eq(2)").
                           and exclude("raise(AssertionFailed, msg)")
  end

  it "falls back to finding a line in a gem when there are no backtrace lines in the app, lib or spec directories" do
    write_file "vendor/gems/before_failure/lib/before_failure.rb", "
      RSpec.configure do |c|
        c.before { raise 'before failure!' }
      end
    "

    write_file "spec/unit/the_spec.rb", "
      require './vendor/gems/before_failure/lib/before_failure'

      RSpec.describe do
        example('1') { }
      end
    "

    run_command ""

    expect(last_cmd_stdout).to include("c.before { raise 'before failure!' }").
                           and exclude("Unable to find matching line from backtrace")
  end
end