Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minitest code lens filter patterns #2522

Merged
merged 5 commits into from
Sep 12, 2024
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
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class MyRequestExpectationsTest < ExpectationsTestRunner
end
```

To automatically create or update `.exp.json` files, you can use the `WRITE_EXPECTATIONS` environment variable. For example:

```sh
WRITE_EXPECTATIONS=1 bin/test test/requests/code_lens_expectations_test.rb
```

## Debugging with VS Code

## Debugging Tests
Expand Down
39 changes: 34 additions & 5 deletions lib/ruby_lsp/listeners/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def initialize(response_builder, global_state, uri, dispatcher)
@group_id_stack = T.let([], T::Array[Integer])
# We want to avoid adding code lenses for nested definitions
@def_depth = T.let(0, Integer)
@spec_id = T.let(0, Integer)

dispatcher.register(
self,
Expand All @@ -70,6 +71,7 @@ def on_class_node_enter(node)
name: class_name,
command: generate_test_command(group_stack: @group_stack),
kind: :group,
id: generate_fully_qualified_id(group_stack: @group_stack),
)

@group_id_stack.push(@group_id)
Expand Down Expand Up @@ -106,6 +108,7 @@ def on_def_node_enter(node)
name: method_name,
command: generate_test_command(method_name: method_name, group_stack: @group_stack),
kind: :example,
id: generate_fully_qualified_id(group_stack: @group_stack, method_name: method_name),
)
end
end
Expand Down Expand Up @@ -166,26 +169,28 @@ def on_call_node_leave(node)
@visibility_stack.push([prev_visibility, prev_visibility])
if node.name == DESCRIBE_KEYWORD
@group_id_stack.pop
@group_stack.pop
end
end

private

sig { params(node: Prism::Node, name: String, command: String, kind: Symbol).void }
def add_test_code_lens(node, name:, command:, kind:)
sig { params(node: Prism::Node, name: String, command: String, kind: Symbol, id: String).void }
def add_test_code_lens(node, name:, command:, kind:, id: name)
# don't add code lenses if the test library is not supported or unknown
return unless SUPPORTED_TEST_LIBRARIES.include?(@global_state.test_library) && @path

arguments = [
@path,
name,
id,
command,
{
start_line: node.location.start_line - 1,
start_column: node.location.start_column,
end_line: node.location.end_line - 1,
end_column: node.location.end_column,
},
name,
]

grouping_data = { group_id: @group_id_stack.last, kind: kind }
Expand Down Expand Up @@ -247,7 +252,7 @@ def generate_test_command(group_stack: [], spec_name: nil, method_name: nil)
# We know the entire path, do an exact match
" --name " + Shellwords.escape(group_stack.join("::")) + "#" + Shellwords.escape(method_name)
elsif spec_name
" --name " + "/#{Shellwords.escape(spec_name)}/"
" --name " + "\"/^#{Shellwords.escape(group_stack.join("::"))}##{Shellwords.escape(spec_name)}$/\""
else
# Execute all tests of the selected class and tests in
# modules/classes nested inside of that class
Expand Down Expand Up @@ -282,15 +287,39 @@ def add_spec_code_lens(node, kind:)

return unless name

if kind == :example
# Increment spec_id for each example
@spec_id += 1
else
# Reset spec_id when entering a new group
@spec_id = 0
@group_stack.push(name)
end

if @path
method_name = format("test_%04d_%s", @spec_id, name) if kind == :example
vinistock marked this conversation as resolved.
Show resolved Hide resolved
add_test_code_lens(
node,
name: name,
command: generate_test_command(spec_name: name),
command: generate_test_command(group_stack: @group_stack, spec_name: method_name),
kind: kind,
id: generate_fully_qualified_id(group_stack: @group_stack, method_name: method_name),
)
end
end

sig { params(group_stack: T::Array[String], method_name: T.nilable(String)).returns(String) }
def generate_fully_qualified_id(group_stack:, method_name: nil)
vinistock marked this conversation as resolved.
Show resolved Hide resolved
if method_name
# For tests, this will be the test class and method name: `Foo::BarTest#test_baz`.
# For specs, this will be the nested descriptions and formatted test name: `a::b::c#test_001_foo`.
group_stack.join("::") + "#" + method_name
else
# For tests, this will be the test class: `Foo::BarTest`.
# For specs, this will be the nested descriptions: `a::b::c`.
group_stack.join("::")
end
end
end
end
end
Loading
Loading