Skip to content

Commit

Permalink
Improve test events for benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Nov 16, 2024
1 parent f68d12a commit 83f62c7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/test/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Context, doSafe, Tail, TestController } from './testing';
import { TestResolver } from './resolver';
import { GoTestItem } from './item';
import { TestRunner } from './runner';
import { TestRunRequest } from './run';
import { TestRunRequest } from './testRun';
import { CodeLensProvider } from './codeLens';
import { EventEmitter } from '../utils/eventEmitter';
import { RunConfig } from './config';
Expand Down
2 changes: 1 addition & 1 deletion src/test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CancellationToken, FileCoverage, TestRun, TestRunProfileKind, Uri } fro
import type vscode from 'vscode';
import { Package, StaticTestCase, TestCase, TestFile } from './item';
import { Context, Workspace } from './testing';
import { PackageTestRun, TestRunRequest } from './run';
import { PackageTestRun, TestRunRequest } from './testRun';
import { flags2args, Spawner } from './utils';
import { ProfileType } from './profile';
import { TestResolver } from './resolver';
Expand Down
65 changes: 38 additions & 27 deletions src/test/run.ts → src/test/testRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,40 +255,51 @@ export class PackageTestRun {
const item = test && (await this.#request.manager.resolveTestItem(test, true));

const elapsed = typeof msg.Elapsed === 'number' ? msg.Elapsed * 1000 : undefined;
switch (msg.Action) {
case 'output': {
if (!msg.Output) {
break;
}

// Track output
const { id } = item || this.testItem;
if (!this.output.has(id)) {
this.output.set(id, []);
}
this.output.get(id)!.push(msg.Output);

if (!item || /^(=== RUN|\s*--- (FAIL|PASS): )/.test(msg.Output)) {
this.append(msg.Output, undefined, this.testItem);
break;
}
if (msg.Action === 'output') {
if (!msg.Output) {
return;
}

const { message, location } = parseOutputLocation(msg.Output, path.join(item.uri!.fsPath, '..'));
// Track output
const { id } = item || this.testItem;
if (!this.output.has(id)) {
this.output.set(id, []);
}
this.output.get(id)!.push(msg.Output);

let location: Location | undefined;
let message = msg.Output;
if (item && !/^(=== RUN|\s*--- (FAIL|PASS): )/.test(msg.Output)) {
const parsed = parseOutputLocation(msg.Output, path.join(item.uri!.fsPath, '..'));
message = parsed.message;
location = parsed.location;
if (location) {
this.currentLocation.set(id, location);
} else {
location = this.currentLocation.get(id);
}
this.append(message, location || this.currentLocation.get(id), item);

// Detect benchmark completion, e.g.
// "BenchmarkFooBar-4 123456 123.4 ns/op 123 B/op 12 allocs/op"
const m = msg.Output.match(/^(?<name>Benchmark[#/\w+]+)(?:-(?<procs>\d+)\s+(?<result>.*))?(?:$|\n)/);
if (m && msg.Test && m.groups?.name === msg.Test) {
this.#run.passed(item);
}
}
this.append(message, location, item || this.testItem);

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

switch (msg.Action) {
case 'run':
case 'start':
if (!msg.Test) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export function spawnProcess(context: Context, scope: Uri, flags: Flags, options
errbuf.onLine(stderr);
errbuf.onDone((x) => x && stderr(x));

// Always use -json, but don't combine it with -v because weird things
// happen (https://github.com/golang/go/issues/70384)
flags.json = true;
delete flags.v;

const tp = cp.spawn(binPath, ['test', ...flags2args(flags)], {
...rest,
stdio: 'pipe',
Expand Down

0 comments on commit 83f62c7

Please sign in to comment.