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

Commit

Permalink
Enable testFlags via settings and keyboard bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Jan 22, 2017
1 parent 2a93232 commit 6ff686f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@
"type": "string",
"default": "",
"description": "Location to install the Go tools that the extension depends on if you don't want them in your GOPATH."
},
"go.testFlags": {
"type": [
"array",
"null"
],
"items": {
"type": "string"
},
"default": null,
"description": "Flags to pass to `go test`. If null, then buildFlags will be used."
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ export function activate(ctx: vscode.ExtensionContext): void {
vscode.window.showInformationMessage('Current GOPATH:' + gopath);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.cursor', () => {
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.cursor', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testAtCursor(goConfig);
testAtCursor(goConfig, args);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.package', () => {
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.package', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testCurrentPackage(goConfig);
testCurrentPackage(goConfig, args);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.file', () => {
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.file', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testCurrentFile(goConfig);
testCurrentFile(goConfig, args);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.previous', () => {
Expand Down
23 changes: 17 additions & 6 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ interface TestConfig {
* Configuration for the Go extension
*/
goConfig: vscode.WorkspaceConfiguration;
/**
* Test flags to override the testFlags and buildFlags from goConfig.
*/
flags: string[];
/**
* Specific function names to test.
*/
Expand All @@ -45,7 +49,7 @@ let lastTestConfig: TestConfig;
* TODO: go test returns filenames with no path information for failures,
* so output doesn't produce navigable line references.
*/
export function testAtCursor(goConfig: vscode.WorkspaceConfiguration) {
export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, args: any) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
Expand All @@ -72,6 +76,7 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration) {
return goTest({
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: getTestFlags(goConfig, args),
functions: [testFunction.name]
});
}).then(null, err => {
Expand All @@ -84,15 +89,16 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration) {
*
* @param goConfig Configuration for the Go extension.
*/
export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration) {
export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, args: string[]) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
goTest({
goConfig: goConfig,
dir: path.dirname(editor.document.fileName)
dir: path.dirname(editor.document.fileName),
flags: getTestFlags(goConfig, args)
}).then(null, err => {
console.error(err);
});
Expand All @@ -103,7 +109,7 @@ export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration) {
*
* @param goConfig Configuration for the Go extension.
*/
export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration): Thenable<boolean> {
export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration, args: string[]): Thenable<boolean> {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
Expand All @@ -117,6 +123,7 @@ export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration): Thenab
return goTest({
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: getTestFlags(goConfig, args),
functions: testFunctions.map(func => { return func.name; })
});
}).then(null, err => {
Expand Down Expand Up @@ -151,9 +158,8 @@ function goTest(testconfig: TestConfig): Thenable<boolean> {
outputChannel.clear();
outputChannel.show(2, true);

let buildFlags: string[] = testconfig.goConfig['buildFlags'];
let buildTags: string = testconfig.goConfig['buildTags'];
let args = ['test', '-v', '-timeout', testconfig.goConfig['testTimeout'], '-tags', buildTags, ...buildFlags];
let args = ['test', ...testconfig.flags, '-timeout', testconfig.goConfig['testTimeout'], '-tags', buildTags];
let testEnvVars = Object.assign({}, process.env, testconfig.goConfig['testEnvVars']);
let goRuntimePath = getGoRuntimePath();

Expand Down Expand Up @@ -207,3 +213,8 @@ function getTestFunctions(doc: vscode.TextDocument): Thenable<vscode.SymbolInfor
function hasTestFunctionPrefix(name: string): boolean {
return name.startsWith('Test') || name.startsWith('Example');
}

function getTestFlags(goConfig: vscode.WorkspaceConfiguration, args: any): string[] {
let testFlags = goConfig['testFlags'] ? goConfig['testFlags'] : ['-v', ...goConfig['buildFlags']];
return (args && args.hasOwnProperty('flags') && Array.isArray(args['flags'])) ? args['flags'] : testFlags;
}

0 comments on commit 6ff686f

Please sign in to comment.