Skip to content

Commit 892724a

Browse files
author
Kartik Raj
committed
Add tests
1 parent e076f87 commit 892724a

File tree

11 files changed

+92
-11
lines changed

11 files changed

+92
-11
lines changed

src/client/pythonEnvironments/common/environmentIdentifier.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import * as fsapi from 'fs-extra';
54
import * as path from 'path';
6-
import { createDeferred } from '../../common/utils/async';
75
import { isWindowsStoreEnvironment } from '../discovery/locators/services/windowsStoreLocator';
86
import { EnvironmentType } from '../info';
9-
10-
function pathExists(absPath: string): Promise<boolean> {
11-
const deferred = createDeferred<boolean>();
12-
fsapi.exists(absPath, (result) => {
13-
deferred.resolve(result);
14-
});
15-
return deferred.promise;
16-
}
7+
import { pathExists } from './externalDependencies';
178

189
/**
1910
* Checks if the given interpreter path belongs to a conda environment. Using

src/client/pythonEnvironments/common/externalDependencies.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
import * as fsapi from 'fs-extra';
45
import { ExecutionResult, IProcessServiceFactory } from '../../common/process/types';
6+
import { createDeferred } from '../../common/utils/async';
57
import { IServiceContainer } from '../../ioc/types';
68

79
let internalServiceContainer: IServiceContainer;
@@ -17,3 +19,15 @@ export async function shellExecute(command: string, timeout: number): Promise<Ex
1719
const proc = await getProcessFactory().create();
1820
return proc.shellExec(command, { timeout });
1921
}
22+
23+
export function pathExists(absPath: string): Promise<boolean> {
24+
const deferred = createDeferred<boolean>();
25+
fsapi.exists(absPath, (result) => {
26+
deferred.resolve(result);
27+
});
28+
return deferred.promise;
29+
}
30+
31+
export function readFile(filePath: string): Promise<string> {
32+
return fsapi.readFile(filePath, 'utf-8');
33+
}

src/test/pythonEnvironments/common/environmentIdentifier.unit.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as path from 'path';
66
import * as sinon from 'sinon';
77
import * as platformApis from '../../../client/common/utils/platform';
88
import { identifyEnvironment } from '../../../client/pythonEnvironments/common/environmentIdentifier';
9+
import * as externalDependencies from '../../../client/pythonEnvironments/common/externalDependencies';
910
import { EnvironmentType } from '../../../client/pythonEnvironments/info';
1011
import { TEST_LAYOUT_ROOT } from './commonTestConstants';
1112

@@ -23,8 +24,46 @@ suite('Environment Identifier', () => {
2324
});
2425
});
2526

27+
suite('Pipenv', () => {
28+
let readFile: sinon.SinonStub;
29+
setup(() => {
30+
getEnvVar = sinon.stub(platformApis, 'getEnvironmentVariable');
31+
readFile = sinon.stub(externalDependencies, 'readFile');
32+
});
33+
34+
teardown(() => {
35+
readFile.restore();
36+
getEnvVar.restore();
37+
});
38+
39+
test('Path to a general global pipenv environment', async () => {
40+
const expectedDotProjectFile = path.join(testLayoutsRoot, 'pipenv', 'globalEnvironments', 'project2-vnNIWe9P', '.project');
41+
const expectedProjectFile = path.join(testLayoutsRoot, 'pipenv', 'project2');
42+
readFile.withArgs(expectedDotProjectFile).resolves(expectedProjectFile);
43+
const interpreterPath: string = path.join(testLayoutsRoot, 'pipenv', 'globalEnvironments', 'project2-vnNIWe9P', 'bin', 'python');
44+
const envType: EnvironmentType = await identifyEnvironment(interpreterPath);
45+
assert.equal(envType, EnvironmentType.Pipenv);
46+
});
47+
48+
test('Path to a global pipenv environment whose Pipfile lies at 3 levels above the project the environment is associated with', async () => {
49+
getEnvVar.withArgs('PIPENV_MAX_DEPTH').returns('5');
50+
const expectedDotProjectFile = path.join(testLayoutsRoot, 'pipenv', 'globalEnvironments', 'grandparent-2s1eXEJ2', '.project');
51+
const expectedProjectFile = path.join(testLayoutsRoot, 'pipenv', 'grandparent', 'parent', 'child', 'project3');
52+
readFile.withArgs(expectedDotProjectFile).resolves(expectedProjectFile);
53+
const interpreterPath: string = path.join(testLayoutsRoot, 'pipenv', 'globalEnvironments', 'grandparent-2s1eXEJ2', 'Scripts', 'python.exe');
54+
const envType: EnvironmentType = await identifyEnvironment(interpreterPath);
55+
assert.equal(envType, EnvironmentType.Pipenv);
56+
});
57+
58+
test('Path to a local pipenv environment with a custom Pipfile name', async () => {
59+
getEnvVar.withArgs('PIPENV_PIPFILE').returns('CustomPipfileName');
60+
const interpreterPath: string = path.join(testLayoutsRoot, 'pipenv', 'project1', '.venv', 'Scripts', 'python.exe');
61+
const envType: EnvironmentType = await identifyEnvironment(interpreterPath);
62+
assert.equal(envType, EnvironmentType.Pipenv);
63+
});
64+
});
65+
2666
suite('Windows Store', () => {
27-
let getEnvVar: sinon.SinonStub;
2867
const fakeLocalAppDataPath = 'X:\\users\\user\\AppData\\Local';
2968
const fakeProgramFilesPath = 'X:\\Program Files';
3069
const executable = ['python.exe', 'python3.exe', 'python3.8.exe'];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Absolute path to \src\test\pythonEnvironments\common\envlayouts\pipenv\grandparent\parent\child\project3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Not real python exe
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Absolute path to \src\test\pythonEnvironments\common\envlayouts\pipenv\project2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Not a real python binary
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
10+
[requires]
11+
python_version = "3.7"

src/test/pythonEnvironments/common/envlayouts/pipenv/grandparent/parent/child/project3/dummyFile

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
10+
[requires]
11+
python_version = "3.7"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
10+
[requires]
11+
python_version = "3.7"

0 commit comments

Comments
 (0)