Skip to content

Commit

Permalink
Add env flag for running tests with pycodestyle
Browse files Browse the repository at this point in the history
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
ryanashcraft committed Mar 4, 2018
1 parent 8762d1c commit ec091eb
Show file tree
Hide file tree
Showing 3 changed files with 2,606 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests_config/pycodestyle.js
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
};
6 changes: 6 additions & 0 deletions tests_config/run_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const extname = require("path").extname;
const prettier = require("prettier");
const massageAST = require("prettier/src/common/clean-ast").massageAST;
const normalizeOptions = require("prettier/src/main/options").normalize;
const getPycodestyleOutput = require("./pycodestyle").getPycodestyleOutput;

const AST_COMPARE = process.env["AST_COMPARE"];
const PEP8_VALIDATE = process.env["PEP8_VALIDATE"];

function run_spec(dirname, parsers, options) {
options = Object.assign(
Expand Down Expand Up @@ -39,6 +41,10 @@ function run_spec(dirname, parsers, options) {
expect(raw(source + "~".repeat(80) + "\n" + output)).toMatchSnapshot(
filename
);

if (PEP8_VALIDATE) {
expect(getPycodestyleOutput(output, mergedOptions)).toEqual("");
}
});

parsers.slice(1).forEach(parserName => {
Expand Down
Loading

0 comments on commit ec091eb

Please sign in to comment.