This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements three new commands that use gotests: - go.test.generate.package: generates all unit-test squeletons for the current package. - go.test.generate.file: generates all unit-test squeletons for the current file. - go.test.generate.function: generates unit-test squeleton for the selected function in the current file.
- Loading branch information
1 parent
f84dcdd
commit 2eed87c
Showing
6 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import cp = require('child_process'); | ||
import path = require('path'); | ||
import vscode = require('vscode'); | ||
import util = require('util'); | ||
|
||
import { getBinPath } from './goPath'; | ||
import { promptForMissingTool } from './goInstallTools'; | ||
import { GoDocumentSymbolProvider } from './goOutline'; | ||
|
||
export function generateTestCurrentPackage() { | ||
let editor = testEditor(); | ||
let dir = path.dirname(editor.document.uri.fsPath); | ||
generateTests({dir: dir}); | ||
vscode.window.showInformationMessage('gotests: Unit tests generated for package:' + path.basename(dir)); | ||
} | ||
|
||
export function generateTestCurrentFile() { | ||
let editor = testEditor(); | ||
let file = editor.document.uri.fsPath; | ||
generateTests({dir: file}); | ||
vscode.window.showInformationMessage('gotests: Unit tests generated for file:' + path.basename(file)); | ||
} | ||
|
||
export function generateTestCurrentFunction() { | ||
let editor = testEditor(); | ||
let file = editor.document.uri.fsPath; | ||
|
||
getFunctions(editor.document).then(functions => { | ||
let currentFunction: vscode.SymbolInformation; | ||
for (let func of functions) { | ||
let selection = editor.selection; | ||
if (selection && func.location.range.contains(selection.start)) { | ||
currentFunction = func; | ||
break; | ||
} | ||
}; | ||
if (!currentFunction) { | ||
vscode.window.setStatusBarMessage('No function found at cursor.', 5000); | ||
return; | ||
} | ||
generateTests({dir: file, func: currentFunction.name}); | ||
vscode.window.showInformationMessage('gotests: Unit test generated for function: ' + currentFunction.name + ' in file:' + path.basename(file)); | ||
}).then(null, err => { | ||
console.error(err); | ||
}); | ||
} | ||
|
||
/** | ||
* Input to goTests. | ||
*/ | ||
interface Config { | ||
/** | ||
* The working directory for `gotests`. | ||
*/ | ||
dir: string; | ||
/** | ||
* Specific function names to generate tests squeleton. | ||
*/ | ||
func?: string; | ||
} | ||
|
||
function generateTests(conf: Config): Thenable<vscode.WorkspaceEdit> { | ||
return new Promise<vscode.WorkspaceEdit>((resolve, reject) => { | ||
let cmd = getBinPath('gotests'); | ||
let args; | ||
if (conf.func) { | ||
args = ['-w', '-only', conf.func, conf.dir]; | ||
} else { | ||
args = ['-w', '-all', conf.dir]; | ||
} | ||
cp.execFile(cmd, args, {}, (err, stdout, stderr) => { | ||
try { | ||
if (err && (<any>err).code === 'ENOENT') { | ||
promptForMissingTool('gotests'); | ||
return resolve(null); | ||
} | ||
if (err) { | ||
return reject('Cannot generate test due to errors: ' + stderr); | ||
} | ||
return resolve(new vscode.WorkspaceEdit()); | ||
} catch (e) { | ||
vscode.window.showInformationMessage(e.msg); | ||
reject(e); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function testEditor(): vscode.TextEditor { | ||
let editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
vscode.window.showInformationMessage('No editor is active.'); | ||
return; | ||
} | ||
return editor; | ||
} | ||
|
||
function getFunctions(doc: vscode.TextDocument): Thenable<vscode.SymbolInformation[]> { | ||
let documentSymbolProvider = new GoDocumentSymbolProvider(); | ||
return documentSymbolProvider | ||
.provideDocumentSymbols(doc, null) | ||
.then(symbols => | ||
symbols.filter(sym => | ||
sym.kind === vscode.SymbolKind.Function) | ||
); | ||
} |