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

Commit

Permalink
Adding new 'gotests' flags option. (#1841)
Browse files Browse the repository at this point in the history
* Adding new 'gotests' flags option.

gotests has many options not used for vscode by default. This allows
similar flags to other tools like lint and build where they are just
free formed flags.

The one difference here is some arguments have values needed but we are
not validating on any specific flags and go with flexibility.

The main use case here is the new 'template_dir' flag added to allow
users to define custom templates.

* Change to Enum since that's the best fit here

* Simplify the logic even further

* Avoid error from goMain when no active editor

* Fix test fails

* Avoid duplicate flags

* lint fixes
  • Loading branch information
jeffbean authored and ramya-rao-a committed Aug 31, 2018
1 parent 85646a7 commit eb1279b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,15 @@
"description": "Flags to pass to `go test`. If null, then buildFlags will be used.",
"scope": "resource"
},
"go.generateTestsFlags": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "Additional command line flags to pass to `gotests` for generating tests.",
"scope": "resource"
},
"go.toolsEnvVars": {
"type": "object",
"default": {},
Expand Down
44 changes: 33 additions & 11 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import vscode = require('vscode');
import { getBinPath, getToolsEnvVars } from './util';
import { promptForMissingTool } from './goInstallTools';
import { GoDocumentSymbolProvider } from './goOutline';
import { outputChannel } from './goStatus';

const generatedWord = 'Generated ';

Expand Down Expand Up @@ -69,25 +70,25 @@ export function generateTestCurrentPackage(): Thenable<boolean> {
if (!editor) {
return;
}
let dir = path.dirname(editor.document.uri.fsPath);
return generateTests({ dir: dir });
return generateTests({ dir: path.dirname(editor.document.uri.fsPath) },
vscode.workspace.getConfiguration('go', editor.document.uri));
}

export function generateTestCurrentFile(): Thenable<boolean> {
let editor = checkActiveEditor();
if (!editor) {
return;
}
let file = editor.document.uri.fsPath;
return generateTests({ dir: file });
return generateTests({ dir: editor.document.uri.fsPath },
vscode.workspace.getConfiguration('go', editor.document.uri));
}

export function generateTestCurrentFunction(): Thenable<boolean> {
let editor = checkActiveEditor();
if (!editor) {
return;
}
let file = editor.document.uri.fsPath;

return getFunctions(editor.document).then(functions => {
let currentFunction: vscode.SymbolInformation;
for (let func of functions) {
Expand All @@ -105,7 +106,8 @@ export function generateTestCurrentFunction(): Thenable<boolean> {
if (funcName.includes('.')) {
funcName = funcName.split('.')[1];
}
return generateTests({ dir: file, func: funcName });
return generateTests({ dir: editor.document.uri.fsPath, func: funcName },
vscode.workspace.getConfiguration('go', editor.document.uri));
});
}

Expand All @@ -123,23 +125,41 @@ interface Config {
func?: string;
}

function generateTests(conf: Config): Thenable<boolean> {
function generateTests(conf: Config, goConfig: vscode.WorkspaceConfiguration): Thenable<boolean> {
return new Promise<boolean>((resolve, reject) => {
let cmd = getBinPath('gotests');
let args;
let args = ['-w'];
let goGenerateTestsFlags: string[] = goConfig['generateTestsFlags'] || [];

for (let i = 0; i < goGenerateTestsFlags.length; i++) {
const flag = goGenerateTestsFlags[i];
if (flag === '-w' || flag === 'all') {
continue;
}
if (flag === '-only') {
i++;
continue;
}
args.push(flag);
}

if (conf.func) {
args = ['-w', '-only', `^${conf.func}$`, conf.dir];
args = args.concat(['-only', `^${conf.func}$`, conf.dir]);
} else {
args = ['-w', '-all', conf.dir];
args = args.concat(['-all', conf.dir]);
}
cp.execFile(cmd, args, {env: getToolsEnvVars()}, (err, stdout, stderr) => {

cp.execFile(cmd, args, { env: getToolsEnvVars() }, (err, stdout, stderr) => {
outputChannel.appendLine('Generating Tests: ' + cmd + ' ' + args.join(' '));

try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gotests');
return resolve(false);
}
if (err) {
console.log(err);
outputChannel.appendLine(err.message);
return reject('Cannot generate test due to errors');
}

Expand All @@ -158,13 +178,15 @@ function generateTests(conf: Config): Thenable<boolean> {
}

vscode.window.showInformationMessage(message);
outputChannel.append(message);
if (testsGenerated) {
toggleTestFile();
}

return resolve(true);
} catch (e) {
vscode.window.showInformationMessage(e.msg);
outputChannel.append(e.msg);
reject(e);
}
});
Expand Down
2 changes: 2 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ function sendTelemetryEventForConfig(goConfig: vscode.WorkspaceConfiguration) {
"buildTags": { "classification": "CustomerContent", "purpose": "FeatureInsight" },
"formatTool": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"formatFlags": { "classification": "CustomerContent", "purpose": "FeatureInsight" },
"generateTestsFlags": { "classification": "CustomerContent", "purpose": "FeatureInsight" },
"lintOnSave": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"lintFlags": { "classification": "CustomerContent", "purpose": "FeatureInsight" },
"lintTool": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
Expand Down Expand Up @@ -479,6 +480,7 @@ function sendTelemetryEventForConfig(goConfig: vscode.WorkspaceConfiguration) {
lintOnSave: goConfig['lintOnSave'] + '',
lintFlags: goConfig['lintFlags'],
lintTool: goConfig['lintTool'],
generateTestsFlags: goConfig['generateTestsFlags'],
vetOnSave: goConfig['vetOnSave'] + '',
vetFlags: goConfig['vetFlags'],
testOnSave: goConfig['testOnSave'] + '',
Expand Down
2 changes: 1 addition & 1 deletion test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { testCurrentFile } from '../src/goTest';
import { getBinPath, getGoVersion, isVendorSupported } from '../src/util';
import { documentSymbols } from '../src/goOutline';
import { listPackages, getTextEditForAddImport } from '../src/goImport';
import { generateTestCurrentFile, generateTestCurrentPackage, generateTestCurrentFunction } from '../src/goGenerateTests';
import { generateTestCurrentFile, generateTestCurrentFunction, generateTestCurrentPackage } from '../src/goGenerateTests';
import { getAllPackages } from '../src/goPackages';
import { getImportPath } from '../src/util';
import { goPlay } from '../src/goPlayground';
Expand Down

0 comments on commit eb1279b

Please sign in to comment.