Skip to content

Commit 83f62c7

Browse files
committed
Improve test events for benchmarks
1 parent f68d12a commit 83f62c7

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

src/test/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Context, doSafe, Tail, TestController } from './testing';
77
import { TestResolver } from './resolver';
88
import { GoTestItem } from './item';
99
import { TestRunner } from './runner';
10-
import { TestRunRequest } from './run';
10+
import { TestRunRequest } from './testRun';
1111
import { CodeLensProvider } from './codeLens';
1212
import { EventEmitter } from '../utils/eventEmitter';
1313
import { RunConfig } from './config';

src/test/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CancellationToken, FileCoverage, TestRun, TestRunProfileKind, Uri } fro
44
import type vscode from 'vscode';
55
import { Package, StaticTestCase, TestCase, TestFile } from './item';
66
import { Context, Workspace } from './testing';
7-
import { PackageTestRun, TestRunRequest } from './run';
7+
import { PackageTestRun, TestRunRequest } from './testRun';
88
import { flags2args, Spawner } from './utils';
99
import { ProfileType } from './profile';
1010
import { TestResolver } from './resolver';

src/test/run.ts renamed to src/test/testRun.ts

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -255,40 +255,51 @@ export class PackageTestRun {
255255
const item = test && (await this.#request.manager.resolveTestItem(test, true));
256256

257257
const elapsed = typeof msg.Elapsed === 'number' ? msg.Elapsed * 1000 : undefined;
258-
switch (msg.Action) {
259-
case 'output': {
260-
if (!msg.Output) {
261-
break;
262-
}
263-
264-
// Track output
265-
const { id } = item || this.testItem;
266-
if (!this.output.has(id)) {
267-
this.output.set(id, []);
268-
}
269-
this.output.get(id)!.push(msg.Output);
270-
271-
if (!item || /^(=== RUN|\s*--- (FAIL|PASS): )/.test(msg.Output)) {
272-
this.append(msg.Output, undefined, this.testItem);
273-
break;
274-
}
258+
if (msg.Action === 'output') {
259+
if (!msg.Output) {
260+
return;
261+
}
275262

276-
const { message, location } = parseOutputLocation(msg.Output, path.join(item.uri!.fsPath, '..'));
263+
// Track output
264+
const { id } = item || this.testItem;
265+
if (!this.output.has(id)) {
266+
this.output.set(id, []);
267+
}
268+
this.output.get(id)!.push(msg.Output);
269+
270+
let location: Location | undefined;
271+
let message = msg.Output;
272+
if (item && !/^(=== RUN|\s*--- (FAIL|PASS): )/.test(msg.Output)) {
273+
const parsed = parseOutputLocation(msg.Output, path.join(item.uri!.fsPath, '..'));
274+
message = parsed.message;
275+
location = parsed.location;
277276
if (location) {
278277
this.currentLocation.set(id, location);
278+
} else {
279+
location = this.currentLocation.get(id);
279280
}
280-
this.append(message, location || this.currentLocation.get(id), item);
281-
282-
// Detect benchmark completion, e.g.
283-
// "BenchmarkFooBar-4 123456 123.4 ns/op 123 B/op 12 allocs/op"
284-
const m = msg.Output.match(/^(?<name>Benchmark[#/\w+]+)(?:-(?<procs>\d+)\s+(?<result>.*))?(?:$|\n)/);
285-
if (m && msg.Test && m.groups?.name === msg.Test) {
286-
this.#run.passed(item);
287-
}
281+
}
282+
this.append(message, location, item || this.testItem);
288283

289-
break;
284+
// go test is not good about reporting the start and end of benchmarks
285+
// so we'll synthesize those events to make life easier.
286+
if (!msg.Test?.startsWith('Benchmark')) {
287+
return;
290288
}
289+
if (msg.Output === `=== RUN ${msg.Test}\n`) {
290+
// === RUN BenchmarkFooBar
291+
msg.Action = 'run';
292+
} else if (
293+
msg.Output?.match(/^(?<name>Benchmark[/\w]+)-(?<procs>\d+)\s+(?<result>.*)(?:$|\n)/)?.[1] === msg.Test
294+
) {
295+
// BenchmarkFooBar-4 123456 123.4 ns/op 123 B/op 12 allocs/op
296+
msg.Action = 'pass';
297+
} else {
298+
return;
299+
}
300+
}
291301

302+
switch (msg.Action) {
292303
case 'run':
293304
case 'start':
294305
if (!msg.Test) {

src/test/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ export function spawnProcess(context: Context, scope: Uri, flags: Flags, options
5353
errbuf.onLine(stderr);
5454
errbuf.onDone((x) => x && stderr(x));
5555

56+
// Always use -json, but don't combine it with -v because weird things
57+
// happen (https://github.com/golang/go/issues/70384)
5658
flags.json = true;
59+
delete flags.v;
60+
5761
const tp = cp.spawn(binPath, ['test', ...flags2args(flags)], {
5862
...rest,
5963
stdio: 'pipe',

0 commit comments

Comments
 (0)