Merge pull request #51005 from zzak/test-runner-did-you-mean

Rails test command suggests similar test files when the given file is not found
This commit is contained in:
Jean Boussier 2024-03-16 10:38:07 +01:00 committed by GitHub
commit 5411787a15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 1 deletions

View File

@ -31,6 +31,8 @@ module Rails
Rails::TestUnit::Runner.parse_options(args)
run_prepare_task if self.args.none?(EXACT_TEST_ARGUMENT_PATTERN)
Rails::TestUnit::Runner.run(args)
rescue Rails::TestUnit::InvalidTestError => error
say error.message
end
# Define Thor tasks to avoid going through Rake and booting twice when using bin/rails test:*

View File

@ -9,6 +9,15 @@ require "rails/test_unit/test_parser"
module Rails
module TestUnit
class InvalidTestError < StandardError
def initialize(path, suggestion)
super(<<~MESSAGE.squish)
Could not load test file: #{path}.
#{suggestion}
MESSAGE
end
end
class Runner
TEST_FOLDERS = [:models, :helpers, :channels, :controllers, :mailers, :integration, :jobs, :mailboxes]
PATH_ARGUMENT_PATTERN = %r"^(?!/.+/$)[.\w]*[/\\]"
@ -48,7 +57,17 @@ module Rails
def load_tests(argv)
patterns = extract_filters(argv)
tests = list_tests(patterns)
tests.to_a.each { |path| require File.expand_path(path) }
tests.to_a.each do |path|
require File.expand_path(path)
rescue LoadError => exception
all_tests = list_tests([default_test_glob])
corrections = DidYouMean::SpellChecker.new(dictionary: all_tests).correct(path)
if corrections.empty?
raise exception
end
raise InvalidTestError.new(path, DidYouMean::Formatter.message_for(corrections))
end
end
def compose_filter(runnable, filter)

View File

@ -964,6 +964,14 @@ module ApplicationTests
assert_match(%r{cannot load such file.+test/not_exists\.rb}, error)
end
def test_did_you_mean_when_specified_file_name_is_close
create_test_file :models, "account"
output = run_test_command("test/models/accnt.rb")
assert_match(%r{Could not load test file.+test/models/accnt\.rb}, output)
assert_match(%r{Did you mean?.+test/models/account_test\.rb}, output)
end
def test_pass_TEST_env_on_rake_test
create_test_file :models, "account"
create_test_file :models, "post", pass: false