This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 647
/
goTest.ts
235 lines (207 loc) · 7.25 KB
/
goTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import path = require('path');
import vscode = require('vscode');
import os = require('os');
import { getTempFilePath } from './util';
import { goTest, TestConfig, getTestFlags, getTestFunctions, getBenchmarkFunctions, extractInstanceTestName, findAllTestSuiteRuns } from './testUtils';
import { applyCodeCoverageToAllEditors } from './goCover';
import { isModSupported } from './goModules';
// lastTestConfig holds a reference to the last executed TestConfig which allows
// the last test to be easily re-executed.
let lastTestConfig: TestConfig;
/**
* Executes the unit test at the primary cursor using `go test`. Output
* is sent to the 'Go' channel.
*
* @param goConfig Configuration for the Go extension.
*/
export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, isBenchmark: boolean, args: any) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (!editor.document.fileName.endsWith('_test.go')) {
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
return;
}
const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions;
const { tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnSingleTest', args);
editor.document.save().then(() => {
return getFunctions(editor.document, null).then(testFunctions => {
let testFunctionName: string;
// We use functionName if it was provided as argument
// Otherwise find any test function containing the cursor.
if (args && args.functionName) {
testFunctionName = args.functionName;
} else {
for (let func of testFunctions) {
let selection = editor.selection;
if (selection && func.location.range.contains(selection.start)) {
testFunctionName = func.name;
break;
}
};
}
if (!testFunctionName) {
vscode.window.showInformationMessage('No test function found at cursor.');
return;
}
let testConfigFns = [testFunctionName];
if (!isBenchmark && extractInstanceTestName(testFunctionName)) {
// find test function with corresponding suite.Run
const testFns = findAllTestSuiteRuns(editor.document, testFunctions);
if (testFns) {
testConfigFns = testConfigFns.concat(testFns.map(t => t.name));
}
}
const testConfig: TestConfig = {
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: testFlags,
functions: testConfigFns,
isBenchmark: isBenchmark,
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
return isModSupported(editor.document.uri).then(isMod => {
testConfig.isMod = isMod;
return goTest(testConfig).then(success => {
if (success && tmpCoverPath) {
return applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
}
});
});
});
}).then(null, err => {
console.error(err);
});
}
/**
* Runs all tests in the package of the source of the active editor.
*
* @param goConfig Configuration for the Go extension.
*/
export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, isBenchmark: boolean, args: any) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
const { tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnTestPackage', args);
const testConfig: TestConfig = {
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: testFlags,
isBenchmark: isBenchmark,
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
isModSupported(editor.document.uri).then(isMod => {
testConfig.isMod = isMod;
return goTest(testConfig).then(success => {
if (success && tmpCoverPath) {
return applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
}
}, err => {
console.log(err);
});
});
}
/**
* Runs all tests from all directories in the workspace.
*
* @param goConfig Configuration for the Go extension.
*/
export function testWorkspace(goConfig: vscode.WorkspaceConfiguration, args: any) {
if (!vscode.workspace.workspaceFolders.length) {
vscode.window.showInformationMessage('No workspace is open to run tests.');
return;
}
let workspaceUri = vscode.workspace.workspaceFolders[0].uri;
if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) {
workspaceUri = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri;
}
const testConfig: TestConfig = {
goConfig: goConfig,
dir: workspaceUri.fsPath,
flags: getTestFlags(goConfig, args),
includeSubDirectories: true
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
isModSupported(workspaceUri).then(isMod => {
testConfig.isMod = isMod;
goTest(testConfig).then(null, err => {
console.error(err);
});
});
}
/**
* Runs all tests in the source of the active editor.
*
* @param goConfig Configuration for the Go extension.
* @param isBenchmark Boolean flag indicating if these are benchmark tests or not.
*/
export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration, isBenchmark: boolean, args: string[]): Thenable<boolean> {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (!editor.document.fileName.endsWith('_test.go')) {
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
return;
}
const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions;
return editor.document.save().then(() => {
return getFunctions(editor.document, null).then(testFunctions => {
const testConfig: TestConfig = {
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: getTestFlags(goConfig, args),
functions: testFunctions.map(sym => sym.name),
isBenchmark: isBenchmark,
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
return isModSupported(editor.document.uri).then(isMod => {
testConfig.isMod = isMod;
return goTest(testConfig);
});
});
}).then(null, err => {
console.error(err);
return Promise.resolve(false);
});
}
/**
* Runs the previously executed test.
*/
export function testPrevious() {
if (!lastTestConfig) {
vscode.window.showInformationMessage('No test has been recently executed.');
return;
}
goTest(lastTestConfig).then(null, err => {
console.error(err);
});
}
/**
* Computes the tmp coverage path and needed flags.
*
* @param goConfig Configuration for the Go extension.
*/
function makeCoverData(goConfig: vscode.WorkspaceConfiguration, confFlag: string, args: any): { tmpCoverPath: string, testFlags: string[] } {
let tmpCoverPath = '';
let testFlags = getTestFlags(goConfig, args) || [];
if (goConfig[confFlag] === true) {
tmpCoverPath = getTempFilePath('go-code-cover');
testFlags.push('-coverprofile=' + tmpCoverPath);
}
return { tmpCoverPath, testFlags };
}