Skip to content

Commit 307c949

Browse files
committed
[release] src/testUtil.ts: switch to -json mode only if -v is set
CL/242540 made runTest uses -json mode to ease streaming test output processing. However, it turned out -json makes the test output more verbose than the standard test output. (golang/go#40588) Verbose output by default is not ideal especially when running tests on many packages. This change brings back the old test output processing logic when users do not use the `-v` flag. The `go test` command will stream test output only if the `-v` flag is specified, so the original issue CL/242540 remains addressed. Fixes #471 Updates #316 Updates golang/go#40588 Change-Id: I29d6d1319acccab2457300c118216ceebb4c4033 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/246918 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> (cherry picked from commit bec9cf3) Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/247017 Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent c469d69 commit 307c949

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

src/testUtils.ts

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
resolvePath
2222
} from './util';
2323
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH, parseEnvFile } from './utils/goPath';
24-
import {killProcessTree} from './utils/processUtils';
24+
import { killProcessTree } from './utils/processUtils';
2525

2626
const testOutputChannel = vscode.window.createOutputChannel('Go Tests');
2727
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
@@ -326,8 +326,11 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {
326326
outTargets.push(...targets);
327327
}
328328

329+
if (args.includes('-v') && !args.includes('-json')) {
330+
args.push('-json');
331+
}
332+
329333
args.push(...targets);
330-
args.push('-json');
331334

332335
// ensure that user provided flags are appended last (allow use of -args ...)
333336
// ignore user provided -run flag if we are already using it
@@ -345,29 +348,9 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {
345348
const errBuf = new LineBuffer();
346349

347350
const testResultLines: string[] = [];
348-
const processTestResultLine = (line: string) => {
349-
try {
350-
const m = <GoTestOutput>(JSON.parse(line));
351-
if (m.Action !== 'output' || !m.Output) {
352-
return;
353-
}
354-
const out = m.Output;
355-
const pkg = m.Package;
356-
if (pkg && (pkgMap.has(pkg) || currentGoWorkspace)) {
357-
const pkgNameArr = pkg.split('/');
358-
const baseDir = pkgMap.get(pkg) || path.join(currentGoWorkspace, ...pkgNameArr);
359-
// go test emits test results on stdout, which contain file names relative to the package under test
360-
outputChannel.appendLine(expandFilePathInOutput(out, baseDir).trimRight());
361-
} else {
362-
outputChannel.appendLine(out.trimRight());
363-
}
364-
} catch (e) {
365-
// TODO: disable this log if it becomes too spammy.
366-
console.log(`failed to parse JSON: ${e}: ${line}`);
367-
// Build failures or other messages come in non-JSON format. So, output as they are.
368-
outputChannel.appendLine(line);
369-
}
370-
};
351+
const processTestResultLine = args.includes('-json') ?
352+
processTestResultLineInJSONMode(pkgMap, currentGoWorkspace, outputChannel) :
353+
processTestResultLineInStandardMode(pkgMap, currentGoWorkspace, testResultLines, outputChannel);
371354

372355
outBuf.onLine((line) => processTestResultLine(line));
373356
outBuf.onDone((last) => {
@@ -421,6 +404,63 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {
421404
return testResult;
422405
}
423406

407+
function processTestResultLineInJSONMode(
408+
pkgMap: Map<string, string>,
409+
currentGoWorkspace: string,
410+
outputChannel: vscode.OutputChannel) {
411+
return (line: string) => {
412+
try {
413+
const m = <GoTestOutput>(JSON.parse(line));
414+
if (m.Action !== 'output' || !m.Output) {
415+
return;
416+
}
417+
const out = m.Output;
418+
const pkg = m.Package;
419+
if (pkg && (pkgMap.has(pkg) || currentGoWorkspace)) {
420+
const pkgNameArr = pkg.split('/');
421+
const baseDir = pkgMap.get(pkg) || path.join(currentGoWorkspace, ...pkgNameArr);
422+
// go test emits test results on stdout, which contain file names relative to the package under test
423+
outputChannel.appendLine(expandFilePathInOutput(out, baseDir).trimRight());
424+
} else {
425+
outputChannel.appendLine(out.trimRight());
426+
}
427+
} catch (e) {
428+
// TODO: disable this log if it becomes too spammy.
429+
console.log(`failed to parse JSON: ${e}: ${line}`);
430+
// Build failures or other messages come in non-JSON format. So, output as they are.
431+
outputChannel.appendLine(line);
432+
}
433+
};
434+
}
435+
436+
function processTestResultLineInStandardMode(
437+
pkgMap: Map<string, string>,
438+
currentGoWorkspace: string,
439+
testResultLines: string[],
440+
outputChannel: vscode.OutputChannel) {
441+
// 1=ok/FAIL, 2=package, 3=time/(cached)
442+
const packageResultLineRE = /^(ok|FAIL)\s+(\S+)\s+([0-9\.]+s|\(cached\))/;
443+
const lineWithErrorRE = /^(\t|\s\s\s\s)\S/;
444+
445+
return (line: string) => {
446+
testResultLines.push(line);
447+
const result = line.match(packageResultLineRE);
448+
if (result && (pkgMap.has(result[2]) || currentGoWorkspace)) {
449+
const hasTestFailed = line.startsWith('FAIL');
450+
const packageNameArr = result[2].split('/');
451+
const baseDir = pkgMap.get(result[2]) || path.join(currentGoWorkspace, ...packageNameArr);
452+
testResultLines.forEach((testResultLine) => {
453+
if (hasTestFailed && lineWithErrorRE.test(testResultLine)) {
454+
outputChannel.appendLine(expandFilePathInOutput(testResultLine, baseDir));
455+
} else {
456+
outputChannel.appendLine(testResultLine);
457+
}
458+
});
459+
testResultLines.splice(0);
460+
}
461+
};
462+
}
463+
424464
/**
425465
* Reveals the output channel in the UI.
426466
*/

0 commit comments

Comments
 (0)