Skip to content

Commit fcdca8f

Browse files
author
Kartik Raj
authored
Fix venv tests for terminal activation (microsoft#16747)
* Unskip venv tests * Run only venv tests * Do not stub experiments service * Use new terminal setting to update shell * Initialize conda shell * Use new terminal setting everywhere * Run all tests * Use python.condaPath as conda binary if set * Add/Fix tests for custom conda path * Use conda init everywhere
1 parent 6314e70 commit fcdca8f

7 files changed

Lines changed: 50 additions & 20 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ jobs:
252252
}
253253
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaExecPath $condaExecPath
254254
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaPath
255+
& $condaExecPath init --all
255256
256257
# 2. For `interpreterLocatorService.testvirtualenvs.ts`
257258

.github/workflows/nightly-coverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ jobs:
119119
}
120120
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaExecPath $condaExecPath
121121
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaPath
122+
& $condaExecPath init --all
122123
123124
# 2. For `interpreterLocatorService.testvirtualenvs.ts`
124125

.github/workflows/pr-check.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ jobs:
231231
}
232232
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaExecPath $condaExecPath
233233
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaPath
234+
& $condaExecPath init --all
234235
235236
# 2. For `interpreterLocatorService.testvirtualenvs.ts`
236237
@@ -545,6 +546,7 @@ jobs:
545546
}
546547
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaExecPath $condaExecPath
547548
& $condaPythonPath ./build/ci/addEnvPath.py ${{env.PYTHON_VIRTUAL_ENVS_LOCATION}} condaPath
549+
& $condaExecPath init --all
548550
549551
# 2. For `interpreterLocatorService.testvirtualenvs.ts`
550552

src/client/pythonEnvironments/common/environmentManagers/conda.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fsapi from 'fs-extra';
22
import * as path from 'path';
33
import { traceVerbose } from '../../../common/logger';
44
import { getEnvironmentVariable, getOSType, getUserHomeDir, OSType } from '../../../common/utils/platform';
5-
import { exec, pathExists, readFile } from '../externalDependencies';
5+
import { exec, getPythonSetting, pathExists, readFile } from '../externalDependencies';
66

