forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathterminalEnvVarCollectionPrompt.unit.test.ts
214 lines (189 loc) · 9.28 KB
/
terminalEnvVarCollectionPrompt.unit.test.ts
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { mock, when, anything, instance, verify, reset } from 'ts-mockito';
import { EventEmitter, Terminal, Uri } from 'vscode';
import { IActiveResourceService, IApplicationShell, ITerminalManager } from '../../../client/common/application/types';
import {
IConfigurationService,
IExperimentService,
IPersistentState,
IPersistentStateFactory,
IPythonSettings,
} from '../../../client/common/types';
import { TerminalEnvVarCollectionPrompt } from '../../../client/interpreter/activation/terminalEnvVarCollectionPrompt';
import { ITerminalEnvVarCollectionService } from '../../../client/interpreter/activation/types';
import { Common, Interpreters } from '../../../client/common/utils/localize';
import { TerminalEnvVarActivation } from '../../../client/common/experiments/groups';
import { sleep } from '../../core';
import { IInterpreterService } from '../../../client/interpreter/contracts';
import { PythonEnvironment } from '../../../client/pythonEnvironments/info';
suite('Terminal Environment Variable Collection Prompt', () => {
let shell: IApplicationShell;
let terminalManager: ITerminalManager;
let experimentService: IExperimentService;
let activeResourceService: IActiveResourceService;
let terminalEnvVarCollectionService: ITerminalEnvVarCollectionService;
let persistentStateFactory: IPersistentStateFactory;
let terminalEnvVarCollectionPrompt: TerminalEnvVarCollectionPrompt;
let terminalEventEmitter: EventEmitter<Terminal>;
let notificationEnabled: IPersistentState<boolean>;
let configurationService: IConfigurationService;
let interpreterService: IInterpreterService;
const prompts = [Common.doNotShowAgain];
const envName = 'env';
const expectedMessage = Interpreters.terminalEnvVarCollectionPrompt.format(`, i.e "(${envName})"`);
setup(async () => {
shell = mock<IApplicationShell>();
terminalManager = mock<ITerminalManager>();
interpreterService = mock<IInterpreterService>();
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
envName,
} as unknown) as PythonEnvironment);
experimentService = mock<IExperimentService>();
activeResourceService = mock<IActiveResourceService>();
persistentStateFactory = mock<IPersistentStateFactory>();
terminalEnvVarCollectionService = mock<ITerminalEnvVarCollectionService>();
configurationService = mock<IConfigurationService>();
when(configurationService.getSettings(anything())).thenReturn(({
terminal: {
activateEnvironment: true,
},
} as unknown) as IPythonSettings);
notificationEnabled = mock<IPersistentState<boolean>>();
terminalEventEmitter = new EventEmitter<Terminal>();
when(persistentStateFactory.createGlobalPersistentState(anything(), true)).thenReturn(
instance(notificationEnabled),
);
when(experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)).thenReturn(true);
when(terminalManager.onDidOpenTerminal).thenReturn(terminalEventEmitter.event);
terminalEnvVarCollectionPrompt = new TerminalEnvVarCollectionPrompt(
instance(shell),
instance(persistentStateFactory),
instance(terminalManager),
[],
instance(activeResourceService),
instance(terminalEnvVarCollectionService),
instance(configurationService),
instance(interpreterService),
instance(experimentService),
);
});
test('Show notification when a new terminal is opened for which there is no prompt set', async () => {
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
when(terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource)).thenReturn(false);
when(notificationEnabled.value).thenReturn(true);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
await terminalEnvVarCollectionPrompt.activate();
terminalEventEmitter.fire(terminal);
await sleep(1);
verify(shell.showInformationMessage(expectedMessage, ...prompts)).once();
});
test('Do not show notification if automatic terminal activation is turned off', async () => {
reset(configurationService);
when(configurationService.getSettings(anything())).thenReturn(({
terminal: {
activateEnvironment: false,
},
} as unknown) as IPythonSettings);
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
when(terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource)).thenReturn(false);
when(notificationEnabled.value).thenReturn(true);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
await terminalEnvVarCollectionPrompt.activate();
terminalEventEmitter.fire(terminal);
await sleep(1);
verify(shell.showInformationMessage(expectedMessage, ...prompts)).never();
});
test('When not in experiment, do not show notification for the same', async () => {
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
when(terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource)).thenReturn(false);
when(notificationEnabled.value).thenReturn(true);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
reset(experimentService);
when(experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)).thenReturn(false);
await terminalEnvVarCollectionPrompt.activate();
terminalEventEmitter.fire(terminal);
await sleep(1);
verify(shell.showInformationMessage(expectedMessage, ...prompts)).never();
});
test('Do not show notification if notification is disabled', async () => {
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
when(terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource)).thenReturn(false);
when(notificationEnabled.value).thenReturn(false);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
await terminalEnvVarCollectionPrompt.activate();
terminalEventEmitter.fire(terminal);
await sleep(1);
verify(shell.showInformationMessage(expectedMessage, ...prompts)).never();
});
test('Do not show notification when a new terminal is opened for which there is prompt set', async () => {
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
when(terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource)).thenReturn(true);
when(notificationEnabled.value).thenReturn(true);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
await terminalEnvVarCollectionPrompt.activate();
terminalEventEmitter.fire(terminal);
await sleep(1);
verify(shell.showInformationMessage(expectedMessage, ...prompts)).never();
});
test("Disable notification if `Don't show again` is clicked", async () => {
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
when(terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource)).thenReturn(false);
when(notificationEnabled.value).thenReturn(true);
when(notificationEnabled.updateValue(false)).thenResolve();
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenReturn(
Promise.resolve(Common.doNotShowAgain),
);
await terminalEnvVarCollectionPrompt.activate();
terminalEventEmitter.fire(terminal);
await sleep(1);
verify(notificationEnabled.updateValue(false)).once();
});
test('Do not disable notification if prompt is closed', async () => {
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
when(terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource)).thenReturn(false);
when(notificationEnabled.value).thenReturn(true);
when(notificationEnabled.updateValue(false)).thenResolve();
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenReturn(Promise.resolve(undefined));
await terminalEnvVarCollectionPrompt.activate();
terminalEventEmitter.fire(terminal);
await sleep(1);
verify(notificationEnabled.updateValue(false)).never();
});
});