-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Prevent multiple values being set to run_via
#27941
Prevent multiple values being set to run_via
#27941
Conversation
r? @arthurnn (@rails-bot has picked a reviewer for you, use r? to override) |
Think this means we can just remove the def run_via
@@run_via
end
def run_via=(runner)
if @@run_via
raise ArgumentError, 'run_via already assigned'
else
@@run_via = RunVia.new(runner)
end
end
class RunVia < Struct.new(:runner)
def ruby?
runner == :ruby
end
def rake?
runner == :rake
end
end (Could be cleaned up with an |
bd44b86
to
4f2c3b7
Compare
Looks great. I updated. I have one concern. There was using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice call on the backwardscompatibility! Agreed a wrapper file would be nicer, as right now we're exposing a private API in a public file, but you know that 😊
end | ||
|
||
def self.rake_run(patterns) # :nodoc: | ||
run_via[:rake] = true | ||
self.run_via = :rake if self.run_via.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this guard?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because rake_run
is called multiple times when running multiple tests in one rake
command(e.g. bin/rake test:models test:controllers
).
@@run_via = nil unless defined? @@run_via | ||
|
||
# For backward compatibility, makes it possible to set via `[]=`. | ||
def @@run_via.[]=(runner, _) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this add the []=
method to nil
? I don't see how it can provide backwardscompatibility reliably?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about revising the surface to:
mattr_reader(:run_via) { RunVia.new }
def self.run_via=(runner)
if run_via.set?
raise # …
else
run_via.runner = runner
end
end
class RunVia
attr_accessor :runner
alias set? runner
# Backwardscompatibility with Rails 5.0 generated plugin test scripts.
alias []= runner=
def ruby?
@runner == :ruby
end
def rake?
@runner == :rake
end
end
Would also prevent NoMethodError
's in cases where run_via.rake?
is called without first having something assigned (albeit an unlikely scenario).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Great! I updated.
@@ -5,6 +5,6 @@ require 'rails/test_unit/minitest_plugin' | |||
|
|||
Rails::TestUnitReporter.executable = 'bin/test' | |||
|
|||
Minitest.run_via[:rails] = true | |||
Minitest.run_via = :rails |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have any tasks akin to rails app:update
for plugins that'll help updating this file and let us avoid the backwardscompatibility line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently there is no update task for plugin. Also, plugin currently does not load the task provided by rails. Therefore, if provide a task similar to rails app:update
for plugin, first need to update the plugin's Rakefile.
However, it is inconvenient that there is no task for updating, so I will add it with another PR.
When executing the test via rake, since `rake` is set for `run_via`, `ruby` should not be set. Related 2cb6c27
4f2c3b7
to
f38a660
Compare
Sweet! ❤️ |
…et_to_run_via Prevent multiple values being set to `run_via`
When executing the test via rake, since
rake
is set forrun_via
,ruby
should not be set.Related 2cb6c27