diff --git a/railties/lib/rails/commands/test/test_command.rb b/railties/lib/rails/commands/test/test_command.rb index 505026556b4..e183ddf0f34 100644 --- a/railties/lib/rails/commands/test/test_command.rb +++ b/railties/lib/rails/commands/test/test_command.rb @@ -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:* diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 7c72d8c2e7f..b04f3c90234 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -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) diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index b4b17dbb371..f30bdb28901 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -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