forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.format.test.ts
More file actions
205 lines (176 loc) · 8.28 KB
/
extension.format.test.ts
File metadata and controls
205 lines (176 loc) · 8.28 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as fs from 'fs-extra';
import * as path from 'path';
import { CancellationTokenSource, Position, Uri, window, workspace } from 'vscode';
import { IProcessServiceFactory } from '../../client/common/process/types';
import { AutoPep8Formatter } from '../../client/formatters/autoPep8Formatter';
import { BlackFormatter } from '../../client/formatters/blackFormatter';
import { YapfFormatter } from '../../client/formatters/yapfFormatter';
import { isPythonVersionInProcess } from '../common';
import { closeActiveWindows, initialize, initializeTest } from '../initialize';
import { MockProcessService } from '../mocks/proc';
import { registerForIOC } from '../pythonEnvironments/legacyIOC';
import { UnitTestIocContainer } from '../testing/serviceRegistry';
import { compareFiles } from '../textUtils';
const ch = window.createOutputChannel('Tests');
const formatFilesPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'formatting');
const workspaceRootPath = path.join(__dirname, '..', '..', '..', 'src', 'test');
const originalUnformattedFile = path.join(formatFilesPath, 'fileToFormat.py');
const autoPep8FileToFormat = path.join(formatFilesPath, 'autoPep8FileToFormat.py');
const autoPep8Formatted = path.join(formatFilesPath, 'autoPep8Formatted.py');
const blackFileToFormat = path.join(formatFilesPath, 'blackFileToFormat.py');
const blackFormatted = path.join(formatFilesPath, 'blackFormatted.py');
const yapfFileToFormat = path.join(formatFilesPath, 'yapfFileToFormat.py');
const yapfFormatted = path.join(formatFilesPath, 'yapfFormatted.py');
let formattedYapf = '';
let formattedBlack = '';
let formattedAutoPep8 = '';
suite('Formatting - General', () => {
let ioc: UnitTestIocContainer;
suiteSetup(async function () {
// https://github.com/microsoft/vscode-python/issues/12564
// Skipping one test in the file is resulting in the next one failing, so skipping the entire suiteuntil further investigation.
return this.skip();
await initialize();
await initializeDI();
[autoPep8FileToFormat, blackFileToFormat, yapfFileToFormat].forEach((file) => {
fs.copySync(originalUnformattedFile, file, { overwrite: true });
});
formattedYapf = fs.readFileSync(yapfFormatted).toString();
formattedAutoPep8 = fs.readFileSync(autoPep8Formatted).toString();
formattedBlack = fs.readFileSync(blackFormatted).toString();
});
async function formattingTestIsBlackSupported(): Promise<boolean> {
const processService = await ioc.serviceContainer
.get<IProcessServiceFactory>(IProcessServiceFactory)
.create(Uri.file(workspaceRootPath));
return !(await isPythonVersionInProcess(processService, '2', '3.0', '3.1', '3.2', '3.3', '3.4', '3.5'));
}
setup(async () => {
await initializeTest();
await initializeDI();
});
suiteTeardown(async () => {
[autoPep8FileToFormat, blackFileToFormat, yapfFileToFormat].forEach((file) => {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
});
ch.dispose();
await closeActiveWindows();
});
teardown(async () => {
await ioc.dispose();
});
async function initializeDI() {
ioc = new UnitTestIocContainer();
ioc.registerCommonTypes();
ioc.registerVariableTypes();
ioc.registerUnitTestTypes();
ioc.registerFormatterTypes();
ioc.registerInterpreterStorageTypes();
// Mocks.
ioc.registerMockProcessTypes();
await ioc.registerMockInterpreterTypes();
await registerForIOC(ioc.serviceManager, ioc.serviceContainer);
}
async function injectFormatOutput(outputFileName: string) {
const procService = (await ioc.serviceContainer
.get<IProcessServiceFactory>(IProcessServiceFactory)
.create()) as MockProcessService;
procService.onExecObservable((_file, args, _options, callback) => {
if (args.indexOf('--diff') >= 0) {
callback({
out: fs.readFileSync(path.join(formatFilesPath, outputFileName), 'utf8'),
source: 'stdout',
});
}
});
}
async function testFormatting(
formatter: AutoPep8Formatter | BlackFormatter | YapfFormatter,
formattedContents: string,
fileToFormat: string,
outputFileName: string,
) {
const textDocument = await workspace.openTextDocument(fileToFormat);
const textEditor = await window.showTextDocument(textDocument);
const options = {
insertSpaces: textEditor.options.insertSpaces! as boolean,
tabSize: textEditor.options.tabSize! as number,
};
await injectFormatOutput(outputFileName);
const edits = await formatter.formatDocument(textDocument, options, new CancellationTokenSource().token);
await textEditor.edit((editBuilder) => {
edits.forEach((edit) => editBuilder.replace(edit.range, edit.newText));
});
compareFiles(formattedContents, textEditor.document.getText());
}
test('AutoPep8', async function () {
// https://github.com/microsoft/vscode-python/issues/12564
return this.skip();
await testFormatting(
new AutoPep8Formatter(ioc.serviceContainer),
formattedAutoPep8,
autoPep8FileToFormat,
'autopep8.output',
);
});
test('Black', async function () {
// https://github.com/microsoft/vscode-python/issues/12564
return this.skip();
if (!(await formattingTestIsBlackSupported())) {
// Skip for versions of python below 3.6, as Black doesn't support them at all.
return this.skip();
}
await testFormatting(
new BlackFormatter(ioc.serviceContainer),
formattedBlack,
blackFileToFormat,
'black.output',
);
});
test('Yapf', async () =>
testFormatting(new YapfFormatter(ioc.serviceContainer), formattedYapf, yapfFileToFormat, 'yapf.output'));
test('Yapf on dirty file', async () => {
const sourceDir = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'formatting');
const targetDir = path.join(__dirname, '..', 'pythonFiles', 'formatting');
const originalName = 'formatWhenDirty.py';
const resultsName = 'formatWhenDirtyResult.py';
const fileToFormat = path.join(targetDir, originalName);
const formattedFile = path.join(targetDir, resultsName);
if (!fs.pathExistsSync(targetDir)) {
fs.mkdirpSync(targetDir);
}
fs.copySync(path.join(sourceDir, originalName), fileToFormat, { overwrite: true });
fs.copySync(path.join(sourceDir, resultsName), formattedFile, { overwrite: true });
const textDocument = await workspace.openTextDocument(fileToFormat);
const textEditor = await window.showTextDocument(textDocument);
await textEditor.edit((builder) => {
// Make file dirty. Trailing blanks will be removed.
builder.insert(new Position(0, 0), '\n \n');
});
const dir = path.dirname(fileToFormat);
const configFile = path.join(dir, '.style.yapf');
try {
// Create yapf configuration file
const content = '[style]\nbased_on_style = pep8\nindent_width=5\n';
fs.writeFileSync(configFile, content);
const options = { insertSpaces: textEditor.options.insertSpaces! as boolean, tabSize: 1 };
const formatter = new YapfFormatter(ioc.serviceContainer);
const edits = await formatter.formatDocument(textDocument, options, new CancellationTokenSource().token);
await textEditor.edit((editBuilder) => {
edits.forEach((edit) => editBuilder.replace(edit.range, edit.newText));
});
const expected = fs.readFileSync(formattedFile).toString();
const actual = textEditor.document.getText();
compareFiles(expected, actual);
} finally {
if (fs.existsSync(configFile)) {
fs.unlinkSync(configFile);
}
}
});
});