DEV: attempt to report last exception as the "cause" for failures

This allows our request specs to report exceptions so we can debug

May have a few false positives but generally should be quiet

TODO only wire magic in for request specs, currently happens for all
This commit is contained in:
Sam 2018-05-21 17:01:30 +10:00
parent e78f1d7589
commit ec337bbcb3
2 changed files with 43 additions and 0 deletions

View File

@ -50,6 +50,10 @@ Discourse::Application.configure do
config.log_level = :fatal
end
if defined? RspecErrorTracker
config.middleware.insert_after ActionDispatch::Flash, RspecErrorTracker
end
config.after_initialize do
SiteSetting.defaults.tap do |s|
s.set_regardless_of_locale(:s3_upload_bucket, 'bucket')

View File

@ -14,6 +14,31 @@ require 'mocha/api'
require 'certified'
require 'webmock/rspec'
class RspecErrorTracker
def self.last_exception=(ex)
@ex = ex
end
def self.last_exception
@ex
end
def initialize(app, config = {})
@app = app
end
def call(env)
begin
@app.call(env)
rescue => e
RspecErrorTracker.last_exception = e
raise e
end
ensure
end
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
@ -96,6 +121,18 @@ RSpec.configure do |config|
end
end
config.after :each do |x|
if x.exception && ex = RspecErrorTracker.last_exception
# magic in a cause if we have none
unless x.exception.cause
class << x.exception
attr_accessor :cause
end
x.exception.cause = ex
end
end
end
config.before :each do |x|
# TODO not sure about this, we could use a mock redis implementation here:
# this gives us really clean "flush" semantics, howere the side-effect is that
@ -126,6 +163,8 @@ RSpec.configure do |config|
I18n.locale = :en
RspecErrorTracker.last_exception = nil
if $test_cleanup_callbacks
$test_cleanup_callbacks.reverse_each(&:call)
$test_cleanup_callbacks = nil