forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplicationDiagnostics.unit.test.ts
More file actions
242 lines (214 loc) · 10.9 KB
/
applicationDiagnostics.unit.test.ts
File metadata and controls
242 lines (214 loc) · 10.9 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import * as typemoq from 'typemoq';
import { DiagnosticSeverity } from 'vscode';
import { ApplicationDiagnostics } from '../../../client/application/diagnostics/applicationDiagnostics';
import { EnvironmentPathVariableDiagnosticsService } from '../../../client/application/diagnostics/checks/envPathVariable';
import { InvalidPythonInterpreterService } from '../../../client/application/diagnostics/checks/pythonInterpreter';
import { DiagnosticScope, IDiagnostic, IDiagnosticsService } from '../../../client/application/diagnostics/types';
import { IApplicationDiagnostics } from '../../../client/application/types';
import { IWorkspaceService } from '../../../client/common/application/types';
import { createDeferred, createDeferredFromPromise } from '../../../client/common/utils/async';
import { ServiceContainer } from '../../../client/ioc/container';
import { IServiceContainer } from '../../../client/ioc/types';
import { sleep } from '../../common';
suite('Application Diagnostics - ApplicationDiagnostics', () => {
let serviceContainer: typemoq.IMock<IServiceContainer>;
let envHealthCheck: typemoq.IMock<IDiagnosticsService>;
let lsNotSupportedCheck: typemoq.IMock<IDiagnosticsService>;
let pythonInterpreterCheck: typemoq.IMock<IDiagnosticsService>;
let workspaceService: typemoq.IMock<IWorkspaceService>;
let appDiagnostics: IApplicationDiagnostics;
const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST;
const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST;
setup(() => {
delete process.env.VSC_PYTHON_UNIT_TEST;
delete process.env.VSC_PYTHON_CI_TEST;
serviceContainer = typemoq.Mock.ofType<IServiceContainer>();
envHealthCheck = typemoq.Mock.ofType<IDiagnosticsService>();
envHealthCheck.setup((service) => service.runInBackground).returns(() => true);
lsNotSupportedCheck = typemoq.Mock.ofType<IDiagnosticsService>();
lsNotSupportedCheck.setup((service) => service.runInBackground).returns(() => false);
pythonInterpreterCheck = typemoq.Mock.ofType<IDiagnosticsService>();
pythonInterpreterCheck.setup((service) => service.runInBackground).returns(() => false);
pythonInterpreterCheck.setup((service) => service.runInUntrustedWorkspace).returns(() => false);
workspaceService = typemoq.Mock.ofType<IWorkspaceService>();
workspaceService.setup((w) => w.isTrusted).returns(() => true);
serviceContainer
.setup((d) => d.getAll(typemoq.It.isValue(IDiagnosticsService)))
.returns(() => [envHealthCheck.object, lsNotSupportedCheck.object, pythonInterpreterCheck.object]);
serviceContainer
.setup((d) => d.get(typemoq.It.isValue(IWorkspaceService)))
.returns(() => workspaceService.object);
appDiagnostics = new ApplicationDiagnostics(serviceContainer.object);
});
teardown(() => {
process.env.VSC_PYTHON_UNIT_TEST = oldValueOfVSC_PYTHON_UNIT_TEST;
process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST;
});
test('Performing Pre Startup Health Check must diagnose all validation checks', async () => {
envHealthCheck
.setup((e) => e.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.once());
lsNotSupportedCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.once());
pythonInterpreterCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.once());
await appDiagnostics.performPreStartupHealthCheck(undefined);
envHealthCheck.verifyAll();
lsNotSupportedCheck.verifyAll();
pythonInterpreterCheck.verifyAll();
});
test('When running in a untrusted workspace skip diagnosing validation checks which do not support it', async () => {
workspaceService.reset();
workspaceService.setup((w) => w.isTrusted).returns(() => false);
envHealthCheck
.setup((e) => e.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.once());
lsNotSupportedCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.once());
pythonInterpreterCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.never());
await appDiagnostics.performPreStartupHealthCheck(undefined);
envHealthCheck.verifyAll();
lsNotSupportedCheck.verifyAll();
pythonInterpreterCheck.verifyAll();
});
test('Performing Pre Startup Health Check must handles all validation checks only once either in background or foreground', async () => {
const diagnostic: IDiagnostic = {
code: 'Error' as any,
message: 'Error',
scope: undefined,
severity: undefined,
resource: undefined,
invokeHandler: 'default',
} as any;
envHealthCheck
.setup((e) => e.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([diagnostic]))
.verifiable(typemoq.Times.once());
envHealthCheck
.setup((p) => p.handle(typemoq.It.isValue([diagnostic])))
.returns(() => Promise.resolve())
.verifiable(typemoq.Times.once());
lsNotSupportedCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([diagnostic]))
.verifiable(typemoq.Times.once());
lsNotSupportedCheck
.setup((p) => p.handle(typemoq.It.isValue([diagnostic])))
.returns(() => Promise.resolve())
.verifiable(typemoq.Times.once());
pythonInterpreterCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([diagnostic]))
.verifiable(typemoq.Times.once());
pythonInterpreterCheck
.setup((p) => p.handle(typemoq.It.isValue([diagnostic])))
.returns(() => Promise.resolve())
.verifiable(typemoq.Times.once());
await appDiagnostics.performPreStartupHealthCheck(undefined);
await sleep(1);
pythonInterpreterCheck.verifyAll();
lsNotSupportedCheck.verifyAll();
envHealthCheck.verifyAll();
});
test('Diagnostics Returned by Pre Startup Health Checks must be logged', async () => {
const diagnostics: IDiagnostic[] = [];
for (let i = 0; i <= Math.random() * 10; i += 1) {
const diagnostic: IDiagnostic = {
code: `Error${i}` as any,
message: `Error${i}`,
scope: i % 2 === 0 ? DiagnosticScope.Global : DiagnosticScope.WorkspaceFolder,
severity: DiagnosticSeverity.Error,
resource: undefined,
invokeHandler: 'default',
};
diagnostics.push(diagnostic);
}
for (let i = 0; i <= Math.random() * 10; i += 1) {
const diagnostic: IDiagnostic = {
code: `Warning${i}` as any,
message: `Warning${i}`,
scope: i % 2 === 0 ? DiagnosticScope.Global : DiagnosticScope.WorkspaceFolder,
severity: DiagnosticSeverity.Warning,
resource: undefined,
invokeHandler: 'default',
};
diagnostics.push(diagnostic);
}
for (let i = 0; i <= Math.random() * 10; i += 1) {
const diagnostic: IDiagnostic = {
code: `Info${i}` as any,
message: `Info${i}`,
scope: i % 2 === 0 ? DiagnosticScope.Global : DiagnosticScope.WorkspaceFolder,
severity: DiagnosticSeverity.Information,
resource: undefined,
invokeHandler: 'default',
};
diagnostics.push(diagnostic);
}
envHealthCheck
.setup((e) => e.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve(diagnostics))
.verifiable(typemoq.Times.once());
lsNotSupportedCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.once());
pythonInterpreterCheck
.setup((p) => p.diagnose(typemoq.It.isAny()))
.returns(() => Promise.resolve([]))
.verifiable(typemoq.Times.once());
await appDiagnostics.performPreStartupHealthCheck(undefined);
await sleep(1);
envHealthCheck.verifyAll();
lsNotSupportedCheck.verifyAll();
pythonInterpreterCheck.verifyAll();
});
test('Ensure diagnostics run in foreground and background', async () => {
const foreGroundService = mock(InvalidPythonInterpreterService);
const backGroundService = mock(EnvironmentPathVariableDiagnosticsService);
const svcContainer = mock(ServiceContainer);
const workspaceService = mock<IWorkspaceService>();
const foreGroundDeferred = createDeferred<IDiagnostic[]>();
const backgroundGroundDeferred = createDeferred<IDiagnostic[]>();
when(svcContainer.get<IWorkspaceService>(IWorkspaceService)).thenReturn(workspaceService);
when(workspaceService.isTrusted).thenReturn(true);
when(svcContainer.getAll<IDiagnosticsService>(IDiagnosticsService)).thenReturn([
instance(foreGroundService),
instance(backGroundService),
]);
when(foreGroundService.runInBackground).thenReturn(false);
when(backGroundService.runInBackground).thenReturn(true);
when(foreGroundService.diagnose(anything())).thenReturn(foreGroundDeferred.promise);
when(backGroundService.diagnose(anything())).thenReturn(backgroundGroundDeferred.promise);
const service = new ApplicationDiagnostics(instance(svcContainer));
const promise = service.performPreStartupHealthCheck(undefined);
const deferred = createDeferredFromPromise(promise);
await sleep(1);
verify(foreGroundService.runInBackground).atLeast(1);
verify(backGroundService.runInBackground).atLeast(1);
assert.strictEqual(deferred.completed, false);
foreGroundDeferred.resolve([]);
await sleep(1);
assert.strictEqual(deferred.completed, true);
backgroundGroundDeferred.resolve([]);
await sleep(1);
verify(foreGroundService.diagnose(anything())).once();
verify(backGroundService.diagnose(anything())).once();
});
});