Skip to content

Commit

Permalink
Update the initialization of the PythonEnvInfoCache to pass in a func…
Browse files Browse the repository at this point in the history
…tion that checks for env info completeness (microsoft#14446)

* Update the initialization of the PythonEnvInfoCache to pass in a function that checks for env info completeness

* Update interface
  • Loading branch information
Kartik Raj authored and luabud committed Oct 26, 2020
1 parent aeed5a0 commit 58591d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/client/pythonEnvironments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CachingLocator } from './base/locators/composite/cachingLocator';
import { PythonEnvsChangedEvent } from './base/watcher';
import { getGlobalPersistentStore } from './common/externalDependencies';
import { ExtensionLocators, WorkspaceLocators } from './discovery/locators';
import { EnvironmentInfoService } from './info/environmentInfoService';
import { registerForIOC } from './legacyIOC';

/**
Expand Down Expand Up @@ -53,9 +54,9 @@ export class PythonEnvironments implements ILocator {
export function createAPI(): [PythonEnvironments, () => void] {
const [locators, activateLocators] = initLocators();

// Update this to pass in an actual function that checks for env info completeness.
const envInfoService = new EnvironmentInfoService();
const envsCache = new PythonEnvInfoCache(
() => true, // "isComplete"
(env: PythonEnvInfo) => envInfoService.isInfoProvided(env.executable.filename), // "isComplete"
() => {
const storage = getGlobalPersistentStore<PythonEnvInfo[]>('PYTHON_ENV_INFO_CACHE');
return {
Expand Down
6 changes: 6 additions & 0 deletions src/client/pythonEnvironments/info/environmentInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IEnvironmentInfoService {
interpreterPath: string,
priority?: EnvironmentInfoServiceQueuePriority
): Promise<InterpreterInformation | undefined>;
isInfoProvided(interpreterPath: string): boolean;
}

async function buildEnvironmentInfo(interpreterPath: string): Promise<InterpreterInformation | undefined> {
Expand Down Expand Up @@ -70,4 +71,9 @@ export class EnvironmentInfoService implements IEnvironmentInfoService {
return r;
});
}

public isInfoProvided(interpreterPath: string): boolean {
const result = this.cache.get(interpreterPath);
return !!(result && result.completed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,38 @@ suite('Environment Info Service', () => {
});
assert.ok(stubShellExec.calledOnce);
});

test('isInfoProvided() returns true for items already processed', async () => {
const envService = new EnvironmentInfoService();
let result: boolean;
const promises: Promise<InterpreterInformation | undefined>[] = [];
const path1 = 'any-path1';
const path2 = 'any-path2';

promises.push(envService.getEnvironmentInfo(path1));
promises.push(envService.getEnvironmentInfo(path2));

await Promise.all(promises);
result = envService.isInfoProvided(path1);
assert.strictEqual(result, true);
result = envService.isInfoProvided(path2);
assert.strictEqual(result, true);
});

test('isInfoProvided() returns false otherwise', async () => {
const envService = new EnvironmentInfoService();
const promises: Promise<InterpreterInformation | undefined>[] = [];
const path1 = 'any-path1';
const path2 = 'any-path2';

promises.push(envService.getEnvironmentInfo(path1));
promises.push(envService.getEnvironmentInfo(path2));

let result = envService.isInfoProvided(path1);
assert.strictEqual(result, false);
result = envService.isInfoProvided(path2);
assert.strictEqual(result, false);
result = envService.isInfoProvided('some-random-path');
assert.strictEqual(result, false);
});
});

0 comments on commit 58591d5

Please sign in to comment.