Skip to content

Commit 5411787

Browse files
authored
Merge pull request rails#51005 from zzak/test-runner-did-you-mean
Rails test command suggests similar test files when the given file is not found
2 parents b3a3c3e + 346f516 commit 5411787

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

railties/lib/rails/commands/test/test_command.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def perform(*args)
3131
Rails::TestUnit::Runner.parse_options(args)
3232
run_prepare_task if self.args.none?(EXACT_TEST_ARGUMENT_PATTERN)
3333
Rails::TestUnit::Runner.run(args)
34+
rescue Rails::TestUnit::InvalidTestError => error
35+
say error.message
3436
end
3537

3638
# Define Thor tasks to avoid going through Rake and booting twice when using bin/rails test:*

railties/lib/rails/test_unit/runner.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99

1010
module Rails
1111
module TestUnit
12+
class InvalidTestError < StandardError
13+
def initialize(path, suggestion)
14+
super(<<~MESSAGE.squish)
15+
Could not load test file: #{path}.
16+
#{suggestion}
17+
MESSAGE
18+
end
19+
end
20+
1221
class Runner
1322
TEST_FOLDERS = [:models, :helpers, :channels, :controllers, :mailers, :integration, :jobs, :mailboxes]
1423
PATH_ARGUMENT_PATTERN = %r"^(?!/.+/$)[.\w]*[/\\]"
@@ -48,7 +57,17 @@ def run(argv = [])
4857
def load_tests(argv)
4958
patterns = extract_filters(argv)
5059
tests = list_tests(patterns)
51-
tests.to_a.each { |path| require File.expand_path(path) }
60+
tests.to_a.each do |path|
61+
require File.expand_path(path)
62+
rescue LoadError => exception
63+
all_tests = list_tests([default_test_glob])
64+
corrections = DidYouMean::SpellChecker.new(dictionary: all_tests).correct(path)
65+
66+
if corrections.empty?
67+
raise exception
68+
end
69+
raise InvalidTestError.new(path, DidYouMean::Formatter.message_for(corrections))
70+
end
5271
end
5372

5473
def compose_filter(runnable, filter)

railties/test/application/test_runner_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,14 @@ def test_raise_error_when_specified_file_does_not_exist
964964
assert_match(%r{cannot load such file.+test/not_exists\.rb}, error)
965965
end
966966

967+
def test_did_you_mean_when_specified_file_name_is_close
968+
create_test_file :models, "account"
969+
output = run_test_command("test/models/accnt.rb")
970+
971+
assert_match(%r{Could not load test file.+test/models/accnt\.rb}, output)
972+
assert_match(%r{Did you mean?.+test/models/account_test\.rb}, output)
973+
end
974+
967975
def test_pass_TEST_env_on_rake_test
968976
create_test_file :models, "account"
969977
create_test_file :models, "post", pass: false

0 commit comments

Comments
 (0)