|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | + |
| 4 | +import { EventEmitter } from 'vscode'; |
| 5 | + |
| 6 | +import * as windowApis from '../../../common/window.apis'; |
| 7 | +import * as workspaceApis from '../../../common/workspace.apis'; |
| 8 | +import { TerminalManagerImpl } from '../../../features/terminal/terminalManager'; |
| 9 | +import { |
| 10 | + ShellScriptEditState, |
| 11 | + ShellSetupState, |
| 12 | + ShellStartupScriptProvider, |
| 13 | +} from '../../../features/terminal/shells/startupProvider'; |
| 14 | +import { ACT_TYPE_COMMAND } from '../../../features/terminal/utils'; |
| 15 | +import * as terminalUtils from '../../../features/terminal/utils'; |
| 16 | +import { |
| 17 | + DidChangeTerminalActivationStateEvent, |
| 18 | + TerminalActivationInternal, |
| 19 | +} from '../../../features/terminal/terminalActivationState'; |
| 20 | + |
| 21 | +type DisposableLike = { dispose(): void }; |
| 22 | + |
| 23 | +suite('TerminalManager shellStartup profile behavior', () => { |
| 24 | + let sandbox: sinon.SinonSandbox; |
| 25 | + let onDidChangeConfigurationHandler: |
| 26 | + | ((e: { affectsConfiguration: (section: string) => boolean }) => void | Promise<void>) |
| 27 | + | undefined; |
| 28 | + |
| 29 | + setup(() => { |
| 30 | + sandbox = sinon.createSandbox(); |
| 31 | + onDidChangeConfigurationHandler = undefined; |
| 32 | + |
| 33 | + const disposable: DisposableLike = { dispose() {} }; |
| 34 | + |
| 35 | + sandbox.stub(windowApis, 'onDidOpenTerminal').returns(disposable as any); |
| 36 | + sandbox.stub(windowApis, 'onDidCloseTerminal').returns(disposable as any); |
| 37 | + sandbox.stub(windowApis, 'onDidChangeWindowState').returns(disposable as any); |
| 38 | + |
| 39 | + sandbox.stub(workspaceApis, 'onDidChangeConfiguration').callsFake((handler: any) => { |
| 40 | + onDidChangeConfigurationHandler = handler; |
| 41 | + return disposable as any; |
| 42 | + }); |
| 43 | + |
| 44 | + // Avoid any real window focus concerns. |
| 45 | + sandbox.stub(windowApis, 'terminals').returns([] as any); |
| 46 | + }); |
| 47 | + |
| 48 | + teardown(() => { |
| 49 | + sandbox.restore(); |
| 50 | + }); |
| 51 | + |
| 52 | + function createTerminalManager(startupScriptProviders: ShellStartupScriptProvider[]): TerminalManagerImpl { |
| 53 | + const emitter = new EventEmitter<DidChangeTerminalActivationStateEvent>(); |
| 54 | + const ta: TerminalActivationInternal = { |
| 55 | + onDidChangeTerminalActivationState: emitter.event, |
| 56 | + isActivated: () => false, |
| 57 | + getEnvironment: () => undefined, |
| 58 | + activate: async () => undefined, |
| 59 | + deactivate: async () => undefined, |
| 60 | + updateActivationState: () => undefined, |
| 61 | + dispose: () => emitter.dispose(), |
| 62 | + }; |
| 63 | + |
| 64 | + return new TerminalManagerImpl(ta, [], startupScriptProviders); |
| 65 | + } |
| 66 | + |
| 67 | + test('does not tear down profile scripts when shellStartup is setup', async () => { |
| 68 | + const provider: ShellStartupScriptProvider = { |
| 69 | + name: 'bash', |
| 70 | + shellType: 'bash', |
| 71 | + isSetup: sandbox.stub().resolves(ShellSetupState.Setup), |
| 72 | + setupScripts: sandbox.stub().resolves(ShellScriptEditState.Edited), |
| 73 | + teardownScripts: sandbox.stub().resolves(ShellScriptEditState.Edited), |
| 74 | + clearCache: sandbox.stub().resolves(), |
| 75 | + }; |
| 76 | + |
| 77 | + const tm = createTerminalManager([provider]); |
| 78 | + await (tm as any).handleSetupCheck('bash'); |
| 79 | + |
| 80 | + sinon.assert.notCalled(provider.teardownScripts as sinon.SinonStub); |
| 81 | + assert.strictEqual((tm as any).shellSetup.get('bash'), true); |
| 82 | + }); |
| 83 | + |
| 84 | + test('clears profile scripts when switching from shellStartup to command', async () => { |
| 85 | + const provider: ShellStartupScriptProvider = { |
| 86 | + name: 'bash', |
| 87 | + shellType: 'bash', |
| 88 | + isSetup: sandbox.stub().resolves(ShellSetupState.Setup), |
| 89 | + setupScripts: sandbox.stub().resolves(ShellScriptEditState.Edited), |
| 90 | + teardownScripts: sandbox.stub().resolves(ShellScriptEditState.Edited), |
| 91 | + clearCache: sandbox.stub().resolves(), |
| 92 | + }; |
| 93 | + |
| 94 | + sandbox.stub(terminalUtils, 'getAutoActivationType').returns(ACT_TYPE_COMMAND); |
| 95 | + |
| 96 | + const tm = createTerminalManager([provider]); |
| 97 | + // Seed a cached setup state so we can verify it is cleared. |
| 98 | + (tm as any).shellSetup.set('bash', true); |
| 99 | + |
| 100 | + assert.ok(onDidChangeConfigurationHandler, 'Expected onDidChangeConfiguration handler to be registered'); |
| 101 | + await onDidChangeConfigurationHandler!({ |
| 102 | + affectsConfiguration: (section: string) => section === 'python-envs.terminal.autoActivationType', |
| 103 | + }); |
| 104 | + |
| 105 | + sinon.assert.calledOnce(provider.teardownScripts as sinon.SinonStub); |
| 106 | + assert.strictEqual((tm as any).shellSetup.size, 0); |
| 107 | + }); |
| 108 | +}); |
0 commit comments