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

Adding new 'gotests' flags option. #1841

Merged
merged 7 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,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 @@ -424,6 +424,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 @@ -465,6 +466,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