mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Make better use of Minitest's lifecycle
http://blog.arkency.com/2013/06/are-we-abusing-at-exit/
This commit is contained in:
@@ -10,49 +10,56 @@ class CaptureWarnings
|
||||
@output_dir = File.join(app_root, 'tmp')
|
||||
FileUtils.mkdir_p(output_dir)
|
||||
@bundle_dir = File.join(app_root, 'bundle')
|
||||
@output = STDOUT
|
||||
end
|
||||
|
||||
def before_tests
|
||||
$stderr.reopen(stderr_file.path)
|
||||
def execute!
|
||||
$VERBOSE = true
|
||||
at_exit { $stderr.reopen(STDERR) }
|
||||
$stderr.reopen(stderr_file.path)
|
||||
|
||||
Minitest.after_run do
|
||||
stderr_file.rewind
|
||||
lines = stderr_file.read.split("\n")
|
||||
stderr_file.close!
|
||||
$stderr.reopen(STDERR)
|
||||
after_tests(lines)
|
||||
end
|
||||
end
|
||||
|
||||
def after_tests
|
||||
stderr_file.rewind
|
||||
lines = stderr_file.read.split("\n")
|
||||
stderr_file.close!
|
||||
|
||||
$stderr.reopen(STDERR)
|
||||
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
def after_tests(lines)
|
||||
app_warnings, other_warnings = lines.partition { |line|
|
||||
line.include?(app_root) && !line.include?(bundle_dir)
|
||||
}
|
||||
|
||||
header = "#{'-' * 22} app warnings: #{'-' * 22}"
|
||||
output.puts
|
||||
output.puts header
|
||||
|
||||
if app_warnings.any?
|
||||
puts <<-WARNINGS
|
||||
#{'-' * 30} app warnings: #{'-' * 30}
|
||||
|
||||
#{app_warnings.join("\n")}
|
||||
|
||||
#{'-' * 75}
|
||||
WARNINGS
|
||||
output.puts app_warnings.join("\n")
|
||||
else
|
||||
output.puts 'None. Yay!'
|
||||
end
|
||||
|
||||
if other_warnings.any?
|
||||
File.write(File.join(output_dir, 'warnings.txt'), other_warnings.join("\n") << "\n")
|
||||
puts
|
||||
puts 'Non-app warnings written to tmp/warnings.txt'
|
||||
puts
|
||||
output.puts
|
||||
output.puts 'Non-app warnings written to tmp/warnings.txt'
|
||||
output.puts
|
||||
end
|
||||
|
||||
output.puts
|
||||
output.puts '-' * header.size
|
||||
|
||||
# fail the build...
|
||||
if fail_on_warnings && app_warnings.any?
|
||||
abort "Failing build due to app warnings: #{app_warnings.inspect}"
|
||||
end
|
||||
end
|
||||
# rubocop:enable Metrics/AbcSize
|
||||
|
||||
private
|
||||
|
||||
attr_reader :stderr_file, :app_root, :output_dir, :bundle_dir, :fail_on_warnings
|
||||
attr_reader :stderr_file, :app_root, :output_dir, :bundle_dir, :fail_on_warnings, :output
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user