forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.unit.test.ts
More file actions
211 lines (166 loc) · 10.3 KB
/
logger.unit.test.ts
File metadata and controls
211 lines (166 loc) · 10.3 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as path from 'path';
import * as sinon from 'sinon';
import * as TypeMoq from 'typemoq';
import { WorkspaceFolder } from 'vscode';
import { IWorkspaceService } from '../../../client/common/application/types';
import { ProcessLogger } from '../../../client/common/process/logger';
import { getOSType, OSType } from '../../../client/common/utils/platform';
import * as logging from '../../../client/logging';
import { untildify } from '../../../client/common/helpers';
suite('ProcessLogger suite', () => {
let workspaceService: TypeMoq.IMock<IWorkspaceService>;
let logger: ProcessLogger;
let traceLogStub: sinon.SinonStub;
suiteSetup(async () => {
workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
workspaceService
.setup((w) => w.workspaceFolders)
.returns(() => [({ uri: { fsPath: path.join('path', 'to', 'workspace') } } as unknown) as WorkspaceFolder]);
logger = new ProcessLogger(workspaceService.object);
});
setup(() => {
traceLogStub = sinon.stub(logging, 'traceLog');
});
teardown(() => {
sinon.restore();
});
test('Logger displays the process command, arguments and current working directory in the output channel', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess('test', ['--foo', '--bar'], options);
sinon.assert.calledWithExactly(traceLogStub, `> test --foo --bar`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger adds quotes around arguments if they contain spaces', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess('test', ['--foo', '--bar', 'import test'], options);
sinon.assert.calledWithExactly(traceLogStub, `> test --foo --bar "import test"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${path.join('debug', 'path')}`);
});
test('Logger preserves quotes around arguments if they contain spaces', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess('test', ['--foo', '--bar', '"import test"'], options);
sinon.assert.calledWithExactly(traceLogStub, `> test --foo --bar "import test"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${path.join('debug', 'path')}`);
});
test('Logger converts single quotes around arguments to double quotes if they contain spaces', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess('test', ['--foo', '--bar', "'import test'"], options);
sinon.assert.calledWithExactly(traceLogStub, `> test --foo --bar "import test"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${path.join('debug', 'path')}`);
});
test('Logger removes single quotes around arguments if they do not contain spaces', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess('test', ['--foo', '--bar', "'importtest'"], options);
sinon.assert.calledWithExactly(traceLogStub, `> test --foo --bar importtest`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${path.join('debug', 'path')}`);
});
test('Logger replaces the path/to/home with ~ in the current working directory', async () => {
const options = { cwd: path.join(untildify('~'), 'debug', 'path') };
logger.logProcess('test', ['--foo', '--bar'], options);
sinon.assert.calledWithExactly(traceLogStub, `> test --foo --bar`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${path.join('~', 'debug', 'path')}`);
});
test('Logger replaces the path/to/home with ~ in the command path where the home path IS at the beginning of the path', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess(path.join(untildify('~'), 'test'), ['--foo', '--bar'], options);
sinon.assert.calledWithExactly(traceLogStub, `> ${path.join('~', 'test')} --foo --bar`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger replaces the path/to/home with ~ in the command path where the home path IS at the beginning of the path but another arg contains other ref to home folder', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess(path.join(untildify('~'), 'test'), ['--foo', path.join(untildify('~'), 'boo')], options);
sinon.assert.calledWithExactly(traceLogStub, `> ${path.join('~', 'test')} --foo ${path.join('~', 'boo')}`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger replaces the path/to/home with ~ in the command path where the home path IS at the beginning of the path between doble quotes', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess(`"${path.join(untildify('~'), 'test')}" "--foo" "--bar"`, undefined, options);
sinon.assert.calledWithExactly(traceLogStub, `> "${path.join('~', 'test')}" "--foo" "--bar"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger replaces the path/to/home with ~ in the command path where the home path IS NOT at the beginning of the path', async () => {
const options = { cwd: path.join('debug', 'path') };
const untildifyStr = untildify('~');
let p1 = path.join('net', untildifyStr, 'test');
if (p1.startsWith('.')) {
if (getOSType() === OSType.Windows) {
p1 = p1.replace(/^\.\\+/, '');
} else {
p1 = p1.replace(/^\.\\/, '');
}
}
logger.logProcess(p1, ['--foo', '--bar'], options);
const path1 = path.join('.', 'net', '~', 'test');
sinon.assert.calledWithExactly(traceLogStub, `> ${path1} --foo --bar`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger replaces the path/to/home with ~ in the command path where the home path IS NOT at the beginning of the path but another arg contains other ref to home folder', async () => {
const options = { cwd: path.join('debug', 'path') };
let p1 = path.join('net', untildify('~'), 'test');
if (p1.startsWith('.')) {
if (getOSType() === OSType.Windows) {
p1 = p1.replace(/^\.\\+/, '');
} else {
p1 = p1.replace(/^\.\\/, '');
}
}
logger.logProcess(p1, ['--foo', path.join(untildify('~'), 'boo')], options);
sinon.assert.calledWithExactly(
traceLogStub,
`> ${path.join('.', 'net', '~', 'test')} --foo ${path.join('~', 'boo')}`,
);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger replaces the path/to/home with ~ in the command path where the home path IS NOT at the beginning of the path between doble quotes', async () => {
const options = { cwd: path.join('debug', 'path') };
let p1 = path.join('net', untildify('~'), 'test');
if (p1.startsWith('.')) {
if (getOSType() === OSType.Windows) {
p1 = p1.replace(/^\.\\+/, '');
} else {
p1 = p1.replace(/^\.\\/, '');
}
}
logger.logProcess(`"${p1}" "--foo" "--bar"`, undefined, options);
sinon.assert.calledWithExactly(traceLogStub, `> "${path.join('.', 'net', '~', 'test')}" "--foo" "--bar"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger replaces the path/to/home with ~ if shell command is provided', async () => {
const options = { cwd: path.join('debug', 'path') };
logger.logProcess(`"${path.join(untildify('~'), 'test')}" "--foo" "--bar"`, undefined, options);
sinon.assert.calledWithExactly(traceLogStub, `> "${path.join('~', 'test')}" "--foo" "--bar"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: ${options.cwd}`);
});
test('Logger replaces the path to workspace with . if exactly one workspace folder is opened', async () => {
const options = { cwd: path.join('path', 'to', 'workspace', 'debug', 'path') };
logger.logProcess(`"${path.join('path', 'to', 'workspace', 'test')}" "--foo" "--bar"`, undefined, options);
sinon.assert.calledWithExactly(traceLogStub, `> ".${path.sep}test" "--foo" "--bar"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: .${path.sep + path.join('debug', 'path')}`);
});
test('On Windows, logger replaces both backwards and forward slash version of path to workspace with . if exactly one workspace folder is opened', async function () {
if (getOSType() !== OSType.Windows) {
return this.skip();
}
let options = { cwd: path.join('path/to/workspace', 'debug', 'path') };
logger.logProcess(`"${path.join('path', 'to', 'workspace', 'test')}" "--foo" "--bar"`, undefined, options);
sinon.assert.calledWithExactly(traceLogStub, `> ".${path.sep}test" "--foo" "--bar"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: .${path.sep + path.join('debug', 'path')}`);
traceLogStub.resetHistory();
options = { cwd: path.join('path\\to\\workspace', 'debug', 'path') };
logger.logProcess(`"${path.join('path', 'to', 'workspace', 'test')}" "--foo" "--bar"`, undefined, options);
sinon.assert.calledWithExactly(traceLogStub, `> ".${path.sep}test" "--foo" "--bar"`);
sinon.assert.calledWithExactly(traceLogStub, `cwd: .${path.sep + path.join('debug', 'path')}`);
});
test("Logger doesn't display the working directory line if there is no options parameter", async () => {
logger.logProcess(path.join(untildify('~'), 'test'), ['--foo', '--bar']);
sinon.assert.calledWithExactly(traceLogStub, `> ${path.join('~', 'test')} --foo --bar`);
});
test("Logger doesn't display the working directory line if there is no cwd key in the options parameter", async () => {
const options = {};
logger.logProcess(path.join(untildify('~'), 'test'), ['--foo', '--bar'], options);
sinon.assert.calledWithExactly(traceLogStub, `> ${path.join('~', 'test')} --foo --bar`);
});
});