Skip to content

Commit 2a171da

Browse files
committed
Hack Minitest to make it less dependent on at_exit
1 parent 3133422 commit 2a171da

File tree

4 files changed

+93
-25
lines changed

4 files changed

+93
-25
lines changed

.rubocop.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,29 @@ Style/MultilineOperationIndentation:
5858
Style/BlockDelimiters:
5959
Enabled: true
6060
EnforcedStyle: line_count_based
61+
62+
########## test_helper.rb sanity
63+
Style/EndBlock:
64+
Exclude:
65+
- test/test_helper.rb
66+
67+
Style/SpecialGlobalVars:
68+
Exclude:
69+
- test/test_helper.rb
70+
71+
Style/GlobalVars:
72+
Exclude:
73+
- test/test_helper.rb
74+
75+
Style/AndOr:
76+
Exclude:
77+
- test/test_helper.rb
78+
- 'lib/active_model/serializer/lint.rb'
79+
80+
Style/Not:
81+
Exclude:
82+
- test/test_helper.rb
83+
84+
Style/ClassCheck:
85+
Exclude:
86+
- test/test_helper.rb

.rubocop_todo.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ Style/AlignHash:
5353
Exclude:
5454
- 'test/action_controller/json_api/pagination_test.rb'
5555

56-
# Offense count: 1
57-
# Cop supports --auto-correct.
58-
# Configuration parameters: EnforcedStyle, SupportedStyles.
59-
Style/AndOr:
60-
Exclude:
61-
- 'lib/active_model/serializer/lint.rb'
62-
6356
# Offense count: 25
6457
# Cop supports --auto-correct.
6558
# Configuration parameters: EnforcedStyle, SupportedStyles.

test/capture_warnings.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ def initialize(fail_on_warnings = true)
1717
@output = STDOUT
1818
end
1919

20-
def execute!
20+
def execute!(minitest_run)
2121
$VERBOSE = true
2222
$stderr.reopen(stderr_file.path)
23-
24-
Minitest.after_run do
23+
at_exit do
2524
stderr_file.rewind
2625
lines = stderr_file.read.split("\n")
2726
stderr_file.close!
2827
$stderr.reopen(STDERR)
2928
after_tests(lines)
3029
end
30+
proc do |argv|
31+
minitest_run.call(argv)
32+
end
3133
end
3234

3335
def after_tests(lines)

test/test_helper.rb

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,43 @@
2020
require 'fileutils'
2121
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
2222

23+
# https://github.com/seattlerb/minitest/blob/master/lib/minitest/autorun.rb
2324
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
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) }
4349
end
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.
4957
if 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)
5260
else
5361
$VERBOSE = true
5462
end
@@ -71,6 +79,45 @@ def Minitest.after_run(&block)
7179
require 'fixtures/poro'
7280

7381
ActiveSupport.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)
7684
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

Comments
 (0)