20
20
require 'fileutils'
21
21
FileUtils . mkdir_p ( File . expand_path ( '../../tmp/cache' , __FILE__ ) )
22
22
23
+ # https://github.com/seattlerb/minitest/blob/master/lib/minitest/autorun.rb
23
24
gem '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
34
33
# Minitest 4
35
34
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/autorun.rb
36
35
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
37
36
# Ensure backward compatibility with Minitest 4
38
37
Minitest = MiniTest unless defined? ( Minitest )
39
38
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 ) }
43
49
end
50
+ require 'minitest/reporters'
51
+ Minitest ::Reporters . use!
44
52
45
53
# If there's no failure info, try disabling capturing stderr:
46
54
# `env CAPTURE_STDERR=false rake`
47
55
# This is way easier than writing a Minitest plugin
48
56
# for 4.x and 5.x.
49
57
if ENV [ 'CAPTURE_STDERR' ] !~ /false|1/i
50
58
require 'capture_warnings'
51
- CaptureWarnings . new ( _fail_build = true ) . execute!
59
+ minitest_run = CaptureWarnings . new ( _fail_build = true ) . execute! ( minitest_run )
52
60
else
53
61
$VERBOSE = true
54
62
end
@@ -71,6 +79,45 @@ def Minitest.after_run(&block)
71
79
require 'fixtures/poro'
72
80
73
81
ActiveSupport . on_load ( :active_model_serializers ) do
74
- $action_controller_logger = ActiveModelSerializers . logger # rubocop:disable Style/GlobalVars
82
+ $action_controller_logger = ActiveModelSerializers . logger
75
83
ActiveModelSerializers . logger = Logger . new ( IO ::NULL )
76
84
end
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