Skip to content

Commit 15bd5f2

Browse files
committed
Simplify locator
1 parent c7ec7ea commit 15bd5f2

File tree

2 files changed

+18
-40
lines changed

2 files changed

+18
-40
lines changed

src/client/pythonEnvironments/discovery/locators/services/windowsStoreLocator.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33

44
import * as fsapi from 'fs-extra';
55
import * as path from 'path';
6-
import { Event, EventEmitter } from 'vscode';
76
import { traceWarning } from '../../../../common/logger';
87
import { Architecture, getEnvironmentVariable } from '../../../../common/utils/platform';
98
import {
109
PythonEnvInfo, PythonEnvKind, PythonReleaseLevel, PythonVersion,
1110
} from '../../../base/info';
1211
import { parseVersion } from '../../../base/info/pythonVersion';
1312
import { ILocator, IPythonEnvsIterator } from '../../../base/locator';
14-
import { PythonEnvsChangedEvent } from '../../../base/watcher';
13+
import { PythonEnvsWatcher } from '../../../base/watcher';
1514
import { getFileInfo } from '../../../common/externalDependencies';
1615
import { isWindowsPythonExe } from '../../../common/windowsUtils';
17-
import { IEnvironmentInfoService } from '../../../info/environmentInfoService';
1816

1917
/**
2018
* Gets path to the Windows Apps directory.
@@ -116,13 +114,9 @@ export async function getWindowsStorePythonExes(): Promise<string[]> {
116114
.filter(isWindowsPythonExe);
117115
}
118116

119-
export class WindowsStoreLocator implements ILocator {
117+
export class WindowsStoreLocator extends PythonEnvsWatcher implements ILocator {
120118
private readonly kind:PythonEnvKind = PythonEnvKind.WindowsStore;
121119

122-
private readonly eventEmitter = new EventEmitter<PythonEnvsChangedEvent>();
123-
124-
public constructor(private readonly envService:IEnvironmentInfoService) { }
125-
126120
public iterEnvs(): IPythonEnvsIterator {
127121
const buildEnvInfo = (exe:string) => this.buildEnvInfo(exe);
128122
const iterator = async function* () {
@@ -135,31 +129,11 @@ export class WindowsStoreLocator implements ILocator {
135129
public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
136130
const executablePath = typeof env === 'string' ? env : env.executable.filename;
137131
if (await isWindowsStoreEnvironment(executablePath)) {
138-
const interpreterInfo = await this.envService.getEnvironmentInfo(executablePath);
139-
if (interpreterInfo) {
140-
const data = await getFileInfo(executablePath);
141-
interpreterInfo.executable = {
142-
...interpreterInfo.executable,
143-
...data,
144-
};
145-
return Promise.resolve({
146-
name: '',
147-
location: '',
148-
kind: this.kind,
149-
executable: interpreterInfo.executable,
150-
version: interpreterInfo.version,
151-
arch: interpreterInfo.arch,
152-
distro: { org: 'Microsoft' },
153-
});
154-
}
132+
return this.buildEnvInfo(executablePath);
155133
}
156134
return undefined;
157135
}
158136

159-
public get onChanged(): Event<PythonEnvsChangedEvent> {
160-
return this.eventEmitter.event;
161-
}
162-
163137
private async buildEnvInfo(exe:string): Promise<PythonEnvInfo> {
164138
let version:PythonVersion;
165139
try {

src/test/pythonEnvironments/discovery/locators/windowsStoreLocator.unit.test.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { InterpreterInformation } from '../../../../client/pythonEnvironments/ba
1414
import { parseVersion } from '../../../../client/pythonEnvironments/base/info/pythonVersion';
1515
import * as externalDep from '../../../../client/pythonEnvironments/common/externalDependencies';
1616
import { getWindowsStorePythonExes, WindowsStoreLocator } from '../../../../client/pythonEnvironments/discovery/locators/services/windowsStoreLocator';
17-
import { EnvironmentInfoService } from '../../../../client/pythonEnvironments/info/environmentInfoService';
1817
import { getEnvs } from '../../base/common';
1918
import { TEST_LAYOUT_ROOT } from '../../common/commonTestConstants';
2019

@@ -167,8 +166,7 @@ suite('Windows Store', () => {
167166
return undefined;
168167
});
169168

170-
const envService = new EnvironmentInfoService();
171-
const locator = new WindowsStoreLocator(envService);
169+
const locator = new WindowsStoreLocator();
172170
const iterator = locator.iterEnvs();
173171
const actualEnvs = (await getEnvs(iterator))
174172
.sort((a, b) => a.executable.filename.localeCompare(b.executable.filename));
@@ -187,11 +185,10 @@ suite('Windows Store', () => {
187185
location: '',
188186
kind: PythonEnvKind.WindowsStore,
189187
distro: { org: 'Microsoft' },
190-
...createExpectedInterpreterInfo(python38path, python383data.sysVersion, python383data.sysPrefix, '3.8.3'),
188+
...createExpectedInterpreterInfo(python38path),
191189
};
192190

193-
const envService = new EnvironmentInfoService();
194-
const locator = new WindowsStoreLocator(envService);
191+
const locator = new WindowsStoreLocator();
195192
const actual = await locator.resolveEnv(python38path);
196193

197194
assertEnvEqual(actual, expected);
@@ -205,7 +202,7 @@ suite('Windows Store', () => {
205202
location: '',
206203
kind: PythonEnvKind.WindowsStore,
207204
distro: { org: 'Microsoft' },
208-
...createExpectedInterpreterInfo(python38path, python383data.sysVersion, python383data.sysPrefix, '3.8.3'),
205+
...createExpectedInterpreterInfo(python38path),
209206
};
210207

211208
// Partially filled in env info object
@@ -229,8 +226,7 @@ suite('Windows Store', () => {
229226
},
230227
};
231228

232-
const envService = new EnvironmentInfoService();
233-
const locator = new WindowsStoreLocator(envService);
229+
const locator = new WindowsStoreLocator();
234230
const actual = await locator.resolveEnv(input);
235231

236232
assertEnvEqual(actual, expected);
@@ -239,8 +235,16 @@ suite('Windows Store', () => {
239235
// Use a non store root path
240236
const python38path = path.join(testLocalAppData, 'python3.8.exe');
241237

242-
const envService = new EnvironmentInfoService();
243-
const locator = new WindowsStoreLocator(envService);
238+
const locator = new WindowsStoreLocator();
239+
const actual = await locator.resolveEnv(python38path);
240+
241+
assert.deepStrictEqual(actual, undefined);
242+
});
243+
test('resolveEnv(string): forbidden path', async () => {
244+
// Use a non store root path
245+
const python38path = path.join(testLocalAppData, 'Program Files', 'WindowsApps', 'python3.8.exe');
246+
247+
const locator = new WindowsStoreLocator();
244248
const actual = await locator.resolveEnv(python38path);
245249

246250
assert.deepStrictEqual(actual, undefined);

0 commit comments

Comments
 (0)