Skip to content

Commit

Permalink
Escape command passed to rdbg
Browse files Browse the repository at this point in the history
When we add commands for changing the cwd to the test command passed to
rdbg we need to start shell escaping the command.
  • Loading branch information
tristil committed Dec 30, 2023
1 parent b4065cb commit 889f2c0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
4 changes: 3 additions & 1 deletion lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ def add_test_code_lens(node, name:, command:, kind:)
data: { type: "test_in_terminal", **grouping_data },
)

shell_escaped_arguments = arguments.clone
shell_escaped_arguments[2] = Shellwords.escape(shell_escaped_arguments[2])
@_response << create_code_lens(
node,
title: "Debug",
command_name: "rubyLsp.debugTest",
arguments: arguments,
arguments: shell_escaped_arguments,
data: { type: "debug", **grouping_data },
)
end
Expand Down
22 changes: 10 additions & 12 deletions test/expectations/code_lens/minitest_tests.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /Test/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /Test/",
{
"start_line": 0,
"start_column": 0,
Expand Down Expand Up @@ -180,7 +180,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /Test\\#test_public/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /Test\\\\\\#test_public/",
{
"start_line": 5,
"start_column": 2,
Expand Down Expand Up @@ -276,7 +276,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"test_public_command",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /Test\\#test_public_command/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /Test\\\\\\#test_public_command/",
{
"start_line": 9,
"start_column": 9,
Expand Down Expand Up @@ -372,7 +372,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"test_another_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /Test\\#test_another_public/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /Test\\\\\\#test_another_public/",
{
"start_line": 11,
"start_column": 9,
Expand Down Expand Up @@ -468,7 +468,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"test_public_vcall",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /Test\\#test_public_vcall/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /Test\\\\\\#test_public_vcall/",
{
"start_line": 17,
"start_column": 2,
Expand Down Expand Up @@ -564,7 +564,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"test_with_q?",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /Test\\#test_with_q\\?/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /Test\\\\\\#test_with_q\\\\\\?/",
{
"start_line": 19,
"start_column": 2,
Expand Down Expand Up @@ -662,7 +662,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /AnotherTest/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /AnotherTest/",
{
"start_line": 24,
"start_column": 0,
Expand Down Expand Up @@ -759,7 +759,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /AnotherTest\\#test_public/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /AnotherTest\\\\\\#test_public/",
{
"start_line": 25,
"start_column": 2,
Expand Down Expand Up @@ -855,7 +855,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"test_public_2",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name /AnotherTest\\#test_public_2/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/minitest_tests.rb\\ --name\\ /AnotherTest\\\\\\#test_public_2/",
{
"start_line": 31,
"start_column": 2,
Expand All @@ -871,7 +871,5 @@
}
}
],
"params": [

]
"params": []
}
18 changes: 8 additions & 10 deletions test/expectations/code_lens/nested_minitest_tests.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"arguments": [
"/fixtures/nested_minitest_tests.rb",
"ParentTest",
"bundle exec ruby -Itest /fixtures/nested_minitest_tests.rb --name /ParentTest/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/nested_minitest_tests.rb\\ --name\\ /ParentTest/",
{
"start_line": 0,
"start_column": 0,
Expand Down Expand Up @@ -180,7 +180,7 @@
"arguments": [
"/fixtures/nested_minitest_tests.rb",
"test_public",
"bundle exec ruby -Itest /fixtures/nested_minitest_tests.rb --name /ParentTest\\#test_public/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/nested_minitest_tests.rb\\ --name\\ /ParentTest\\\\\\#test_public/",
{
"start_line": 1,
"start_column": 2,
Expand Down Expand Up @@ -278,7 +278,7 @@
"arguments": [
"/fixtures/nested_minitest_tests.rb",
"FirstChildTest",
"bundle exec ruby -Itest /fixtures/nested_minitest_tests.rb --name /FirstChildTest/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/nested_minitest_tests.rb\\ --name\\ /FirstChildTest/",
{
"start_line": 5,
"start_column": 2,
Expand Down Expand Up @@ -375,7 +375,7 @@
"arguments": [
"/fixtures/nested_minitest_tests.rb",
"test_public",
"bundle exec ruby -Itest /fixtures/nested_minitest_tests.rb --name /FirstChildTest\\#test_public/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/nested_minitest_tests.rb\\ --name\\ /FirstChildTest\\\\\\#test_public/",
{
"start_line": 6,
"start_column": 4,
Expand Down Expand Up @@ -473,7 +473,7 @@
"arguments": [
"/fixtures/nested_minitest_tests.rb",
"SecondChildTest",
"bundle exec ruby -Itest /fixtures/nested_minitest_tests.rb --name /SecondChildTest/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/nested_minitest_tests.rb\\ --name\\ /SecondChildTest/",
{
"start_line": 13,
"start_column": 2,
Expand Down Expand Up @@ -570,7 +570,7 @@
"arguments": [
"/fixtures/nested_minitest_tests.rb",
"test_public",
"bundle exec ruby -Itest /fixtures/nested_minitest_tests.rb --name /SecondChildTest\\#test_public/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/nested_minitest_tests.rb\\ --name\\ /SecondChildTest\\\\\\#test_public/",
{
"start_line": 14,
"start_column": 4,
Expand Down Expand Up @@ -666,7 +666,7 @@
"arguments": [
"/fixtures/nested_minitest_tests.rb",
"test_public_again",
"bundle exec ruby -Itest /fixtures/nested_minitest_tests.rb --name /ParentTest\\#test_public_again/",
"bundle\\ exec\\ ruby\\ -Itest\\ /fixtures/nested_minitest_tests.rb\\ --name\\ /ParentTest\\\\\\#test_public_again/",
{
"start_line": 19,
"start_column": 2,
Expand All @@ -682,7 +682,5 @@
}
}
],
"params": [

]
"params": []
}
12 changes: 9 additions & 3 deletions test/requests/code_lens_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def test_bar; end
"bundle exec ruby -Itest /fake.rb --testcase /FooTest/ --name test_bar",
T.must(response[4]).command.arguments[2],
)
assert_equal("Debug", T.must(response[5]).command.title)
assert_equal(
"bundle\\ exec\\ ruby\\ -Itest\\ /fake.rb\\ --testcase\\ /FooTest/\\ --name\\ test_bar",
T.must(response[5]).command.arguments[2],
)
end

def test_escapes_command_for_debug
Expand All @@ -67,9 +72,10 @@ def test_bar; end

assert_equal("Debug", T.must(response[5]).command.title)
assert_equal(
"cd engines/my_engine " \
"&& bundle exec ruby -Itest /engines/my_engine/fake.rb --testcase /FooTest/ --name test_bar " \
"&& cd ../..",
"cd\\ engines/my_engine\\ " \
"\\&\\&\\ bundle\\ exec\\ ruby\\ -Itest\\ /engines/my_engine/fake.rb\\ --testcase\\ /FooTest/\\ " \
"--name\\ test_bar\\ " \
"\\&\\&\\ cd\\ ../..",
T.must(response[5]).command.arguments[2],
)
end
Expand Down

0 comments on commit 889f2c0

Please sign in to comment.