-
Notifications
You must be signed in to change notification settings - Fork 797
/
Copy pathgoCheck.ts
150 lines (133 loc) · 4.44 KB
/
goCheck.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
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable @typescript-eslint/no-explicit-any */
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import path = require('path');
import vscode = require('vscode');
import { getGoplsConfig } from './config';
import { goBuild } from './goBuild';
import { goLint } from './goLint';
import { isModSupported } from './goModules';
import { diagnosticsStatusBarItem, outputChannel } from './goStatus';
import { goVet } from './goVet';
import { getTestFlags, goTest, TestConfig } from './testUtils';
import { ICheckResult } from './util';
import { GoExtensionContext } from './context';
const STATUS_BAR_ITEM_NAME = 'Go Test';
const statusBarItem = vscode.window.createStatusBarItem(STATUS_BAR_ITEM_NAME, vscode.StatusBarAlignment.Left);
statusBarItem.name = STATUS_BAR_ITEM_NAME;
statusBarItem.command = 'go.test.showOutput';
const neverAgain = { title: "Don't Show Again" };
export function removeTestStatus(e: vscode.TextDocumentChangeEvent) {
if (e.document.isUntitled) {
return;
}
statusBarItem.hide();
statusBarItem.text = '';
}
export function notifyIfGeneratedFile(this: void, e: vscode.TextDocumentChangeEvent) {
const ctx: any = this;
if (e.document.isUntitled || e.document.languageId !== 'go') {
return;
}
if (
ctx.globalState.get('ignoreGeneratedCodeWarning') !== true &&
e.document.lineAt(0).text.match(/^\/\/ Code generated .* DO NOT EDIT\.$/)
) {
vscode.window.showWarningMessage('This file seems to be generated. DO NOT EDIT.', neverAgain).then((result) => {
if (result === neverAgain) {
ctx.globalState.update('ignoreGeneratedCodeWarning', true);
}
});
}
}
interface IToolCheckResults {
diagnosticCollection: vscode.DiagnosticCollection;
errors: ICheckResult[];
}
export function check(
goCtx: GoExtensionContext,
fileUri: vscode.Uri,
goConfig: vscode.WorkspaceConfiguration
): Promise<IToolCheckResults[]> {
diagnosticsStatusBarItem.hide();
outputChannel.appendLine('Running checks...');
const runningToolsPromises = [];
const cwd = path.dirname(fileUri.fsPath);
// If a user has enabled diagnostics via a language server,
// then we disable running build or vet to avoid duplicate errors and warnings.
const disableBuildAndVet = goConfig.get('useLanguageServer');
let testPromise: Thenable<boolean>;
const testConfig: TestConfig = {
goConfig,
dir: cwd,
flags: getTestFlags(goConfig),
background: true,
applyCodeCoverage: !!goConfig['coverOnSave']
};
const runTest = () => {
if (testPromise) {
return testPromise;
}
testPromise = isModSupported(fileUri).then((isMod) => {
testConfig.isMod = isMod;
return goTest(testConfig);
});
return testPromise;
};
const { buildDiagnosticCollection, lintDiagnosticCollection, vetDiagnosticCollection } = goCtx;
if (
buildDiagnosticCollection &&
!disableBuildAndVet &&
!!goConfig['buildOnSave'] &&
goConfig['buildOnSave'] !== 'off'
) {
runningToolsPromises.push(
isModSupported(fileUri)
.then((isMod) => goBuild(fileUri, isMod, goConfig, goConfig['buildOnSave'] === 'workspace'))
.then((errors) => ({ diagnosticCollection: buildDiagnosticCollection, errors }))
);
}
if (goConfig['testOnSave']) {
statusBarItem.show();
statusBarItem.text = 'Tests Running';
runTest().then((success) => {
if (statusBarItem.text === '') {
return;
}
if (success) {
statusBarItem.text = 'Tests Passed';
} else {
statusBarItem.text = 'Tests Failed';
}
});
}
if (lintDiagnosticCollection && !!goConfig['lintOnSave'] && goConfig['lintOnSave'] !== 'off') {
const goplsConfig = getGoplsConfig(fileUri);
runningToolsPromises.push(
goLint(fileUri, goConfig, goplsConfig, goConfig['lintOnSave']).then((errors) => ({
diagnosticCollection: lintDiagnosticCollection,
errors
}))
);
}
if (vetDiagnosticCollection && !disableBuildAndVet && !!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') {
runningToolsPromises.push(
goVet(fileUri, goConfig, goConfig['vetOnSave'] === 'workspace').then((errors) => ({
diagnosticCollection: vetDiagnosticCollection,
errors
}))
);
}
if (goConfig['coverOnSave']) {
runTest().then((success) => {
if (!success) {
return [];
}
});
}
return Promise.all(runningToolsPromises);
}