From 0677cf62968eaed4a9c37eec315a3e85a1e60935 Mon Sep 17 00:00:00 2001 From: Dan Mace Date: Wed, 14 Sep 2016 21:26:30 -0400 Subject: [PATCH] Add a command to execute the previous test Add a `go.test.previous` command that runs the last test which was executed. --- package.json | 6 ++++++ src/goMain.ts | 6 +++++- src/goTest.ts | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 064d67ebf..227a4463b 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "onCommand:go.test.cursor", "onCommand:go.test.package", "onCommand:go.test.file", + "onCommand:go.test.previous", "onCommand:go.test.coverage" ], "main": "./out/src/goMain", @@ -96,6 +97,11 @@ "title": "Go: Test (file)", "description": "Runs all unit tests in the current file." }, + { + "command": "go.test.previous", + "title": "Go: Test Previous", + "description": "Re-runs the last executed test." + }, { "command": "go.test.coverage", "title": "Go: Test coverage in current package", diff --git a/src/goMain.ts b/src/goMain.ts index 30e24c8c3..aae841af2 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -24,7 +24,7 @@ import { updateGoPathGoRootFromConfig, setupGoPathAndOfferToInstallTools } from import { GO_MODE } from './goMode'; import { showHideStatus } from './goStatus'; import { coverageCurrentPackage, getCodeCoverage, removeCodeCoverage } from './goCover'; -import { testAtCursor, testCurrentPackage, testCurrentFile } from './goTest'; +import { testAtCursor, testCurrentPackage, testCurrentFile, testPrevious } from './goTest'; import { addImport } from './goImport'; import { installAllTools } from './goInstallTools'; @@ -72,6 +72,10 @@ export function activate(ctx: vscode.ExtensionContext): void { testCurrentFile(goConfig['testTimeout']); })); + ctx.subscriptions.push(vscode.commands.registerCommand('go.test.previous', () => { + testPrevious(); + })); + ctx.subscriptions.push(vscode.commands.registerCommand('go.test.coverage', () => { coverageCurrentPackage(); })); diff --git a/src/goTest.ts b/src/goTest.ts index bb4242b5d..c4d9fe41f 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -31,6 +31,10 @@ interface TestConfig { functions?: string[]; } +// lastTestConfig holds a reference to the last executed TestConfig which allows +// the last test to be easily re-executed. +let lastTestConfig: TestConfig; + /** * Executes the unit test at the primary cursor using `go test`. Output * is sent to the 'Go' channel. @@ -111,6 +115,20 @@ export function testCurrentFile(timeout: string) { }); } +/** + * Runs the previously executed test. + */ +export function testPrevious() { + let editor = vscode.window.activeTextEditor; + if (!lastTestConfig) { + vscode.window.showInformationMessage('No test has been recently executed.'); + return; + } + goTest(lastTestConfig).then(null, err => { + console.error(err); + }); +} + /** * Runs go test and presents the output in the 'Go' channel. * @@ -118,6 +136,8 @@ export function testCurrentFile(timeout: string) { */ function goTest(config: TestConfig): Thenable { return new Promise((resolve, reject) => { + // Remember this config as the last executed test. + lastTestConfig = config; outputChannel.clear(); outputChannel.show(2); let buildFlags: string[] = vscode.workspace.getConfiguration('go')['buildFlags'];