Skip to content

Commit

Permalink
Fix linter stdout/stderr assertions (#16)
Browse files Browse the repository at this point in the history
* Fix stdout linter assertions in test suite
* Allow negative timezones
* Fix assertions
* Disable stdout assertion for prettier auto-fix
  • Loading branch information
ascandella authored Feb 9, 2020
1 parent 1eb82cd commit a421b0e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/linters/gofmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Gofmt {
// start. Without these strings, this would not be possible, because file names may include
// spaces, which are not escaped in unified diffs. As a workaround, these lines are filtered out
// from the gofmt diff so the diff parser can read the diff without errors
const filteredOutput = output.stderr
const filteredOutput = output.stdout
.split(/\r?\n/)
.filter(line => !line.startsWith("diff -u"))
.join("\n");
Expand Down
16 changes: 8 additions & 8 deletions test/linters/linters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ describe.each([

// stdout
const stdout = normalizeDates(cmdOutput.stdout);
if ("stdoutParts" in expected.lintResult) {
expected.lintResult.stdoutParts.forEach(stdoutPart =>
if ("stdoutParts" in expected.cmdOutput) {
expected.cmdOutput.stdoutParts.forEach(stdoutPart =>
expect(stdout).toEqual(expect.stringContaining(stdoutPart)),
);
} else if ("stdout" in expected.lintResult) {
expect(stdout).toEqual(expected.stdout);
} else if ("stdout" in expected.cmdOutput) {
expect(stdout).toEqual(expected.cmdOutput.stdout);
}

// stderr
const stderr = normalizeDates(cmdOutput.stderr);
if ("stderrParts" in expected.lintResult) {
expected.lintResult.stderrParts.forEach(stderrParts =>
if ("stderrParts" in expected.cmdOutput) {
expected.cmdOutput.stderrParts.forEach(stderrParts =>
expect(stderr).toEqual(expect.stringContaining(stderrParts)),
);
} else if ("stderr" in expected.lintResult) {
expect(stderr).toEqual(expected.stderr);
} else if ("stderr" in expected.cmdOutput) {
expect(stderr).toEqual(expected.cmdOutput.stderr);
}
});

Expand Down
10 changes: 5 additions & 5 deletions test/linters/params/gofmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const extensions = ["go"];

// Linting without auto-fixing
function getLintParams(dir) {
const stderrFile1 = `diff -u file1.go.orig file1.go\n--- file1.go.orig ${TEST_DATE}\n+++ file1.go ${TEST_DATE}\n@@ -4,7 +4,7 @@\n \n var str = "world"\n \n-func main () { // Whitespace error\n+func main() { // Whitespace error\n fmt.Println("hello " + str)\n }\n \n@@ -17,5 +17,5 @@\n }\n \n func multiply(num1 int, num2 int) int {\n- return num1 * num2 // Indentation error\n+ return num1 * num2 // Indentation error\n }`;
const stderrFile2 = `diff -u file2.go.orig file2.go\n--- file2.go.orig ${TEST_DATE}\n+++ file2.go ${TEST_DATE}\n@@ -1,5 +1,5 @@\n package main\n \n func divide(num1 int, num2 int) int {\n- return num1 / num2 // Whitespace error\n+ return num1 / num2 // Whitespace error\n }`;
const stdoutFile1 = `diff -u file1.go.orig file1.go\n--- file1.go.orig ${TEST_DATE}\n+++ file1.go ${TEST_DATE}\n@@ -4,7 +4,7 @@\n \n var str = "world"\n \n-func main () { // Whitespace error\n+func main() { // Whitespace error\n fmt.Println("hello " + str)\n }\n \n@@ -17,5 +17,5 @@\n }\n \n func multiply(num1 int, num2 int) int {\n- return num1 * num2 // Indentation error\n+ return num1 * num2 // Indentation error\n }`;
const stdoutFile2 = `diff -u file2.go.orig file2.go\n--- file2.go.orig ${TEST_DATE}\n+++ file2.go ${TEST_DATE}\n@@ -1,5 +1,5 @@\n package main\n \n func divide(num1 int, num2 int) int {\n- return num1 / num2 // Whitespace error\n+ return num1 / num2 // Whitespace error\n }`;
return {
// Expected output of the linting function
cmdOutput: {
status: 0, // gofmt always uses exit code 0
stderrParts: [stderrFile1, stderrFile2],
stderr: `${stderrFile1}\n${stderrFile2}`,
stdoutParts: [stdoutFile1, stdoutFile2],
stdout: `${stdoutFile1}\n${stdoutFile2}`,
},
// Expected output of the parsing function
lintResult: {
Expand Down Expand Up @@ -50,7 +50,7 @@ function getFixParams(dir) {
// Expected output of the linting function
cmdOutput: {
status: 0, // gofmt always uses exit code 0
stderr: "",
stdout: "",
},
// Expected output of the parsing function
lintResult: {
Expand Down
4 changes: 3 additions & 1 deletion test/linters/params/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ function getLintParams(dir) {

// Linting with auto-fixing
function getFixParams(dir) {
const stdoutFile1 = `file1.js`;
const stdoutFile2 = `file2.css`;
return {
// Expected output of the linting function
cmdOutput: {
status: 0,
stdout: "",
stdoutParts: [stdoutFile1, stdoutFile2],
},
// Expected output of the parsing function
lintResult: {
Expand Down
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { join } = require("path");

const DATE_REGEX = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ \+\d{4}/g;
const DATE_REGEX = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d+ [+-]\d{4}/g;
const TEST_DATE = "2019-01-01 00:00:00.000000 +0000";

/**
Expand Down

0 comments on commit a421b0e

Please sign in to comment.