From e3b9597d1ae18cf3848fd5627829cad2cd458bcc Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Mon, 11 Jan 2016 23:48:40 -0600 Subject: [PATCH] Remove warning capture; more trouble than worth --- .rubocop_todo.yml | 1 - .travis.yml | 4 +-- test/capture_warnings.rb | 77 ---------------------------------------- test/test_helper.rb | 62 ++------------------------------ 4 files changed, 3 insertions(+), 141 deletions(-) delete mode 100644 test/capture_warnings.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 865022c3..c9c8e604 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -98,7 +98,6 @@ Style/EachWithObject: Style/GuardClause: Exclude: - 'lib/active_model/serializer.rb' - - 'test/capture_warnings.rb' # Offense count: 12 # Cop supports --auto-correct. diff --git a/.travis.yml b/.travis.yml index 6db23056..422e849d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ cache: - vendor/bundle script: - - env CAPTURE_STDERR=${CAPTURE_STDERR:-false} bundle exec rake ci + - bundle exec rake ci env: - "RAILS_VERSION=4.0" @@ -31,8 +31,6 @@ matrix: - rvm: 2.1 env: RAILS_VERSION=master include: - - rvm: 2.2 - env: CAPTURE_STDERR=true - rvm: jruby-9.0.4.0 env: JRUBY_OPTS='-Xcompat.version=2.0 --server -Xcompile.invokedynamic=false -Xcli.debug=true --debug' allow_failures: diff --git a/test/capture_warnings.rb b/test/capture_warnings.rb deleted file mode 100644 index 2c4d7b64..00000000 --- a/test/capture_warnings.rb +++ /dev/null @@ -1,77 +0,0 @@ -# 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) - @ignore_dirs = [ - File.join(app_root, '.bundle'), - File.join(app_root, 'bundle'), - File.join(app_root, 'vendor') - ] - @output = STDOUT - end - - def execute!(minitest_run) - $VERBOSE = true - $stderr.reopen(stderr_file.path) - at_exit do - stderr_file.rewind - lines = stderr_file.read.split("\n") - stderr_file.close! - $stderr.reopen(STDERR) - after_tests(lines) - end - proc do |argv| - minitest_run.call(argv) - end - end - - def after_tests(lines) - app_warnings, other_warnings = lines.partition do |line| - line.include?(app_root) && ignore_dirs.none? { |ignore_dir| line.include?(ignore_dir) } - end - - if app_warnings.any? - warnings_message = app_warnings.join("\n") - print_warnings = true - else - warnings_message = 'None. Yay!' - ENV['FULL_BUILD'] ||= ENV['CI'] - running_ci = ENV['FULL_BUILD'] =~ /\Atrue\z/i - print_warnings = running_ci - end - - if other_warnings.any? - File.write(File.join(output_dir, 'warnings.txt'), other_warnings.join("\n") << "\n") - warnings_message << "\nNon-app warnings written to tmp/warnings.txt" - print_warnings = true - end - - header = "#{'-' * 22} app warnings: #{'-' * 22}" - message = <<-EOF.strip_heredoc - - #{header} - - #{warnings_message} - - #{'-' * header.size} - EOF - - output.puts(message) if print_warnings - - # 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, :ignore_dirs, :fail_on_warnings, :output -end diff --git a/test/test_helper.rb b/test/test_helper.rb index b87a78f8..809f3ca4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -20,47 +20,28 @@ require 'active_support/json' require 'fileutils' FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__)) -# https://github.com/seattlerb/minitest/blob/master/lib/minitest/autorun.rb gem 'minitest' begin require 'minitest' rescue LoadError # Minitest 4 - require 'minitest/unit' - require 'minitest/spec' - require 'minitest/mock' + require 'minitest/autorun' $minitest_version = 4 - # 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 - minitest_run = ->(argv) { MiniTest::Unit.new.run(argv) } else # Minitest 5 + require 'minitest/autorun' $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 require 'minitest/reporters' Minitest::Reporters.use! -# If there's no failure info, try disabling capturing stderr: -# `env CAPTURE_STDERR=false rake` -# This is way easier than writing a Minitest plugin -# for 4.x and 5.x. -if ENV['CAPTURE_STDERR'] !~ /false|1/i - require 'capture_warnings' - minitest_run = CaptureWarnings.new(_fail_build = true).execute!(minitest_run) -else - $VERBOSE = true -end - require 'active_model_serializers' require 'active_model/serializer/railtie' @@ -82,42 +63,3 @@ ActiveSupport.on_load(:active_model_serializers) do $action_controller_logger = ActiveModelSerializers.logger ActiveModelSerializers.logger = Logger.new(IO::NULL) 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) }