-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathfastPath.unit.test.ts
More file actions
313 lines (260 loc) · 13 KB
/
fastPath.unit.test.ts
File metadata and controls
313 lines (260 loc) · 13 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as assert from 'assert';
import * as path from 'path';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import { PythonEnvironment } from '../../../api';
import { EventNames } from '../../../common/telemetry/constants';
import * as telemetrySender from '../../../common/telemetry/sender';
import { createDeferred } from '../../../common/utils/deferred';
import { FastPathOptions, tryFastPathGet } from '../../../managers/common/fastPath';
function createMockEnv(envPath: string): PythonEnvironment {
return {
envId: { id: 'test-env', managerId: 'test' },
name: 'Test Env',
displayName: 'Test Env',
version: '3.11.0',
displayPath: envPath,
environmentPath: Uri.file(envPath),
sysPrefix: envPath,
execInfo: { run: { executable: envPath } },
};
}
interface FastPathTestOptions {
opts: FastPathOptions;
setInitialized: sinon.SinonStub;
}
function createOpts(overrides?: Partial<FastPathOptions>): FastPathTestOptions {
const setInitialized = sinon.stub();
const persistedPath = path.resolve('persisted', 'path');
return {
opts: {
initialized: undefined,
setInitialized,
scope: Uri.file(path.resolve('test', 'workspace')),
label: 'test',
getProjectFsPath: (s) => s.fsPath,
getPersistedPath: sinon.stub().resolves(persistedPath),
resolve: sinon.stub().resolves(createMockEnv(persistedPath)),
startBackgroundInit: sinon.stub().resolves(),
...overrides,
},
setInitialized,
};
}
suite('tryFastPathGet', () => {
let sendTelemetryStub: sinon.SinonStub;
setup(() => {
sendTelemetryStub = sinon.stub(telemetrySender, 'sendTelemetryEvent');
});
teardown(() => {
sinon.restore();
});
test('returns resolved env when persisted path exists and init not started', async () => {
const { opts } = createOpts();
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should return a result');
assert.strictEqual(result!.env.envId.id, 'test-env');
assert.ok(sendTelemetryStub.notCalled, 'Should not emit global cache telemetry for workspace scope');
});
test('returns undefined when scope is undefined and no getGlobalPersistedPath', async () => {
const { opts } = createOpts({ scope: undefined });
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined);
assert.ok((opts.getPersistedPath as sinon.SinonStub).notCalled);
});
test('returns resolved env for global scope when getGlobalPersistedPath returns a path', async () => {
const globalPath = path.resolve('usr', 'bin', 'python3');
const resolve = sinon.stub().resolves(createMockEnv(globalPath));
const { opts } = createOpts({
scope: undefined,
getGlobalPersistedPath: sinon.stub().resolves(globalPath),
resolve,
});
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should return a result for global scope');
assert.strictEqual(result!.env.envId.id, 'test-env');
assert.ok(resolve.calledOnceWith(globalPath), 'Should resolve the global persisted path');
assert.ok((opts.getPersistedPath as sinon.SinonStub).notCalled, 'Should not call workspace getPersistedPath');
// Verify cache hit telemetry
assert.ok(sendTelemetryStub.calledOnce, 'Should send telemetry for global cache hit');
const [eventName, , props] = sendTelemetryStub.firstCall.args;
assert.strictEqual(eventName, EventNames.GLOBAL_ENV_CACHE);
assert.strictEqual(props.result, 'hit');
assert.strictEqual(props.managerLabel, 'test');
});
test('returns undefined for global scope when getGlobalPersistedPath returns undefined', async () => {
const { opts } = createOpts({
scope: undefined,
getGlobalPersistedPath: sinon.stub().resolves(undefined),
});
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined);
// Verify cache miss telemetry
assert.ok(sendTelemetryStub.calledOnce, 'Should send telemetry for global cache miss');
const [eventName, , props] = sendTelemetryStub.firstCall.args;
assert.strictEqual(eventName, EventNames.GLOBAL_ENV_CACHE);
assert.strictEqual(props.result, 'miss');
});
test('reports stale when global cached path resolves to undefined', async () => {
const globalPath = path.resolve('usr', 'bin', 'python3');
const { opts } = createOpts({
scope: undefined,
getGlobalPersistedPath: sinon.stub().resolves(globalPath),
resolve: sinon.stub().resolves(undefined),
});
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined, 'Should fall through when cached env resolves to undefined');
assert.ok(sendTelemetryStub.calledOnce, 'Should send telemetry for stale cache');
const [eventName, , props] = sendTelemetryStub.firstCall.args;
assert.strictEqual(eventName, EventNames.GLOBAL_ENV_CACHE);
assert.strictEqual(props.result, 'stale');
});
test('returns undefined for global scope when cached path resolve fails', async () => {
const globalPath = path.resolve('usr', 'bin', 'python3');
const { opts } = createOpts({
scope: undefined,
getGlobalPersistedPath: sinon.stub().resolves(globalPath),
resolve: sinon.stub().rejects(new Error('python was uninstalled')),
});
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined, 'Should fall through when cached global env is stale');
// Verify cache stale telemetry
assert.ok(sendTelemetryStub.calledOnce, 'Should send telemetry for stale global cache');
const [eventName, , props] = sendTelemetryStub.firstCall.args;
assert.strictEqual(eventName, EventNames.GLOBAL_ENV_CACHE);
assert.strictEqual(props.result, 'stale');
});
test('global scope fast path starts background init when initialized is undefined', async () => {
const globalPath = path.resolve('usr', 'bin', 'python3');
const startBackgroundInit = sinon.stub().resolves();
const { opts, setInitialized } = createOpts({
scope: undefined,
getGlobalPersistedPath: sinon.stub().resolves(globalPath),
startBackgroundInit,
});
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should return fast-path result');
assert.ok(startBackgroundInit.calledOnce, 'Should start background init for global scope');
assert.ok(setInitialized.calledOnce, 'Should set initialized for global scope');
});
test('returns undefined when init is already completed', async () => {
const deferred = createDeferred<void>();
deferred.resolve();
const { opts } = createOpts({ initialized: deferred });
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined);
assert.ok((opts.getPersistedPath as sinon.SinonStub).notCalled);
});
test('returns undefined when no persisted path', async () => {
const { opts } = createOpts({
getPersistedPath: sinon.stub().resolves(undefined),
});
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined);
});
test('returns undefined when resolve returns undefined', async () => {
const { opts } = createOpts({
resolve: sinon.stub().resolves(undefined),
});
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined);
});
test('returns undefined when resolve throws', async () => {
const { opts } = createOpts({
resolve: sinon.stub().rejects(new Error('resolve failed')),
});
const result = await tryFastPathGet(opts);
assert.strictEqual(result, undefined);
});
test('calls getProjectFsPath with the scope Uri', async () => {
const scope = Uri.file(path.resolve('my', 'project'));
const getProjectFsPath = sinon.stub().returns(scope.fsPath);
const { opts } = createOpts({ scope, getProjectFsPath });
await tryFastPathGet(opts);
assert.ok(getProjectFsPath.calledOnce);
assert.strictEqual(getProjectFsPath.firstCall.args[0], scope);
});
test('passes project fsPath to getPersistedPath', async () => {
const projectPath = path.resolve('project', 'path');
const getProjectFsPath = sinon.stub().returns(projectPath);
const getPersistedPath = sinon.stub().resolves(path.resolve('persisted'));
const { opts } = createOpts({
getProjectFsPath,
getPersistedPath,
resolve: sinon.stub().resolves(undefined),
});
await tryFastPathGet(opts);
assert.strictEqual(getPersistedPath.firstCall.args[0], projectPath);
});
test('does not call startBackgroundInit when initialized already exists (in-progress)', async () => {
const existing = createDeferred<void>(); // not resolved
const startBackgroundInit = sinon.stub().resolves();
const { opts, setInitialized } = createOpts({ initialized: existing, startBackgroundInit });
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should return env');
assert.ok(startBackgroundInit.notCalled, 'Should not start background init');
assert.ok(setInitialized.notCalled, 'Should not update initialized state');
});
test('kicks off background init and sets initialized when initialized is undefined', async () => {
const startBackgroundInit = sinon.stub().resolves();
const { opts, setInitialized } = createOpts({ startBackgroundInit });
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should return fast-path result');
assert.ok(startBackgroundInit.calledOnce, 'Should call startBackgroundInit');
assert.ok(setInitialized.calledOnce, 'Should set initialized immediately');
});
test('background init failure resets initialized for retry', async () => {
const startBackgroundInit = sinon.stub().rejects(new Error('init crashed'));
const { opts, setInitialized } = createOpts({ startBackgroundInit });
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should still return resolved env');
assert.ok(setInitialized.called, 'Should set initialized before async work');
// Allow background init promise rejection handler to run.
await new Promise((resolve) => setImmediate(resolve));
const lastCallArg = setInitialized.lastCall.args[0] as unknown;
assert.strictEqual(lastCallArg, undefined, 'Should clear initialized after background init failure');
});
test('sets initialized before awaiting persisted path', async () => {
let releasePersistedRead: (() => void) | undefined;
const getPersistedPath = sinon.stub().callsFake(
() =>
new Promise<string | undefined>((resolve) => {
releasePersistedRead = () => resolve(path.resolve('persisted', 'path'));
}),
);
const { opts, setInitialized } = createOpts({ getPersistedPath });
const pending = tryFastPathGet(opts);
assert.ok(setInitialized.calledOnce, 'Should set initialized before hitting first await');
releasePersistedRead!();
await pending;
});
test('works with Thenable return from startBackgroundInit', async () => {
// Simulate withProgress returning a Thenable (not a full Promise)
const thenable = { then: (resolve: () => void) => resolve() };
const { opts } = createOpts({
startBackgroundInit: sinon.stub().returns(thenable),
});
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should resolve successfully with Thenable init');
});
test('synchronous background init failure resets initialized for retry', async () => {
const startBackgroundInit = sinon.stub().throws(new Error('init crashed sync'));
const { opts, setInitialized } = createOpts({ startBackgroundInit });
const result = await tryFastPathGet(opts);
assert.ok(result, 'Should still return resolved env even when background init throws synchronously');
assert.ok(
setInitialized.called,
'Should set initialized before attempting background init even when it throws synchronously',
);
// Allow any background init error handling to run.
await new Promise((resolve) => setImmediate(resolve));
const lastCallArg = setInitialized.lastCall.args[0] as unknown;
assert.strictEqual(
lastCallArg,
undefined,
'Should clear initialized after synchronous background init failure',
);
});
});