-
Notifications
You must be signed in to change notification settings - Fork 31.3k
/
Copy pathterminalEnvironment.test.ts
222 lines (217 loc) · 13.7 KB
/
terminalEnvironment.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
215
216
217
218
219
220
221
222
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { deepStrictEqual, ok, strictEqual } from 'assert';
import { homedir, userInfo } from 'os';
import { isWindows } from 'vs/base/common/platform';
import { NullLogService } from 'vs/platform/log/common/log';
import { IProductService } from 'vs/platform/product/common/productService';
import { ITerminalProcessOptions } from 'vs/platform/terminal/common/terminal';
import { getShellIntegrationInjection, getWindowsBuildNumber, IShellIntegrationConfigInjection } from 'vs/platform/terminal/node/terminalEnvironment';
const enabledProcessOptions: ITerminalProcessOptions = { shellIntegration: { enabled: true, suggestEnabled: false }, windowsEnableConpty: true, environmentVariableCollections: undefined, workspaceFolder: undefined };
const disabledProcessOptions: ITerminalProcessOptions = { shellIntegration: { enabled: false, suggestEnabled: false }, windowsEnableConpty: true, environmentVariableCollections: undefined, workspaceFolder: undefined };
const winptyProcessOptions: ITerminalProcessOptions = { shellIntegration: { enabled: true, suggestEnabled: false }, windowsEnableConpty: false, environmentVariableCollections: undefined, workspaceFolder: undefined };
const pwshExe = process.platform === 'win32' ? 'pwsh.exe' : 'pwsh';
const repoRoot = process.platform === 'win32' ? process.cwd()[0].toLowerCase() + process.cwd().substring(1) : process.cwd();
const logService = new NullLogService();
const productService = { applicationName: 'vscode' } as IProductService;
const defaultEnvironment = {};
suite('platform - terminalEnvironment', () => {
suite('getShellIntegrationInjection', () => {
suite('should not enable', () => {
// This test is only expected to work on Windows 10 build 18309 and above
(getWindowsBuildNumber() < 18309 ? test.skip : test)('when isFeatureTerminal or when no executable is provided', () => {
ok(!getShellIntegrationInjection({ executable: pwshExe, args: ['-l', '-NoLogo'], isFeatureTerminal: true }, enabledProcessOptions, defaultEnvironment, logService, productService));
ok(getShellIntegrationInjection({ executable: pwshExe, args: ['-l', '-NoLogo'], isFeatureTerminal: false }, enabledProcessOptions, defaultEnvironment, logService, productService));
});
if (isWindows) {
test('when on windows with conpty false', () => {
ok(!getShellIntegrationInjection({ executable: pwshExe, args: ['-l'], isFeatureTerminal: false }, winptyProcessOptions, defaultEnvironment, logService, productService));
});
}
});
// These tests are only expected to work on Windows 10 build 18309 and above
(getWindowsBuildNumber() < 18309 ? suite.skip : suite)('pwsh', () => {
const expectedPs1 = process.platform === 'win32'
? `try { . "${repoRoot}\\out\\vs\\workbench\\contrib\\terminal\\browser\\media\\shellIntegration.ps1" } catch {}`
: `. "${repoRoot}/out/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1"`;
suite('should override args', () => {
const enabledExpectedResult = Object.freeze<IShellIntegrationConfigInjection>({
newArgs: [
'-noexit',
'-command',
expectedPs1
],
envMixin: {
VSCODE_INJECTION: '1'
}
});
test('when undefined, []', () => {
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: [] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: undefined }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
});
suite('when no logo', () => {
test('array - case insensitive', () => {
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: ['-NoLogo'] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: ['-NOLOGO'] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: ['-nol'] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: ['-NOL'] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
});
test('string - case insensitive', () => {
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: '-NoLogo' }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: '-NOLOGO' }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: '-nol' }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: '-NOL' }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
});
});
});
suite('should incorporate login arg', () => {
const enabledExpectedResult = Object.freeze<IShellIntegrationConfigInjection>({
newArgs: [
'-l',
'-noexit',
'-command',
expectedPs1
],
envMixin: {
VSCODE_INJECTION: '1'
}
});
test('when array contains no logo and login', () => {
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: ['-l', '-NoLogo'] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
});
test('when string', () => {
deepStrictEqual(getShellIntegrationInjection({ executable: pwshExe, args: '-l' }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
});
});
suite('should not modify args', () => {
test('when shell integration is disabled', () => {
strictEqual(getShellIntegrationInjection({ executable: pwshExe, args: ['-l'] }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
strictEqual(getShellIntegrationInjection({ executable: pwshExe, args: '-l' }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
strictEqual(getShellIntegrationInjection({ executable: pwshExe, args: undefined }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
});
test('when using unrecognized arg', () => {
strictEqual(getShellIntegrationInjection({ executable: pwshExe, args: ['-l', '-NoLogo', '-i'] }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
});
test('when using unrecognized arg (string)', () => {
strictEqual(getShellIntegrationInjection({ executable: pwshExe, args: '-i' }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
});
});
});
if (process.platform !== 'win32') {
suite('zsh', () => {
suite('should override args', () => {
const username = userInfo().username;
const expectedDir = new RegExp(`.+\/${username}-vscode-zsh`);
const customZdotdir = '/custom/zsh/dotdir';
const expectedDests = [
new RegExp(`.+\\/${username}-vscode-zsh\\/\\.zshrc`),
new RegExp(`.+\\/${username}-vscode-zsh\\/\\.zprofile`),
new RegExp(`.+\\/${username}-vscode-zsh\\/\\.zshenv`),
new RegExp(`.+\\/${username}-vscode-zsh\\/\\.zlogin`)
];
const expectedSources = [
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-rc.zsh/,
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-profile.zsh/,
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-env.zsh/,
/.+\/out\/vs\/workbench\/contrib\/terminal\/browser\/media\/shellIntegration-login.zsh/
];
function assertIsEnabled(result: IShellIntegrationConfigInjection, globalZdotdir = homedir()) {
strictEqual(Object.keys(result.envMixin!).length, 3);
ok(result.envMixin!['ZDOTDIR']?.match(expectedDir));
strictEqual(result.envMixin!['USER_ZDOTDIR'], globalZdotdir);
ok(result.envMixin!['VSCODE_INJECTION']?.match('1'));
strictEqual(result.filesToCopy?.length, 4);
ok(result.filesToCopy[0].dest.match(expectedDests[0]));
ok(result.filesToCopy[1].dest.match(expectedDests[1]));
ok(result.filesToCopy[2].dest.match(expectedDests[2]));
ok(result.filesToCopy[3].dest.match(expectedDests[3]));
ok(result.filesToCopy[0].source.match(expectedSources[0]));
ok(result.filesToCopy[1].source.match(expectedSources[1]));
ok(result.filesToCopy[2].source.match(expectedSources[2]));
ok(result.filesToCopy[3].source.match(expectedSources[3]));
}
test('when undefined, []', () => {
const result1 = getShellIntegrationInjection({ executable: 'zsh', args: [] }, enabledProcessOptions, defaultEnvironment, logService, productService);
deepStrictEqual(result1?.newArgs, ['-i']);
assertIsEnabled(result1);
const result2 = getShellIntegrationInjection({ executable: 'zsh', args: undefined }, enabledProcessOptions, defaultEnvironment, logService, productService);
deepStrictEqual(result2?.newArgs, ['-i']);
assertIsEnabled(result2);
});
suite('should incorporate login arg', () => {
test('when array', () => {
const result = getShellIntegrationInjection({ executable: 'zsh', args: ['-l'] }, enabledProcessOptions, defaultEnvironment, logService, productService);
deepStrictEqual(result?.newArgs, ['-il']);
assertIsEnabled(result);
});
});
suite('should not modify args', () => {
test('when shell integration is disabled', () => {
strictEqual(getShellIntegrationInjection({ executable: 'zsh', args: ['-l'] }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
strictEqual(getShellIntegrationInjection({ executable: 'zsh', args: undefined }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
});
test('when using unrecognized arg', () => {
strictEqual(getShellIntegrationInjection({ executable: 'zsh', args: ['-l', '-fake'] }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
});
});
suite('should incorporate global ZDOTDIR env variable', () => {
test('when custom ZDOTDIR', () => {
const result1 = getShellIntegrationInjection({ executable: 'zsh', args: [] }, enabledProcessOptions, { ...defaultEnvironment, ZDOTDIR: customZdotdir }, logService, productService);
deepStrictEqual(result1?.newArgs, ['-i']);
assertIsEnabled(result1, customZdotdir);
});
test('when undefined', () => {
const result1 = getShellIntegrationInjection({ executable: 'zsh', args: [] }, enabledProcessOptions, undefined, logService, productService);
deepStrictEqual(result1?.newArgs, ['-i']);
assertIsEnabled(result1);
});
});
});
});
suite('bash', () => {
suite('should override args', () => {
test('when undefined, [], empty string', () => {
const enabledExpectedResult = Object.freeze<IShellIntegrationConfigInjection>({
newArgs: [
'--init-file',
`${repoRoot}/out/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh`
],
envMixin: {
VSCODE_INJECTION: '1'
}
});
deepStrictEqual(getShellIntegrationInjection({ executable: 'bash', args: [] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: 'bash', args: '' }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
deepStrictEqual(getShellIntegrationInjection({ executable: 'bash', args: undefined }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
});
suite('should set login env variable and not modify args', () => {
const enabledExpectedResult = Object.freeze<IShellIntegrationConfigInjection>({
newArgs: [
'--init-file',
`${repoRoot}/out/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh`
],
envMixin: {
VSCODE_INJECTION: '1',
VSCODE_SHELL_LOGIN: '1'
}
});
test('when array', () => {
deepStrictEqual(getShellIntegrationInjection({ executable: 'bash', args: ['-l'] }, enabledProcessOptions, defaultEnvironment, logService, productService), enabledExpectedResult);
});
});
suite('should not modify args', () => {
test('when shell integration is disabled', () => {
strictEqual(getShellIntegrationInjection({ executable: 'bash', args: ['-l'] }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
strictEqual(getShellIntegrationInjection({ executable: 'bash', args: undefined }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
});
test('when custom array entry', () => {
strictEqual(getShellIntegrationInjection({ executable: 'bash', args: ['-l', '-i'] }, disabledProcessOptions, defaultEnvironment, logService, productService), undefined);
});
});
});
});
}
});
});