Skip to content

Commit f35af85

Browse files
Clarify what ILocator.resolveEnv() does.
1 parent 9a76026 commit f35af85

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

src/client/pythonEnvironments/base/locator.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,29 @@ export interface ILocator<E extends BasicPythonEnvsChangedEvent = PythonEnvsChan
6262
/**
6363
* Iterate over the enviroments known tos this locator.
6464
*
65+
* Locators are not required to have provide all info about
66+
* an environment. However, each yielded item will at least
67+
* include all the `PythonEnvBaseInfo` data.
68+
*
6569
* @param query - if provided, the locator will limit results to match
6670
*/
6771
iterEnvs(query?: QueryForEvent<E>): PythonEnvsIterator;
6872

6973
/**
70-
* Fill in any missing info in the given data, if possible.
74+
* Find the given Python environment and fill in as much missing info as possible.
75+
*
76+
* If the locator can find the environment then the result is as
77+
* much info about that env as the locator has. At the least this
78+
* will include all the `PythonEnvBaseInfo` data. If a `PythonEnvInfo`
79+
* was provided then the result will be a copy with any updates or
80+
* extra info applied.
81+
*
82+
* If the locator could not find the environment then `undefined`
83+
* is returned.
7184
*
72-
* The result is a copy of whatever was passed in.
85+
* @param env - the Python executable path or partial env info to find and update
7386
*/
74-
resolveEnv(env: PythonEnvInfo): Promise<PythonEnvInfo | undefined>;
87+
resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined>;
7588
}
7689

7790
interface IEmitter<E extends BasicPythonEnvsChangedEvent> {
@@ -100,7 +113,7 @@ export abstract class LocatorBase<E extends BasicPythonEnvsChangedEvent = Python
100113

101114
public abstract iterEnvs(query?: QueryForEvent<E>): PythonEnvsIterator;
102115

103-
public async resolveEnv(_env: PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
116+
public async resolveEnv(_env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
104117
return undefined;
105118
}
106119
}

src/client/pythonEnvironments/base/locators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class Locators extends PythonEnvsWatchers implements ILocator {
2424
return chain(iterators);
2525
}
2626

27-
public async resolveEnv(env: PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
27+
public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
2828
for (const locator of this.locators) {
2929
const resolved = await locator.resolveEnv(env);
3030
if (resolved !== undefined) {
@@ -57,7 +57,7 @@ export class DisableableLocator extends DisableableEnvsWatcher implements ILocat
5757
return this.locator.iterEnvs(query);
5858
}
5959

60-
public async resolveEnv(env: PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
60+
public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
6161
if (!this.enabled) {
6262
return undefined;
6363
}

src/test/pythonEnvironments/base/common.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { createDeferred } from '../../../client/common/utils/async';
55
import { Architecture } from '../../../client/common/utils/platform';
6-
import { parseBasicVersionInfo } from '../../../client/common/utils/version';
6+
import { EMPTY_VERSION, parseBasicVersionInfo } from '../../../client/common/utils/version';
77
import {
88
PythonEnvInfo,
99
PythonEnvKind,
@@ -16,13 +16,41 @@ import { PythonEnvsChangedEvent } from '../../../client/pythonEnvironments/base/
1616
export function createEnv(
1717
name: string,
1818
versionStr: string,
19-
kind = PythonEnvKind.Unknown,
20-
executable = 'python',
19+
kind?: PythonEnvKind,
20+
executable?: string,
2121
idStr?: string
2222
): PythonEnvInfo {
23+
if (kind === undefined) {
24+
kind = PythonEnvKind.Unknown;
25+
}
26+
if (executable === undefined || executable === '') {
27+
executable = 'python';
28+
}
2329
const id = idStr ? idStr : `${kind}-${name}`;
30+
const version = parseVersion(versionStr);
31+
return {
32+
id,
33+
kind,
34+
version,
35+
name,
36+
location: '',
37+
arch: Architecture.x86,
38+
executable: {
39+
filename: executable,
40+
sysPrefix: '',
41+
mtime: -1,
42+
ctime: -1
43+
},
44+
distro: { org: '' }
45+
};
46+
}
47+
48+
function parseVersion(versionStr: string): PythonVersion {
2449
const parsed = parseBasicVersionInfo<PythonVersion>(versionStr);
2550
if (!parsed) {
51+
if (versionStr === '') {
52+
return EMPTY_VERSION as PythonVersion;
53+
}
2654
throw Error(`invalid version ${versionStr}`);
2755
}
2856
const { version, after } = parsed;
@@ -44,21 +72,7 @@ export function createEnv(
4472
serial: parseInt(serialStr, 10)
4573
};
4674
}
47-
return {
48-
id,
49-
kind,
50-
version,
51-
name,
52-
location: '',
53-
arch: Architecture.x86,
54-
executable: {
55-
filename: executable,
56-
sysPrefix: '',
57-
mtime: -1,
58-
ctime: -1
59-
},
60-
distro: { org: 'PSF' }
61-
};
75+
return version;
6276
}
6377

6478
export function createLocatedEnv(
@@ -125,13 +139,14 @@ export class SimpleLocator extends Locator {
125139
}
126140
return iterator();
127141
}
128-
public async resolveEnv(env: PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
142+
public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
143+
const envInfo: PythonEnvInfo = typeof env === 'string' ? createEnv('', '', undefined, env) : env;
129144
if (this.callbacks?.resolve === undefined) {
130-
return env;
145+
return envInfo;
131146
} else if (this.callbacks?.resolve === null) {
132147
return undefined;
133148
} else {
134-
return this.callbacks.resolve(env);
149+
return this.callbacks.resolve(envInfo);
135150
}
136151
}
137152
}

0 commit comments

Comments
 (0)