diff --git a/package.json b/package.json index 483e5ea96..14656ceee 100644 --- a/package.json +++ b/package.json @@ -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": {}, diff --git a/src/goGenerateTests.ts b/src/goGenerateTests.ts index 63b4fbc9b..15e294e00 100644 --- a/src/goGenerateTests.ts +++ b/src/goGenerateTests.ts @@ -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 '; @@ -69,8 +70,8 @@ export function generateTestCurrentPackage(): Thenable { 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 { @@ -78,8 +79,8 @@ export function generateTestCurrentFile(): Thenable { 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 { @@ -87,7 +88,7 @@ export function generateTestCurrentFunction(): Thenable { if (!editor) { return; } - let file = editor.document.uri.fsPath; + return getFunctions(editor.document).then(functions => { let currentFunction: vscode.SymbolInformation; for (let func of functions) { @@ -105,7 +106,8 @@ export function generateTestCurrentFunction(): Thenable { 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)); }); } @@ -123,16 +125,33 @@ interface Config { func?: string; } -function generateTests(conf: Config): Thenable { +function generateTests(conf: Config, goConfig: vscode.WorkspaceConfiguration): Thenable { return new Promise((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 && (err).code === 'ENOENT') { promptForMissingTool('gotests'); @@ -140,6 +159,7 @@ function generateTests(conf: Config): Thenable { } if (err) { console.log(err); + outputChannel.appendLine(err.message); return reject('Cannot generate test due to errors'); } @@ -158,6 +178,7 @@ function generateTests(conf: Config): Thenable { } vscode.window.showInformationMessage(message); + outputChannel.append(message); if (testsGenerated) { toggleTestFile(); } @@ -165,6 +186,7 @@ function generateTests(conf: Config): Thenable { return resolve(true); } catch (e) { vscode.window.showInformationMessage(e.msg); + outputChannel.append(e.msg); reject(e); } }); diff --git a/src/goMain.ts b/src/goMain.ts index f0aedf9d0..d0a9965a8 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -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" }, @@ -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'] + '', diff --git a/test/go.test.ts b/test/go.test.ts index 2fe2cf6b7..c84d4f8d7 100644 --- a/test/go.test.ts +++ b/test/go.test.ts @@ -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';