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

Minitest references Rails in non-Rails code #996

Closed
soulcutter opened this issue May 2, 2024 · 2 comments
Closed

Minitest references Rails in non-Rails code #996

soulcutter opened this issue May 2, 2024 · 2 comments

Comments

@soulcutter
Copy link

soulcutter commented May 2, 2024

Given a file "minitest.rb" that requires minitest/autorun
And minitest 5.22.3 is being used
When I run ruby -Ilib:test ./minitest.rb

I get the output on a failing test:

bin/rails test ./minitest.rb:5

This file does not require any rails, there is no Gemfile, AFAIK nothing that might load any rails thing. bin/rails does not exist at this location. I would expect there be no reference to Rails.

Here's minitest.rb

require 'minitest/autorun'

class MyTestThatProducesADiff < Minitest::Test

  def test_diff
    assert_equal "Mayonnaise is delicious", "Mayonnaise is delicioous"
  end
end

Output

$ ruby -Ilib:test ./minitest.rb
Run options: --seed 43692

# Running:

F

Failure:
MyTestThatProducesADiff#test_diff [./minitest.rb:6]:
Expected: "Mayonnaise is delicious"
  Actual: "Mayonnaise is delicioous"


bin/rails test ./minitest.rb:5



Finished in 0.000435s, 2298.8505 runs/s, 2298.8505 assertions/s.
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

In case it might be getting polluted from something automatically trying to require rails, here's a filtered list of what gems are present (but not required in my code):

$ gem list | grep rails
autoprefixer-rails (10.4.16.0)
dotenv-rails (3.1.0)
factory_bot_rails (6.4.3)
pry-rails (0.3.9)
rails (7.1.3.2)
rails-dom-testing (2.2.0)
rails-html-sanitizer (1.6.0)
rspec-rails (6.1.2)
ruby-lsp-rails (0.3.5, 0.3.3)
sass-rails (6.0.0)
sassc-rails (2.1.2)
sprockets-rails (3.4.2)
@blowmage
Copy link

blowmage commented May 2, 2024

Minitest plugin system looks at all installed gems, so its possible you have a plugin installed that is altering the behavior.

Here is what your test looks like in a clean gemset on my system:

 >> chruby 3.3.1

 >> ruby -v
ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin23]

 >> omg clean
Switching gemset to clean

 >> gem install minitest
Fetching minitest-5.22.3.gem
Successfully installed minitest-5.22.3
1 gem installed

 >> echo 'require "minitest/autorun"\n\nclass MyTestThatProducesADiff < Minitest::Test\n\n  def test_diff\n    assert_equal "Mayonnaise is delicious", "Mayonnaise is delicioous"\n  end\nend' > clean_test.rb
 
 >> cat clean_test.rb
require "minitest/autorun"

class MyTestThatProducesADiff < Minitest::Test

  def test_diff
    assert_equal "Mayonnaise is delicious", "Mayonnaise is delicioous"
  end
end

 >> ruby ./clean_test.rb
Run options: --seed 59813

# Running:

F

Finished in 0.000258s, 3875.9690 runs/s, 3875.9690 assertions/s.

  1) Failure:
MyTestThatProducesADiff#test_diff [./clean_test.rb:6]:
Expected: "Mayonnaise is delicious"
  Actual: "Mayonnaise is delicioous"

1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

@soulcutter
Copy link
Author

https://github.com/minitest/minitest?tab=readme-ov-file#writing-extensions-

➜  temp irb             
>> Gem.find_files("minitest/*_plugin.rb")
=> 
["/Users/bradleyschaefer/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/minitest-5.22.3/lib/minitest/pride_plugin.rb",
 "/Users/bradleyschaefer/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/minitest-5.20.0/lib/minitest/pride_plugin.rb",
 "/Users/bradleyschaefer/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/minitest/rails_plugin.rb",
 "/Users/bradleyschaefer/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/simplecov-0.22.0/lib/minitest/simplecov_plugin.rb"]

I don't suppose that behavior is up for re-examination. I will register my personal comment that the result of this design is behavior that I would not expect. Thanks.

tenderlove added a commit to rails/rails that referenced this issue May 2, 2024
Minitest will automatically scan all installed gems and load plugins
from those gems.  We should detect whether or not we're being run within
the context of a Rails app and only change MT behavior in that case.

To determine if we're being run via `bin/rails` we'll just check if
RAILS_ENV is set.

Ref: minitest/minitest#996
Ref: minitest/minitest#725
fractaledmind pushed a commit to fractaledmind/rails that referenced this issue May 13, 2024
Minitest will automatically scan all installed gems and load plugins
from those gems.  We should detect whether or not we're being run within
the context of a Rails app and only change MT behavior in that case.

To determine if we're being run via `bin/rails` we'll just check if
RAILS_ENV is set.

Ref: minitest/minitest#996
Ref: minitest/minitest#725
xjunior pushed a commit to xjunior/rails that referenced this issue Jun 9, 2024
Minitest will automatically scan all installed gems and load plugins
from those gems.  We should detect whether or not we're being run within
the context of a Rails app and only change MT behavior in that case.

To determine if we're being run via `bin/rails` we'll just check if
RAILS_ENV is set.

Ref: minitest/minitest#996
Ref: minitest/minitest#725
jianbo pushed a commit to jianbo/setter-with-association that referenced this issue Jul 8, 2024
Minitest will automatically scan all installed gems and load plugins
from those gems.  We should detect whether or not we're being run within
the context of a Rails app and only change MT behavior in that case.

To determine if we're being run via `bin/rails` we'll just check if
RAILS_ENV is set.

Ref: minitest/minitest#996
Ref: minitest/minitest#725
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants