Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit 24b493b

Browse files
committed
-coverprofile flag shld be set before user provided flags
1 parent 9ab7b8b commit 24b493b

File tree

4 files changed

+46
-80
lines changed

4 files changed

+46
-80
lines changed

src/goCheck.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
import vscode = require('vscode');
99
import path = require('path');
1010
import { goBuild } from './goBuild';
11-
import { applyCodeCoverageToAllEditors } from './goCover';
1211
import { parseLanguageServerConfig } from './goLanguageServer';
1312
import { goLint } from './goLint';
1413
import { buildDiagnosticCollection, lintDiagnosticCollection, vetDiagnosticCollection } from './goMain';
1514
import { isModSupported } from './goModules';
1615
import { diagnosticsStatusBarItem, outputChannel } from './goStatus';
1716
import { goVet } from './goVet';
1817
import { getTestFlags, goTest, TestConfig } from './testUtils';
19-
import { getTempFilePath, ICheckResult } from './util';
18+
import { ICheckResult } from './util';
2019

2120
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
2221
statusBarItem.command = 'go.test.showOutput';
@@ -61,24 +60,19 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati
6160
const disableBuildAndVet = lspConfig.enabled && lspConfig.features.diagnostics;
6261

6362
let testPromise: Thenable<boolean>;
64-
let tmpCoverPath: string;
6563
const testConfig: TestConfig = {
6664
goConfig,
6765
dir: cwd,
6866
flags: getTestFlags(goConfig),
69-
background: true
67+
background: true,
68+
applyCodeCoverage: !!goConfig['coverOnSave']
7069
};
7170

7271
const runTest = () => {
7372
if (testPromise) {
7473
return testPromise;
7574
}
7675

77-
if (goConfig['coverOnSave']) {
78-
tmpCoverPath = getTempFilePath('go-code-cover');
79-
testConfig.flags.push('-coverprofile=' + tmpCoverPath);
80-
}
81-
8276
testPromise = isModSupported(fileUri).then(isMod => {
8377
testConfig.isMod = isMod;
8478
return goTest(testConfig);
@@ -122,8 +116,6 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati
122116
if (!success) {
123117
return [];
124118
}
125-
// FIXME: it's not obvious that tmpCoverPath comes from runTest()
126-
return applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
127119
});
128120
}
129121

