Skip to content

Commit 7b99f11

Browse files
committed
chore: clean-up
1 parent af5ab88 commit 7b99f11

2 files changed

Lines changed: 0 additions & 36 deletions

File tree

src/managers/builtin/venvUtils.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -422,39 +422,24 @@ export async function createPythonVenv(
422422
return createStepBasedVenvFlow(nativeFinder, api, log, manager, basePythons, venvRoot, options);
423423
}
424424

425-
/**
426-
* Checks if a path is a drive root (e.g., "C:\" on Windows or "/" on Unix).
427-
*/
428425
function isDriveRoot(fsPath: string): boolean {
429426
const normalized = path.normalize(fsPath);
430427
if (os.platform() === 'win32') {
431-
// Windows drive root: "C:\" or "C:"
432428
return /^[a-zA-Z]:[\\/]?$/.test(normalized);
433429
}
434-
// Unix root
435430
return normalized === '/';
436431
}
437432

438-
/**
439-
* Checks if a path has a minimum number of directory components to be considered safe.
440-
* This helps prevent accidental deletion of high-level directories.
441-
*/
442433
function hasMinimumPathDepth(fsPath: string, minDepth: number = 2): boolean {
443434
const normalized = path.normalize(fsPath);
444435
const parts = normalized.split(path.sep).filter((p) => p.length > 0 && p !== '.');
445436

446437
if (os.platform() === 'win32') {
447-
// On Windows, the drive letter counts as one component
448-
// e.g., "C:\folder" has parts ["C:", "folder"] -> depth 2
449438
return parts.length >= minDepth;
450439
}
451-
// On Unix, "/home/user" has parts ["home", "user"] -> depth 2
452440
return parts.length >= minDepth;
453441
}
454442

455-
/**
456-
* Validates that a path appears to be a virtual environment by checking for pyvenv.cfg.
457-
*/
458443
async function isValidVenvPath(fsPath: string): Promise<boolean> {
459444
try {
460445
const pyvenvCfgPath = path.join(fsPath, 'pyvenv.cfg');
@@ -464,24 +449,17 @@ async function isValidVenvPath(fsPath: string): Promise<boolean> {
464449
}
465450
}
466451

467-
/**
468-
* Validates that a path is safe to remove as a virtual environment.
469-
* Returns an error message if unsafe, or undefined if safe.
470-
*/
471452
async function validateVenvRemovalPath(envPath: string, log: LogOutputChannel): Promise<string | undefined> {
472-
// Check if it's a drive root
473453
if (isDriveRoot(envPath)) {
474454
log.error(`Refusing to remove drive root: ${envPath}`);
475455
return VenvManagerStrings.venvRemoveUnsafePath;
476456
}
477457

478-
// Check minimum path depth (at least 2 components to be safe)
479458
if (!hasMinimumPathDepth(envPath, 2)) {
480459
log.error(`Refusing to remove path with insufficient depth: ${envPath}`);
481460
return VenvManagerStrings.venvRemoveUnsafePath;
482461
}
483462

484-
// Check if it's actually a venv (has pyvenv.cfg)
485463
if (!(await isValidVenvPath(envPath))) {
486464
log.error(`Path does not appear to be a valid venv (no pyvenv.cfg): ${envPath}`);
487465
return VenvManagerStrings.venvRemoveInvalidPath;
@@ -493,11 +471,9 @@ async function validateVenvRemovalPath(envPath: string, log: LogOutputChannel):
493471
export async function removeVenv(environment: PythonEnvironment, log: LogOutputChannel): Promise<boolean> {
494472
const pythonPath = os.platform() === 'win32' ? 'python.exe' : 'python';
495473

496-
// Normalize path separators before checking endsWith
497474
const envFsPath = path.normalize(environment.environmentPath.fsPath);
498475
const envPath = envFsPath.endsWith(pythonPath) ? path.dirname(path.dirname(envFsPath)) : envFsPath;
499476

500-
// Safety validation before proceeding
501477
const validationError = await validateVenvRemovalPath(envPath, log);
502478
if (validationError) {
503479
showErrorMessage(validationError);

src/test/managers/builtin/venvUtils.removeVenv.unit.test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import * as assert from 'assert';
22
import * as os from 'os';
33
import * as path from 'path';
44

5-
// We need to test the helper functions that are now part of venvUtils
6-
// Since they are not exported, we'll test the behavior through removeVenv
7-
// For unit testing the validation logic, we can re-implement the pure logic tests here
8-
95
suite('venvUtils Path Validation', () => {
106
suite('isDriveRoot behavior', () => {
117
test('should identify Windows drive roots correctly', function () {
@@ -14,9 +10,6 @@ suite('venvUtils Path Validation', () => {
1410
return;
1511
}
1612

17-
// Test cases that represent drive roots
18-
// Note: path.normalize('C:') returns 'C:.' on Windows, so we exclude bare 'C:'
19-
// The actual drive roots we care about are 'C:\' style paths
2013
const driveRoots = ['C:\\', 'D:\\', 'c:\\', 'C:/'];
2114

2215
for (const root of driveRoots) {
@@ -100,10 +93,8 @@ suite('venvUtils Path Validation', () => {
10093

10194
suite('Path normalization in removeVenv', () => {
10295
test('should normalize path separators before checking python.exe suffix', () => {
103-
// This test verifies that paths with mixed separators are handled correctly
10496
const pythonPath = os.platform() === 'win32' ? 'python.exe' : 'python';
10597

106-
// Example of a path that might have mixed separators on Windows
10798
const mixedPath =
10899
os.platform() === 'win32' ? 'C:/Users/test/.venv/Scripts/python.exe' : '/home/user/.venv/bin/python';
109100

@@ -112,7 +103,6 @@ suite('venvUtils Path Validation', () => {
112103

113104
assert.strictEqual(endsWithPython, true, 'Normalized path should end with python executable');
114105

115-
// Verify the resulting env path after going up two directories
116106
const envPath = path.dirname(path.dirname(normalizedPath));
117107
const expectedEnvPath =
118108
os.platform() === 'win32' ? path.normalize('C:/Users/test/.venv') : '/home/user/.venv';
@@ -123,7 +113,6 @@ suite('venvUtils Path Validation', () => {
123113
test('should correctly derive venv path from python executable path', () => {
124114
const pythonPath = os.platform() === 'win32' ? 'python.exe' : 'python';
125115

126-
// Simulate what removeVenv does
127116
const testPaths =
128117
os.platform() === 'win32'
129118
? [
@@ -151,7 +140,6 @@ suite('venvUtils removeVenv validation integration', () => {
151140

152141
const expectedCfgPath = path.join(testEnvPath, 'pyvenv.cfg');
153142

154-
// Just verify the path is constructed correctly
155143
assert.strictEqual(
156144
expectedCfgPath,
157145
os.platform() === 'win32' ? 'C:\\Users\\test\\.venv\\pyvenv.cfg' : '/home/user/.venv/pyvenv.cfg',

0 commit comments

Comments
 (0)