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 647
/
goTest.ts
181 lines (159 loc) · 5.03 KB
/
goTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*---------------------------------------------------------
* 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 path = require('path');
import vscode = require('vscode');
import os = require('os');
import { goTest, TestConfig, getTestEnvVars, getTestFlags, getTestFunctions } from './testUtils';
import { getCoverage } from './goCover';
// 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.
*
* @param goConfig Configuration for the Go extension.
*/
export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, args: any) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (!editor.document.fileName.endsWith('_test.go')) {
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
return;
}
if (editor.document.isDirty) {
vscode.window.showInformationMessage('File has unsaved changes. Save and try again.');
return;
}
getTestFunctions(editor.document).then(testFunctions => {
let testFunctionName: string;
// We use functionName if it was provided as argument
// Otherwise find any test function containing the cursor.
if (args && args.functionName) {
testFunctionName = args.functionName;
} else {
for (let func of testFunctions) {
let selection = editor.selection;
if (selection && func.location.range.contains(selection.start)) {
testFunctionName = func.name;
break;
}
};
}
if (!testFunctionName) {
vscode.window.showInformationMessage('No test function found at cursor.');
return;
}
const testConfig = {
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: getTestFlags(goConfig, args),
functions: [testFunctionName]
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
return goTest(testConfig);
}).then(null, err => {
console.error(err);
});
}
/**
* Runs all tests in the package of the source of the active editor.
*
* @param goConfig Configuration for the Go extension.
*/
export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, args: any) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
let tmpCoverPath = '';
let testFlags = getTestFlags(goConfig, args) || [];
if (goConfig['coverOnTestPackage'] === true) {
tmpCoverPath = path.normalize(path.join(os.tmpdir(), 'go-code-cover'));
testFlags.push('-coverprofile=' + tmpCoverPath);
}
const testConfig = {
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: testFlags,
showTestCoverage: true
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
goTest(testConfig).then(success => {
if (success && tmpCoverPath) {
return getCoverage(tmpCoverPath);
}
}, err => {
console.log(err);
});
}
/**
* Runs all tests from all directories in the workspace.
*
* @param goConfig Configuration for the Go extension.
*/
export function testWorkspace(goConfig: vscode.WorkspaceConfiguration, args: any) {
const testConfig = {
goConfig: goConfig,
dir: vscode.workspace.rootPath,
flags: getTestFlags(goConfig, args),
includeSubDirectories: true
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
goTest(testConfig).then(null, err => {
console.error(err);
});
}
/**
* Runs all tests in the source of the active editor.
*
* @param goConfig Configuration for the Go extension.
*/
export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration, args: string[]): Thenable<boolean> {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (!editor.document.fileName.endsWith('_test.go')) {
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
return;
}
return getTestFunctions(editor.document).then(testFunctions => {
const testConfig = {
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: getTestFlags(goConfig, args),
functions: testFunctions.map(func => { return func.name; })
};
// Remember this config as the last executed test.
lastTestConfig = testConfig;
return goTest(testConfig);
}).then(null, err => {
console.error(err);
return Promise.resolve(false);
});
}
/**
* 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);
});
}