Skip to content

Commit dc35f2f

Browse files
committed
Fixed linting unit test errors
1 parent 3a51765 commit dc35f2f

3 files changed

Lines changed: 104 additions & 89 deletions

File tree

src/test/extension.format.test.ts

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,102 +19,102 @@ let pythonSettings = settings.PythonSettings.getInstance();
1919
let ch = vscode.window.createOutputChannel("Tests");
2020
let pythoFilesPath = path.join(__dirname, "..", "..", "src", "test", "pythonFiles", "formatting");
2121

22-
function closeActiveEditor() {
23-
if (vscode.window.activeTextEditor) {
24-
vscode.commands.executeCommand("workbench.action.closeActiveEditor");
25-
}
26-
}
2722
suite("Formatting", () => {
23+
setup(() => {
24+
return new Promise<any>(resolve => {
25+
setTimeout(function () {
26+
resolve();
27+
}, 1000);
28+
});
29+
});
30+
teardown(() => {
31+
if (vscode.window.activeTextEditor) {
32+
return vscode.commands.executeCommand("workbench.action.closeActiveEditor");
33+
}
34+
return Promise.resolve();
35+
});
2836
test("AutoPep8", done => {
2937
let fileToFormat = path.join(pythoFilesPath, "beforeAutoPep8.py");
30-
vscode.workspace.openTextDocument(fileToFormat).then(textDocument => {
31-
vscode.window.showTextDocument(textDocument).then(textEditor => {
32-
let formatter = new AutoPep8Formatter(ch, pythonSettings, pythoFilesPath);
33-
return formatter.formatDocument(textDocument, null, null).then(edits => {
34-
textEditor.edit(editBuilder => {
35-
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
36-
}).then(edited => {
37-
let formattedFile = path.join(pythoFilesPath, "afterAutoPep8.py");
38-
let formattedContents = fs.readFile(formattedFile, "utf-8", (error, data) => {
39-
if (error) {
40-
return assert.fail(error, "", "Failed to read formatted file");
41-
}
42-
assert.equal(textEditor.document.getText(), data, "Formatted text is not the same");
43-
});
44-
});
45-
}, error => {
46-
assert.fail(error, "", "Error in Formatting, " + error);
47-
});
48-
}, error => {
49-
assert.fail(error, "", "Error in Formatting, " + error);
38+
let textEditor: vscode.TextEditor;
39+
let textDocument: vscode.TextDocument;
40+
vscode.workspace.openTextDocument(fileToFormat).then(document => {
41+
textDocument = document;
42+
return vscode.window.showTextDocument(textDocument);
43+
}).then(editor => {
44+
textEditor = editor;
45+
let formatter = new AutoPep8Formatter(ch, pythonSettings, pythoFilesPath);
46+
return formatter.formatDocument(textDocument, null, null);
47+
}).then(edits => {
48+
return textEditor.edit(editBuilder => {
49+
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
5050
});
51-
}, error => {
52-
assert.fail(error, "", "Error in Opening Document, " + error);
53-
}).then(() => done(), done).then(() => closeActiveEditor(), closeActiveEditor);
51+
}).then(edited => {
52+
let formattedFile = path.join(pythoFilesPath, "afterAutoPep8.py");
53+
let formattedContents = fs.readFile(formattedFile, "utf-8", (error, data) => {
54+
if (error) {
55+
return assert.fail(error, "", "Failed to read formatted file");
56+
}
57+
assert.equal(textEditor.document.getText(), data, "Formatted text is not the same");
58+
});
59+
}).then(done, done);
5460
});
5561

5662
test("Yapf", done => {
5763
let fileToFormat = path.join(pythoFilesPath, "beforeYapf.py");
58-
vscode.workspace.openTextDocument(fileToFormat).then(textDocument => {
59-
vscode.window.showTextDocument(textDocument).then(textEditor => {
60-
let formatter = new YapfFormatter(ch, pythonSettings, pythoFilesPath);
61-
return formatter.formatDocument(textDocument, null, null).then(edits => {
62-
textEditor.edit(editBuilder => {
63-
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
64-
}).then(edited => {
65-
let formattedFile = path.join(pythoFilesPath, "afterYapf.py");
66-
let formattedContents = fs.readFile(formattedFile, "utf-8", (error, data) => {
67-
if (error) {
68-
return assert.fail(error, "", "Failed to read formatted file");
69-
}
70-
assert.equal(textEditor.document.getText(), data, "Formatted text is not the same");
71-
});
72-
});
73-
}, error => {
74-
assert.fail(error, "", "Error in Formatting, " + error);
75-
});
76-
}, error => {
77-
assert.fail(error, "", "Error in Formatting, " + error);
64+
let textEditor: vscode.TextEditor;
65+
let textDocument: vscode.TextDocument;
66+
vscode.workspace.openTextDocument(fileToFormat).then(document => {
67+
textDocument = document;
68+
return vscode.window.showTextDocument(textDocument);
69+
}).then(editor => {
70+
textEditor = editor;
71+
let formatter = new YapfFormatter(ch, pythonSettings, pythoFilesPath);
72+
return formatter.formatDocument(textDocument, null, null);
73+
}).then(edits => {
74+
return textEditor.edit(editBuilder => {
75+
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
76+
});
77+
}).then(edited => {
78+
let formattedFile = path.join(pythoFilesPath, "afterYapf.py");
79+
let formattedContents = fs.readFile(formattedFile, "utf-8", (error, data) => {
80+
if (error) {
81+
return assert.fail(error, "", "Failed to read formatted file");
82+
}
83+
var x = textEditor.document.getText();
84+
assert.equal(textEditor.document.getText(), data, "Formatted text is not the same");
7885
});
79-
}, error => {
80-
assert.fail(error, "", "Error in Opening Document, " + error);
81-
}).then(() => done(), done).then(() => closeActiveEditor(), closeActiveEditor);
86+
}).then(done, done);
8287
});
8388

8489
test("Yapf autoformat on save", done => {
8590
let formattedFile = path.join(pythoFilesPath, "afterYapfFormatOnSave.py");
86-
let fileToFormat = path.join(pythoFilesPath, "beforeYapfFormatOnSave.py");
8791
let fileToCopyFrom = path.join(pythoFilesPath, "beforeYapfFormatOnSaveOriginal.py");
8892
let formattedFileContents = fs.readFileSync(formattedFile, "utf-8");
93+
94+
let fileToFormat = path.join(pythoFilesPath, "beforeYapf.py");
95+
let textDocument: vscode.TextDocument;
96+
8997
if (fs.existsSync(fileToFormat)) { fs.unlinkSync(fileToFormat); }
9098
fs.copySync(fileToCopyFrom, fileToFormat);
9199
const FORMAT_ON_SAVE = pythonSettings.formatting.formatOnSave;
92100
pythonSettings.formatting.formatOnSave = true;
93101
pythonSettings.formatting.provider = "yapf";
94102

95-
vscode.workspace.openTextDocument(fileToFormat).then(textDocument => {
96-
return vscode.window.showTextDocument(textDocument).then(textEditor => {
97-
return textEditor.edit(editBuilder => {
98-
editBuilder.insert(new vscode.Position(0, 0), "#");
99-
}).then(edited => {
100-
return textDocument.save().then(saved => {
101-
return new Promise<any>((resolve, reject) => {
102-
setTimeout(() => {
103-
assert.equal(textDocument.getText(), formattedFileContents, "Formatted contents are not the same");
104-
resolve();
105-
}, 1000);
106-
});
107-
}, error => {
108-
assert.fail(error, "", "Error in Saving Document, " + error);
109-
});
110-
}, error => {
111-
assert.fail(error, "", "Error in Editing Document, " + error);
112-
});
113-
}, error => {
114-
assert.fail(error, "", "Error in Showing Document, " + error);
103+
vscode.workspace.openTextDocument(fileToFormat).then(document => {
104+
textDocument = document;
105+
return vscode.window.showTextDocument(textDocument);
106+
}).then(editor => {
107+
return editor.edit(editBuilder => {
108+
editBuilder.insert(new vscode.Position(0, 0), "#\n");
109+
});
110+
}).then(saved => {
111+
return new Promise<any>((resolve, reject) => {
112+
setTimeout(() => {
113+
resolve();
114+
}, 1000);
115115
});
116-
}, error => {
117-
assert.fail(error, "", "Error in Opening Document, " + error);
118-
}).then(() => done(), done).then(() => closeActiveEditor(), closeActiveEditor);
116+
}).then(() => {
117+
assert.equal(textDocument.getText(), formattedFileContents, "Formatted contents are not the same");
118+
}).then(done, done);
119119
});
120120
});

src/test/pythonFiles/formatting/beforeYapf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import math, sys;
23

34
def example1():
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
#
2-
import math, sys;
2+
import math, sys
3+
34

45
def example1():
56
####This is a long comment. This should be wrapped to fit within 72 characters.
6-
some_tuple=( 1,2, 3,'a' );
7-
some_variable={'long':'Long code lines should be wrapped within 79 characters.',
8-
'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
9-
'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
10-
20,300,40000,500000000,60000000000000000]}}
7+
some_tuple = (1, 2, 3, 'a')
8+
some_variable = {'long':
9+
'Long code lines should be wrapped within 79 characters.',
10+
'other': [math.pi, 100, 200, 300, 9876543210,
11+
'This is a long string that goes on'],
12+
'more': {'inner':
13+
'This whole logical line should be wrapped.',
14+
some_tuple: [1, 20, 300, 40000, 500000000,
15+
60000000000000000]}}
1116
return (some_tuple, some_variable)
12-
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
13-
class Example3( object ):
14-
def __init__ ( self, bar ):
15-
#Comments should have a space after the hash.
16-
if bar : bar+=1; bar=bar* bar ; return bar
17-
else:
18-
some_string = """
17+
18+
19+
def example2():
20+
return {'has_key() is deprecated': True}.has_key({'f': 2}.has_key(''))
21+
22+
23+
class Example3(object):
24+
def __init__(self, bar):
25+
#Comments should have a space after the hash.
26+
if bar:
27+
bar += 1
28+
bar = bar * bar
29+
return bar
30+
else:
31+
some_string = """
1932
Indentation in multiline strings should not be touched.
2033
Only actual code should be reindented.
2134
"""
22-
return (sys.path, some_string)
35+
36+
return (sys.path, some_string)

0 commit comments

Comments
 (0)