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/ruby_lsp_rails/rails_test_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ def resolve_test_commands(items)

if tags.include?("test_dir")
if children.empty?
full_files.concat(Dir.glob(
"#{path}/**/{*_test,test_*}.rb",
File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME,
))
full_files.concat(
Dir.glob(
"#{path}/**/{*_test,test_*}.rb",
File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME,
).map! { |f| Shellwords.escape(f) },
)
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")
commands << "#{BASE_COMMAND} #{path} --name \"/#{Shellwords.escape(item[:id])}(#|::)/\""
commands << "#{BASE_COMMAND} #{Shellwords.escape(path)} --name \"/#{Shellwords.escape(item[:id])}(#|::)/\""
else
full_files << "#{path}:#{item.dig(:range, :start, :line) + 1}"
full_files << "#{Shellwords.escape(path)}:#{item.dig(:range, :start, :line) + 1}"
end

queue.concat(children)
Expand Down
130 changes: 130 additions & 0 deletions test/ruby_lsp_rails/rails_test_style_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,136 @@ class SpecialCharsTest < ActiveSupport::TestCase
end
end

test "resolve test escapes file paths in groups" do
with_server do |server|
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@rails_runner_client).is_a?(NullClient)

server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "GroupTest",
uri: "file:///test/group(v2)_test.rb",
label: "GroupTest",
range: {
start: { line: 0, character: 0 },
end: { line: 30, character: 3 },
},
tags: ["framework:rails", "test_group"],
children: [],
},
],
},
})

result = pop_result(server)
response = result.response

assert_equal(
["#{RailsTestStyle::BASE_COMMAND} /test/group\\(v2\\)_test.rb --name \"/GroupTest(#|::)/\""],
response[:commands],
)
end
end

test "resolve test escapes single file paths" do
with_server do |server|
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@rails_runner_client).is_a?(NullClient)

server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "file:///test/example(v2)_test.rb",
uri: "file:///test/example(v2)_test.rb",
label: "/test/example(v2)_test.rb",
tags: ["framework:rails", "test_file"],
children: [],
},
],
},
})

result = pop_result(server)
response = result.response

assert_equal(
["#{RailsTestStyle::BASE_COMMAND} /test/example\\(v2\\)_test.rb"],
response[:commands],
)
end
end

test "resolve test escapes file paths inside directories" do
Dir.stubs(:glob).returns(["/test/example(v2)_test.rb"])

with_server do |server|
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@rails_runner_client).is_a?(NullClient)

server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "file:///test",
uri: "file:///test",
label: "/test",
tags: ["test_dir", "framework:rails"],
children: [],
},
],
},
})

result = pop_result(server)
response = result.response

assert_equal(
["#{RailsTestStyle::BASE_COMMAND} /test/example\\(v2\\)_test.rb"],
response[:commands],
)
end
end

test "resolve test escapes file paths for specific examples" do
with_server do |server|
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@rails_runner_client).is_a?(NullClient)

server.process_message({
id: 1,
method: "rubyLsp/resolveTestCommands",
params: {
items: [
{
id: "ExampleTest#test_something",
uri: "file:///test/example(v2)_test.rb",
label: "test something",
tags: ["framework:rails"],
range: {
start: { line: 10, character: 0 },
end: { line: 15, character: 3 },
},
children: [],
},
],
},
})

result = pop_result(server)
response = result.response

assert_equal(
["#{RailsTestStyle::BASE_COMMAND} /test/example\\(v2\\)_test.rb:11"],
response[:commands],
)
end
end

test "tests with backslashes" do
source = File.read(File.join(__dir__, "..", "fixtures", "test_with_escaped_quotes.rb"))

Expand Down
Loading