Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make better use of Minitest's lifecycle #1109

Merged
merged 1 commit into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions test/capture_warnings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,56 @@ def initialize(fail_on_warnings = true)
@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) }
end

def after_tests
stderr_file.rewind
lines = stderr_file.read.split("\n")
stderr_file.close!
$stderr.reopen(stderr_file.path)

$stderr.reopen(STDERR)
Minitest.after_run do
stderr_file.rewind
lines = stderr_file.read.split("\n")
stderr_file.close!
$stderr.reopen(STDERR)
after_tests(lines)
end
end

# rubocop:disable Metrics/AbcSize
def after_tests(lines)
app_warnings, other_warnings = lines.partition { |line|
line.include?(app_root) && !line.include?(bundle_dir)
}

if app_warnings.any?
puts <<-WARNINGS
#{'-' * 30} app warnings: #{'-' * 30}

#{app_warnings.join("\n")}
header = "#{'-' * 22} app warnings: #{'-' * 22}"
output.puts
output.puts header

#{'-' * 75}
WARNINGS
if app_warnings.any?
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
1 change: 1 addition & 0 deletions test/support/stream_capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
begin
require 'active_support/testing/stream'
rescue LoadError
require 'tempfile'
module ActiveSupport
module Testing
module Stream #:nodoc:
Expand Down
29 changes: 16 additions & 13 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@
require 'fileutils'
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))

gem 'minitest'
require 'minitest/autorun'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

require 'capture_warnings'
@capture_warnings = CaptureWarnings.new(fail_build = true)
@capture_warnings.before_tests
if Minitest.respond_to?(:after_run)
Minitest.after_run do
@capture_warnings.after_tests
end
if defined?(Minitest::Test)
Copy link
Member Author

# 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
else
at_exit do
STDOUT.puts 'Minitest.after_run not available.'
@capture_warnings.after_tests
# Minitest 4
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/autorun.rb
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
# Ensure backward compatibility with Minitest 4
Minitest = MiniTest unless defined?(Minitest)
Minitest::Test = MiniTest::Unit::TestCase
def Minitest.after_run(&block)
MiniTest::Unit.after_tests(&block)
end
end

require 'capture_warnings'
CaptureWarnings.new(_fail_build = true).execute!

require 'active_model_serializers'

require 'support/stream_capture'
Expand Down