Skip to content

Commit fd9ec10

Browse files
authored
Fix formatting tests (#10801)
* Only my branch * Use preformatted files to compare output * Update autopep8 diff * . * Restrict tests * More tweaks * Few more tweaks * Revert "Restrict tests" This reverts commit f7baf78. * Revert "Only my branch" This reverts commit c52d19f.
1 parent cfe1fd5 commit fd9ec10

5 files changed

Lines changed: 126 additions & 31 deletions

File tree

src/test/format/extension.format.test.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as fs from 'fs-extra';
66
import * as path from 'path';
77
import { CancellationTokenSource, Position, Uri, window, workspace } from 'vscode';
8-
import { IProcessServiceFactory, IPythonExecutionFactory } from '../../client/common/process/types';
8+
import { IProcessServiceFactory } from '../../client/common/process/types';
99
import { AutoPep8Formatter } from '../../client/formatters/autoPep8Formatter';
1010
import { BlackFormatter } from '../../client/formatters/blackFormatter';
1111
import { YapfFormatter } from '../../client/formatters/yapfFormatter';
@@ -27,9 +27,11 @@ const workspaceRootPath = path.join(__dirname, '..', '..', '..', 'src', 'test');
2727
const originalUnformattedFile = path.join(formatFilesPath, 'fileToFormat.py');
2828

2929
const autoPep8FileToFormat = path.join(formatFilesPath, 'autoPep8FileToFormat.py');
30+
const autoPep8Formatted = path.join(formatFilesPath, 'autoPep8Formatted.py');
3031
const blackFileToFormat = path.join(formatFilesPath, 'blackFileToFormat.py');
31-
const blackReferenceFile = path.join(formatFilesPath, 'blackFileReference.py');
32+
const blackFormatted = path.join(formatFilesPath, 'blackFormatted.py');
3233
const yapfFileToFormat = path.join(formatFilesPath, 'yapfFileToFormat.py');
34+
const yapfFormatted = path.join(formatFilesPath, 'yapfFormatted.py');
3335

3436
let formattedYapf = '';
3537
let formattedBlack = '';
@@ -42,31 +44,12 @@ suite('Formatting - General', () => {
4244
suiteSetup(async () => {
4345
await initialize();
4446
initializeDI();
45-
[autoPep8FileToFormat, blackFileToFormat, blackReferenceFile, yapfFileToFormat].forEach(file => {
47+
[autoPep8FileToFormat, blackFileToFormat, yapfFileToFormat].forEach(file => {
4648
fs.copySync(originalUnformattedFile, file, { overwrite: true });
4749
});
48-
fs.ensureDirSync(path.dirname(autoPep8FileToFormat));
49-
const pythonProcess = await ioc.serviceContainer
50-
.get<IPythonExecutionFactory>(IPythonExecutionFactory)
51-
.create({ resource: Uri.file(workspaceRootPath) });
52-
const yapf = pythonProcess.execModule('yapf', [originalUnformattedFile], { cwd: workspaceRootPath });
53-
const autoPep8 = pythonProcess.execModule('autopep8', [originalUnformattedFile], { cwd: workspaceRootPath });
54-
const formatters = [yapf, autoPep8];
55-
if (await formattingTestIsBlackSupported()) {
56-
// Black doesn't support emitting only to stdout; it either works
57-
// through a pipe, emits a diff, or rewrites the file in-place.
58-
// Thus it's easier to let it do its in-place rewrite and then
59-
// read the reference file from there.
60-
const black = pythonProcess.execModule('black', [blackReferenceFile], { cwd: workspaceRootPath });
61-
formatters.push(black);
62-
}
63-
await Promise.all(formatters).then(async formattedResults => {
64-
formattedYapf = formattedResults[0].stdout;
65-
formattedAutoPep8 = formattedResults[1].stdout;
66-
if (await formattingTestIsBlackSupported()) {
67-
formattedBlack = fs.readFileSync(blackReferenceFile).toString();
68-
}
69-
});
50+
formattedYapf = fs.readFileSync(yapfFormatted).toString();
51+
formattedAutoPep8 = fs.readFileSync(autoPep8Formatted).toString();
52+
formattedBlack = fs.readFileSync(blackFormatted).toString();
7053
});
7154

7255
async function formattingTestIsBlackSupported(): Promise<boolean> {
@@ -81,7 +64,7 @@ suite('Formatting - General', () => {
8164
initializeDI();
8265
});
8366
suiteTeardown(async () => {
84-
[autoPep8FileToFormat, blackFileToFormat, blackReferenceFile, yapfFileToFormat].forEach(file => {
67+
[autoPep8FileToFormat, blackFileToFormat, yapfFileToFormat].forEach(file => {
8568
if (fs.existsSync(file)) {
8669
fs.unlinkSync(file);
8770
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import math
2+
import sys
3+
4+
5+
def example1():
6+
# This is a long comment. This should be wrapped to fit within 72 characters.
7+
some_tuple = (1, 2, 3, 'a')
8+
some_variable = {'long': 'Long code lines should be wrapped within 79 characters.',
9+
'other': [math.pi, 100, 200, 300, 9876543210, 'This is a long string that goes on'],
10+
'more': {'inner': 'This whole logical line should be wrapped.', some_tuple: [1,
11+
20, 300, 40000, 500000000, 60000000000000000]}}
12+
return (some_tuple, some_variable)
13+
14+
15+
def example2(): return {'has_key() is deprecated': True}.has_key(
16+
{'f': 2}.has_key(''))
17+
18+
19+
class Example3(object):
20+
def __init__(self, bar):
21+
# Comments should have a space after the hash.
22+
if bar:
23+
bar += 1
24+
bar = bar * bar
25+
return bar
26+
else:
27+
some_string = """
28+
Indentation in multiline strings should not be touched.
29+
Only actual code should be reindented.
30+
"""
31+
return (sys.path, some_string)

src/test/pythonFiles/formatting/autopep8.output

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
--- original//Users/donjayamanne/.vscode/extensions/pythonVSCode/src/test/pythonFiles/formatting/autoPep8FileToFormat.py
2-
+++ fixed//Users/donjayamanne/.vscode/extensions/pythonVSCode/src/test/pythonFiles/formatting/autoPep8FileToFormat.py
1+
--- original/.\src\test\pythonFiles\formatting\autoPep8FileToFormat.py
2+
+++ fixed/.\src\test\pythonFiles\formatting\autoPep8FileToFormat.py
33
@@ -1,21 +1,31 @@
44
-import math, sys;
55
+import math
66
+import sys
77
+
8-
8+
99
def example1():
1010
- ####This is a long comment. This should be wrapped to fit within 72 characters.
1111
- some_tuple=( 1,2, 3,'a' );
@@ -30,7 +30,7 @@
3030
+
3131
+
3232
+def example2(): return {'has_key() is deprecated': True}.has_key(
33-
+ {'f': 2}.has_key(''));
33+
+ {'f': 2}.has_key(''))
3434
+
3535
+
3636
+class Example3(object):
@@ -46,4 +46,4 @@
4646
Only actual code should be reindented.
4747
"""
4848
- return (sys.path, some_string)
49-
+ return (sys.path, some_string)
49+
+ return (sys.path, some_string)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import math, sys
2+
3+
4+
def example1():
5+
####This is a long comment. This should be wrapped to fit within 72 characters.
6+
some_tuple = (1, 2, 3, "a")
7+
some_variable = {
8+
"long": "Long code lines should be wrapped within 79 characters.",
9+
"other": [
10+
math.pi,
11+
100,
12+
200,
13+
300,
14+
9876543210,
15+
"This is a long string that goes on",
16+
],
17+
"more": {
18+
"inner": "This whole logical line should be wrapped.",
19+
some_tuple: [1, 20, 300, 40000, 500000000, 60000000000000000],
20+
},
21+
}
22+
return (some_tuple, some_variable)
23+
24+
25+
def example2():
26+
return {"has_key() is deprecated": True}.has_key({"f": 2}.has_key(""))
27+
28+
29+
class Example3(object):
30+
def __init__(self, bar):
31+
# Comments should have a space after the hash.
32+
if bar:
33+
bar += 1
34+
bar = bar * bar
35+
return bar
36+
else:
37+
some_string = """
38+
Indentation in multiline strings should not be touched.
39+
Only actual code should be reindented.
40+
"""
41+
return (sys.path, some_string)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import math, sys
2+
3+
4+
def example1():
5+
####This is a long comment. This should be wrapped to fit within 72 characters.
6+
some_tuple = (1, 2, 3, 'a')
7+
some_variable = {
8+
'long':
9+
'Long code lines should be wrapped within 79 characters.',
10+
'other': [
11+
math.pi, 100, 200, 300, 9876543210,
12+
'This is a long string that goes on'
13+
],
14+
'more': {
15+
'inner': 'This whole logical line should be wrapped.',
16+
some_tuple: [1, 20, 300, 40000, 500000000, 60000000000000000]
17+
}
18+
}
19+
return (some_tuple, some_variable)
20+
21+
22+
def example2():
23+
return {
24+
'has_key() is deprecated': True
25+
}.has_key({'f': 2}.has_key(''))
26+
27+
28+
class Example3(object):
29+
def __init__(self, bar):
30+
#Comments should have a space after the hash.
31+
if bar:
32+
bar += 1
33+
bar = bar * bar
34+
return bar
35+
else:
36+
some_string = """
37+
Indentation in multiline strings should not be touched.
38+
Only actual code should be reindented.
39+
"""
40+
return (sys.path, some_string)

0 commit comments

Comments
 (0)