guard/guard-minitest

Minitest auto-discovery causes guard-minitest reporters to run outside of guard

tmichel opened this issue · 5 comments

Minitest auto-discovers plugins and as guard-minitest provides one it will load it regardless of how it was run. This causes that the Guard::Minitest::Reporter gets loaded and called even if we run the tests without guard. Combine this with any guard notification plugin and after running a simple ruby my_test.rb a notification pops up when clearly if I run the tests directly I don't want to see extra notifications popping up.

I'm more than happy to send a PR but I wanted to discuss the approach first. I see two obvious ways:

  1. keep the auto-discovery in place but add the Guard::Minitest::Reporter only when running with guard
  2. disable auto-discovery (by moving the file lib/minitest/guard_minitest_plugin outside of the minitest directory) and add the reporter when the gem gets required.

UPDATE: I did some digging and guard-minitest runs the tests in a sub-process so the second option would not work. Guard::Minitest::Reporter should recognize if it was started from the guard-minitest plugin or not.

e2 commented

I've worked in similar issues before: fc1047e

I'm an RSpec person, so I have even less patience towards Minitest. But I am a maintainer here, so I'm glad to accept PRs. And I do look out not to break this plugin.

Just saying I'm not that excited about debugging and fixing this - mostly because I don't want to break something else for other users in another scenario.

The best starting point is a failing spec. Although a usable fix is fine too. (Sometimes it helps get clear on how to reproduce the problem in a test case).

I switched this project to use RSpec simply because using the Minitest here was just "inception within inception". So if you need help on the RSpec side, let me know.

I think you can use certain environment variables, such that the reporter would check if it's running from Guard (with that variable).

There's a catch though with environment variables - those may not be properly set when using spring or zeus (when forking happens). But there's usually some workaround possible.

Anyway, my guess is that the best approach is to set an environment variable in https://github.com/guard/guard-minitest/blob/master/lib/guard/minitest.rb and check that variable in https://github.com/guard/guard-minitest/blob/master/lib/minitest/guard_minitest_plugin.rb

If you're committed to getting this to work, I'll be happy to help you get this merged and released.

I think using an environment variable is the way to go. It's easy and simple. I'll send a PR sometime next week when I have some free time.

e2 commented

Check out: https://github.com/guard/guard-rspec/wiki/Warning%3A-no-environment

It shows the issues of using environment variables within guard and things like Zeus/Spring.

You'll likely want to do more or less what guard-rspec does with it's results file location (environment variable that the user can set from within the Guardfile and which is passed down deep into the process).

I came here to report this only to see the OP get inundated with editorializing FUD. "inception within inception"... really? If minitest can test itself, you can test with minitest. It isn't hard and the commentary is off topic and unnecessary. Disappointing.

The problem is here: https://github.com/guard/guard-minitest/blob/master/lib/minitest/guard_minitest_plugin.rb#L17

and a simple solution is to add a --guard option (in the method above it) and only add the GuardReporter if that option has been activated. Then you spawn the subprocess from guard with the option on. Likewise, the requires should be pushed down to plugin init.

You can look at https://github.com/seattlerb/minitest-gcstats for inspiration.

e2 commented

@zenspider - I didn't mean that testing minitest with minitest is problematic. I meant that testing guard-minitest with guard-minitest is "inception within inception". My brain hurt every time I stumbled on (or someone reported) a problem with it. I switched to RSpec because no one else stepped out to fix the issues.

Those are issues with Guard, not Minitest. I wasn't clear, sorry.

I'm only here to get this working with Guard. I simply don't enjoy working with Minitest. Maybe because I'm dumb and lazy, but the fact is: I don't enjoy it.

I also try to reproduce the issue in tests and then fix stuff.

I didn't know to do that how here (without using overkill like chroot or docker or something).

Recently I created: https://github.com/e2/gem_isolator for something else. If someone reports another issue with this, I'll likely add an acceptance test here.

Until then, I'm relying on people reporting problems.

Fixed in v2.4.6: https://github.com/guard/guard-minitest/releases/tag/v2.4.6

Feel free to open a new issue if there's something wrong.