From 8e7d352aecccd1275f4e9970e215d1e8c5387d9b Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Tue, 1 Nov 2016 13:50:51 -0700 Subject: [PATCH] Command to open test files --- package.json | 10 +++++++ src/goGenerateTests.ts | 68 ++++++++++++++++++++++++++++++++++++++++-- src/goMain.ts | 16 +++++++--- 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 68cbc5eee..d5134e94f 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,16 @@ "command": "go.tools.install", "title": "Go: Install Tools", "description": "install/update the required go packages" + }, + { + "command": "go.open.test.file", + "title": "Go: Open Test File", + "description": "Opens test file (if any) corresponding to the Go file in the current active editor" + }, + { + "command": "go.open.test.implementation", + "title": "Go: Open Implementation For Test File", + "description": "Opens the Go file with implementation for the test file in the current active editor" } ], "debuggers": [ diff --git a/src/goGenerateTests.ts b/src/goGenerateTests.ts index e52ea2199..476f01dfd 100644 --- a/src/goGenerateTests.ts +++ b/src/goGenerateTests.ts @@ -14,6 +14,9 @@ import { getBinPath } from './goPath'; import { promptForMissingTool } from './goInstallTools'; import { GoDocumentSymbolProvider } from './goOutline'; +const generatedWord = 'Generated '; +const commandTitle = 'Open generated test for current file'; + /** * If current active editor has a Go file, returns the editor. */ @@ -30,6 +33,44 @@ function checkActiveEditor(): vscode.TextEditor { return editor; } +/** + * Opens test file (if any) corresponding to the Go file in the current active editor + */ +export function openTestFile(): void { + let editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage('Cannot open test file. No editor selected.'); + return; + } + let filePath = editor.document.fileName; + if (!filePath.endsWith('.go')) { + vscode.window.showInformationMessage('Cannot open test file. File in the editor is not a Go file.'); + return; + } + let testFilePath = filePath.substr(0, filePath.lastIndexOf('.go')) + '_test.go'; + + vscode.commands.executeCommand('vscode.open', vscode.Uri.file(testFilePath)); +} + +/** + * Opens the Go file with implementation for the test file in the current active editor + */ +export function openImplementationForTestFile(): void { + let editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage('Cannot open file. No editor selected.'); + return; + } + let filePath = editor.document.fileName; + if (!filePath.endsWith('_test.go')) { + vscode.window.showInformationMessage('Cannot open file. File in the editor is not a Go test file.'); + return; + } + let testFilePath = filePath.substr(0, filePath.lastIndexOf('_test.go')) + '.go'; + + vscode.commands.executeCommand('vscode.open', vscode.Uri.file(testFilePath)); +} + export function generateTestCurrentPackage(): Thenable { let editor = checkActiveEditor(); if (!editor) { @@ -112,8 +153,31 @@ function generateTests(conf: Config): Thenable { if (err) { return reject('Cannot generate test due to errors: ' + stderr); } - let message = 'gotests: ' + conf.msg; - vscode.window.showInformationMessage(message); + + let message = stdout; + let testsGenerated = false; + + // Expected stdout is of the format "Generated TestMain\nGenerated Testhello\n" + if (stdout.startsWith(generatedWord)) { + let lines = stdout.split('\n').filter(element => { + return element.startsWith(generatedWord); + }).map((element) => { + return element.substr(generatedWord.length); + }); + message = `Generated ${lines.join(', ')}`; + testsGenerated = true; + } + + if (testsGenerated) { + vscode.window.showInformationMessage(message, commandTitle).then(selected => { + if (selected === commandTitle) { + openTestFile(); + } + }); + } else { + vscode.window.showInformationMessage(message); + } + return resolve(true); } catch (e) { vscode.window.showInformationMessage(e.msg); diff --git a/src/goMain.ts b/src/goMain.ts index 192931ef6..69c9606d3 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -25,7 +25,7 @@ import { GO_MODE } from './goMode'; import { showHideStatus } from './goStatus'; import { coverageCurrentPackage, getCodeCoverage, removeCodeCoverage } from './goCover'; import { testAtCursor, testCurrentPackage, testCurrentFile, testPrevious } from './goTest'; -import { generateTestCurrentPackage, generateTestCurrentFile, generateTestCurrentFunction } from './goGenerateTests'; +import * as goGenerateTests from './goGenerateTests'; import { addImport } from './goImport'; import { installAllTools } from './goInstallTools'; @@ -95,15 +95,23 @@ export function activate(ctx: vscode.ExtensionContext): void { })); ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.package', () => { - generateTestCurrentPackage(); + goGenerateTests.generateTestCurrentPackage(); })); ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.file', () => { - generateTestCurrentFile(); + goGenerateTests.generateTestCurrentFile(); })); ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.function', () => { - generateTestCurrentFunction(); + goGenerateTests.generateTestCurrentFunction(); + })); + + ctx.subscriptions.push(vscode.commands.registerCommand('go.open.test.file', () => { + goGenerateTests.openTestFile(); + })); + + ctx.subscriptions.push(vscode.commands.registerCommand('go.open.test.implementation', () => { + goGenerateTests.openImplementationForTestFile(); })); vscode.languages.setLanguageConfiguration(GO_MODE.language, {