Skip to content

Commit b2754cc

Browse files
authored
Merge pull request rails#52415 from Shopify/fix-rails-test-wrong-file
Fix test command to exit 1 on LoadError
2 parents 77019de + 0f6b122 commit b2754cc

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def perform(*args)
3232
run_prepare_task if self.args.none?(EXACT_TEST_ARGUMENT_PATTERN)
3333
Rails::TestUnit::Runner.run(args)
3434
rescue Rails::TestUnit::InvalidTestError => error
35-
say error.message
35+
raise ArgumentError, error.message
3636
end
3737

3838
# 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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ def load_tests(argv)
5858
patterns = extract_filters(argv)
5959
tests = list_tests(patterns)
6060
tests.to_a.each do |path|
61-
require File.expand_path(path)
61+
abs_path = File.expand_path(path)
62+
require abs_path
6263
rescue LoadError => exception
63-
all_tests = list_tests([default_test_glob])
64-
corrections = DidYouMean::SpellChecker.new(dictionary: all_tests).correct(path)
64+
if exception.path == abs_path
65+
all_tests = list_tests([default_test_glob])
66+
corrections = DidYouMean::SpellChecker.new(dictionary: all_tests).correct(path)
6567

66-
if corrections.empty?
67-
raise exception
68+
if corrections.empty?
69+
raise exception
70+
end
71+
raise InvalidTestError.new(path, DidYouMean::Formatter.message_for(corrections))
72+
else
73+
raise
6874
end
69-
raise InvalidTestError.new(path, DidYouMean::Formatter.message_for(corrections))
7075
end
7176
end
7277

railties/test/application/test_runner_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,25 @@ def test_did_you_mean_when_specified_file_name_is_close
971971

972972
assert_match(%r{Could not load test file.+test/models/accnt\.rb}, output)
973973
assert_match(%r{Did you mean?.+test/models/account_test\.rb}, output)
974+
assert_not_predicate $?, :success?
975+
end
976+
977+
def test_unrelated_load_error
978+
app_file "test/models/account_test.rb", <<-RUBY
979+
require "test_helper"
980+
981+
require "does-not-exist"
982+
983+
class AccountsTest < ActiveSupport::TestCase
984+
def test_truth
985+
assert true
986+
end
987+
end
988+
RUBY
989+
990+
output = run_test_command("test/models/account_test.rb")
991+
assert_match("cannot load such file -- does-not-exist", output)
992+
assert_not_predicate $?, :success?
974993
end
975994

976995
def test_pass_TEST_env_on_rake_test

0 commit comments

Comments
 (0)