mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Hack Minitest to make it less dependent on at_exit
This commit is contained in:
26
.rubocop.yml
26
.rubocop.yml
@@ -58,3 +58,29 @@ Style/MultilineOperationIndentation:
|
|||||||
Style/BlockDelimiters:
|
Style/BlockDelimiters:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
EnforcedStyle: line_count_based
|
EnforcedStyle: line_count_based
|
||||||
|
|
||||||
|
########## test_helper.rb sanity
|
||||||
|
Style/EndBlock:
|
||||||
|
Exclude:
|
||||||
|
- test/test_helper.rb
|
||||||
|
|
||||||
|
Style/SpecialGlobalVars:
|
||||||
|
Exclude:
|
||||||
|
- test/test_helper.rb
|
||||||
|
|
||||||
|
Style/GlobalVars:
|
||||||
|
Exclude:
|
||||||
|
- test/test_helper.rb
|
||||||
|
|
||||||
|
Style/AndOr:
|
||||||
|
Exclude:
|
||||||
|
- test/test_helper.rb
|
||||||
|
- 'lib/active_model/serializer/lint.rb'
|
||||||
|
|
||||||
|
Style/Not:
|
||||||
|
Exclude:
|
||||||
|
- test/test_helper.rb
|
||||||
|
|
||||||
|
Style/ClassCheck:
|
||||||
|
Exclude:
|
||||||
|
- test/test_helper.rb
|
||||||
|
|||||||
@@ -53,13 +53,6 @@ Style/AlignHash:
|
|||||||
Exclude:
|
Exclude:
|
||||||
- 'test/action_controller/json_api/pagination_test.rb'
|
- 'test/action_controller/json_api/pagination_test.rb'
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
Style/AndOr:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/active_model/serializer/lint.rb'
|
|
||||||
|
|
||||||
# Offense count: 25
|
# Offense count: 25
|
||||||
# Cop supports --auto-correct.
|
# Cop supports --auto-correct.
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
||||||
|
|||||||
@@ -17,17 +17,19 @@ class CaptureWarnings
|
|||||||
@output = STDOUT
|
@output = STDOUT
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute!
|
def execute!(minitest_run)
|
||||||
$VERBOSE = true
|
$VERBOSE = true
|
||||||
$stderr.reopen(stderr_file.path)
|
$stderr.reopen(stderr_file.path)
|
||||||
|
at_exit do
|
||||||
Minitest.after_run do
|
|
||||||
stderr_file.rewind
|
stderr_file.rewind
|
||||||
lines = stderr_file.read.split("\n")
|
lines = stderr_file.read.split("\n")
|
||||||
stderr_file.close!
|
stderr_file.close!
|
||||||
$stderr.reopen(STDERR)
|
$stderr.reopen(STDERR)
|
||||||
after_tests(lines)
|
after_tests(lines)
|
||||||
end
|
end
|
||||||
|
proc do |argv|
|
||||||
|
minitest_run.call(argv)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_tests(lines)
|
def after_tests(lines)
|
||||||
|
|||||||
@@ -20,27 +20,35 @@ require 'active_support/json'
|
|||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
||||||
|
|
||||||
|
# https://github.com/seattlerb/minitest/blob/master/lib/minitest/autorun.rb
|
||||||
gem 'minitest'
|
gem 'minitest'
|
||||||
require 'minitest/autorun'
|
begin
|
||||||
require 'minitest/reporters'
|
require 'minitest'
|
||||||
Minitest::Reporters.use!
|
rescue LoadError
|
||||||
if defined?(Minitest::Test)
|
# Minitest 4
|
||||||
$minitest_version = 5 # rubocop:disable Style/GlobalVars
|
require 'minitest/unit'
|
||||||
# Minitest 5
|
require 'minitest/spec'
|
||||||
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest/autorun.rb
|
require 'minitest/mock'
|
||||||
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
|
$minitest_version = 4
|
||||||
else
|
|
||||||
$minitest_version = 4 # rubocop:disable Style/GlobalVars
|
|
||||||
# Minitest 4
|
# Minitest 4
|
||||||
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/autorun.rb
|
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/autorun.rb
|
||||||
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
|
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
|
||||||
# Ensure backward compatibility with Minitest 4
|
# Ensure backward compatibility with Minitest 4
|
||||||
Minitest = MiniTest unless defined?(Minitest)
|
Minitest = MiniTest unless defined?(Minitest)
|
||||||
Minitest::Test = MiniTest::Unit::TestCase
|
Minitest::Test = MiniTest::Unit::TestCase
|
||||||
def Minitest.after_run(&block)
|
minitest_run = ->(argv) { MiniTest::Unit.new.run(argv) }
|
||||||
MiniTest::Unit.after_tests(&block)
|
else
|
||||||
end
|
# Minitest 5
|
||||||
|
$minitest_version = 5
|
||||||
|
# Minitest 5
|
||||||
|
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest/autorun.rb
|
||||||
|
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
|
||||||
|
require 'minitest/spec'
|
||||||
|
require 'minitest/mock'
|
||||||
|
minitest_run = ->(argv) { Minitest.run(argv) }
|
||||||
end
|
end
|
||||||
|
require 'minitest/reporters'
|
||||||
|
Minitest::Reporters.use!
|
||||||
|
|
||||||
# If there's no failure info, try disabling capturing stderr:
|
# If there's no failure info, try disabling capturing stderr:
|
||||||
# `env CAPTURE_STDERR=false rake`
|
# `env CAPTURE_STDERR=false rake`
|
||||||
@@ -48,7 +56,7 @@ end
|
|||||||
# for 4.x and 5.x.
|
# for 4.x and 5.x.
|
||||||
if ENV['CAPTURE_STDERR'] !~ /false|1/i
|
if ENV['CAPTURE_STDERR'] !~ /false|1/i
|
||||||
require 'capture_warnings'
|
require 'capture_warnings'
|
||||||
CaptureWarnings.new(_fail_build = true).execute!
|
minitest_run = CaptureWarnings.new(_fail_build = true).execute!(minitest_run)
|
||||||
else
|
else
|
||||||
$VERBOSE = true
|
$VERBOSE = true
|
||||||
end
|
end
|
||||||
@@ -71,6 +79,45 @@ require 'fixtures/active_record'
|
|||||||
require 'fixtures/poro'
|
require 'fixtures/poro'
|
||||||
|
|
||||||
ActiveSupport.on_load(:active_model_serializers) do
|
ActiveSupport.on_load(:active_model_serializers) do
|
||||||
$action_controller_logger = ActiveModelSerializers.logger # rubocop:disable Style/GlobalVars
|
$action_controller_logger = ActiveModelSerializers.logger
|
||||||
ActiveModelSerializers.logger = Logger.new(IO::NULL)
|
ActiveModelSerializers.logger = Logger.new(IO::NULL)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# From:
|
||||||
|
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
|
||||||
|
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
|
||||||
|
# But we've replaced `at_exit` with `END` called before the 'at_exit' hook.
|
||||||
|
class MiniTestHack
|
||||||
|
def self.autorun(minitest_run)
|
||||||
|
# don't run if there was a non-exit exception
|
||||||
|
return if $! and not ($!.kind_of? SystemExit and $!.success?)
|
||||||
|
|
||||||
|
# Original Comment:
|
||||||
|
# the order here is important. The at_exit handler must be
|
||||||
|
# installed before anyone else gets a chance to install their
|
||||||
|
# own, that way we can be assured that our exit will be last
|
||||||
|
# to run (at_exit stacks).
|
||||||
|
#
|
||||||
|
# Now:
|
||||||
|
# The after_run blocks now only run on SigEXIT, which is fine.
|
||||||
|
exit_code = nil
|
||||||
|
|
||||||
|
trap('EXIT') do
|
||||||
|
if $minitest_version == 5
|
||||||
|
@@after_run.reverse_each(&:call)
|
||||||
|
else
|
||||||
|
@@after_tests.reverse_each(&:call)
|
||||||
|
end
|
||||||
|
|
||||||
|
exit exit_code || false
|
||||||
|
end
|
||||||
|
|
||||||
|
exit_code = minitest_run.call(ARGV)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# Run MiniTest in `END`, so that it finishes before `at_exit` fires,
|
||||||
|
# which guarantees we can run code after MiniTest finishes
|
||||||
|
# via an `at_exit` block.
|
||||||
|
# This is in service of silencing non-app warnings during test run,
|
||||||
|
# and leaves us with the warnings in our app.
|
||||||
|
END { MiniTestHack.autorun(minitest_run) }
|
||||||
|
|||||||
Reference in New Issue
Block a user