2020require 'fileutils'
2121FileUtils . mkdir_p ( File . expand_path ( '../../tmp/cache' , __FILE__ ) )
2222
23+ # https://github.com/seattlerb/minitest/blob/master/lib/minitest/autorun.rb
2324gem 'minitest'
24- require 'minitest/autorun'
25- require 'minitest/reporters'
26- Minitest ::Reporters . use!
27- if defined? ( Minitest ::Test )
28- $minitest_version = 5 # rubocop:disable Style/GlobalVars
29- # Minitest 5
30- # https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest/autorun.rb
31- # https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
32- else
33- $minitest_version = 4 # rubocop:disable Style/GlobalVars
25+ begin
26+ require 'minitest'
27+ rescue LoadError
28+ # Minitest 4
29+ require 'minitest/unit'
30+ require 'minitest/spec'
31+ require 'minitest/mock'
32+ $minitest_version = 4
3433 # Minitest 4
3534 # https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/autorun.rb
3635 # https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
3736 # Ensure backward compatibility with Minitest 4
3837 Minitest = MiniTest unless defined? ( Minitest )
3938 Minitest ::Test = MiniTest ::Unit ::TestCase
40- def Minitest . after_run ( &block )
41- MiniTest ::Unit . after_tests ( &block )
42- end
39+ minitest_run = -> ( argv ) { MiniTest ::Unit . new . run ( argv ) }
40+ else
41+ # Minitest 5
42+ $minitest_version = 5
43+ # Minitest 5
44+ # https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest/autorun.rb
45+ # https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
46+ require 'minitest/spec'
47+ require 'minitest/mock'
48+ minitest_run = -> ( argv ) { Minitest . run ( argv ) }
4349end
50+ require 'minitest/reporters'
51+ Minitest ::Reporters . use!
4452
4553# If there's no failure info, try disabling capturing stderr:
4654# `env CAPTURE_STDERR=false rake`
4755# This is way easier than writing a Minitest plugin
4856# for 4.x and 5.x.
4957if ENV [ 'CAPTURE_STDERR' ] !~ /false|1/i
5058 require 'capture_warnings'
51- CaptureWarnings . new ( _fail_build = true ) . execute!
59+ minitest_run = CaptureWarnings . new ( _fail_build = true ) . execute! ( minitest_run )
5260else
5361 $VERBOSE = true
5462end
@@ -71,6 +79,45 @@ def Minitest.after_run(&block)
7179require 'fixtures/poro'
7280
7381ActiveSupport . on_load ( :active_model_serializers ) do
74- $action_controller_logger = ActiveModelSerializers . logger # rubocop:disable Style/GlobalVars
82+ $action_controller_logger = ActiveModelSerializers . logger
7583 ActiveModelSerializers . logger = Logger . new ( IO ::NULL )
7684end
85+
86+ # From:
87+ # https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
88+ # https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
89+ # But we've replaced `at_exit` with `END` called before the 'at_exit' hook.
90+ class MiniTestHack
91+ def self . autorun ( minitest_run )
92+ # don't run if there was a non-exit exception
93+ return if $! and not ( $!. kind_of? SystemExit and $!. success? )
94+
95+ # Original Comment:
96+ # the order here is important. The at_exit handler must be
97+ # installed before anyone else gets a chance to install their
98+ # own, that way we can be assured that our exit will be last
99+ # to run (at_exit stacks).
100+ #
101+ # Now:
102+ # The after_run blocks now only run on SigEXIT, which is fine.
103+ exit_code = nil
104+
105+ trap ( 'EXIT' ) do
106+ if $minitest_version == 5
107+ @@after_run . reverse_each ( &:call )
108+ else
109+ @@after_tests . reverse_each ( &:call )
110+ end
111+
112+ exit exit_code || false
113+ end
114+
115+ exit_code = minitest_run . call ( ARGV )
116+ end
117+ end
118+ # Run MiniTest in `END`, so that it finishes before `at_exit` fires,
119+ # which guarantees we can run code after MiniTest finishes
120+ # via an `at_exit` block.
121+ # This is in service of silencing non-app warnings during test run,
122+ # and leaves us with the warnings in our app.
123+ END { MiniTestHack . autorun ( minitest_run ) }
0 commit comments