From 51a88df69494d1596bec8b498f90a14373f10fa2 Mon Sep 17 00:00:00 2001 From: Kemalau <2024302111194@whu.edu.cn> Date: Mon, 16 Mar 2026 18:34:39 +0000 Subject: [PATCH] fix(test-style): avoid escaping test-unit name regex --- lib/ruby_lsp/listeners/test_style.rb | 8 ++-- test/requests/resolve_test_commands_test.rb | 44 +++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/ruby_lsp/listeners/test_style.rb b/lib/ruby_lsp/listeners/test_style.rb index b18ed7d320..9d2ef847b0 100644 --- a/lib/ruby_lsp/listeners/test_style.rb +++ b/lib/ruby_lsp/listeners/test_style.rb @@ -130,11 +130,13 @@ def handle_test_unit_groups(file_path, groups_and_examples) command = +"#{COMMAND} -Itest #{Shellwords.escape(file_path)} --testcase \"/^#{group_regex}\\$/\"" unless examples.empty? - command << if examples.length == 1 - " --name \"/#{examples[0]}\\$/\"" + name_pattern = if examples.length == 1 + examples[0] else - " --name \"/(#{examples.join("|")})\\$/\"" + "(#{examples.join("|")})" end + + command << %Q( --name "/#{name_pattern}$/") end command diff --git a/test/requests/resolve_test_commands_test.rb b/test/requests/resolve_test_commands_test.rb index b84848a469..6b7f17d376 100644 --- a/test/requests/resolve_test_commands_test.rb +++ b/test/requests/resolve_test_commands_test.rb @@ -909,6 +909,50 @@ def test_resolve_test_command_specific_examples end end + def test_test_unit_examples_do_not_shell_escape_name_patterns + with_server do |server| + server.process_message({ + id: 1, + method: "rubyLsp/resolveTestCommands", + params: { + items: [ + { + id: "ServerTest", + uri: "file:///test/server_test.rb", + label: "ServerTest", + range: { + start: { line: 0, character: 0 }, + end: { line: 30, character: 3 }, + }, + tags: ["framework:test_unit", "test_group"], + children: [ + { + id: "ServerTest#test_server", + uri: "file:///test/server_test.rb", + label: "test_server", + range: { + start: { line: 1, character: 2 }, + end: { line: 10, character: 3 }, + }, + tags: ["framework:test_unit"], + children: [], + }, + ], + }, + ], + }, + }) + + result = server.pop_response.response + assert_equal( + [ + "#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" --name \"/test_server$/\"", + ], + result[:commands], + ) + end + end + def test_resolve_test_command_group_mixed_with_examples with_server do |server| server.process_message({