Skip to content

Don't eagerly require Rails' minitest plugin. - #29572

Merged
kaspth merged 1 commit into
rails:masterfrom
kaspth:test-runner-no-eager-require
Jul 10, 2017
Merged

kaspth merged 1 commit into
rails:masterfrom
kaspth:test-runner-no-eager-require

Conversation

@kaspth

@kaspth kaspth commented Jun 25, 2017 •

Copy link
Copy Markdown
Contributor

Fixes #29469. Fixes #25046.

By making the Rails minitest behave like a standard minitest plugin
we're much more likely to not break when people use other minitest
plugins. Like minitest-focus and pride.

To do this, we need to behave like minitest: require files up front
and then perform the plugin behavior via the at_exit hook.
This also saves us a fair bit of wrangling with test file loading.

Finally, since the environment and warnings options have to be applied
as early as possible, and since minitest loads plugins at_exit, they
have to be moved to the test command.

Ideally, I'd like to backport this to both 5-1-stable and 5-0-stable. It's an extensive fix for sure though.

Comment thread railties/lib/minitest/rails_plugin.rb Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming this file to be "minitest/*_plugin.rb" makes minitest autoload us.

Comment thread railties/lib/rails/test_help.rb Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should try removing this altogether now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a later PR, I'd like to follow minitest-focus'es lead to make the filters regexps. I.e. move the filter definitions into plugin_rails_init by resolving line numbers to a specific method name.

Needs to handle a file creating multiple Runnables though and running with multiple line numbers (test/models/post_test.rb:4:8). Tried to do it for this, but faced way too much head-desking.

Comment thread railties/lib/rails/test_unit/runner.rb Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really despise this code. At first I had defined these as Thor options in the test command, but I couldn't figure out a way to make Thor remove the options from ARGV after parsing them (that also broke the test/generators/plugin_test_runner_test.rb tests).

@kaspth

kaspth commented Jun 25, 2017

Copy link
Copy Markdown
Contributor Author

I've been running a locally --dev generated app and can confirm minitest plugins like -p and minitest-focus both work 😊

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test runner of the rails plugin does not use TestCommand.
Therefore, TestCommand#help will not be called and rails' help will not be displayed.

$ rails plugin new yaffle  
$ cd yaffle 
$ <edit some files> 
$ ./bin/test --help
minitest options:
    -h, --help                       Display this help.
    -s, --seed SEED                  Sets random seed. Also via env. Eg: SEED=n rake
    -v, --verbose                    Verbose. Show progress processing files.
    -n, --name PATTERN               Filter run on /regexp/ or string.
    -e, --exclude PATTERN            Exclude /regexp/ or string from run.

Known extensions: rails, pride
    -b, --backtrace                  Show the complete backtrace
    -d, --defer-output               Output test failures and errors after the test run
    -f, --fail-fast                  Abort test run on first failure or error
    -c, --[no-]color                 Enable color in the output
    -p, --pride                      Pride. Show your testing pride!

