Skip to content

Commit 0bb04cb

Browse files
committed
event-driven + "pick a different env" pattern
1 parent 0047da9 commit 0bb04cb

File tree

1 file changed

+60
-40
lines changed

1 file changed

+60
-40
lines changed

src/test/integration/pythonProjects.integration.test.ts

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -274,30 +274,41 @@ suite('Integration: Python Projects', function () {
274274
const projects = api.getPythonProjects();
275275
const environments = await api.getEnvironments('all');
276276

277-
if (projects.length === 0 || environments.length === 0) {
277+
if (projects.length === 0 || environments.length < 2) {
278278
this.skip();
279279
return;
280280
}
281281

282282
const project = projects[0];
283-
const env = environments[0];
284-
285-
// Set environment first
286-
await api.setEnvironment(project.uri, env);
287-
288-
// Wait for it to be set
289-
// Use 15s timeout - CI runners can be slow with settings persistence
290-
let clearTestLastId: string | undefined;
291-
await waitForCondition(
292-
async () => {
293-
const retrieved = await api.getEnvironment(project.uri);
294-
clearTestLastId = retrieved?.envId?.id;
295-
return retrieved !== undefined && retrieved.envId.id === env.envId.id;
296-
},
297-
15_000,
298-
() =>
299-
`Environment was not set before clearing. Expected: ${env.envId.id} (manager: ${env.envId.managerId}), got: ${clearTestLastId ?? 'undefined'}`,
300-
);
283+
284+
// Pick an environment different from the current one to guarantee a change event
285+
const currentEnv = await api.getEnvironment(project.uri);
286+
let env = environments[0];
287+
if (currentEnv && currentEnv.envId.id === env.envId.id) {
288+
env = environments[1];
289+
}
290+
291+
// Set environment first, using event-driven wait
292+
await new Promise<void>((resolve, reject) => {
293+
const timeout = setTimeout(() => {
294+
subscription.dispose();
295+
reject(new Error(`onDidChangeEnvironment did not fire within 15s. Expected envId: ${env.envId.id}`));
296+
}, 15_000);
297+
298+
const subscription = api.onDidChangeEnvironment((e) => {
299+
if (e.uri?.toString() === project.uri.toString() && e.new?.envId.id === env.envId.id) {
300+
clearTimeout(timeout);
301+
subscription.dispose();
302+
resolve();
303+
}
304+
});
305+
306+
api.setEnvironment(project.uri, env).catch((err) => {
307+
clearTimeout(timeout);
308+
subscription.dispose();
309+
reject(err);
310+
});
311+
});
301312

302313
// Verify it was set
303314
const beforeClear = await api.getEnvironment(project.uri);
@@ -336,32 +347,41 @@ suite('Integration: Python Projects', function () {
336347
const projects = api.getPythonProjects();
337348
const environments = await api.getEnvironments('all');
338349

339-
if (projects.length === 0 || environments.length === 0) {
350+
if (projects.length === 0 || environments.length < 2) {
340351
this.skip();
341352
return;
342353
}
343354

344355
const project = projects[0];
345-
const env = environments[0];
346-
347-
// Set environment for project
348-
await api.setEnvironment(project.uri, env);
349-
350-
// Wait for it to be set
351-
// Use 15s timeout - CI runners can be slow with settings persistence
352-
let fileTestLastId: string | undefined;
353-
let fileTestLastManagerId: string | undefined;
354-
await waitForCondition(
355-
async () => {
356-
const retrieved = await api.getEnvironment(project.uri);
357-
fileTestLastId = retrieved?.envId?.id;
358-
fileTestLastManagerId = retrieved?.envId?.managerId;
359-
return retrieved !== undefined && retrieved.envId.id === env.envId.id;
360-
},
361-
15_000,
362-
() =>
363-
`Environment was not set for project. Expected: ${env.envId.id} (manager: ${env.envId.managerId}), got: ${fileTestLastId ?? 'undefined'} (manager: ${fileTestLastManagerId ?? 'undefined'})`,
364-
);
356+
357+
// Pick an environment different from the current one to guarantee a change event
358+
const currentEnv = await api.getEnvironment(project.uri);
359+
let env = environments[0];
360+
if (currentEnv && currentEnv.envId.id === env.envId.id) {
361+
env = environments[1];
362+
}
363+
364+
// Set environment for project, using event-driven wait
365+
await new Promise<void>((resolve, reject) => {
366+
const timeout = setTimeout(() => {
367+
subscription.dispose();
368+
reject(new Error(`onDidChangeEnvironment did not fire within 15s. Expected envId: ${env.envId.id}`));
369+
}, 15_000);
370+
371+
const subscription = api.onDidChangeEnvironment((e) => {
372+
if (e.uri?.toString() === project.uri.toString() && e.new?.envId.id === env.envId.id) {
373+
clearTimeout(timeout);
374+
subscription.dispose();
375+
resolve();
376+
}
377+
});
378+
379+
api.setEnvironment(project.uri, env).catch((err) => {
380+
clearTimeout(timeout);
381+
subscription.dispose();
382+
reject(err);
383+
});
384+
});
365385

366386
// Create a hypothetical file path inside the project
367387
const fileUri = vscode.Uri.joinPath(project.uri, 'some_script.py');

0 commit comments

Comments
 (0)