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

Commit

Permalink
Add benchmark debuging codelens (#1566)
Browse files Browse the repository at this point in the history
* Add benchmark debuging codelens

* Collected common code for tests and bench debuging to a helper function
  • Loading branch information
harkal authored and ramya-rao-a committed Mar 15, 2018
1 parent fc3f145 commit 30e5d6b
Showing 1 changed file with 34 additions and 19 deletions.
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

0 comments on commit 30e5d6b

Please sign in to comment.