I think that it is better to keep help displayed on plugin_rails_options, what about it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that but opted to separate them out. If we're running with ruby -Itest test/models --help we aren't running directly through the Rails test command, so I liked that we're slightly indicating that in the different help output. For instance when tests are run with ruby we can't support the -e option (iirc the app is booted once rails/test_help is run, so invoking the test command from there won't work either).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. Thanks for you explanation!

Comment thread railties/lib/rails/test_unit/runner.rb Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would Display Ruby warning be more clear option name than Enable?

@kirs

kirs commented Jul 2, 2017

Copy link
Copy Markdown
Member

I cloned the branch and tried it on a dummy app with minitest-focus and minitest/pride enabled. Works just fine 👌
Also tried to run it against 5-0-stable and Shopify (we have a lot of minitest plugins and edge cases), but it looks like it requires a bit more effort to backport the patch to 5.0.

Great work!

@kaspth

kaspth commented Jul 2, 2017

Copy link
Copy Markdown
Contributor Author

@kirs sweet! I'll try to prepare a version of this that works on 5-0-stable, so you can try it out there 😊

@kaspth

kaspth commented Jul 2, 2017

Copy link
Copy Markdown
Contributor Author

@kirs got a branch for 5-0-stable set up: 5-0-stable...test-runner-no-eager-require-5-0-stable

The branches are slightly out of sync as I haven't pushed the master test fixes there (I wanna see what the failures will be on 5-0-stable first).

Comment thread tools/test.rb Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we need Rails::TestUnit::Runner.parse_options(ARGV) here?

Comment thread railties/lib/rails/plugin/test.rb Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use Rails::TestUnit::Runner.run?

By making the Rails minitest behave like a standard minitest plugin
we're much more likely to not break when people use other minitest
plugins. Like minitest-focus and pride.

To do this, we need to behave like minitest: require files up front
and then perform the plugin behavior via the at_exit hook.
This also saves us a fair bit of wrangling with test file loading.

Finally, since the environment and warnings options have to be applied
as early as possible, and since minitest loads plugins at_exit, they
have to be moved to the test command.

* Don't expect the root method.

It's likely this worked because we eagerly loaded the Rails minitest plugin
and that somehow defined a root method on `Rails`.

* Assign a backtrace to failed exceptions.

Otherwise Minitest pukes when attempting to filter the backtrace (which
Rails' backtrace cleaner then removes).

Means the exception message test has to be revised too.

This is likely caused by the rails minitest plugin now being loaded for
these tests and assigning a default backtrace cleaner.
@kaspth
kaspth force-pushed the test-runner-no-eager-require branch from 86da9ab to 0d72489 Compare July 10, 2017 18:40
@kaspth

kaspth commented Jul 10, 2017

Copy link
Copy Markdown
Contributor Author

Failed on the new secrets test again: https://travis-ci.org/rails/rails/jobs/252119183#L1973 — I'm going to merge this since it's unrelated.

@kaspth
kaspth merged commit 3fa6693 into rails:master Jul 10, 2017
@kaspth
kaspth deleted the test-runner-no-eager-require branch July 10, 2017 19:13
kaspth added a commit that referenced this pull request Jul 10, 2017
Don't eagerly require Rails' minitest plugin.
@kaspth

kaspth commented Jul 10, 2017

Copy link
Copy Markdown
Contributor Author

5-1-stable: de0d960...5b07011

Looked at the wrong build when I pushed to 5-0-stable then tried to fix it live. I then reverted it on 5-0-stable as I couldn't reproduce the test failures locally, then I figured it the issue was with rerun snippets on Travis since the plugin is now loaded there as well. 631c596
Original failing build: https://travis-ci.org/rails/rails/builds/252123620

Think these should do it! Phew 😅

@kirs

kirs commented Jul 16, 2017

Copy link
Copy Markdown
Member

Since this PR was merged, any failing test in ActiveRecord/ActiveJob/ActiveSupport suite would fail in the reporter step:

/rails/railties/lib/rails/test_unit/reporter.rb:74:in `app_root': undefined method `root' for Rails:Module (NoMethodError)
        from /rails/railties/lib/rails/test_unit/reporter.rb:52:in `relative_path_for'
        from /rails/railties/lib/rails/test_unit/reporter.rb:70:in `format_rerun_snippet'
        from /rails/railties/lib/rails/test_unit/reporter.rb:22:in `record'
        from /rails/vendor/bundle/gems/minitest-5.3.3/lib/minitest.rb:613:in `block in record'
        from /rails/vendor/bundle/gems/minitest-5.3.3/lib/minitest.rb:612:in `each'
        from /rails/vendor/bundle/gems/minitest-5.3.3/lib/minitest.rb:612:in `record'
        from /rails/vendor/bundle/gems/minitest-5.3.3/lib/minitest.rb:293:in `run_one_method'

I fixed the issue in #29818

@kaspth

kaspth commented Jul 16, 2017

Copy link
Copy Markdown
Contributor Author

Likewise in 519467b and 5b07011 😄

y-yagi added a commit to y-yagi/rails that referenced this pull request Jul 30, 2017
Since rails#29572, minitest options are available but are no longer showed
in help.
This fixed to show minitest option in help as with Rails 5.1.2.

**before**

```
./bin/rails t --help
You can run a single test by appending a line number to a filename:

    bin/rails test test/models/user_test.rb:27

You can run multiple files and directories at the same time:

    bin/rails test test/controllers test/integration/login_test.rb

By default test failures and errors are reported inline during a run.

Rails options:
    -w, --warnings                   Run with Ruby warnings enabled
    -e, --environment                Run tests in the ENV environment
    -b, --backtrace                  Show the complete backtrace
    -d, --defer-output               Output test failures and errors after the test run
    -f, --fail-fast                  Abort test run on first failure or error
    -c, --[no-]color                 Enable color in the output
```

**after**

```
./bin/rails t --help
You can run a single test by appending a line number to a filename:

    bin/rails test test/models/user_test.rb:27

You can run multiple files and directories at the same time:

    bin/rails test test/controllers test/integration/login_test.rb

By default test failures and errors are reported inline during a run.

minitest options:
    -h, --help                       Display this help.
    -s, --seed SEED                  Sets random seed. Also via env. Eg: SEED=n rake
    -v, --verbose                    Verbose. Show progress processing files.
    -n, --name PATTERN               Filter run on /regexp/ or string.
        --exclude PATTERN            Exclude /regexp/ or string from run.

Known extensions: rails, pride
    -w, --warnings                   Run with Ruby warnings enabled
    -e, --environment                Run tests in the ENV environment
    -b, --backtrace                  Show the complete backtrace
    -d, --defer-output               Output test failures and errors after the test run
    -f, --fail-fast                  Abort test run on first failure or error
    -c, --[no-]color                 Enable color in the output
    -p, --pride                      Pride. Show your testing pride!
```
k0kubun added a commit to k0kubun/hamlit that referenced this pull request Aug 24, 2017
y-yagi added a commit to y-yagi/rails that referenced this pull request Feb 4, 2018
Since rails#29572, test runner will be loaded as minitest's plugin.
Therefore, if specify a value in `Minitest.extensions` before the Minitest
initialization process, the extension will not load and the test runner will
not work.
https://github.com/seattlerb/minitest/blob/44eee51ed9716c789c7cea8a90c131cf736b8915/lib/minitest.rb#L86

Also, load processing of adapter is done before minitest option processing,
so set the adapter in the file so that the `-a` option works.
y-yagi added a commit that referenced this pull request Feb 4, 2018
Since #29572, test runner will be loaded as minitest's plugin.
Therefore, if specify a value in `Minitest.extensions` before the Minitest
initialization process, the extension will not load and the test runner will
not work.
https://github.com/seattlerb/minitest/blob/44eee51ed9716c789c7cea8a90c131cf736b8915/lib/minitest.rb#L86

Also, load processing of adapter is done before minitest option processing,
so set the adapter in the file so that the `-a` option works.
bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Mar 13, 2018
- Add a description of major features in Rails 5.2.
- Add a reference to Pull Request/Commit to every entry in CHANGELOGs,
  note that some of them combined.
- Add section "Ruby on Rails Guides" with notable changes.
- Note that
  - Skipped this since encrypted secrets are already on the way out.
    ```
    *   Add `rails secrets:show` command.
        ([Pull Request](rails#29695))
    ```
  - Skipped this since it was backported all the way to 5-0-stable.
    ```
    *   Make Rails' test runner work better with minitest plugins.
        ([Pull Request](rails#29572))
    ```

Thanks to everyone who has been working on Rails 5.2! <3
feliperenan added a commit to heartcombo/devise that referenced this pull request Jan 29, 2019
The command bin/test stop running single tests once Devise started to
support Rails 5.2. The problem is because we used `rails/test_unit/minitest_plugin`
and this file was moved to another place.

See: rails/rails#29572

I'm not sure if we should require the `minitest-plugin` directly from
Rails like we were doing, I tried it and it didn't work. So I'm
changing this `bin/test` completely following based on how Rails does
that [here](https://github.com/rails/rails/blob/master/tools/test.rb)
feliperenan added a commit to heartcombo/devise that referenced this pull request Jan 29, 2019
The command bin/test stop running single tests once Devise started to
support Rails 5.2. The problem is because we used `rails/test_unit/minitest_plugin`
and this file was moved to another place.

See: rails/rails#29572

I'm not sure if we should require the `minitest-plugin` directly from
Rails like we were doing, I tried it and it didn't work. So I'm
changing this `bin/test` completely following based on how Rails does
that [here](https://github.com/rails/rails/blob/master/tools/test.rb)
feliperenan added a commit to heartcombo/devise that referenced this pull request Jan 29, 2019
The command bin/test stop running single tests once Devise started to
support Rails 5.2. The problem is because we used `rails/test_unit/minitest_plugin`
and this file was moved to another place.

See: rails/rails#29572

I'm not sure if we should require the `minitest-plugin` directly from
Rails like we were doing, I tried it and it didn't work. So I'm
changing this `bin/test` completely following based on how Rails does
that [here](https://github.com/rails/rails/blob/master/tools/test.rb)
feliperenan added a commit to heartcombo/devise that referenced this pull request Jan 29, 2019
The command bin/test stop running single tests once Devise started to
support Rails 5.2. The problem is because we used `rails/test_unit/minitest_plugin`
and this file was moved to another place.

See: rails/rails#29572

I'm not sure if we should require the `minitest-plugin` directly from
Rails like we were doing, I tried it and it didn't work. So I'm
changing this `bin/test` completely following based on how Rails does
that [here](https://github.com/rails/rails/blob/master/tools/test.rb)
feliperenan added a commit to heartcombo/devise that referenced this pull request Jan 29, 2019
The command bin/test stop running single tests once Devise started to
support Rails 5.2. The problem is because we used `rails/test_unit/minitest_plugin`
and this file was moved to another place.

See: rails/rails#29572

I'm not sure if we should require the `minitest-plugin` directly from
Rails like we were doing, I tried it and it didn't work. So I'm
changing this `bin/test` completely based on how Rails does that [here](https://github.com/rails/rails/blob/master/tools/test.rb)
michaelzedwards added a commit to michaelzedwards/hamlit that referenced this pull request Feb 11, 2022
johnmulder8 pushed a commit to johnmulder8/hamlit that referenced this pull request Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rails 5 isn't playing well with minitest-focus Overwriting minitest option parsing breaks minitest plugins that add options

3 participants