fastruby / next_rails

A toolkit to upgrade your next Rails application
https://www.fastruby.io/blog/upgrade-rails/dual-boot/dual-boot-with-rails-6-0-beta.html?utm_source=github&utm_medium=description&utm_campaign=github&utm_term=next-rails
MIT License
489 stars 31 forks source link

[BUG] undefined method after_run when running Minitest version 4 #114

Open JuanVqz opened 8 months ago

JuanVqz commented 8 months ago

Expected Behavior

Be able to use the Minitest deprecation tracker with Ruby 2.3.8 and Rails 3.2.22.5 using the Minitest gem at version 4.7.5

Actual Behavior

If I run the deprecation tracker it breaks with the following error:

vendor/bundle/ruby/2.3.0/gems/next_rails-1.3.0/lib/deprecation_tracker.rb:107:in `track_minitest': undefined method `after_run' for MiniTest:Module (NoMethodError)

Possible Fix

Add a check in the next_rails gem if the app is using Minitest v4, then instead of using after_run it should use the correct method (I think it was after_tests).

To Reproduce

  1. Have a Rails 3.2 with Ruby 2.3 and have Minitest v4.7.3 installed
  2. Configure the deprecation tracker for Minitest
  3. Run the test suite saving the deprecations
  4. see the error

Additional Information

I researched a little bit it seems like the Minitest gem at version 4.7.3 doesn't have the after_run method, it was initially introduced at version 5.0.0

If there are more questions feel free to ping me, :+1:

I will abide by the code of conduct

fbuys commented 8 months ago

Here is some more information that I found for Minitest version 4.7.3.

The below console results show how we can determine that the after_tests method exists.

irb(main):031:0> Minitest::Unit.respond_to?(:after_tests)
=> true
irb(main):032:0> defined?(Minitest::Unit)
=> "constant"

If after_run fails but the above results show that after_tests is available, then we can use after_tests to add the required hook.

JuanVqz commented 8 months ago

The workaround we used to solve this:

  # config/initializers/minitest.rb

  module Minitest
    def self.after_run(&block)
      Minitest::Unit.after_tests(&block)
    end
  end
etagwerker commented 8 months ago

@JuanVqz @fbuys Thanks for sharing all this!

When you have a moment, could you please submit a PR that adds this monkeypatch for applications that are using Minitest < 5.0?

fbuys commented 8 months ago

After more investigation it turns out that a simple monkey patch is not enough to get next_rails to work with minitest 4.7

I believe we need to investigate how to hook into the test suites for minitest 4.7. The Minitest extension we currently use for the deprecation tracker is also not compatible with this version of minitest.

https://github.com/fastruby/next_rails/blob/4752fc9dd4d49d5ca35f779f66622604b208c6f2/lib/deprecation_tracker.rb#L36-L53