forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathactivation.unit.test.ts
More file actions
176 lines (152 loc) · 7.35 KB
/
activation.unit.test.ts
File metadata and controls
176 lines (152 loc) · 7.35 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import * as TypeMoq from 'typemoq';
import { Terminal, Uri } from 'vscode';
import { ActiveResourceService } from '../../../client/common/application/activeResource';
import { TerminalManager } from '../../../client/common/application/terminalManager';
import { IActiveResourceService, ITerminalManager } from '../../../client/common/application/types';
import { TerminalActivator } from '../../../client/common/terminal/activator';
import { ITerminalActivator } from '../../../client/common/terminal/types';
import { IDisposable } from '../../../client/common/types';
import { TerminalAutoActivation } from '../../../client/terminals/activation';
import { ITerminalAutoActivation } from '../../../client/terminals/types';
import { noop } from '../../core';
import * as extapi from '../../../client/envExt/api.internal';
suite('Terminal Auto Activation', () => {
let activator: ITerminalActivator;
let terminalManager: ITerminalManager;
let terminalAutoActivation: ITerminalAutoActivation;
let activeResourceService: IActiveResourceService;
const resource = Uri.parse('a');
let terminal: Terminal;
setup(() => {
sinon.stub(extapi, 'shouldEnvExtHandleActivation').returns(false);
terminal = ({
dispose: noop,
hide: noop,
name: 'Python',
creationOptions: {},
processId: Promise.resolve(0),
sendText: noop,
show: noop,
exitStatus: { code: 0 },
} as unknown) as Terminal;
terminalManager = mock(TerminalManager);
activator = mock(TerminalActivator);
activeResourceService = mock(ActiveResourceService);
terminalAutoActivation = new TerminalAutoActivation(
instance(terminalManager),
[],
instance(activator),
instance(activeResourceService),
);
});
teardown(() => {
sinon.restore();
});
test('New Terminals should be activated', async () => {
type EventHandler = (e: Terminal) => void;
let handler: undefined | EventHandler;
const handlerDisposable = TypeMoq.Mock.ofType<IDisposable>();
const onDidOpenTerminal = (cb: EventHandler) => {
handler = cb;
return handlerDisposable.object;
};
when(activeResourceService.getActiveResource()).thenReturn(resource);
when(terminalManager.onDidOpenTerminal).thenReturn(onDidOpenTerminal);
when(activator.activateEnvironmentInTerminal(anything(), anything())).thenResolve();
terminalAutoActivation.register();
expect(handler).not.to.be.an('undefined', 'event handler not initialized');
handler!.bind(terminalAutoActivation)(terminal);
verify(activator.activateEnvironmentInTerminal(terminal, anything())).once();
});
test('New Terminals should not be activated if hidden from user', async () => {
terminal = ({
dispose: noop,
hide: noop,
name: 'Python',
creationOptions: { hideFromUser: true },
processId: Promise.resolve(0),
sendText: noop,
show: noop,
exitStatus: { code: 0 },
} as unknown) as Terminal;
type EventHandler = (e: Terminal) => void;
let handler: undefined | EventHandler;
const handlerDisposable = TypeMoq.Mock.ofType<IDisposable>();
const onDidOpenTerminal = (cb: EventHandler) => {
handler = cb;
return handlerDisposable.object;
};
when(activeResourceService.getActiveResource()).thenReturn(resource);
when(terminalManager.onDidOpenTerminal).thenReturn(onDidOpenTerminal);
when(activator.activateEnvironmentInTerminal(anything(), anything())).thenResolve();
terminalAutoActivation.register();
expect(handler).not.to.be.an('undefined', 'event handler not initialized');
handler!.bind(terminalAutoActivation)(terminal);
verify(activator.activateEnvironmentInTerminal(terminal, anything())).never();
});
test('New Terminals should not be activated if auto activation is to be disabled', async () => {
terminal = ({
dispose: noop,
hide: noop,
name: 'Python',
creationOptions: { hideFromUser: false },
processId: Promise.resolve(0),
sendText: noop,
show: noop,
exitStatus: { code: 0 },
} as unknown) as Terminal;
type EventHandler = (e: Terminal) => void;
let handler: undefined | EventHandler;
const handlerDisposable = TypeMoq.Mock.ofType<IDisposable>();
const onDidOpenTerminal = (cb: EventHandler) => {
handler = cb;
return handlerDisposable.object;
};
when(activeResourceService.getActiveResource()).thenReturn(resource);
when(terminalManager.onDidOpenTerminal).thenReturn(onDidOpenTerminal);
when(activator.activateEnvironmentInTerminal(anything(), anything())).thenResolve();
terminalAutoActivation.register();
terminalAutoActivation.disableAutoActivation(terminal);
expect(handler).not.to.be.an('undefined', 'event handler not initialized');
handler!.bind(terminalAutoActivation)(terminal);
verify(activator.activateEnvironmentInTerminal(terminal, anything())).never();
});
test('New Terminals should be activated with resource of single workspace', async () => {
type EventHandler = (e: Terminal) => void;
let handler: undefined | EventHandler;
const handlerDisposable = TypeMoq.Mock.ofType<IDisposable>();
const onDidOpenTerminal = (cb: EventHandler) => {
handler = cb;
return handlerDisposable.object;
};
when(activeResourceService.getActiveResource()).thenReturn(resource);
when(terminalManager.onDidOpenTerminal).thenReturn(onDidOpenTerminal);
when(activator.activateEnvironmentInTerminal(anything(), anything())).thenResolve();
terminalAutoActivation.register();
expect(handler).not.to.be.an('undefined', 'event handler not initialized');
handler!.bind(terminalAutoActivation)(terminal);
verify(activator.activateEnvironmentInTerminal(terminal, anything())).once();
});
test('New Terminals should be activated with resource of main workspace', async () => {
type EventHandler = (e: Terminal) => void;
let handler: undefined | EventHandler;
const handlerDisposable = TypeMoq.Mock.ofType<IDisposable>();
const onDidOpenTerminal = (cb: EventHandler) => {
handler = cb;
return handlerDisposable.object;
};
when(activeResourceService.getActiveResource()).thenReturn(resource);
when(terminalManager.onDidOpenTerminal).thenReturn(onDidOpenTerminal);
when(activator.activateEnvironmentInTerminal(anything(), anything())).thenResolve();
terminalAutoActivation.register();
expect(handler).not.to.be.an('undefined', 'event handler not initialized');
handler!.bind(terminalAutoActivation)(terminal);
verify(activator.activateEnvironmentInTerminal(terminal, anything())).once();
});
});