Skip to content

Commit eb9569a

Browse files
committed
Refactor
1 parent 94f04b3 commit eb9569a

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

src/client/pythonEnvironments/creation/provider/venvDeleteUtils.ts renamed to src/client/pythonEnvironments/creation/provider/envDeleteUtils.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as fs from 'fs-extra';
55
import * as path from 'path';
66
import { WorkspaceFolder } from 'vscode';
77
import { traceError, traceInfo } from '../../../logging';
8-
import { getVenvPath, showErrorMessageWithLogs } from '../common/commonUtils';
8+
import { showErrorMessageWithLogs } from '../common/commonUtils';
99
import { CreateEnv } from '../../../common/utils/localize';
1010
import { sleep } from '../../../common/utils/async';
1111
import { switchSelectedPython } from './venvSwitchPython';
@@ -40,59 +40,59 @@ async function tryDeleteDir(dir: string): Promise<boolean> {
4040
}
4141
}
4242

43-
export async function deleteEnvironmentNonWindows(workspaceFolder: WorkspaceFolder): Promise<boolean> {
44-
const venvPath = getVenvPath(workspaceFolder);
45-
if (await tryDeleteDir(venvPath)) {
46-
traceInfo(`Deleted venv dir: ${venvPath}`);
43+
export async function deleteEnvironmentNonWindows(envDir: string): Promise<boolean> {
44+
const name = path.basename(envDir);
45+
if (await tryDeleteDir(envDir)) {
46+
traceInfo(`Deleted "${name}" dir: ${envDir}`);
4747
return true;
4848
}
4949
showErrorMessageWithLogs(CreateEnv.Venv.errorDeletingEnvironment);
5050
return false;
5151
}
5252

5353
export async function deleteEnvironmentWindows(
54+
envDir: string,
55+
envPythonBin: string,
5456
workspaceFolder: WorkspaceFolder,
5557
interpreter: string | undefined,
5658
): Promise<boolean> {
57-
const venvPath = getVenvPath(workspaceFolder);
58-
const venvPythonPath = path.join(venvPath, 'Scripts', 'python.exe');
59-
60-
if (await tryDeleteFile(venvPythonPath)) {
61-
traceInfo(`Deleted python executable: ${venvPythonPath}`);
62-
if (await tryDeleteDir(venvPath)) {
63-
traceInfo(`Deleted ".venv" dir: ${venvPath}`);
59+
const name = path.basename(envDir);
60+
if (await tryDeleteFile(envPythonBin)) {
61+
traceInfo(`Deleted python executable: ${envPythonBin}`);
62+
if (await tryDeleteDir(envDir)) {
63+
traceInfo(`Deleted "${name}" dir: ${envDir}`);
6464
return true;
6565
}
6666

67-
traceError(`Failed to delete ".venv" dir: ${venvPath}`);
67+
traceError(`Failed to delete "${name}" dir: ${envDir}`);
6868
traceError(
69-
'This happens if the virtual environment is still in use, or some binary in the venv is still running.',
69+
'This happens if the virtual environment is still in use, or some binary in the "${name}" is still running.',
7070
);
71-
traceError(`Please delete the ".venv" manually: [${venvPath}]`);
71+
traceError(`Please delete the "${name}" manually: [${envDir}]`);
7272
showErrorMessageWithLogs(CreateEnv.Venv.errorDeletingEnvironment);
7373
return false;
7474
}
75-
traceError(`Failed to delete python executable: ${venvPythonPath}`);
75+
traceError(`Failed to delete python executable: ${envPythonBin}`);
7676
traceError('This happens if the virtual environment is still in use.');
7777

7878
if (interpreter) {
79-
traceError('We will attempt to switch python temporarily to delete the ".venv"');
79+
traceError('We will attempt to switch python temporarily to delete the "${name}"');
8080

81-
await switchSelectedPython(interpreter, workspaceFolder.uri, 'temporarily to delete the ".venv"');
81+
await switchSelectedPython(interpreter, workspaceFolder.uri, 'temporarily to delete the "${name}"');
8282

83-
traceInfo(`Attempting to delete ".venv" again: ${venvPath}`);
83+
traceInfo(`Attempting to delete "${name}" again: ${envDir}`);
8484
const ms = 500;
8585
for (let i = 0; i < 5; i = i + 1) {
8686
traceInfo(`Waiting for ${ms}ms to let processes exit, before a delete attempt.`);
8787
await sleep(ms);
88-
if (await tryDeleteDir(venvPath)) {
89-
traceInfo(`Deleted ".venv" dir: ${venvPath}`);
88+
if (await tryDeleteDir(envDir)) {
89+
traceInfo(`Deleted "${name}" dir: ${envDir}`);
9090
return true;
9191
}
92-
traceError(`Failed to delete ".venv" dir [${venvPath}] (attempt ${i + 1}/5).`);
92+
traceError(`Failed to delete "${name}" dir [${envDir}] (attempt ${i + 1}/5).`);
9393
}
9494
} else {
95-
traceError(`Please delete the ".venv" dir manually: [${venvPath}] manually.`);
95+
traceError(`Please delete the "${name}" dir manually: [${envDir}] manually.`);
9696
}
9797
showErrorMessageWithLogs(CreateEnv.Venv.errorDeletingEnvironment);
9898
return false;

src/client/pythonEnvironments/creation/provider/venvUtils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { traceError, traceVerbose } from '../../../logging';
1818
import { Commands } from '../../../common/constants';
1919
import { isWindows } from '../../../common/platform/platformService';
2020
import { getVenvPath, hasVenv } from '../common/commonUtils';
21-
import { deleteEnvironmentNonWindows, deleteEnvironmentWindows } from './venvDeleteUtils';
21+
import { deleteEnvironmentNonWindows, deleteEnvironmentWindows } from './envDeleteUtils';
2222
import { sendTelemetryEvent } from '../../../telemetry';
2323
import { EventName } from '../../../telemetry/constants';
2424

@@ -248,9 +248,10 @@ async function deleteEnvironment(workspaceFolder: WorkspaceFolder, interpreter:
248248
},
249249
async () => {
250250
if (isWindows()) {
251-
return deleteEnvironmentWindows(workspaceFolder, interpreter);
251+
const venvPythonPath = path.join(venvPath, 'Scripts', 'python.exe');
252+
return deleteEnvironmentWindows(venvPath, venvPythonPath, workspaceFolder, interpreter);
252253
}
253-
return deleteEnvironmentNonWindows(workspaceFolder);
254+
return deleteEnvironmentNonWindows(venvPath);
254255
},
255256
);
256257
}

src/test/pythonEnvironments/creation/provider/venvDeleteUtils.unit.test.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as commonUtils from '../../../../client/pythonEnvironments/creation/com
1010
import {
1111
deleteEnvironmentNonWindows,
1212
deleteEnvironmentWindows,
13-
} from '../../../../client/pythonEnvironments/creation/provider/venvDeleteUtils';
13+
} from '../../../../client/pythonEnvironments/creation/provider/envDeleteUtils';
1414
import * as switchPython from '../../../../client/pythonEnvironments/creation/provider/venvSwitchPython';
1515
import * as asyncApi from '../../../../client/common/utils/async';
1616

@@ -27,6 +27,8 @@ suite('Test Delete environments (windows)', () => {
2727
name: 'workspace1',
2828
index: 0,
2929
};
30+
const venvPath = path.join(workspace1.uri.fsPath, 'venv');
31+
const venvPythonPath = path.join(venvPath, 'scripts', 'python.exe');
3032

3133
setup(() => {
3234
pathExistsStub = sinon.stub(fs, 'pathExists');
@@ -52,7 +54,7 @@ suite('Test Delete environments (windows)', () => {
5254
test('Delete venv folder succeeded', async () => {
5355
rmdirStub.resolves();
5456
unlinkStub.resolves();
55-
assert.ok(await deleteEnvironmentWindows(workspace1, 'python.exe'));
57+
assert.ok(await deleteEnvironmentWindows(venvPath, venvPythonPath, workspace1, 'python.exe'));
5658

5759
assert.ok(rmdirStub.calledOnce);
5860
assert.ok(unlinkStub.calledOnce);
@@ -62,7 +64,7 @@ suite('Test Delete environments (windows)', () => {
6264
test('Delete python.exe succeeded but venv dir failed', async () => {
6365
rmdirStub.rejects();
6466
unlinkStub.resolves();
65-
assert.notOk(await deleteEnvironmentWindows(workspace1, 'python.exe'));
67+
assert.notOk(await deleteEnvironmentWindows(venvPath, venvPythonPath, workspace1, 'python.exe'));
6668

6769
assert.ok(rmdirStub.calledOnce);
6870
assert.ok(unlinkStub.calledOnce);
@@ -72,7 +74,7 @@ suite('Test Delete environments (windows)', () => {
7274
test('Delete python.exe failed first attempt', async () => {
7375
unlinkStub.rejects();
7476
rmdirStub.resolves();
75-
assert.ok(await deleteEnvironmentWindows(workspace1, 'python.exe'));
77+
assert.ok(await deleteEnvironmentWindows(venvPath, venvPythonPath, workspace1, 'python.exe'));
7678

7779
assert.ok(rmdirStub.calledOnce);
7880
assert.ok(switchPythonStub.calledOnce);
@@ -82,15 +84,15 @@ suite('Test Delete environments (windows)', () => {
8284
test('Delete python.exe failed all attempts', async () => {
8385
unlinkStub.rejects();
8486
rmdirStub.rejects();
85-
assert.notOk(await deleteEnvironmentWindows(workspace1, 'python.exe'));
87+
assert.notOk(await deleteEnvironmentWindows(venvPath, venvPythonPath, workspace1, 'python.exe'));
8688
assert.ok(switchPythonStub.calledOnce);
8789
assert.ok(showErrorMessageWithLogsStub.calledOnce);
8890
});
8991

9092
test('Delete python.exe failed no interpreter', async () => {
9193
unlinkStub.rejects();
9294
rmdirStub.rejects();
93-
assert.notOk(await deleteEnvironmentWindows(workspace1, undefined));
95+
assert.notOk(await deleteEnvironmentWindows(venvPath, venvPythonPath, workspace1, undefined));
9496
assert.ok(switchPythonStub.notCalled);
9597
assert.ok(showErrorMessageWithLogsStub.calledOnce);
9698
});
@@ -106,6 +108,7 @@ suite('Test Delete environments (linux/mac)', () => {
106108
name: 'workspace1',
107109
index: 0,
108110
};
111+
const venvPath = path.join(workspace1.uri.fsPath, 'venv');
109112

110113
setup(() => {
111114
pathExistsStub = sinon.stub(fs, 'pathExists');
@@ -123,7 +126,7 @@ suite('Test Delete environments (linux/mac)', () => {
123126
pathExistsStub.resolves(true);
124127
rmdirStub.resolves();
125128

126-
assert.ok(await deleteEnvironmentNonWindows(workspace1));
129+
assert.ok(await deleteEnvironmentNonWindows(venvPath));
127130

128131
assert.ok(pathExistsStub.calledOnce);
129132
assert.ok(rmdirStub.calledOnce);
@@ -133,7 +136,7 @@ suite('Test Delete environments (linux/mac)', () => {
133136
test('Delete venv folder failed', async () => {
134137
pathExistsStub.resolves(true);
135138
rmdirStub.rejects();
136-
assert.notOk(await deleteEnvironmentNonWindows(workspace1));
139+
assert.notOk(await deleteEnvironmentNonWindows(venvPath));
137140

138141
assert.ok(pathExistsStub.calledOnce);
139142
assert.ok(rmdirStub.calledOnce);

0 commit comments

Comments
 (0)