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
a0fa2d2
commit ff397c2
Showing
7 changed files
with
192 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/*--------------------------------------------------------- | ||
* 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(): Thenable<boolean> { | ||
let editor = testEditor(); | ||
if (!editor) { | ||
vscode.window.showInformationMessage('gotests: No editor selected'); | ||
return; | ||
} | ||
let dir = path.dirname(editor.document.uri.fsPath); | ||
let message = 'Unit tests generated for package:' + path.basename(dir); | ||
return generateTests({dir: dir, msg: message }); | ||
} | ||
|
||
export function generateTestCurrentFile(): Thenable<boolean> { | ||
let editor = testEditor(); | ||
if (!editor) { | ||
vscode.window.showInformationMessage('gotests: No editor selected'); | ||
return; | ||
} | ||
let file = editor.document.uri.fsPath; | ||
let message = 'Unit tests generated for file:' + path.basename(file); | ||
return generateTests({dir: file, msg: message }); | ||
} | ||
|
||
export function generateTestCurrentFunction(): Thenable<boolean> { | ||
let editor = testEditor(); | ||
if (!editor) { | ||
vscode.window.showInformationMessage('gotests: No selected Editor'); | ||
return; | ||
} | ||
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; | ||
} | ||
let message = 'Unit test generated for function: ' + currentFunction.name + ' in file:' + path.basename(file); | ||
return generateTests({dir: file, msg: message, func: currentFunction.name}); | ||
}).then(null, err => { | ||
console.error(err); | ||
}); | ||
} | ||
|
||
/** | ||
* Input to goTests. | ||
*/ | ||
interface Config { | ||
/** | ||
* The working directory for `gotests`. | ||
*/ | ||
dir: string; | ||
/** | ||
* The Message that show up in case of success | ||
*/ | ||
msg: string; | ||
/** | ||
* Specific function names to generate tests squeleton. | ||
*/ | ||
func?: string; | ||
} | ||
|
||
function generateTests(conf: Config): Thenable<boolean> { | ||
return new Promise<boolean>((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(false); | ||
} | ||
if (err) { | ||
return reject('Cannot generate test due to errors: ' + stderr); | ||
} | ||
let message = 'gotests: ' + conf.msg; | ||
vscode.window.showInformationMessage(message); | ||
return resolve(true); | ||
} 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) | ||
); | ||
} |
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