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

Conversation

bf4
Copy link
Member

@bf4 bf4 commented Aug 31, 2015

http://blog.arkency.com/2013/06/are-we-abusing-at-exit/

Extracted from #1069

Changes are basically that we have to use Minitest.after_run because of minitest's dependencies on at_exit for when it starts to run its suite.

  1. capture warnings now 'runs' entires in execute!
  2. make sure we have after_run

basically, this is the meat and potatoes of the change

gem 'minitest'
require 'minitest/autorun'
if defined?(Minitest::Test)
  # 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
  # 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!

and

class CaptureWarnings
  def execute!
    $VERBOSE = true
    $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(lines)
    app_warnings, other_warnings = lines.partition { |line|
      line.include?(app_root) && !line.include?(bundle_dir)
    }
   #......
  end
end

@bf4
Copy link
Member Author

bf4 commented Aug 31, 2015

I'm used to RSpec's test run handlers. I didn't realize that Minitest 100% relies on the at_exit hook so I pretty much need to use Minitest.after_run { } to ensure things run in the right order at the expected time

@zenspider
Copy link

It should probably be: if defined?(Minitest::Test)

@bf4
Copy link
Member Author

bf4 commented Aug 31, 2015

@zenspider Thanks for that! (esp since I don't want to rely on MiniTest = Minitest unless defined?(MiniTest))

@bf4 bf4 force-pushed the fix_minitest_after_run branch from 509e89d to 45ba45e Compare August 31, 2015 10:48
@blowmage
Copy link

If the app is using Rails 4.0 or below, and is using minitest-rails, then Minitest::Test will be defined. This is because minitest-rails uses the minitest-test gem to enable parts of the Minitest 5.0 API in MiniTest 4.0.

@bf4
Copy link
Member Author

bf4 commented Sep 4, 2015

@blowmage Thanks for that. minitest-rails isn't installed in the gem, but I do call gem 'minitest' so maybe we don't need the compatibility later.

@bf4
Copy link
Member Author

bf4 commented Sep 4, 2015

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

@joaomdmoura
Copy link
Member

I confess that I was not that concerned about the at_exit usage, but indeed it seems hackish, just hope their future changes don't end up implying in further work on our side just to keep it working. I don't see how it could happen right now let's move on with it.
Why not go with the latest version of the gem instead of treating multiple versions?
You might also need to rebase it, sorry 😁

@bf4 bf4 force-pushed the fix_minitest_after_run branch from 45ba45e to e8e292c Compare September 6, 2015 12:24
@bf4
Copy link
Member Author

bf4 commented Sep 6, 2015

@joaomdmoura I just rebased... As long as we support Rails 4.0, we need to support Minitest 4.x. See #1109 (comment)

@bf4
Copy link
Member Author

bf4 commented Sep 7, 2015

@joaomdmoura This fixes issues with coverage on Rails 4.0.. any objections to merging?

@NullVoxPopuli
Copy link
Contributor

👍

NullVoxPopuli added a commit that referenced this pull request Sep 15, 2015
Make better use of Minitest's lifecycle
@NullVoxPopuli NullVoxPopuli merged commit a453de1 into rails-api:master Sep 15, 2015
@bf4 bf4 deleted the fix_minitest_after_run branch December 21, 2015 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants