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

Add benchmark debuging codelens #1566

Merged
merged 2 commits into from
Mar 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions src/goRunTestCodelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
private getCodeLensForFunctions(vsConfig: vscode.WorkspaceConfiguration, document: TextDocument, token: CancellationToken): Thenable<CodeLens[]> {
const codelens: CodeLens[] = [];

// Helper function to generate the debug command
function getDebugCmd(title:string, debugConfig:any, args:any[]) : vscode.Command {
const program = path.dirname(document.fileName);
const env = Object.assign({}, debugConfig.env, vsConfig['testEnvVars']);
const envFile = vsConfig['testEnvFile'];
let buildFlags = getTestFlags(vsConfig, null);
if (vsConfig['buildTags'] && buildFlags.indexOf('-tags') === -1) {
buildFlags.push('-tags');
buildFlags.push(`${vsConfig['buildTags']}`);
}

let config = Object.assign({}, debugConfig, { args, program, env, envFile, buildFlags: buildFlags.map(x => `'${x}'`).join(' ') });
let debugTestCmd: Command = {
title: title,
command: 'go.debug.startSession',
arguments: [config]
};
return debugTestCmd
}

const testPromise = getTestFunctions(document, token).then(testFunctions => {
testFunctions.forEach(func => {
let runTestCmd: Command = {
Expand All @@ -82,25 +102,13 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
arguments: [{ functionName: func.name }]
};

const args = ['-test.run', '^' + func.name + '$'];
const program = path.dirname(document.fileName);
const env = Object.assign({}, this.debugConfig.env, vsConfig['testEnvVars']);
const envFile = vsConfig['testEnvFile'];
let buildFlags = getTestFlags(vsConfig, null);
if (vsConfig['buildTags'] && buildFlags.indexOf('-tags') === -1) {
buildFlags.push('-tags');
buildFlags.push(`${vsConfig['buildTags']}`);
}

let config = Object.assign({}, this.debugConfig, { args, program, env, envFile, buildFlags: buildFlags.map(x => `'${x}'`).join(' ') });
let debugTestCmd: Command = {
title: 'debug test',
command: 'go.debug.startSession',
arguments: [config]
};

codelens.push(new CodeLens(func.location.range, runTestCmd));
codelens.push(new CodeLens(func.location.range, debugTestCmd));

const debugArgs = [
'-test.run', '^' + func.name + '$'
];

codelens.push(new CodeLens(func.location.range, getDebugCmd("debug test", this.debugConfig, debugArgs)));
});
});

Expand All @@ -113,8 +121,15 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
};

codelens.push(new CodeLens(func.location.range, runBenchmarkCmd));
});

const debugArgs = [
'-test.bench', '^' + func.name + '$',
'-test.run', 'a^' // Don't match any test
];

codelens.push(new CodeLens(func.location.range, getDebugCmd('debug benchmark', this.debugConfig, debugArgs)));
});

});

return Promise.all([testPromise, benchmarkPromise]).then(() => codelens);
Expand Down