|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import * as sinon from 'sinon'; |
| 5 | +import * as TypeMoq from 'typemoq'; |
| 6 | +import { GlobalEnvironmentVariableCollection, Uri, WorkspaceConfiguration } from 'vscode'; |
| 7 | +import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; |
| 8 | +import { registerPythonStartup } from '../../../client/terminals/pythonStartup'; |
| 9 | +import { IExtensionContext } from '../../../client/common/types'; |
| 10 | + |
| 11 | +suite('Terminal - Shell Integration with PYTHONSTARTUP', () => { |
| 12 | + let getConfigurationStub: sinon.SinonStub; |
| 13 | + let pythonConfig: TypeMoq.IMock<WorkspaceConfiguration>; |
| 14 | + let editorConfig: TypeMoq.IMock<WorkspaceConfiguration>; |
| 15 | + let context: TypeMoq.IMock<IExtensionContext>; |
| 16 | + let createDirectoryStub: sinon.SinonStub; |
| 17 | + let copyStub: sinon.SinonStub; |
| 18 | + let globalEnvironmentVariableCollection: TypeMoq.IMock<GlobalEnvironmentVariableCollection>; |
| 19 | + |
| 20 | + setup(() => { |
| 21 | + context = TypeMoq.Mock.ofType<IExtensionContext>(); |
| 22 | + globalEnvironmentVariableCollection = TypeMoq.Mock.ofType<GlobalEnvironmentVariableCollection>(); |
| 23 | + |
| 24 | + // Question: Why do we have to set up environmentVariableCollection and globalEnvironmentVariableCollection in this flip-flop way? |
| 25 | + // Reference: /vscode-python/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts |
| 26 | + context.setup((c) => c.environmentVariableCollection).returns(() => globalEnvironmentVariableCollection.object); |
| 27 | + context.setup((c) => c.storageUri).returns(() => Uri.parse('a')); |
| 28 | + |
| 29 | + globalEnvironmentVariableCollection |
| 30 | + .setup((c) => c.replace(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())) |
| 31 | + .returns(() => Promise.resolve()); |
| 32 | + |
| 33 | + globalEnvironmentVariableCollection.setup((c) => c.delete(TypeMoq.It.isAny())).returns(() => Promise.resolve()); |
| 34 | + |
| 35 | + getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration'); |
| 36 | + createDirectoryStub = sinon.stub(workspaceApis, 'createDirectory'); |
| 37 | + copyStub = sinon.stub(workspaceApis, 'copy'); |
| 38 | + |
| 39 | + pythonConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>(); |
| 40 | + editorConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>(); |
| 41 | + getConfigurationStub.callsFake((section: string) => { |
| 42 | + if (section === 'python') { |
| 43 | + return pythonConfig.object; |
| 44 | + } |
| 45 | + return editorConfig.object; |
| 46 | + }); |
| 47 | + |
| 48 | + createDirectoryStub.callsFake((_) => Promise.resolve()); |
| 49 | + copyStub.callsFake((_, __, ___) => Promise.resolve()); |
| 50 | + }); |
| 51 | + |
| 52 | + teardown(() => { |
| 53 | + sinon.restore(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('Verify createDirectory is called when shell integration is enabled', async () => { |
| 57 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => true); |
| 58 | + |
| 59 | + await registerPythonStartup(context.object); |
| 60 | + |
| 61 | + sinon.assert.calledOnce(createDirectoryStub); |
| 62 | + }); |
| 63 | + |
| 64 | + test('Verify createDirectory is not called when shell integration is disabled', async () => { |
| 65 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => false); |
| 66 | + |
| 67 | + await registerPythonStartup(context.object); |
| 68 | + |
| 69 | + sinon.assert.notCalled(createDirectoryStub); |
| 70 | + }); |
| 71 | + |
| 72 | + test('Verify copy is called when shell integration is enabled', async () => { |
| 73 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => true); |
| 74 | + |
| 75 | + await registerPythonStartup(context.object); |
| 76 | + |
| 77 | + sinon.assert.calledOnce(copyStub); |
| 78 | + }); |
| 79 | + |
| 80 | + test('Verify copy is not called when shell integration is disabled', async () => { |
| 81 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => false); |
| 82 | + |
| 83 | + await registerPythonStartup(context.object); |
| 84 | + |
| 85 | + sinon.assert.notCalled(copyStub); |
| 86 | + }); |
| 87 | + |
| 88 | + test('PYTHONSTARTUP is set when enableShellIntegration setting is true', async () => { |
| 89 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => true); |
| 90 | + |
| 91 | + await registerPythonStartup(context.object); |
| 92 | + |
| 93 | + globalEnvironmentVariableCollection.verify( |
| 94 | + (c) => c.replace('PYTHONSTARTUP', TypeMoq.It.isAny(), TypeMoq.It.isAny()), |
| 95 | + TypeMoq.Times.once(), |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + test('environmentCollection should not remove PYTHONSTARTUP when enableShellIntegration setting is true', async () => { |
| 100 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => true); |
| 101 | + |
| 102 | + await registerPythonStartup(context.object); |
| 103 | + |
| 104 | + globalEnvironmentVariableCollection.verify((c) => c.delete('PYTHONSTARTUP'), TypeMoq.Times.never()); |
| 105 | + }); |
| 106 | + |
| 107 | + test('PYTHONSTARTUP is not set when enableShellIntegration setting is false', async () => { |
| 108 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => false); |
| 109 | + |
| 110 | + await registerPythonStartup(context.object); |
| 111 | + |
| 112 | + globalEnvironmentVariableCollection.verify( |
| 113 | + (c) => c.replace('PYTHONSTARTUP', TypeMoq.It.isAny(), TypeMoq.It.isAny()), |
| 114 | + TypeMoq.Times.never(), |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + test('PYTHONSTARTUP is deleted when enableShellIntegration setting is false', async () => { |
| 119 | + pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => false); |
| 120 | + |
| 121 | + await registerPythonStartup(context.object); |
| 122 | + |
| 123 | + globalEnvironmentVariableCollection.verify((c) => c.delete('PYTHONSTARTUP'), TypeMoq.Times.once()); |
| 124 | + }); |
| 125 | +}); |
0 commit comments