@@ -31,50 +31,68 @@ let lastDebugWorkspaceFolder: vscode.WorkspaceFolder;
31
31
32
32
export type TestAtCursorCmd = 'debug' | 'test' | 'benchmark' ;
33
33
34
- /**
35
- * Executes the unit test at the primary cursor using `go test`. Output
36
- * is sent to the 'Go' channel.
37
- * @param goConfig Configuration for the Go extension.
38
- * @param cmd Whether the command is test , benchmark or debug.
39
- * @param args
40
- */
41
- export function testAtCursor ( goConfig : vscode . WorkspaceConfiguration , cmd : TestAtCursorCmd , args : any ) {
34
+ class NotFoundError extends Error { }
35
+
36
+ async function _testAtCursor ( goConfig : vscode . WorkspaceConfiguration , cmd : TestAtCursorCmd , args : any ) {
42
37
const editor = vscode . window . activeTextEditor ;
43
38
if ( ! editor ) {
44
- vscode . window . showInformationMessage ( 'No editor is active.' ) ;
45
- return ;
39
+ throw new NotFoundError ( 'No editor is active.' ) ;
46
40
}
47
41
if ( ! editor . document . fileName . endsWith ( '_test.go' ) ) {
48
- vscode . window . showInformationMessage ( 'No tests found. Current file is not a test file.' ) ;
49
- return ;
42
+ throw new NotFoundError ( 'No tests found. Current file is not a test file.' ) ;
50
43
}
51
44
52
45
const getFunctions = cmd === 'benchmark' ? getBenchmarkFunctions : getTestFunctions ;
46
+ const testFunctions = await getFunctions ( editor . document , null ) ;
47
+ // We use functionName if it was provided as argument
48
+ // Otherwise find any test function containing the cursor.
49
+ const testFunctionName =
50
+ args && args . functionName
51
+ ? args . functionName
52
+ : testFunctions . filter ( ( func ) => func . range . contains ( editor . selection . start ) ) . map ( ( el ) => el . name ) [ 0 ] ;
53
+ if ( ! testFunctionName ) {
54
+ throw new NotFoundError ( 'No test function found at cursor.' ) ;
55
+ }
53
56
54
- editor . document . save ( ) . then ( async ( ) => {
55
- try {
56
- const testFunctions = await getFunctions ( editor . document , null ) ;
57
- // We use functionName if it was provided as argument
58
- // Otherwise find any test function containing the cursor.
59
- const testFunctionName =
60
- args && args . functionName
61
- ? args . functionName
62
- : testFunctions
63
- . filter ( ( func ) => func . range . contains ( editor . selection . start ) )
64
- . map ( ( el ) => el . name ) [ 0 ] ;
65
- if ( ! testFunctionName ) {
66
- vscode . window . showInformationMessage ( 'No test function found at cursor.' ) ;
67
- return ;
68
- }
57
+ await editor . document . save ( ) ;
69
58
70
- if ( cmd === 'debug' ) {
71
- await debugTestAtCursor ( editor , testFunctionName , testFunctions , goConfig ) ;
72
- } else if ( cmd === 'benchmark' || cmd === 'test' ) {
73
- await runTestAtCursor ( editor , testFunctionName , testFunctions , goConfig , cmd , args ) ;
74
- } else {
75
- throw new Error ( 'Unsupported command.' ) ;
76
- }
77
- } catch ( err ) {
59
+ if ( cmd === 'debug' ) {
60
+ return debugTestAtCursor ( editor , testFunctionName , testFunctions , goConfig ) ;
61
+ } else if ( cmd === 'benchmark' || cmd === 'test' ) {
62
+ return runTestAtCursor ( editor , testFunctionName , testFunctions , goConfig , cmd , args ) ;
63
+ } else {
64
+ throw new Error ( `Unsupported command: ${ cmd } ` ) ;
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Executes the unit test at the primary cursor using `go test`. Output
70
+ * is sent to the 'Go' channel.
71
+ * @param goConfig Configuration for the Go extension.
72
+ * @param cmd Whether the command is test, benchmark, or debug.
73
+ * @param args
74
+ */
75
+ export function testAtCursor ( goConfig : vscode . WorkspaceConfiguration , cmd : TestAtCursorCmd , args : any ) {
76
+ _testAtCursor ( goConfig , cmd , args ) . catch ( ( err ) => {
77
+ if ( err instanceof NotFoundError ) {
78
+ vscode . window . showInformationMessage ( err . message ) ;
79
+ } else {
80
+ console . error ( err ) ;
81
+ }
82
+ } ) ;
83
+ }
84
+
85
+ /**
86
+ * Executes the unit test at the primary cursor if found, otherwise re-runs the previous test.
87
+ * @param goConfig Configuration for the Go extension.
88
+ * @param cmd Whether the command is test, benchmark, or debug.
89
+ * @param args
90
+ */
91
+ export function testAtCursorOrPrevious ( goConfig : vscode . WorkspaceConfiguration , cmd : TestAtCursorCmd , args : any ) {
92
+ _testAtCursor ( goConfig , cmd , args ) . catch ( ( err ) => {
93
+ if ( err instanceof NotFoundError ) {
94
+ testPrevious ( ) ;
95
+ } else {
78
96
console . error ( err ) ;
79
97
}
80
98
} ) ;
0 commit comments