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

Commit

Permalink
Command to open test files
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Nov 1, 2016
1 parent 5df5cd2 commit 8e7d352
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
68 changes: 66 additions & 2 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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<boolean> {
let editor = checkActiveEditor();
if (!editor) {
Expand Down Expand Up @@ -112,8 +153,31 @@ function generateTests(conf: Config): Thenable<boolean> {
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);
Expand Down
16 changes: 12 additions & 4 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit 8e7d352

Please sign in to comment.