Skip to content

Commit 08c7230

Browse files
authored
Merge pull request rails#54489 from Edouard-chin/ec-readable-error
Fix unreadable output when running `rails test`:
2 parents 4244685 + 7492a49 commit 08c7230

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ 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-
raise ArgumentError, error.message
3634
end
3735

3836
# 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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99

1010
module Rails
1111
module TestUnit
12-
class InvalidTestError < StandardError
12+
class InvalidTestError < ArgumentError
1313
def initialize(path, suggestion)
14-
super("Could not load test file: #{path}. #{suggestion}")
14+
super(<<~MESSAGE.rstrip)
15+
Could not load test file: #{path}.
16+
#{suggestion}
17+
MESSAGE
18+
end
19+
20+
def backtrace(*args)
21+
[]
1522
end
1623
end
1724

@@ -65,7 +72,7 @@ def load_tests(argv)
6572
if corrections.empty?
6673
raise exception
6774
end
68-
raise InvalidTestError.new(path, DidYouMean::Formatter.message_for(corrections))
75+
raise(InvalidTestError.new(path, DidYouMean::Formatter.message_for(corrections)), cause: nil)
6976
else
7077
raise
7178
end

railties/test/application/test_runner_test.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,13 @@ def test_did_you_mean_when_specified_file_name_is_close
969969
create_test_file :models, "account"
970970
output = run_test_command("test/models/accnt.rb")
971971

972-
assert_match(%r{Could not load test file.+test/models/accnt\.rb}, output)
973-
assert_match(%r{Did you mean?.+test/models/account_test\.rb}, output)
972+
expected = <<~MSG
973+
bin/rails: Could not load test file: test/models/accnt.rb. (Rails::TestUnit::InvalidTestError)
974+
975+
Did you mean? test/models/account_test.rb
976+
MSG
977+
978+
assert_equal(expected, output)
974979
assert_not_predicate $?, :success?
975980
end
976981

railties/test/generators/plugin_test_runner_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_fail_fast
9393

9494
def test_raise_error_when_specified_file_does_not_exist
9595
error = capture(:stderr) { run_test_command("test/not_exists.rb") }
96-
assert_match(%r{cannot load such file.+test/not_exists\.rb}, error)
96+
assert_match(%r{Could not load test file.+test/not_exists\.rb}, error)
9797
end
9898

9999
def test_executed_only_once

0 commit comments

Comments
 (0)