-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpoetryUtils.unit.test.ts
More file actions
210 lines (162 loc) · 8.21 KB
/
poetryUtils.unit.test.ts
File metadata and controls
210 lines (162 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import assert from 'node:assert';
import * as sinon from 'sinon';
import { EnvironmentManager, PythonEnvironment, PythonEnvironmentApi, PythonEnvironmentInfo } from '../../../api';
import * as childProcessApis from '../../../common/childProcess.apis';
import { NativeEnvInfo } from '../../../managers/common/nativePythonFinder';
import * as utils from '../../../managers/common/utils';
import {
getPoetryVersion,
isPoetryVirtualenvsInProject,
nativeToPythonEnv,
} from '../../../managers/poetry/poetryUtils';
suite('isPoetryVirtualenvsInProject', () => {
test('should return false when env var is not set', () => {
assert.strictEqual(isPoetryVirtualenvsInProject(undefined), false);
});
test('should return true when env var is "true"', () => {
assert.strictEqual(isPoetryVirtualenvsInProject('true'), true);
});
test('should return true when env var is "True" (case insensitive)', () => {
assert.strictEqual(isPoetryVirtualenvsInProject('True'), true);
});
test('should return true when env var is "TRUE" (case insensitive)', () => {
assert.strictEqual(isPoetryVirtualenvsInProject('TRUE'), true);
});
test('should return true when env var is "1"', () => {
assert.strictEqual(isPoetryVirtualenvsInProject('1'), true);
});
test('should return false when env var is "false"', () => {
assert.strictEqual(isPoetryVirtualenvsInProject('false'), false);
});
test('should return false when env var is "0"', () => {
assert.strictEqual(isPoetryVirtualenvsInProject('0'), false);
});
test('should return false when env var is empty string', () => {
assert.strictEqual(isPoetryVirtualenvsInProject(''), false);
});
test('should return false when env var is arbitrary string', () => {
assert.strictEqual(isPoetryVirtualenvsInProject('yes'), false);
});
test('should read from process.env when no argument given', () => {
const original = process.env.POETRY_VIRTUALENVS_IN_PROJECT;
try {
process.env.POETRY_VIRTUALENVS_IN_PROJECT = 'true';
assert.strictEqual(isPoetryVirtualenvsInProject(), true);
delete process.env.POETRY_VIRTUALENVS_IN_PROJECT;
assert.strictEqual(isPoetryVirtualenvsInProject(), false);
} finally {
if (original === undefined) {
delete process.env.POETRY_VIRTUALENVS_IN_PROJECT;
} else {
process.env.POETRY_VIRTUALENVS_IN_PROJECT = original;
}
}
});
});
suite('nativeToPythonEnv - POETRY_VIRTUALENVS_IN_PROJECT integration', () => {
let capturedInfo: PythonEnvironmentInfo | undefined;
let originalEnv: string | undefined;
const mockApi = {
createPythonEnvironmentItem: (info: PythonEnvironmentInfo, _manager: EnvironmentManager) => {
capturedInfo = info;
return { ...info, envId: { id: 'test-id', managerId: 'test-manager' } } as PythonEnvironment;
},
} as unknown as PythonEnvironmentApi;
const mockManager = {} as EnvironmentManager;
const baseEnvInfo: NativeEnvInfo = {
prefix: '/home/user/myproject/.venv',
executable: '/home/user/myproject/.venv/bin/python',
version: '3.12.0',
name: 'myproject-venv',
project: '/home/user/myproject',
};
setup(() => {
capturedInfo = undefined;
originalEnv = process.env.POETRY_VIRTUALENVS_IN_PROJECT;
sinon.stub(utils, 'getShellActivationCommands').resolves({
shellActivation: new Map(),
shellDeactivation: new Map(),
});
});
teardown(() => {
sinon.restore();
if (originalEnv === undefined) {
delete process.env.POETRY_VIRTUALENVS_IN_PROJECT;
} else {
process.env.POETRY_VIRTUALENVS_IN_PROJECT = originalEnv;
}
});
test('env var set + project present → environment is NOT classified as global', async () => {
process.env.POETRY_VIRTUALENVS_IN_PROJECT = 'true';
const result = await nativeToPythonEnv(baseEnvInfo, mockApi, mockManager, '/usr/bin/poetry');
assert.ok(result, 'Should return a PythonEnvironment');
assert.ok(capturedInfo, 'Should have captured environment info');
assert.strictEqual(capturedInfo!.group, undefined, 'In-project env should not have POETRY_GLOBAL group');
});
test('env var set to "1" + project present → environment is NOT classified as global', async () => {
process.env.POETRY_VIRTUALENVS_IN_PROJECT = '1';
const result = await nativeToPythonEnv(baseEnvInfo, mockApi, mockManager, '/usr/bin/poetry');
assert.ok(result, 'Should return a PythonEnvironment');
assert.ok(capturedInfo, 'Should have captured environment info');
assert.strictEqual(capturedInfo!.group, undefined, 'In-project env should not have POETRY_GLOBAL group');
});
test('env var set + project absent → falls through to normal global check', async () => {
process.env.POETRY_VIRTUALENVS_IN_PROJECT = 'true';
const envWithoutProject: NativeEnvInfo = {
...baseEnvInfo,
project: undefined,
};
const result = await nativeToPythonEnv(envWithoutProject, mockApi, mockManager, '/usr/bin/poetry');
assert.ok(result, 'Should return a PythonEnvironment');
assert.ok(capturedInfo, 'Should have captured environment info');
// Without project, falls through to global check; since prefix is not in global dir, group is undefined
assert.strictEqual(capturedInfo!.group, undefined, 'Non-global path without project should not be global');
});
test('env var NOT set → original classification behavior is preserved', async () => {
delete process.env.POETRY_VIRTUALENVS_IN_PROJECT;
const result = await nativeToPythonEnv(baseEnvInfo, mockApi, mockManager, '/usr/bin/poetry');
assert.ok(result, 'Should return a PythonEnvironment');
assert.ok(capturedInfo, 'Should have captured environment info');
// Prefix is not in global virtualenvs dir, so not classified as global
assert.strictEqual(capturedInfo!.group, undefined, 'Non-global path should not be global');
});
test('env var set to "false" → original classification behavior is preserved', async () => {
process.env.POETRY_VIRTUALENVS_IN_PROJECT = 'false';
const result = await nativeToPythonEnv(baseEnvInfo, mockApi, mockManager, '/usr/bin/poetry');
assert.ok(result, 'Should return a PythonEnvironment');
assert.ok(capturedInfo, 'Should have captured environment info');
// Falls through to normal check since env var is falsy
assert.strictEqual(capturedInfo!.group, undefined, 'Non-global path should not be global');
});
});
suite('getPoetryVersion - childProcess.apis mocking pattern', () => {
let execProcessStub: sinon.SinonStub;
setup(() => {
execProcessStub = sinon.stub(childProcessApis, 'execProcess');
});
teardown(() => {
sinon.restore();
});
test('should parse Poetry 1.x version format', async () => {
execProcessStub.resolves({ stdout: 'Poetry version 1.5.1\n', stderr: '' });
const version = await getPoetryVersion('/usr/bin/poetry');
assert.strictEqual(version, '1.5.1');
assert.ok(execProcessStub.calledOnce);
assert.ok(execProcessStub.calledWith('"/usr/bin/poetry" --version'));
});
test('should parse Poetry 2.x version format', async () => {
execProcessStub.resolves({ stdout: 'Poetry (version 2.1.3)\n', stderr: '' });
const version = await getPoetryVersion('/usr/bin/poetry');
assert.strictEqual(version, '2.1.3');
});
test('should return undefined when command fails', async () => {
execProcessStub.rejects(new Error('Command not found'));
const version = await getPoetryVersion('/nonexistent/poetry');
assert.strictEqual(version, undefined);
});
test('should return undefined when output does not match expected format', async () => {
execProcessStub.resolves({ stdout: 'unexpected output', stderr: '' });
const version = await getPoetryVersion('/usr/bin/poetry');
assert.strictEqual(version, undefined);
});
});