Skip to content
This repository was archived by the owner on Oct 1, 2019. It is now read-only.

Commit d75742f

Browse files
ryanashcraftpatrick91
authored andcommitted
Add env flag for running tests with pycodestyle (#70)
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 #1.
1 parent 9e3c138 commit d75742f

3 files changed

Lines changed: 2608 additions & 0 deletions

File tree

tests_config/pycodestyle.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"use strict";
2+
3+
const spawnSync = require("child_process").spawnSync;
4+
const path = require("path");
5+
const normalizeOptions = require("prettier/src/main/options").normalize;
6+
7+
// Only enable subset of errors that prettier should handle and use the
8+
// default ignored error list.
9+
// See http://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
10+
const selectedErrors = ["E1", "E2", "E3", "W1", "W2", "W3", "W5"];
11+
const ignoredErrors = [
12+
"E121",
13+
"E123",
14+
"E126",
15+
"E133",
16+
"E226",
17+
"E241",
18+
"E242",
19+
"E704",
20+
"W503",
21+
"W504"
22+
];
23+
24+
function getPycodestyleOutput(string, options) {
25+
const normalizedOptions = normalizeOptions(options);
26+
27+
// Use the same version of Python to run pycodestyle that we're using for
28+
// prettier.
29+
const pythonExectuable = `python${
30+
normalizedOptions.pythonVersion == "2" ? "" : "3"
31+
}`;
32+
33+
const executionResult = spawnSync(
34+
pythonExectuable,
35+
[
36+
path.join(__dirname, "../vendor/python/pycodestyle.py"),
37+
`--select=${selectedErrors.join(",")}`,
38+
`--ignore=${ignoredErrors.join(",")}`,
39+
`--max-line-length=${normalizedOptions.printWidth}`,
40+
"-"
41+
],
42+
{
43+
input: string
44+
}
45+
);
46+
47+
if (!executionResult.stderr) {
48+
throw new Error("Failed to execute pycodestyle");
49+
}
50+
51+
const error = executionResult.stderr.toString();
52+
53+
if (error) {
54+
throw new Error(error);
55+
}
56+
57+
return executionResult.stdout.toString();
58+
}
59+
60+
module.exports = {
61+
getPycodestyleOutput: getPycodestyleOutput
62+
};

tests_config/run_spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const extname = require("path").extname;
55
const prettier = require("prettier");
66
const massageAST = require("prettier/src/common/clean-ast").massageAST;
77
const normalizeOptions = require("prettier/src/main/options").normalize;
8+
const getPycodestyleOutput = require("./pycodestyle").getPycodestyleOutput;
89

910
const AST_COMPARE = process.env["AST_COMPARE"];
11+
const PEP8_VALIDATE = process.env["PEP8_VALIDATE"];
1012

1113
function run_spec(dirname, parsers, options) {
1214
options = Object.assign(
@@ -39,6 +41,10 @@ function run_spec(dirname, parsers, options) {
3941
expect(raw(source + "~".repeat(80) + "\n" + output)).toMatchSnapshot(
4042
filename
4143
);
44+
45+
if (PEP8_VALIDATE) {
46+
expect(getPycodestyleOutput(output, mergedOptions)).toEqual("");
47+
}
4248
});
4349

4450
parsers.slice(1).forEach(parserName => {

0 commit comments

Comments
 (0)