Skip to content

Commit 4bed560

Browse files
authored
Getting env vars of activated environments should ignore `term… (microsoft#11193)
* Disable activation of terminals higher up * News file * Fix tests
1 parent a809b62 commit 4bed560

File tree

6 files changed

+34
-41
lines changed

6 files changed

+34
-41
lines changed

news/2 Fixes/10370.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Getting environment variables of activated environments should ignore the setting `python.terminal.activateEnvironment`.

src/client/common/terminal/activator/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { inject, injectable, multiInject } from 'inversify';
77
import { Terminal } from 'vscode';
8+
import { IConfigurationService } from '../../types';
89
import { ITerminalActivationHandler, ITerminalActivator, ITerminalHelper, TerminalActivationOptions } from '../types';
910
import { BaseTerminalActivator } from './base';
1011

@@ -13,14 +14,21 @@ export class TerminalActivator implements ITerminalActivator {
1314
protected baseActivator!: ITerminalActivator;
1415
constructor(
1516
@inject(ITerminalHelper) readonly helper: ITerminalHelper,
16-
@multiInject(ITerminalActivationHandler) private readonly handlers: ITerminalActivationHandler[]
17+
@multiInject(ITerminalActivationHandler) private readonly handlers: ITerminalActivationHandler[],
18+
@inject(IConfigurationService) private readonly configurationService: IConfigurationService
1719
) {
1820
this.initialize();
1921
}
2022
public async activateEnvironmentInTerminal(
2123
terminal: Terminal,
2224
options?: TerminalActivationOptions
2325
): Promise<boolean> {
26+
const settings = this.configurationService.getSettings(options?.resource);
27+
const activateEnvironment = settings.terminal.activateEnvironment;
28+
if (!activateEnvironment) {
29+
return false;
30+
}
31+
2432
const activated = await this.baseActivator.activateEnvironmentInTerminal(terminal, options);
2533
this.handlers.forEach((handler) =>
2634
handler

src/client/common/terminal/helper.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ export class TerminalHelper implements ITerminalHelper {
125125
providers: ITerminalActivationCommandProvider[]
126126
): Promise<string[] | undefined> {
127127
const settings = this.configurationService.getSettings(resource);
128-
const activateEnvironment = settings.terminal.activateEnvironment;
129-
if (!activateEnvironment) {
130-
return;
131-
}
132128

133129
// If we have a conda environment, then use that.
134130
const isCondaEnvironment = interpreter

src/test/common/terminals/activation.conda.unit.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,6 @@ suite('Terminal Environment Activation conda', () => {
112112
});
113113
});
114114

115-
test('Ensure no activation commands are returned if the feature is disabled', async () => {
116-
terminalSettings.setup((t) => t.activateEnvironment).returns(() => false);
117-
118-
const activationCommands = await terminalHelper.getEnvironmentActivationCommands(
119-
TerminalShellType.bash,
120-
undefined
121-
);
122-
expect(activationCommands).to.equal(undefined, 'Activation commands should be undefined');
123-
});
124-
125115
test('Conda activation for fish escapes spaces in conda filename', async () => {
126116
conda = 'path to conda';
127117
const envName = 'EnvA';
@@ -283,7 +273,6 @@ suite('Terminal Environment Activation conda', () => {
283273
shellType: TerminalShellType,
284274
envName: string
285275
) {
286-
terminalSettings.setup((t) => t.activateEnvironment).returns(() => true);
287276
platformService.setup((p) => p.isLinux).returns(() => isLinux);
288277
platformService.setup((p) => p.isWindows).returns(() => isWindows);
289278
platformService.setup((p) => p.isMac).returns(() => isOsx);
@@ -384,7 +373,6 @@ suite('Terminal Environment Activation conda', () => {
384373
isLinux: boolean,
385374
pythonPath: string
386375
) {
387-
terminalSettings.setup((t) => t.activateEnvironment).returns(() => true);
388376
platformService.setup((p) => p.isLinux).returns(() => isLinux);
389377
platformService.setup((p) => p.isWindows).returns(() => isWindows);
390378
platformService.setup((p) => p.isMac).returns(() => isOsx);
@@ -432,7 +420,6 @@ suite('Terminal Environment Activation conda', () => {
432420

433421
test('Get activation script command if environment is not a conda environment', async () => {
434422
const pythonPath = path.join('users', 'xyz', '.conda', 'envs', 'enva', 'bin', 'python');
435-
terminalSettings.setup((t) => t.activateEnvironment).returns(() => true);
436423
condaService.setup((c) => c.isCondaEnvironment(TypeMoq.It.isAny())).returns(() => Promise.resolve(false));
437424
pythonSettings.setup((s) => s.pythonPath).returns(() => pythonPath);
438425

@@ -462,7 +449,6 @@ suite('Terminal Environment Activation conda', () => {
462449
isLinux: boolean,
463450
pythonPath: string
464451
) {
465-
terminalSettings.setup((t) => t.activateEnvironment).returns(() => true);
466452
platformService.setup((p) => p.isLinux).returns(() => isLinux);
467453
platformService.setup((p) => p.isWindows).returns(() => isWindows);
468454
platformService.setup((p) => p.isMac).returns(() => isOsx);
@@ -512,7 +498,6 @@ suite('Terminal Environment Activation conda', () => {
512498
test('Return undefined if unable to get activation command', async () => {
513499
const pythonPath = path.join('c', 'users', 'xyz', '.conda', 'envs', 'enva', 'python.exe');
514500

515-
terminalSettings.setup((t) => t.activateEnvironment).returns(() => true);
516501
condaService.setup((c) => c.isCondaEnvironment(TypeMoq.It.isAny())).returns(() => Promise.resolve(false));
517502

518503
pythonSettings.setup((s) => s.pythonPath).returns(() => pythonPath);

src/test/common/terminals/activator/index.unit.test.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
'use strict';
55

6+
import { assert } from 'chai';
67
import * as TypeMoq from 'typemoq';
78
import { Terminal } from 'vscode';
89
import { TerminalActivator } from '../../../../client/common/terminal/activator';
@@ -11,29 +12,43 @@ import {
1112
ITerminalActivator,
1213
ITerminalHelper
1314
} from '../../../../client/common/terminal/types';
15+
import { IConfigurationService, IPythonSettings, ITerminalSettings } from '../../../../client/common/types';
1416

1517
// tslint:disable-next-line:max-func-body-length
1618
suite('Terminal Activator', () => {
1719
let activator: TerminalActivator;
1820
let baseActivator: TypeMoq.IMock<ITerminalActivator>;
1921
let handler1: TypeMoq.IMock<ITerminalActivationHandler>;
2022
let handler2: TypeMoq.IMock<ITerminalActivationHandler>;
21-
23+
let terminalSettings: TypeMoq.IMock<ITerminalSettings>;
2224
setup(() => {
2325
baseActivator = TypeMoq.Mock.ofType<ITerminalActivator>();
26+
terminalSettings = TypeMoq.Mock.ofType<ITerminalSettings>();
2427
handler1 = TypeMoq.Mock.ofType<ITerminalActivationHandler>();
2528
handler2 = TypeMoq.Mock.ofType<ITerminalActivationHandler>();
29+
const configService = TypeMoq.Mock.ofType<IConfigurationService>();
30+
configService
31+
.setup((c) => c.getSettings(TypeMoq.It.isAny()))
32+
.returns(() => {
33+
return ({
34+
terminal: terminalSettings.object
35+
} as unknown) as IPythonSettings;
36+
});
2637
activator = new (class extends TerminalActivator {
2738
protected initialize() {
2839
this.baseActivator = baseActivator.object;
2940
}
30-
})(TypeMoq.Mock.ofType<ITerminalHelper>().object, [handler1.object, handler2.object]);
41+
})(TypeMoq.Mock.ofType<ITerminalHelper>().object, [handler1.object, handler2.object], configService.object);
3142
});
3243
async function testActivationAndHandlers(activationSuccessful: boolean) {
44+
terminalSettings
45+
.setup((b) => b.activateEnvironment)
46+
.returns(() => activationSuccessful)
47+
.verifiable(TypeMoq.Times.once());
3348
baseActivator
3449
.setup((b) => b.activateEnvironmentInTerminal(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
3550
.returns(() => Promise.resolve(activationSuccessful))
36-
.verifiable(TypeMoq.Times.once());
51+
.verifiable(TypeMoq.Times.exactly(activationSuccessful ? 1 : 0));
3752
handler1
3853
.setup((h) =>
3954
h.handleActivation(
@@ -44,7 +59,7 @@ suite('Terminal Activator', () => {
4459
)
4560
)
4661
.returns(() => Promise.resolve())
47-
.verifiable(TypeMoq.Times.once());
62+
.verifiable(TypeMoq.Times.exactly(activationSuccessful ? 1 : 0));
4863
handler2
4964
.setup((h) =>
5065
h.handleActivation(
@@ -55,11 +70,14 @@ suite('Terminal Activator', () => {
5570
)
5671
)
5772
.returns(() => Promise.resolve())
58-
.verifiable(TypeMoq.Times.once());
73+
.verifiable(TypeMoq.Times.exactly(activationSuccessful ? 1 : 0));
5974

6075
const terminal = TypeMoq.Mock.ofType<Terminal>();
61-
await activator.activateEnvironmentInTerminal(terminal.object, { preserveFocus: activationSuccessful });
76+
const activated = await activator.activateEnvironmentInTerminal(terminal.object, {
77+
preserveFocus: activationSuccessful
78+
});
6279

80+
assert.equal(activated, activationSuccessful);
6381
baseActivator.verifyAll();
6482
handler1.verifyAll();
6583
handler2.verifyAll();

src/test/common/terminals/helper.unit.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,6 @@ suite('Terminal Service helpers', () => {
167167
doSetup();
168168
when(configurationService.getSettings(resource)).thenReturn(instance(pythonSettings));
169169
});
170-
test('Activation command must be empty if activation of terminals is disabled', async () => {
171-
when(pythonSettings.terminal).thenReturn({ activateEnvironment: false } as any);
172-
173-
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
174-
175-
expect(cmd).to.equal(undefined, 'Command must be undefined');
176-
verify(pythonSettings.terminal).once();
177-
});
178170
function ensureCondaIsSupported(
179171
isSupported: boolean,
180172
pythonPath: string,
@@ -195,7 +187,6 @@ suite('Terminal Service helpers', () => {
195187
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
196188

197189
expect(cmd).to.equal(condaActivationCommands);
198-
verify(pythonSettings.terminal).once();
199190
verify(pythonSettings.pythonPath).once();
200191
verify(condaService.isCondaEnvironment(pythonPath)).once();
201192
verify(condaActivationProvider.getActivationCommands(resource, anything())).once();
@@ -215,7 +206,6 @@ suite('Terminal Service helpers', () => {
215206
);
216207

217208
expect(cmd).to.equal(undefined, 'Command must be undefined');
218-
verify(pythonSettings.terminal).once();
219209
verify(pythonSettings.pythonPath).once();
220210
verify(condaService.isCondaEnvironment(pythonPath)).once();
221211
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
@@ -238,7 +228,6 @@ suite('Terminal Service helpers', () => {
238228
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
239229

240230
expect(cmd).to.deep.equal(expectCommand);
241-
verify(pythonSettings.terminal).once();
242231
verify(pythonSettings.pythonPath).once();
243232
verify(condaService.isCondaEnvironment(pythonPath)).once();
244233
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
@@ -265,7 +254,6 @@ suite('Terminal Service helpers', () => {
265254
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
266255

267256
expect(cmd).to.deep.equal(expectCommand);
268-
verify(pythonSettings.terminal).once();
269257
verify(pythonSettings.pythonPath).once();
270258
verify(condaService.isCondaEnvironment(pythonPath)).once();
271259
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
@@ -290,7 +278,6 @@ suite('Terminal Service helpers', () => {
290278
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
291279

292280
expect(cmd).to.deep.equal(expectCommand);
293-
verify(pythonSettings.terminal).once();
294281
verify(pythonSettings.pythonPath).once();
295282
verify(condaService.isCondaEnvironment(pythonPath)).once();
296283
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
@@ -315,7 +302,6 @@ suite('Terminal Service helpers', () => {
315302
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
316303

317304
expect(cmd).to.deep.equal(expectCommand);
318-
verify(pythonSettings.terminal).once();
319305
verify(pythonSettings.pythonPath).once();
320306
verify(condaService.isCondaEnvironment(pythonPath)).once();
321307
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
@@ -359,7 +345,6 @@ suite('Terminal Service helpers', () => {
359345
);
360346

361347
expect(cmd).to.equal(undefined, 'Command must be undefined');
362-
verify(pythonSettings.terminal).once();
363348
verify(pythonSettings.pythonPath).times(interpreter ? 0 : 1);
364349
verify(condaService.isCondaEnvironment(pythonPath)).times(interpreter ? 0 : 1);
365350
verify(bashActivationProvider.isShellSupported(shellToExpect)).atLeast(1);

0 commit comments

Comments
 (0)