Repository navigation
Expand file tree
/
Copy pathhelper.test.ts
More file actions
267 lines (234 loc) · 14.9 KB
/
Copy pathhelper.test.ts
File metadata and controls
267 lines (234 loc) · 14.9 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import * as fs from 'fs-extra';
import { EOL } from 'os';
import * as path from 'path';
import * as TypeMoq from 'typemoq';
import { Range, Selection, TextDocument, TextEditor, TextLine, Uri } from 'vscode';
import { IApplicationShell, IDocumentManager } from '../../../client/common/application/types';
import { EXTENSION_ROOT_DIR, PYTHON_LANGUAGE } from '../../../client/common/constants';
import '../../../client/common/extensions';
import { BufferDecoder } from '../../../client/common/process/decoder';
import { ProcessService } from '../../../client/common/process/proc';
import { IProcessService, IProcessServiceFactory } from '../../../client/common/process/types';
import { IConfigurationService, IPythonSettings } from '../../../client/common/types';
import { OSType } from '../../../client/common/utils/platform';
import { IEnvironmentVariablesProvider } from '../../../client/common/variables/types';
import { IServiceContainer } from '../../../client/ioc/types';
import { CodeExecutionHelper } from '../../../client/terminals/codeExecution/helper';
import { ICodeExecutionHelper } from '../../../client/terminals/types';
import { isOs, isPythonVersion, PYTHON_PATH } from '../../common';
const TEST_FILES_PATH = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'terminalExec');
// tslint:disable-next-line:max-func-body-length
suite('Terminal - Code Execution Helper', () => {
let documentManager: TypeMoq.IMock<IDocumentManager>;
let applicationShell: TypeMoq.IMock<IApplicationShell>;
let helper: ICodeExecutionHelper;
let document: TypeMoq.IMock<TextDocument>;
let editor: TypeMoq.IMock<TextEditor>;
let processService: TypeMoq.IMock<IProcessService>;
let configService: TypeMoq.IMock<IConfigurationService>;
setup(() => {
const serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
applicationShell = TypeMoq.Mock.ofType<IApplicationShell>();
const envVariablesProvider = TypeMoq.Mock.ofType<IEnvironmentVariablesProvider>();
processService = TypeMoq.Mock.ofType<IProcessService>();
configService = TypeMoq.Mock.ofType<IConfigurationService>();
const pythonSettings = TypeMoq.Mock.ofType<IPythonSettings>();
pythonSettings.setup(p => p.pythonPath).returns(() => PYTHON_PATH);
// tslint:disable-next-line:no-any
processService.setup((x: any) => x.then).returns(() => undefined);
configService.setup(c => c.getSettings(TypeMoq.It.isAny())).returns(() => pythonSettings.object);
envVariablesProvider.setup(e => e.getEnvironmentVariables(TypeMoq.It.isAny())).returns(() => Promise.resolve({}));
const processServiceFactory = TypeMoq.Mock.ofType<IProcessServiceFactory>();
processServiceFactory.setup(p => p.create(TypeMoq.It.isAny())).returns(() => Promise.resolve(processService.object));
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IProcessServiceFactory), TypeMoq.It.isAny())).returns(() => processServiceFactory.object);
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IDocumentManager), TypeMoq.It.isAny())).returns(() => documentManager.object);
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IApplicationShell), TypeMoq.It.isAny())).returns(() => applicationShell.object);
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IEnvironmentVariablesProvider), TypeMoq.It.isAny())).returns(() => envVariablesProvider.object);
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IConfigurationService), TypeMoq.It.isAny())).returns(() => configService.object);
helper = new CodeExecutionHelper(serviceContainer.object);
document = TypeMoq.Mock.ofType<TextDocument>();
editor = TypeMoq.Mock.ofType<TextEditor>();
editor.setup(e => e.document).returns(() => document.object);
});
async function ensureBlankLinesAreRemoved(source: string, expectedSource: string) {
const actualProcessService = new ProcessService(new BufferDecoder());
processService.setup(p => p.exec(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => {
return actualProcessService.exec.apply(actualProcessService, [file, args, options]);
});
const normalizedZCode = await helper.normalizeLines(source);
// In case file has been saved with different line endings.
expectedSource = expectedSource.splitLines({ removeEmptyEntries: false, trim: false }).join(EOL);
expect(normalizedZCode).to.be.equal(expectedSource);
}
test('Ensure blank lines are NOT removed when code is not indented (simple)', async function () {
// This test has not been working for many months in Python 2.7 under
// Windows.Tracked by #2544.
if (isOs(OSType.Windows) && await isPythonVersion('2.7')) {
// tslint:disable-next-line:no-invalid-this
return this.skip();
}
const code = ['import sys', '', '', '', 'print(sys.executable)', '', 'print("1234")', '', '', 'print(1)', 'print(2)'];
const expectedCode = code.filter(line => line.trim().length > 0).join(EOL);
await ensureBlankLinesAreRemoved(code.join(EOL), expectedCode);
});
test('Ensure there are no multiple-CR elements in the normalized code.', async () => {
const code = ['import sys', '', '', '', 'print(sys.executable)', '', 'print("1234")', '', '', 'print(1)', 'print(2)'];
const actualProcessService = new ProcessService(new BufferDecoder());
processService.setup(p => p.exec(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => {
return actualProcessService.exec.apply(actualProcessService, [file, args, options]);
});
const normalizedCode = await helper.normalizeLines(code.join(EOL));
const doubleCrIndex = normalizedCode.indexOf('\r\r');
expect(doubleCrIndex).to.be.equal(-1, 'Double CR (CRCRLF) line endings detected in normalized code snippet.');
});
['', '1', '2', '3', '4', '5', '6', '7', '8'].forEach(fileNameSuffix => {
test(`Ensure blank lines are removed (Sample${fileNameSuffix})`, async function () {
// This test has not been working for many months in Python 2.7 under
// Windows.Tracked by #2544.
if (isOs(OSType.Windows) && await isPythonVersion('2.7')) {
// tslint:disable-next-line:no-invalid-this
return this.skip();
}
const code = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_raw.py`), 'utf8');
const expectedCode = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_normalized.py`), 'utf8');
await ensureBlankLinesAreRemoved(code, expectedCode);
});
test(`Ensure last two blank lines are preserved (Sample${fileNameSuffix})`, async function () {
// This test has not been working for many months in Python 2.7 under
// Windows.Tracked by #2544.
if (isOs(OSType.Windows) && await isPythonVersion('2.7')) {
// tslint:disable-next-line:no-invalid-this
return this.skip();
}
const code = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_raw.py`), 'utf8');
const expectedCode = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_normalized.py`), 'utf8');
await ensureBlankLinesAreRemoved(code + EOL, expectedCode + EOL);
});
test(`Ensure last two blank lines are preserved even if we have more than 2 trailing blank lines (Sample${fileNameSuffix})`, async function () {
// This test has not been working for many months in Python 2.7 under
// Windows.Tracked by #2544.
if (isOs(OSType.Windows) && await isPythonVersion('2.7')) {
// tslint:disable-next-line:no-invalid-this
return this.skip();
}
const code = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_raw.py`), 'utf8');
const expectedCode = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_normalized.py`), 'utf8');
await ensureBlankLinesAreRemoved(code + EOL + EOL + EOL + EOL, expectedCode + EOL);
});
});
test('Display message if there\s no active file', async () => {
documentManager.setup(doc => doc.activeTextEditor).returns(() => undefined);
const uri = await helper.getFileToExecute();
expect(uri).to.be.an('undefined');
applicationShell.verify(a => a.showErrorMessage(TypeMoq.It.isAnyString()), TypeMoq.Times.once());
});
test('Display message if active file is unsaved', async () => {
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
document.setup(doc => doc.isUntitled).returns(() => true);
const uri = await helper.getFileToExecute();
expect(uri).to.be.an('undefined');
applicationShell.verify(a => a.showErrorMessage(TypeMoq.It.isAnyString()), TypeMoq.Times.once());
});
test('Display message if active file is non-python', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.languageId).returns(() => 'html');
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.an('undefined');
applicationShell.verify(a => a.showErrorMessage(TypeMoq.It.isAnyString()), TypeMoq.Times.once());
});
test('Returns file uri', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.languageId).returns(() => PYTHON_LANGUAGE);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
});
test('Returns file uri even if saving fails', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => true);
document.setup(doc => doc.languageId).returns(() => PYTHON_LANGUAGE);
document.setup(doc => doc.save()).returns(() => Promise.resolve(false));
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
});
test('Dirty files are saved', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => true);
document.setup(doc => doc.languageId).returns(() => PYTHON_LANGUAGE);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
document.verify(doc => doc.save(), TypeMoq.Times.once());
});
test('Non-Dirty files are not-saved', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => false);
document.setup(doc => doc.languageId).returns(() => PYTHON_LANGUAGE);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
document.verify(doc => doc.save(), TypeMoq.Times.never());
});
test('Returns current line if nothing is selected', async () => {
const lineContents = 'Line Contents';
editor.setup(e => e.selection).returns(() => new Selection(3, 0, 3, 0));
const textLine = TypeMoq.Mock.ofType<TextLine>();
textLine.setup(t => t.text).returns(() => lineContents);
document.setup(d => d.lineAt(TypeMoq.It.isAny())).returns(() => textLine.object);
const content = await helper.getSelectedTextToExecute(editor.object);
expect(content).to.be.equal(lineContents);
});
test('Returns selected text', async () => {
const lineContents = 'Line Contents';
editor.setup(e => e.selection).returns(() => new Selection(3, 0, 10, 5));
const textLine = TypeMoq.Mock.ofType<TextLine>();
textLine.setup(t => t.text).returns(() => lineContents);
document.setup(d => d.getText(TypeMoq.It.isAny())).returns((r: Range) => `${r.start.line}.${r.start.character}.${r.end.line}.${r.end.character}`);
const content = await helper.getSelectedTextToExecute(editor.object);
expect(content).to.be.equal('3.0.10.5');
});
test('saveFileIfDirty will not fail if file is not opened', async () => {
documentManager.setup(d => d.textDocuments).returns(() => []).verifiable(TypeMoq.Times.once());
await helper.saveFileIfDirty(Uri.file(`${__filename}.py`));
documentManager.verifyAll();
});
test('File will be saved if file is dirty', async () => {
documentManager.setup(d => d.textDocuments).returns(() => [document.object]).verifiable(TypeMoq.Times.once());
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => true);
document.setup(doc => doc.languageId).returns(() => PYTHON_LANGUAGE);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
await helper.saveFileIfDirty(expectedUri);
documentManager.verifyAll();
document.verify(doc => doc.save(), TypeMoq.Times.once());
});
test('File will be not saved if file is not dirty', async () => {
documentManager.setup(d => d.textDocuments).returns(() => [document.object]).verifiable(TypeMoq.Times.once());
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => false);
document.setup(doc => doc.languageId).returns(() => PYTHON_LANGUAGE);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
await helper.saveFileIfDirty(expectedUri);
documentManager.verifyAll();
document.verify(doc => doc.save(), TypeMoq.Times.never());
});
});