forked from prettier/plugin-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add env flag for running tests with pycodestyle
Add optional PEP8_VALIDATE env flag for running prettier output from tests into pycodestyle. If pycodestyle returns any errors, the test fails. Adds pycodestyle as a vendor dependency. Several tests currently fail with the PEP8_VALIDATE flag enabled. Proof of concept for prettier#1.
- Loading branch information
1 parent
8762d1c
commit ec091eb
Showing
3 changed files
with
2,606 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use strict"; | ||
|
||
const spawnSync = require("child_process").spawnSync; | ||
const path = require("path"); | ||
const normalizeOptions = require("prettier/src/main/options").normalize; | ||
|
||
// Only enable style-related errors, and use default ignored error list | ||
// See http://pycodestyle.pycqa.org/en/latest/intro.html#error-codes | ||
const selectedErrors = ["E1", "E2", "E3", "W1", "W2", "W3", "W5"]; | ||
const ignoredErrors = [ | ||
"E121", | ||
"E123", | ||
"E126", | ||
"E133", | ||
"E226", | ||
"E241", | ||
"E242", | ||
"E704", | ||
"W503", | ||
"W504" | ||
]; | ||
|
||
function getPycodestyleOutput(string, options) { | ||
const normalizedOptions = normalizeOptions(options); | ||
|
||
// Use the same version of Python to run pycodestyle that we're using for prettier | ||
const pythonExectuable = `python${ | ||
normalizedOptions.pythonVersion == "2" ? "" : "3" | ||
}`; | ||
|
||
const executionResult = spawnSync( | ||
pythonExectuable, | ||
[ | ||
path.join(__dirname, "../vendor/python/pycodestyle.py"), | ||
`--select=${selectedErrors.join(",")}`, | ||
`--ignore=${ignoredErrors.join(",")}`, | ||
`--max-line-length=${normalizedOptions.printWidth}`, | ||
"-" | ||
], | ||
{ | ||
input: string | ||
} | ||
); | ||
|
||
if (!executionResult.stderr) { | ||
throw new Error("Failed to execute pycodestyle"); | ||
} | ||
|
||
const error = executionResult.stderr.toString(); | ||
|
||
if (error) { | ||
throw new Error(error); | ||
} | ||
|
||
return executionResult.stdout.toString(); | ||
} | ||
|
||
module.exports = { | ||
getPycodestyleOutput: getPycodestyleOutput | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.