Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/ruby_lsp/listeners/test_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ def resolve_test_commands(items)

if tags.include?("test_dir")
if children.empty?
full_files.concat(Dir.glob(
"#{path}/**/{*_test,test_*,*_spec}.rb",
File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME,
))
full_files.concat(
Dir.glob(
"#{path}/**/{*_test,test_*,*_spec}.rb",
File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME,
).map! { |p| Shellwords.escape(p) },
)
end
elsif tags.include?("test_file")
full_files << path if children.empty?
full_files << Shellwords.escape(path) if children.empty?
elsif tags.include?("test_group")
# If all of the children of the current test group are other groups, then there's no need to add it to the
# aggregated examples
Expand Down Expand Up @@ -114,7 +116,7 @@ def handle_minitest_groups(file_path, groups_and_examples)
end

load_path = spec?(file_path) ? "-Ispec" : "-Itest"
"#{COMMAND} #{load_path} #{file_path} --name \"/#{regex}/\""
"#{COMMAND} #{load_path} #{Shellwords.escape(file_path)} --name \"/#{regex}/\""
end

#: (String, Hash[String, Hash[Symbol, untyped]]) -> Array[String]
Expand All @@ -125,7 +127,7 @@ def handle_test_unit_groups(file_path, groups_and_examples)
Shellwords.escape(TestDiscovery::DYNAMIC_REFERENCE_MARKER),
".*",
)
command = +"#{COMMAND} -Itest #{file_path} --testcase \"/^#{group_regex}\\$/\""
command = +"#{COMMAND} -Itest #{Shellwords.escape(file_path)} --testcase \"/^#{group_regex}\\$/\""

unless examples.empty?
command << if examples.length == 1
Expand Down
166 changes: 166 additions & 0 deletions test/requests/resolve_test_commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,89 @@ def test_resolve_test_command_for_minitest_spec_with_backticks
)
end
end

def test_resolve_properly_escapes_single_file_paths
with_server do |server|
server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "file:///test/server(v2)_test.rb",
uri: "file:///test/server(v2)_test.rb",
label: "/test/server(v2)_test.rb",
tags: ["test_file", "framework:minitest"],
children: [],
},
],
},
})

result = server.pop_response.response
assert_equal(
["#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /test/server\\(v2\\)_test.rb"],
result[:commands],
)
end
end

def test_resolve_properly_escapes_file_paths_within_directories
with_server do |server|
Dir.stubs(:glob).returns(["/other/test/fake(v2)_test.rb"])
server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "file:///other/test",
uri: "file:///other/test",
label: "/other/test",
tags: ["test_dir", "framework:minitest"],
children: [],
},
],
},
})

result = server.pop_response.response
assert_equal(
["#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /other/test/fake\\(v2\\)_test.rb"],
result[:commands],
)
end
end

def test_resolve_properly_escapes_file_paths_in_groups
with_server do |server|
server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "ServerTest",
uri: "file:///test/server(v2)_test.rb",
label: "ServerTest",
range: {
start: { line: 0, character: 0 },
end: { line: 30, character: 3 },
},
tags: ["framework:minitest", "test_group"],
children: [],
},
],
},
})

result = server.pop_response.response
assert_equal(
["#{COMMAND} -Itest /test/server\\(v2\\)_test.rb --name \"/^ServerTest(#|::)/\""],
result[:commands],
)
end
end
end

class ResolveTestCommandsTestUnitTest < Minitest::Test
Expand Down Expand Up @@ -1254,6 +1337,89 @@ def test_resolve_test_command_mix_of_directories_and_examples
)
end
end

def test_resolve_properly_escapes_single_file_paths
with_server do |server|
server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "file:///test/server(v2)_test.rb",
uri: "file:///test/server(v2)_test.rb",
label: "/test/server(v2)_test.rb",
tags: ["test_file", "framework:test_unit"],
children: [],
},
],
},
})

result = server.pop_response.response
assert_equal(
["#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /test/server\\(v2\\)_test.rb"],
result[:commands],
)
end
end

def test_resolve_properly_escapes_file_paths_within_directories
with_server do |server|
Dir.stubs(:glob).returns(["/other/test/fake(v2)_test.rb"])
server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "file:///other/test",
uri: "file:///other/test",
label: "/other/test",
tags: ["test_dir", "framework:test_unit"],
children: [],
},
],
},
})

result = server.pop_response.response
assert_equal(
["#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /other/test/fake\\(v2\\)_test.rb"],
result[:commands],
)
end
end

def test_resolve_properly_escapes_file_paths_in_groups
with_server do |server|
server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "ServerTest",
uri: "file:///test/server(v2)_test.rb",
label: "ServerTest",
range: {
start: { line: 0, character: 0 },
end: { line: 30, character: 3 },
},
tags: ["framework:test_unit", "test_group"],
children: [],
},
],
},
})

result = server.pop_response.response
assert_equal(
["#{COMMAND} -Itest /test/server\\(v2\\)_test.rb --testcase \"/^ServerTest\\$/\""],
result[:commands],
)
end
end
end

class ResolveTestCommandsOtherFrameworksTest < Minitest::Test
Expand Down