Skip to content

Commit 572bea4

Browse files
author
Kartik Raj
committed
More code reviews
1 parent 1b6e36c commit 572bea4

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

src/client/pythonEnvironments/common/externalDependencies.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import * as fsapi from 'fs-extra';
55
import * as path from 'path';
66
import { ExecutionResult, IProcessServiceFactory } from '../../common/process/types';
7-
import { createDeferred } from '../../common/utils/async';
87
import { getOSType, OSType } from '../../common/utils/platform';
98
import { IServiceContainer } from '../../ioc/types';
109

@@ -23,11 +22,7 @@ export async function shellExecute(command: string, timeout: number): Promise<Ex
2322
}
2423

2524
export function pathExists(absPath: string): Promise<boolean> {
26-
const deferred = createDeferred<boolean>();
27-
fsapi.exists(absPath, (result) => {
28-
deferred.resolve(result);
29-
});
30-
return deferred.promise;
25+
return fsapi.pathExists(absPath);
3126
}
3227

3328
export function readFile(filePath: string): Promise<string> {

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import { traceError } from '../../../../common/logger';
66
import { getEnvironmentVariable } from '../../../../common/utils/platform';
77
import { arePathsSame, pathExists, readFile } from '../../../common/externalDependencies';
88

9-
function getSearchDepth() {
9+
function getSearchHeight() {
1010
// PIPENV_MAX_DEPTH tells pipenv the maximum number of directories to recursively search for
1111
// a Pipfile, defaults to 3: https://pipenv.pypa.io/en/latest/advanced/#pipenv.environments.PIPENV_MAX_DEPTH
1212
const maxDepthStr = getEnvironmentVariable('PIPENV_MAX_DEPTH');
13-
if (maxDepthStr !== undefined) {
14-
const maxDepth = parseInt(maxDepthStr, 10);
15-
// eslint-disable-next-line no-restricted-globals
16-
if (isNaN(maxDepth)) {
17-
traceError(`PIPENV_MAX_DEPTH is incorrectly set. Converting value '${maxDepthStr}' to number results in NaN`);
18-
return 1;
19-
}
20-
return maxDepth;
13+
if (maxDepthStr === undefined) {
14+
return 3;
15+
}
16+
const maxDepth = parseInt(maxDepthStr, 10);
17+
// eslint-disable-next-line no-restricted-globals
18+
if (isNaN(maxDepth)) {
19+
traceError(`PIPENV_MAX_DEPTH is incorrectly set. Converting value '${maxDepthStr}' to number results in NaN`);
20+
return 1;
2121
}
22-
return 3;
22+
return maxDepth;
2323
}
2424

2525
/**
@@ -32,15 +32,15 @@ export async function _getAssociatedPipfile(
3232
options: {lookIntoParentDirectories: boolean},
3333
): Promise<string | undefined> {
3434
const pipFileName = getEnvironmentVariable('PIPENV_PIPFILE') || 'Pipfile';
35-
let depthToSearch = options.lookIntoParentDirectories ? getSearchDepth() : 1;
36-
while (depthToSearch > 0 && !arePathsSame(searchDir, path.dirname(searchDir))) {
35+
let heightToSearch = options.lookIntoParentDirectories ? getSearchHeight() : 1;
36+
while (heightToSearch > 0 && !arePathsSame(searchDir, path.dirname(searchDir))) {
3737
const pipFile = path.join(searchDir, pipFileName);
3838
// eslint-disable-next-line no-await-in-loop
3939
if (await pathExists(pipFile)) {
4040
return pipFile;
4141
}
4242
searchDir = path.dirname(searchDir);
43-
depthToSearch -= 1;
43+
heightToSearch -= 1;
4444
}
4545
return undefined;
4646
}
@@ -97,8 +97,8 @@ async function getProjectDir(envFolder: string): Promise<string | undefined> {
9797
*/
9898
async function getPipfileIfGlobal(interpreterPath: string): Promise<string | undefined> {
9999
const envFolder = path.dirname(path.dirname(interpreterPath));
100-
const project = await getProjectDir(envFolder);
101-
if (project === undefined) {
100+
const projectDir = await getProjectDir(envFolder);
101+
if (projectDir === undefined) {
102102
return undefined;
103103
}
104104

@@ -107,11 +107,11 @@ async function getPipfileIfGlobal(interpreterPath: string): Promise<string | und
107107
// |__ Pipfile <--- check if Pipfile exists here and return it
108108
// The name of the project (directory where Pipfile resides) is used as a prefix in the environment folder
109109
const envFolderName = path.basename(envFolder);
110-
if (!envFolderName.startsWith(`${path.basename(project)}-`)) {
110+
if (!envFolderName.match(new RegExp(`^${path.basename(projectDir)}-[0-9a-fA-F]+$`)) === null) {
111111
return undefined;
112112
}
113113

114-
return _getAssociatedPipfile(project, { lookIntoParentDirectories: false });
114+
return _getAssociatedPipfile(projectDir, { lookIntoParentDirectories: false });
115115
}
116116

117117
/**

src/test/pythonEnvironments/discovery/locators/pipEnvHelper.functional.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ suite('Pipenv utils', () => {
2626
readFile.withArgs(expectedDotProjectFile).resolves(project);
2727
const interpreterPath: string = path.join(TEST_LAYOUT_ROOT, 'pipenv', 'globalEnvironments', 'project3-2s1eXEJ2', 'Scripts', 'python.exe');
2828
const folder = path.join(project, 'parent', 'child', 'folder');
29+
2930
const isRelated = await isPipenvEnvironmentRelatedToFolder(interpreterPath, folder);
31+
3032
assert.equal(isRelated, true);
3133
});
3234
});

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as assert from 'assert';
2-
import * as path from 'path';
2+
import * as pathModule from 'path';
33
import * as sinon from 'sinon';
44
import * as platformApis from '../../../../client/common/utils/platform';
55
import * as externalDependencies from '../../../../client/pythonEnvironments/common/externalDependencies';
66
import { _getAssociatedPipfile, isPipenvEnvironment, isPipenvEnvironmentRelatedToFolder } from '../../../../client/pythonEnvironments/discovery/locators/services/pipEnvHelper';
77

8+
const path = platformApis.getOSType() === platformApis.OSType.Windows ? pathModule.win32 : pathModule.posix;
9+
810
suite('Pipenv helper', () => {
9-
suite('Method isPipenvEnvironmentRelatedToFolder()', async () => {
11+
suite('isPipenvEnvironmentRelatedToFolder()', async () => {
1012
let readFile: sinon.SinonStub;
1113
let getEnvVar: sinon.SinonStub;
1214
let pathExists: sinon.SinonStub;
@@ -74,7 +76,6 @@ suite('Pipenv helper', () => {
7476
const pipFileAssociatedWithFolder = path.join(folder, 'Pipfile');
7577
// Pipfile associated with folder exists
7678
pathExists.withArgs(pipFileAssociatedWithFolder).resolves(true);
77-
7879
// But the paths to both Pipfiles aren't the same
7980
arePathsSame.withArgs(pipFileAssociatedWithEnvironment, pipFileAssociatedWithFolder).resolves(false);
8081

@@ -107,7 +108,7 @@ suite('Pipenv helper', () => {
107108
});
108109
});
109110

110-
suite('Method isPipenvEnvironment()', async () => {
111+
suite('isPipenvEnvironment()', async () => {
111112
let readFile: sinon.SinonStub;
112113
let getEnvVar: sinon.SinonStub;
113114
let pathExists: sinon.SinonStub;
@@ -203,7 +204,7 @@ suite('Pipenv helper', () => {
203204
});
204205
});
205206

206-
suite('Method _getAssociatedPipfile()', async () => {
207+
suite('_getAssociatedPipfile()', async () => {
207208
let getEnvVar: sinon.SinonStub;
208209
let pathExists: sinon.SinonStub;
209210
setup(() => {

0 commit comments

Comments
 (0)