Skip to content

Commit 00067a2

Browse files
committed
Normalize Rails test names for proper test discovery
Rails allows writing tests with the syntax `test "should do something"`, but internally converts these to standard Minitest method names `test_should_do_something`. This normalization ensures our test identifiers match what Rails uses at runtime, allowing proper test discovery and execution.
1 parent be302df commit 00067a2

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ def on_call_node_enter(node)
9090

9191
test_name = first_arg.content
9292
test_name = "<empty test name>" if test_name.empty?
93+
rails_normalized_name = "test_#{test_name.gsub(/\s+/, "_")}"
9394

94-
add_test_item(node, test_name)
95+
add_test_item(node, rails_normalized_name)
9596
end
9697

9798
#: (Prism::DefNode node) -> void

test/ruby_lsp_rails/rails_test_style_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class SampleTest < ActiveSupport::TestCase
2626
assert_equal(2, test_class[:children].length)
2727

2828
test_labels = test_class[:children].map { |i| i[:label] }
29-
assert_includes(test_labels, "first test")
30-
assert_includes(test_labels, "second test")
29+
assert_includes(test_labels, "test_first_test")
30+
assert_includes(test_labels, "test_second_test")
3131
assert_all_items_tagged_with(items, :rails)
3232
end
3333
end
@@ -52,7 +52,7 @@ class EmptyTest < ActiveSupport::TestCase
5252
assert_equal(2, test_class[:children].length)
5353

5454
test_labels = test_class[:children].map { |i| i[:label] }
55-
assert_includes(test_labels, "<empty test name>")
55+
assert_includes(test_labels, "test_<empty_test_name>")
5656
assert_all_items_tagged_with(items, :rails)
5757
end
5858
end
@@ -110,11 +110,11 @@ def test_second_test
110110
test "handles tests with special characters in name" do
111111
source = <<~RUBY
112112
class SpecialCharsTest < ActiveSupport::TestCase
113-
test "test with spaces and punctuation!" do
113+
test "spaces and punctuation!" do
114114
assert true
115115
end
116116
117-
test "test with unicode: 你好" do
117+
test "unicode: 你好" do
118118
assert true
119119
end
120120
end
@@ -127,8 +127,8 @@ class SpecialCharsTest < ActiveSupport::TestCase
127127
assert_equal(2, test_class[:children].length)
128128

129129
test_labels = test_class[:children].map { |i| i[:label] }
130-
assert_includes(test_labels, "test with spaces and punctuation!")
131-
assert_includes(test_labels, "test with unicode: 你好")
130+
assert_includes(test_labels, "test_spaces_and_punctuation!")
131+
assert_includes(test_labels, "test_unicode:_你好")
132132
assert_all_items_tagged_with(items, :rails)
133133
end
134134
end

0 commit comments

Comments
 (0)