Skip to content

Commit 337ae93

Browse files
committed
src/testUtils: tighten test function detection regex
According to https://golang.org/pkg/testing/ test function names should be of form func TestXxx(*testing.T) where Xxx does not start with a lowercase letter. Go uses unicode for this. Mimic the behavior encoded in https://github.com/golang/go/blob/117b1c84d3678a586c168a5f7f2f0a750c27f0c2/src/cmd/go/internal/load/test.go#L48 by using unicode regexp and property. Fixes #1417 Change-Id: I3fa510e5e567722b5b09c2618034686a9c5c3d90 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/309631 Trust: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent bded07b commit 337ae93

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/testUtils.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ statusBarItem.text = '$(x) Cancel Running Tests';
3232
*/
3333
const runningTestProcesses: cp.ChildProcess[] = [];
3434

35-
const testFuncRegex = /^Test.*|^Example.*/;
36-
const testMethodRegex = /^\(([^)]+)\)\.(Test.*)$/;
37-
const benchmarkRegex = /^Benchmark.*/;
35+
// https://github.com/golang/go/blob/117b1c84d3678a586c168a5f7f2f0a750c27f0c2/src/cmd/go/internal/load/test.go#L487
36+
// uses !unicode.isLower to find test/example/benchmark functions.
37+
// There could be slight difference between \P{Ll} (not lowercase letter)
38+
// & go unicode package's uppercase detection. But hopefully
39+
// these will be replaced by gopls's codelens computation soon.
40+
const testFuncRegex = /^Test\P{Ll}.*|^Example\P{Ll}.*/u;
41+
const testMethodRegex = /^\(([^)]+)\)\.(Test\P{Ll}.*)$/u;
42+
const benchmarkRegex = /^Benchmark\P{Ll}.*/u;
3843

3944
/**
4045
* Input to goTest.

test/integration/codelens.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,21 @@ suite('Code lenses for testing and benchmarking', function () {
129129
assert.equal(codeLenses[i].command.command, wantCommands[i]);
130130
}
131131
});
132+
133+
test('Test codelenses include only valid test function names', async () => {
134+
const uri = vscode.Uri.file(path.join(fixturePath, 'codelens2_test.go'));
135+
const benchmarkDocument = await vscode.workspace.openTextDocument(uri);
136+
const codeLenses = await codeLensProvider.provideCodeLenses(benchmarkDocument, cancellationTokenSource.token);
137+
assert.equal(codeLenses.length, 12, JSON.stringify(codeLenses, null, 2));
138+
const found = [] as string[];
139+
for (let i = 0; i < codeLenses.length; i++) {
140+
const lens = codeLenses[i];
141+
if (lens.command.command === 'go.test.cursor') {
142+
found.push(lens.command.arguments[0].functionName);
143+
}
144+
}
145+
found.sort();
146+
// Results should match `go test -list`.
147+
assert.deepStrictEqual(found, ['Test1Function', 'TestFunction', 'Test_foobar', 'TestΣυνάρτηση', 'Test함수']);
148+
});
132149
});
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// As of Go1.16, `go test -list` returns
8+
// TestFunction
9+
// Test1Function
10+
// TestΣυνάρτηση
11+
// Test함수
12+
// Test_foobar
13+
func TestFunction(t *testing.T) {
14+
t.Log("this is a valid test function")
15+
}
16+
17+
func Testfunction(t *testing.T) {
18+
t.Fatal("this is not a valid test function")
19+
}
20+
21+
func Test1Function(t *testing.T) {
22+
t.Log("this is an acceptable test function")
23+
}
24+
25+
func TestΣυνάρτηση(t *testing.T) {
26+
t.Log("this is a valid test function")
27+
}
28+
29+
func Testσυνάρτηση(t *testing.T) {
30+
t.Fatal("this is not a valid test function")
31+
}
32+
33+
func Test함수(t *testing.T) {
34+
t.Log("this is a valid test function")
35+
}
36+
37+
func Test_foobar(t *testing.T) {
38+
t.Log("this is an acceptable test function")
39+
}

0 commit comments

Comments
 (0)