From e7174a782028b4d97070ef127cbcda54f55fe7dd Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Fri, 26 Jun 2015 10:10:26 -0500 Subject: [PATCH] Capture and print app warnings on test run Configure not to fail the test, for now --- test/capture_warnings.rb | 57 ++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 6 +++++ 2 files changed, 63 insertions(+) create mode 100644 test/capture_warnings.rb diff --git a/test/capture_warnings.rb b/test/capture_warnings.rb new file mode 100644 index 000000000..f7ea759ba --- /dev/null +++ b/test/capture_warnings.rb @@ -0,0 +1,57 @@ +# https://raw.githubusercontent.com/metric_fu/metric_fu/master/spec/capture_warnings.rb +require "tempfile" +require "fileutils" + +class CaptureWarnings + def initialize(fail_on_warnings = true) + @fail_on_warnings = fail_on_warnings + @stderr_file = Tempfile.new("app.stderr") + @app_root ||= Dir.pwd + @output_dir = File.join(app_root, "tmp") + FileUtils.mkdir_p(output_dir) + @bundle_dir = File.join(app_root, "bundle") + end + + def before_tests + $stderr.reopen(stderr_file.path) + $VERBOSE = true + at_exit { $stderr.reopen(STDERR) } + end + + def after_tests + stderr_file.rewind + lines = stderr_file.read.split("\n").uniq + stderr_file.close! + + $stderr.reopen(STDERR) + + 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")} + +#{'-' * 75} + WARNINGS + 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 + end + + # fail the build... + if fail_on_warnings && app_warnings.any? + abort "Failing build due to app warnings: #{app_warnings.inspect}" + end + end + + private + attr_reader :stderr_file, :app_root, :output_dir, :bundle_dir, :fail_on_warnings +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 1ba903b35..8afe49c05 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,6 +11,12 @@ # Ensure backward compatibility with Minitest 4 Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) +require "capture_warnings" +@capture_warnings = CaptureWarnings.new(fail_build = false) +@capture_warnings.before_tests +at_exit do + @capture_warnings.after_tests +end require 'active_model_serializers' class Foo < Rails::Application