77
import { PythonVersion, UNKNOWN_PYTHON_VERSION } from '../../base/info';
88
import { parseVersion } from '../../base/info/pythonVersion';
@@ -238,6 +238,11 @@ export class Conda {
238238

239239
// Produce a list of candidate binaries to be probed by exec'ing them.
240240
async function* getCandidates() {
241+
const customCondaPath = getPythonSetting<string>('condaPath');
242+
if (customCondaPath && customCondaPath !== 'conda') {
243+
// If user has specified a custom conda path, use it first.
244+
yield customCondaPath;
245+
}
241246
// Check unqualified filename first, in case it's on PATH.
242247
yield 'conda';
243248
if (getOSType() === OSType.Windows) {

src/test/common/terminals/environmentActivationProviders/terminalActivation.testvirtualenvs.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import {
2424
import { EXTENSION_ROOT_DIR_FOR_TESTS, TEST_TIMEOUT } from '../../../constants';
2525
import { sleep } from '../../../core';
2626
import { initialize, initializeTest } from '../../../initialize';
27-
import * as ExperimentHelpers from '../../../../client/common/experiments/helpers';
2827

29-
suite.skip('Activation of Environments in Terminal', () => {
28+
suite('Activation of Environments in Terminal', () => {
3029
const file = path.join(
3130
EXTENSION_ROOT_DIR_FOR_TESTS,
3231
'src',
@@ -61,13 +60,12 @@ suite.skip('Activation of Environments in Terminal', () => {
6160
let experiments: IExperimentService;
6261
const sandbox = sinon.createSandbox();
6362
suiteSetup(async () => {
64-
sandbox.stub(ExperimentHelpers, 'inDiscoveryExperiment').resolves(false);
6563
envPaths = await fs.readJson(envsLocation);
6664
terminalSettings = vscode.workspace.getConfiguration('terminal', vscode.workspace.workspaceFolders![0].uri);
6765
pythonSettings = vscode.workspace.getConfiguration('python', vscode.workspace.workspaceFolders![0].uri);
68-
defaultShell.Windows = terminalSettings.inspect('integrated.shell.windows').globalValue;
69-
defaultShell.Linux = terminalSettings.inspect('integrated.shell.linux').globalValue;
70-
await terminalSettings.update('integrated.shell.linux', '/bin/bash', vscode.ConfigurationTarget.Global);
66+
defaultShell.Windows = terminalSettings.inspect('integrated.defaultProfile.windows').globalValue;
67+
defaultShell.Linux = terminalSettings.inspect('integrated.defaultProfile.linux').globalValue;
68+
await terminalSettings.update('integrated.defaultProfile.linux', 'bash', vscode.ConfigurationTarget.Global);
7169
experiments = (await initialize()).serviceContainer.get<IExperimentService>(IExperimentService);
7270
});
7371

@@ -105,11 +103,15 @@ suite.skip('Activation of Environments in Terminal', () => {
105103
vscode.ConfigurationTarget.WorkspaceFolder,
106104
);
107105
await terminalSettings.update(
108-
'integrated.shell.windows',
106+
'integrated.defaultProfile.windows',
109107
defaultShell.Windows,
110108
vscode.ConfigurationTarget.Global,
111109
);
112-
await terminalSettings.update('integrated.shell.linux', defaultShell.Linux, vscode.ConfigurationTarget.Global);
110+
await terminalSettings.update(
111+
'integrated.defaultProfile.linux',
112+
defaultShell.Linux,
113+
vscode.ConfigurationTarget.Global,
114+
);
113115
await pythonSettings.update('condaPath', undefined, vscode.ConfigurationTarget.Workspace);
114116
if (experiments.inExperimentSync(DeprecatePythonPath.experiment)) {
115117
await resetGlobalInterpreterPathSetting();
@@ -189,9 +191,10 @@ suite.skip('Activation of Environments in Terminal', () => {
189191
await testActivation(envPaths.virtualEnvPath);
190192
});
191193
test('Should activate with conda', async () => {
194+
// Powershell does not work with conda by default, hence use cmd.
192195
await terminalSettings.update(
193-
'integrated.shell.windows',
194-
'C:\\Windows\\System32\\cmd.exe',
196+
'integrated.defaultProfile.windows',
197+
'Command Prompt',
195198
vscode.ConfigurationTarget.Global,
196199
);
197200
await pythonSettings.update('condaPath', envPaths.condaExecPath, vscode.ConfigurationTarget.Workspace);

src/test/pythonEnvironments/base/locators/composite/resolverUtils.unit.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@ import {
2727
import { resolveBasicEnv } from '../../../../../client/pythonEnvironments/base/locators/composite/resolverUtils';
2828

2929
suite('Resolver Utils', () => {
30+
let getWorkspaceFolders: sinon.SinonStub;
31+
setup(() => {
32+
sinon.stub(externalDependencies, 'getPythonSetting').withArgs('condaPath').returns('conda');
33+
getWorkspaceFolders = sinon.stub(externalDependencies, 'getWorkspaceFolders');
34+
getWorkspaceFolders.returns([]);
35+
});
36+
37+
teardown(() => {
38+
sinon.restore();
39+
});
40+
3041
suite('Pyenv', () => {
3142
const testPyenvRoot = path.join(TEST_LAYOUT_ROOT, 'pyenvhome', '.pyenv');
3243
const testPyenvVersionsDir = path.join(testPyenvRoot, 'versions');
3344
setup(() => {
34-
sinon.stub(externalDependencies, 'getWorkspaceFolders').returns([]);
3545
sinon.stub(platformApis, 'getEnvironmentVariable').withArgs('PYENV_ROOT').returns(testPyenvRoot);
3646
});
3747

@@ -68,7 +78,6 @@ suite('Resolver Utils', () => {
6878
const testStoreAppRoot = path.join(testLocalAppData, 'Microsoft', 'WindowsApps');
6979

7080
setup(() => {
71-
sinon.stub(externalDependencies, 'getWorkspaceFolders').returns([]);
7281
sinon.stub(platformApis, 'getEnvironmentVariable').withArgs('LOCALAPPDATA').returns(testLocalAppData);
7382
});
7483

@@ -199,10 +208,6 @@ suite('Resolver Utils', () => {
199208
};
200209
}
201210

202-
setup(() => {
203-
sinon.stub(externalDependencies, 'getWorkspaceFolders').returns([]);
204-
});
205-
206211
teardown(() => {
207212
sinon.restore();
208213
});
@@ -265,7 +270,7 @@ suite('Resolver Utils', () => {
265270
suite('Simple envs', () => {
266271
const testVirtualHomeDir = path.join(TEST_LAYOUT_ROOT, 'virtualhome');
267272
setup(() => {
268-
sinon.stub(externalDependencies, 'getWorkspaceFolders').returns([testVirtualHomeDir]);
273+
getWorkspaceFolders.returns([testVirtualHomeDir]);
269274
});
270275

271276
teardown(() => {
@@ -318,7 +323,6 @@ suite('Resolver Utils', () => {
318323
const testPosixKnownPathsRoot = path.join(TEST_LAYOUT_ROOT, 'posixroot');
319324
const testLocation3 = path.join(testPosixKnownPathsRoot, 'location3');
320325
setup(() => {
321-
sinon.stub(externalDependencies, 'getWorkspaceFolders').returns([]);
322326
sinon.stub(platformApis, 'getOSType').callsFake(() => platformApis.OSType.Linux);
323327
});
324328

@@ -514,7 +518,6 @@ suite('Resolver Utils', () => {
514518
setup(async () => {
515519
sinon.stub(winreg, 'readRegistryValues').callsFake(fakeRegistryValues);
516520
sinon.stub(winreg, 'readRegistryKeys').callsFake(fakeRegistryKeys);
517-
sinon.stub(externalDependencies, 'getWorkspaceFolders').returns([]);
518521
sinon.stub(platformApis, 'getOSType').callsFake(() => platformApis.OSType.Windows);
519522
});
520523

src/test/pythonEnvironments/common/environmentManagers/conda.unit.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ suite('Conda and its environments are located correctly', () => {
8585
};
8686
}
8787

88+
let getPythonSetting: sinon.SinonStub;
89+
8890
setup(() => {
8991
osType = platform.OSType.Unknown;
92+
getPythonSetting = sinon.stub(externalDependencies, 'getPythonSetting');
93+
getPythonSetting.withArgs('condaPath').returns('conda');
9094
homeDir = undefined;
9195
execPath = [];
9296
files = {};
@@ -189,6 +193,17 @@ suite('Conda and its environments are located correctly', () => {
189193
expect(conda).to.equal(undefined, 'conda should be missing');
190194
});
191195

196+
test('Must find conda using `python.condaPath` setting and prefer it', async () => {
197+
getPythonSetting.withArgs('condaPath').returns('condaPath/conda');
198+
199+
files = {
200+
condaPath: {
201+
conda: JSON.stringify(condaInfo('4.8.0')),
202+
},
203+
};
204+
await expectConda('/condaPath/conda');
205+
});
206+
192207
test('Must find conda on PATH, and prefer it', async () => {
193208
osType = platform.OSType.Linux;
194209
execPath = ['/bin'];

0 commit comments

Comments
 (0)