Skip to content

Commit 7f18fb7

Browse files
committed
fix: build & test
1 parent 91dbc3a commit 7f18fb7

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

src/managers/conda/condaEnvManager.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import { createDeferred, Deferred } from '../../common/utils/deferred';
3838
import { withProgress } from '../../common/window.apis';
3939
import { CondaStrings } from '../../common/localize';
4040
import { showErrorMessage } from '../../common/errors/utils';
41+
import { isWindows } from '../common/utils';
42+
import { normalizePath } from '../../common/utils/pathUtils';
4143

4244
export class CondaEnvManager implements EnvironmentManager, Disposable {
4345
private collection: PythonEnvironment[] = [];
@@ -74,6 +76,16 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
7476
this.disposablesMap.forEach((d) => d.dispose());
7577
}
7678

79+
private setFsPathToEnv(fsPath: string, environment: PythonEnvironment): void {
80+
this.fsPathToEnv.set(isWindows() ? normalizePath(fsPath) : fsPath, environment);
81+
}
82+
private getFsPathToEnv(fsPath: string): PythonEnvironment | undefined {
83+
return this.fsPathToEnv.get(isWindows() ? normalizePath(fsPath) : fsPath);
84+
}
85+
private deleteFsPathToEnv(fsPath: string): void {
86+
this.fsPathToEnv.delete(isWindows() ? normalizePath(fsPath) : fsPath);
87+
}
88+
7789
private _initialized: Deferred<void> | undefined;
7890
async initialize(): Promise<void> {
7991
if (this._initialized) {
@@ -174,7 +186,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
174186
this.collection = this.collection.filter((env) => env.envId.id !== result?.envId.id);
175187
Array.from(this.fsPathToEnv.entries())
176188
.filter(([, env]) => env.envId.id === result?.envId.id)
177-
.forEach(([uri]) => this.fsPathToEnv.delete(uri));
189+
.forEach(([uri]) => this.deleteFsPathToEnv(uri));
178190
this.disposablesMap.delete(result.envId.id);
179191
}
180192
}),
@@ -236,13 +248,13 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
236248
async get(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
237249
await this.initialize();
238250
if (scope instanceof Uri) {
239-
let env = this.fsPathToEnv.get(scope.fsPath);
251+
let env = this.getFsPathToEnv(scope.fsPath);
240252
if (env) {
241253
return env;
242254
}
243255
const project = this.api.getPythonProject(scope);
244256
if (project) {
245-
env = this.fsPathToEnv.get(project.uri.fsPath);
257+
env = this.getFsPathToEnv(project.uri.fsPath);
246258
if (env) {
247259
return env;
248260
}
@@ -260,9 +272,9 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
260272
const fsPath = folder?.uri?.fsPath ?? scope.fsPath;
261273
if (fsPath) {
262274
if (environment) {
263-
this.fsPathToEnv.set(fsPath, environment);
275+
this.setFsPathToEnv(fsPath, environment);
264276
} else {
265-
this.fsPathToEnv.delete(fsPath);
277+
this.deleteFsPathToEnv(fsPath);
266278
}
267279
await setCondaForWorkspace(fsPath, environment?.environmentPath.fsPath);
268280
}
@@ -278,11 +290,11 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
278290

279291
const before: Map<string, PythonEnvironment | undefined> = new Map();
280292
projects.forEach((p) => {
281-
before.set(p.uri.fsPath, this.fsPathToEnv.get(p.uri.fsPath));
293+
before.set(p.uri.fsPath, this.getFsPathToEnv(p.uri.fsPath));
282294
if (environment) {
283-
this.fsPathToEnv.set(p.uri.fsPath, environment);
295+
this.setFsPathToEnv(p.uri.fsPath, environment);
284296
} else {
285-
this.fsPathToEnv.delete(p.uri.fsPath);
297+
this.deleteFsPathToEnv(p.uri.fsPath);
286298
}
287299
});
288300

@@ -371,14 +383,14 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
371383
const found = this.findEnvironmentByPath(env);
372384

373385
if (found) {
374-
this.fsPathToEnv.set(p, found);
386+
this.setFsPathToEnv(p, found);
375387
} else {
376388
// If not found, resolve the conda path. Could be portable conda.
377389
const resolved = await resolveCondaPath(env, this.nativeFinder, this.api, this.log, this);
378390

379391
if (resolved) {
380392
// If resolved add it to the collection
381-
this.fsPathToEnv.set(p, resolved);
393+
this.setFsPathToEnv(p, resolved);
382394
this.collection.push(resolved);
383395
} else {
384396
this.log.error(`Failed to resolve conda environment: ${env}`);
@@ -388,7 +400,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
388400
// If there is not an environment already assigned by user to this project
389401
// then see if there is one in the collection
390402
if (pathSorted.length === 1) {
391-
this.fsPathToEnv.set(p, pathSorted[0]);
403+
this.setFsPathToEnv(p, pathSorted[0]);
392404
} else {
393405
// If there is more than one environment then we need to check if the project
394406
// is a subfolder of one of the environments
@@ -397,7 +409,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
397409
return t && path.normalize(t) === p;
398410
});
399411
if (found) {
400-
this.fsPathToEnv.set(p, found);
412+
this.setFsPathToEnv(p, found);
401413
}
402414
}
403415
}
@@ -406,15 +418,15 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
406418

407419
private fromEnvMap(uri: Uri): PythonEnvironment | undefined {
408420
// Find environment directly using the URI mapping
409-
const env = this.fsPathToEnv.get(uri.fsPath);
421+
const env = this.getFsPathToEnv(uri.fsPath);
410422
if (env) {
411423
return env;
412424
}
413425

414426
// Find environment using the Python project for the Uri
415427
const project = this.api.getPythonProject(uri);
416428
if (project) {
417-
return this.fsPathToEnv.get(project.uri.fsPath);
429+
return this.getFsPathToEnv(project.uri.fsPath);
418430
}
419431

420432
return undefined;

src/test/common/pathUtils.unit.test.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ import { getResourceUri, normalizePath } from '../../common/utils/pathUtils';
55
import * as utils from '../../managers/common/utils';
66

77
suite('Path Utilities', () => {
8-
let isWindowsStub: sinon.SinonStub;
9-
10-
setup(() => {
11-
isWindowsStub = sinon.stub(utils, 'isWindows');
12-
});
13-
14-
teardown(() => {
15-
sinon.restore();
16-
});
178
suite('getResourceUri', () => {
189
const testRoot = process.cwd();
1910

@@ -36,13 +27,16 @@ suite('Path Utilities', () => {
3627
assert.strictEqual(result?.path, testPath);
3728
});
3829

39-
test('creates file URI from Windows path', () => {
30+
test('creates file URI from Windows path', function () {
31+
if (!utils.isWindows()) {
32+
this.skip();
33+
}
4034
const testPath = 'C:\\path\\to\\file.txt';
4135
const result = getResourceUri(testPath, testRoot);
4236

4337
assert.ok(result instanceof Uri);
4438
assert.strictEqual(result?.scheme, 'file');
45-
assert.strictEqual(result?.path, 'C:/path/to/file.txt');
39+
assert.strictEqual(result?.path, '/C:/path/to/file.txt');
4640
});
4741

4842
test('parses existing URI correctly', () => {
@@ -99,6 +93,15 @@ suite('Path Utilities', () => {
9993
});
10094

10195
suite('normalizePath', () => {
96+
let isWindowsStub: sinon.SinonStub;
97+
98+
setup(() => {
99+
isWindowsStub = sinon.stub(utils, 'isWindows');
100+
});
101+
102+
teardown(() => {
103+
sinon.restore();
104+
});
102105
test('replaces backslashes with forward slashes', () => {
103106
const testPath = 'C:\\path\\to\\file.txt';
104107
const result = normalizePath(testPath);

0 commit comments

Comments
 (0)