Skip to content
Open
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
26 changes: 19 additions & 7 deletions lib/ruby_lsp/listeners/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,37 +236,49 @@ def generate_minitest_command(group_stack, method_name, spec_name)
dynamic_stack = group_stack[last_dynamic_reference_index + 1..] #: as !nil

if method_name
" --name " + "/::#{Shellwords.escape(dynamic_stack.join("::")) + "#" + Shellwords.escape(method_name)}$/"
" --name " + "/::#{escape_for_shell(dynamic_stack.join("::")) + "#" + escape_for_shell(method_name)}$/"
else
# When clicking on a CodeLens for `Test`, `(#|::)` will match all tests
# that are registered on the class itself (matches after `#`) and all tests
# that are nested inside of that class in other modules/classes (matches after `::`)
" --name " + "\"/::#{Shellwords.escape(dynamic_stack.join("::"))}(#|::)/\""
" --name " + "\"/::#{escape_for_shell(dynamic_stack.join("::"))}(#|::)/\""
end
elsif method_name
# We know the entire path, do an exact match
" --name " + Shellwords.escape(group_stack.join("::")) + "#" + Shellwords.escape(method_name)
" --name " + escape_for_shell(group_stack.join("::")) + "#" + escape_for_shell(method_name)
elsif spec_name
" --name " + "\"/^#{Shellwords.escape(group_stack.join("::"))}##{Shellwords.escape(spec_name)}$/\""
" --name " + "\"/^#{escape_for_shell(group_stack.join("::"))}##{escape_for_shell(spec_name)}$/\""
else
# Execute all tests of the selected class and tests in
# modules/classes nested inside of that class
" --name " + "\"/^#{Shellwords.escape(group_stack.join("::"))}(#|::)/\""
" --name " + "\"/^#{escape_for_shell(group_stack.join("::"))}(#|::)/\""
end
end

#: (Array[String] group_stack, String? method_name) -> String
def generate_test_unit_command(group_stack, method_name)
group_name = group_stack.last #: as !nil
command = " --testcase " + "/#{Shellwords.escape(group_name)}/"
command = " --testcase " + "/#{escape_for_shell(group_name)}/"

if method_name
command += " --name " + Shellwords.escape(method_name)
command += " --name " + escape_for_shell(method_name)
end

command
end

#: (String text) -> String
def escape_for_shell(text)
if Gem.win_platform?
# On Windows, Shellwords.escape incorrectly escapes characters like `$`
# which breaks regex patterns. Windows doesn't need the same escaping
# as Unix shells for these characters.
text
Comment on lines +273 to +276
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you positive there's no need for any escaping? There are differences between PowerShell and exe and we have been bitten before with syntax errors that only happened in one or the other.

Would it be possible to add integration tests that generate the test commands and then attempt to run it on both PowerShell and exe? That would be super helpful for ensuring correctness.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All shells need escaping, but not for the arguments being passed to this method. They are all generated by our code, not based on user input, no? They would only need escaping if we were passing quoted strings as arguments to this method.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also make sure we avoid escaping in the calls that would receive a regexp. I think it is only the method name

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is escaping the user's test class names and test method names, which may include regex wild cards for dynamically defined classes.

For example:

module Foo
  module variable
    class MyTest < Minitest::Test
    end
  end
end

This will generate a regex with a wild card to allow us to execute the test despite the dynamic portion of the nesting.

Are all characters involved valid for PowerShell and exe? That's the part I'm not sure.

For example, we started escaping the regex anchor $ because not escaping it breaks execution on the fish shell.

At the end of the day, we need proper tests to verify that we generate can be correctly executed on Windows or we'll always be subject to a small modification not being syntax compatible with its shells.

else
Shellwords.escape(text)
end
end

#: (Prism::CallNode node, kind: Symbol) -> void
def add_spec_code_lens(node, kind:)
arguments = node.arguments
Expand Down
Loading