src/goCover.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export function removeCodeCoverageOnFileChange(e: vscode.TextDocumentChangeEvent
282282
* If current editor has Code coverage applied, then remove it.
283283
* Else run tests to get the coverage and apply.
284284
*/
285-
export function toggleCoverageCurrentPackage() {
285+
export async function toggleCoverageCurrentPackage() {
286286
const editor = vscode.window.activeTextEditor;
287287
if (!editor) {
288288
vscode.window.showInformationMessage('No editor is active.');
@@ -298,22 +298,20 @@ export function toggleCoverageCurrentPackage() {
298298
const cwd = path.dirname(editor.document.uri.fsPath);
299299

300300
const testFlags = getTestFlags(goConfig);
301-
const tmpCoverPath = getTempFilePath('go-code-cover');
302-
const args = ['-coverprofile=' + tmpCoverPath, ...testFlags];
301+
const isMod = await isModSupported(editor.document.uri);
303302
const testConfig: TestConfig = {
304303
goConfig,
305304
dir: cwd,
306-
flags: args,
307-
background: true
305+
flags: testFlags,
306+
background: true,
307+
isMod,
308+
applyCodeCoverage: true
308309
};
309-
return isModSupported(editor.document.uri).then(isMod => {
310-
testConfig.isMod = isMod;
311-
return goTest(testConfig).then(success => {
312-
if (!success) {
313-
showTestOutput();
314-
}
315-
return applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
316-
});
310+
311+
return goTest(testConfig).then(success => {
312+
if (!success) {
313+
showTestOutput();
314+
}
317315
});
318316
}
319317

src/goTest.ts

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestA
6868
* Runs the test at cursor.
6969
*/
7070
async function runTestAtCursor(editor: vscode.TextEditor, testFunctionName: string, testFunctions: vscode.DocumentSymbol[], goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) {
71-
const { tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnSingleTest', args);
7271

7372
const testConfigFns = cmd !== 'benchmark' && extractInstanceTestName(testFunctionName)
7473
? [testFunctionName, ...findAllTestSuiteRuns(editor.document, testFunctions).map(t => t.name)]
@@ -78,17 +77,15 @@ async function runTestAtCursor(editor: vscode.TextEditor, testFunctionName: stri
7877
const testConfig: TestConfig = {
7978
goConfig,
8079
dir: path.dirname(editor.document.fileName),
81-
flags: testFlags,
80+
flags: getTestFlags(goConfig, args),
8281
functions: testConfigFns,
8382
isBenchmark: cmd === 'benchmark',
84-
isMod
83+
isMod,
84+
applyCodeCoverage: goConfig.get<boolean>('coverOnSingleTest')
8585
};
8686
// Remember this config as the last executed test.
8787
lastTestConfig = testConfig;
88-
await goTest(testConfig);
89-
if (tmpCoverPath) {
90-
return applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
91-
}
88+
return goTest(testConfig);
9289
}
9390

9491
/**
@@ -132,34 +129,25 @@ async function debugTestAtCursor(editor: vscode.TextEditor, testFunctionName: st
132129
*
133130
* @param goConfig Configuration for the Go extension.
134131
*/
135-
export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, isBenchmark: boolean, args: any) {
132+
export async function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, isBenchmark: boolean, args: any) {
136133
const editor = vscode.window.activeTextEditor;
137134
if (!editor) {
138135
vscode.window.showInformationMessage('No editor is active.');
139136
return;
140137
}
141138

142-
const { tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnTestPackage', args);
143-
139+
const isMod = await isModSupported(editor.document.uri);
144140
const testConfig: TestConfig = {
145141
goConfig,
146142
dir: path.dirname(editor.document.fileName),
147-
flags: testFlags,
143+
flags: getTestFlags(goConfig, args),
148144
isBenchmark,
145+
isMod,
146+
applyCodeCoverage: goConfig.get<boolean>('coverOnTestPackage')
149147
};
150148
// Remember this config as the last executed test.
151149
lastTestConfig = testConfig;
152-
153-
isModSupported(editor.document.uri).then(isMod => {
154-
testConfig.isMod = isMod;
155-
return goTest(testConfig).then(success => {
156-
if (tmpCoverPath) {
157-
return applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
158-
}
159-
}, err => {
160-
console.log(err);
161-
});
162-
});
150+
return goTest(testConfig);
163151
}
164152

165153
/**
@@ -200,7 +188,7 @@ export function testWorkspace(goConfig: vscode.WorkspaceConfiguration, args: any
200188
* @param goConfig Configuration for the Go extension.
201189
* @param isBenchmark Boolean flag indicating if these are benchmark tests or not.
202190
*/
203-
export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration, isBenchmark: boolean, args: string[]): Thenable<boolean> {
191+
export async function testCurrentFile(goConfig: vscode.WorkspaceConfiguration, isBenchmark: boolean, args: string[]): Promise<boolean> {
204192
const editor = vscode.window.activeTextEditor;
205193
if (!editor) {
206194
vscode.window.showInformationMessage('No editor is active.');
@@ -211,32 +199,23 @@ export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration, isBench
211199
return;
212200
}
213201

214-
const { tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnSingleTestFile', args);
215202
const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions;
203+
const isMod = await isModSupported(editor.document.uri);
216204

217205
return editor.document.save().then(() => {
218206
return getFunctions(editor.document, null).then(testFunctions => {
219207
const testConfig: TestConfig = {
220208
goConfig,
221209
dir: path.dirname(editor.document.fileName),
222-
flags: testFlags,
210+
flags: getTestFlags(goConfig, args),
223211
functions: testFunctions.map(sym => sym.name),
224212
isBenchmark,
213+
isMod,
214+
applyCodeCoverage: goConfig.get<boolean>('coverOnSingleTestFile')
225215
};
226216
// Remember this config as the last executed test.
227217
lastTestConfig = testConfig;
228-
229-
return isModSupported(editor.document.uri).then(isMod => {
230-
testConfig.isMod = isMod;
231-
return goTest(testConfig).then(success => {
232-
if (tmpCoverPath) {
233-
applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
234-
}
235-
return Promise.resolve(success);
236-
}, err => {
237-
console.log(err);
238-
});
239-
});
218+
return goTest(testConfig);
240219
});
241220
}).then(null, err => {
242221
console.error(err);
@@ -256,19 +235,3 @@ export function testPrevious() {
256235
console.error(err);
257236
});
258237
}
259-
260-
/**
261-
* Computes the tmp coverage path and needed flags.
262-
*
263-
* @param goConfig Configuration for the Go extension.
264-
*/
265-
function makeCoverData(goConfig: vscode.WorkspaceConfiguration, confFlag: string, args: any): { tmpCoverPath: string, testFlags: string[] } {
266-
let tmpCoverPath = '';
267-
const testFlags = getTestFlags(goConfig, args) || [];
268-
if (goConfig[confFlag] === true) {
269-
tmpCoverPath = getTempFilePath('go-code-cover');
270-
testFlags.push('-coverprofile=' + tmpCoverPath);
271-
}
272-
273-
return { tmpCoverPath, testFlags };
274-
}

src/testUtils.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { getCurrentPackage } from './goModules';
1111
import { GoDocumentSymbolProvider } from './goOutline';
1212
import { getNonVendorPackages } from './goPackages';
1313
import { envPath, getCurrentGoWorkspaceFromGOPATH, parseEnvFile } from './goPath';
14-
import { getBinPath, getCurrentGoPath, getGoVersion, getToolsEnvVars, LineBuffer, resolvePath } from './util';
14+
import { getBinPath, getCurrentGoPath, getGoVersion, getToolsEnvVars, LineBuffer, resolvePath, getTempFilePath } from './util';
15+
import { applyCodeCoverageToAllEditors } from './goCover';
1516

1617
const sendSignal = 'SIGKILL';
1718
const outputChannel = vscode.window.createOutputChannel('Go Tests');
@@ -64,6 +65,10 @@ export interface TestConfig {
6465
* Whether the tests are being run in a project that uses Go modules
6566
*/
6667
isMod?: boolean;
68+
/**
69+
* Whether code coverage should be generated and applied.
70+
*/
71+
applyCodeCoverage?: boolean;
6772
}
6873

6974
export function getTestEnvVars(config: vscode.WorkspaceConfiguration): any {
@@ -187,8 +192,9 @@ export function getBenchmarkFunctions(doc: vscode.TextDocument, token: vscode.Ca
187192
*
188193
* @param goConfig Configuration for the Go extension.
189194
*/
190-
export function goTest(testconfig: TestConfig): Thenable<boolean> {
191-
return new Promise<boolean>(async (resolve, reject) => {
195+
export async function goTest(testconfig: TestConfig): Promise<boolean> {
196+
const tmpCoverPath = getTempFilePath('go-code-cover');
197+
const testResult = await new Promise<boolean>(async (resolve, reject) => {
192198

193199
// We do not want to clear it if tests are already running, as that could
194200
// lose valuable output.
@@ -208,6 +214,9 @@ export function goTest(testconfig: TestConfig): Thenable<boolean> {
208214
args.push('-benchmem', '-run=^$');
209215
} else {
210216
args.push('-timeout', testconfig.goConfig['testTimeout']);
217+
if (testconfig.applyCodeCoverage) {
218+
args.push('-coverprofile=' + tmpCoverPath);
219+
}
211220
}
212221
if (testTags && testconfig.flags.indexOf('-tags') === -1) {
213222
args.push('-tags', testTags);
@@ -348,6 +357,10 @@ export function goTest(testconfig: TestConfig): Thenable<boolean> {
348357
resolve(false);
349358
});
350359
});
360+
if (testconfig.applyCodeCoverage) {
361+
await applyCodeCoverageToAllEditors(tmpCoverPath, testconfig.dir);
362+
}
363+
return testResult;
351364
}
352365

353366
/**

0 commit comments

Comments
 (0)