Skip to content

Commit 630ab6b

Browse files
authored
Show message when trying to run a test that hasn't been discovered (#3631)
* Show message when trying to run a test that hasn't been discovered * Don't show code lens for tests not matching the expected path
1 parent 38edb1f commit 630ab6b

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed

jekyll/test_explorer.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ There is limited support to using multiple test frameworks in the same codebase.
4949
and we will not make further investments into supporting it, in line with our design principle of [favoring common
5050
setups](design-and-roadmap#favoring-common-development-setups)
5151
52+
{: .important }
53+
To discover all test files in the workspace with decent performance, the Ruby LSP uses a glob pattern based on
54+
conventions. For a test file to be discovered, the file path must match this glob:
55+
`**/{test,spec,features}/**/{*_test.rb,test_*.rb,*_spec.rb,*.feature}`
56+
5257
### Dynamically defined tests
5358
5459
There is limited support for tests defined via meta-programming. Initially, they will not be present in the test

lib/ruby_lsp/requests/code_lens.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ def initialize(global_state, document, dispatcher)
2727
@document = document
2828
@test_builder = ResponseBuilders::TestCollection.new #: ResponseBuilders::TestCollection
2929
uri = document.uri
30+
file_path = uri.full_path
3031
code_lens_config = global_state.feature_configuration(:codeLens)
31-
test_lenses_enabled = !code_lens_config || code_lens_config.enabled?(:enableTestCodeLens)
32+
test_lenses_enabled = (!code_lens_config || code_lens_config.enabled?(:enableTestCodeLens)) &&
33+
file_path && File.fnmatch?(TEST_PATH_PATTERN, file_path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
3234

3335
if global_state.enabled_feature?(:fullTestDiscovery)
3436
if test_lenses_enabled

lib/ruby_lsp/utils.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module RubyLsp
2020
"Gemfile"
2121
end #: String
2222
GUESSED_TYPES_URL = "https://shopify.github.io/ruby-lsp/#guessed-types"
23+
TEST_PATH_PATTERN = "**/{test,spec,features}/**/{*_test.rb,test_*.rb,*_spec.rb,*.feature}"
2324

2425
# Request delegation for embedded languages is not yet standardized into the language server specification. Here we
2526
# use this custom error class as a way to return a signal to the client that the request should be delegated to the

test/requests/discover_tests_test.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_something_else; end
143143
end
144144
RUBY
145145

146-
with_server(source) do |server, uri|
146+
with_server(source, URI::Generic.from_path(path: "/test/foo_test.rb")) do |server, uri|
147147
server.global_state.index.index_single(URI("/other_file.rb"), <<~RUBY)
148148
module Test
149149
module Unit
@@ -227,7 +227,7 @@ def test_something_else; end
227227
end
228228
RUBY
229229

230-
with_server(source) do |server, uri|
230+
with_server(source, URI::Generic.from_path(path: "/test/foo_test.rb")) do |server, uri|
231231
server.global_state.index.index_single(URI("/other_file.rb"), <<~RUBY)
232232
module Test
233233
module Unit
@@ -259,6 +259,39 @@ class TestCase; end
259259
end
260260
end
261261

262+
def test_does_not_collect_code_lenses_for_files_not_matching_path_convention
263+
source = <<~RUBY
264+
module Foo
265+
class MyTest < Test::Unit::TestCase
266+
def test_something; end
267+
268+
def test_something_else; end
269+
end
270+
end
271+
RUBY
272+
273+
with_server(source, URI::Generic.from_path(path: "/tests/something.rb")) do |server, uri|
274+
server.global_state.index.index_single(URI("/other_file.rb"), <<~RUBY)
275+
module Test
276+
module Unit
277+
class TestCase; end
278+
end
279+
end
280+
RUBY
281+
282+
server.global_state.stubs(:enabled_feature?).returns(true)
283+
284+
server.process_message(id: 1, method: "textDocument/codeLens", params: {
285+
textDocument: { uri: uri },
286+
})
287+
288+
# Discard the indexing log message
289+
server.pop_response
290+
items = get_response(server)
291+
assert_empty(items)
292+
end
293+
end
294+
262295
def test_ignores_minitest_tests_that_extend_active_support_declarative
263296
source = <<~RUBY
264297
class MyTest < ActiveSupport::TestCase

vscode/src/test/suite/client.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,15 @@ suite("Client", () => {
359359
" end",
360360
"end",
361361
].join("\n");
362+
const uri = vscode.Uri.joinPath(
363+
workspaceUri,
364+
"test",
365+
"server_test.rb",
366+
).toString();
362367

363368
await client.sendNotification("textDocument/didOpen", {
364369
textDocument: {
365-
uri: documentUri.toString(),
370+
uri,
366371
version: 1,
367372
text,
368373
},
@@ -371,7 +376,7 @@ suite("Client", () => {
371376
"textDocument/codeLens",
372377
{
373378
textDocument: {
374-
uri: documentUri.toString(),
379+
uri,
375380
},
376381
},
377382
);

vscode/src/testController.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,17 @@ export class TestController {
384384
async runViaCommand(path: string, name: string, mode: Mode) {
385385
const uri = vscode.Uri.file(path);
386386
const testItem = await this.findTestItem(name, uri);
387-
if (!testItem) return;
387+
388+
if (!testItem) {
389+
await vscode.window.showErrorMessage(
390+
`Attempted to run "${name}" defined in "${path}", but that test has not been discovered.
391+
Does the file path match the expected glob pattern?
392+
[Read more](https://shopify.github.io/ruby-lsp/test_explorer.html)
393+
394+
Expected pattern: "**/{test,spec,features}/**/{*_test.rb,test_*.rb,*_spec.rb,*.feature}"`,
395+
);
396+
return;
397+
}
388398

389399
if (mode === Mode.Run) {
390400
await vscode.commands.executeCommand(

0 commit comments

Comments
 (0)