From 00ddfffe6b4b4244e4680b0f92f2c6697fad136f Mon Sep 17 00:00:00 2001 From: Michael Wall Date: Mon, 23 Dec 2019 20:56:28 +0000 Subject: [PATCH] Fix: Windows path parsing for JUnit (fixes #12507) (#12509) * Fix: Windows path parsing for JUnit (fixes #12507) This commit aims to fix the issues described in #12507 by removing the assumption that POSIX style filePaths are supplied by messages. The tests for this are also updated to run on both Windows and POSIX based systems. The results can be seen by running ESLint with the JUnit formatter on a Windows system. Full details can be seen in the issue mentioned above. * Fix: use const instead of function Addresses some comments. --- lib/cli-engine/formatters/junit.js | 2 +- tests/lib/cli-engine/formatters/junit.js | 44 +++++++++++++----------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/cli-engine/formatters/junit.js b/lib/cli-engine/formatters/junit.js index c32425883f7..a994b4b1980 100644 --- a/lib/cli-engine/formatters/junit.js +++ b/lib/cli-engine/formatters/junit.js @@ -32,7 +32,7 @@ function getMessageType(message) { * @private */ function pathWithoutExt(filePath) { - return path.posix.join(path.posix.dirname(filePath), path.basename(filePath, path.extname(filePath))); + return path.join(path.dirname(filePath), path.basename(filePath, path.extname(filePath))); } //------------------------------------------------------------------------------ diff --git a/tests/lib/cli-engine/formatters/junit.js b/tests/lib/cli-engine/formatters/junit.js index e52b63a42a0..b77c177885a 100644 --- a/tests/lib/cli-engine/formatters/junit.js +++ b/tests/lib/cli-engine/formatters/junit.js @@ -12,12 +12,16 @@ //------------------------------------------------------------------------------ const assert = require("chai").assert, - formatter = require("../../../../lib/cli-engine/formatters/junit"); + formatter = require("../../../../lib/cli-engine/formatters/junit"), + process = require("process"); //------------------------------------------------------------------------------ // Tests //------------------------------------------------------------------------------ +const suppliedFilePath = (process.platform === "win32") ? "C:\\path\\to\\foo.js" : "/path/to/foo.js"; +const expectedClassName = (process.platform === "win32") ? "C:\\path\\to\\foo" : "/path/to/foo"; + describe("formatter:junit", () => { describe("when there are no problems", () => { const code = []; @@ -31,7 +35,7 @@ describe("formatter:junit", () => { describe("when passed a single message", () => { const code = [{ - filePath: "/path/to/foo.js", + filePath: suppliedFilePath, messages: [{ message: "Unexpected foo.", severity: 2, @@ -44,20 +48,20 @@ describe("formatter:junit", () => { it("should return a single with a message and the line and col number in the body (error)", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); it("should return a single with a message and the line and col number in the body (warning)", () => { code[0].messages[0].severity = 1; const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed a fatal error message", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [{ fatal: true, message: "Unexpected foo.", @@ -70,13 +74,13 @@ describe("formatter:junit", () => { it("should return a single and an ", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed a fatal error message with no line or column", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [{ fatal: true, message: "Unexpected foo." @@ -86,13 +90,13 @@ describe("formatter:junit", () => { it("should return a single and an ", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed a fatal error message with no line, column, or message text", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [{ fatal: true }] @@ -101,13 +105,13 @@ describe("formatter:junit", () => { it("should return a single and an ", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed multiple messages", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [{ message: "Unexpected foo.", severity: 2, @@ -126,13 +130,13 @@ describe("formatter:junit", () => { it("should return a multiple 's", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed special characters", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [{ message: "Unexpected \b\t\n\f\r牛逼.", severity: 1, @@ -145,13 +149,13 @@ describe("formatter:junit", () => { it("should make them go away", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed multiple files with 1 message each", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [{ message: "Unexpected foo.", severity: 1, @@ -173,13 +177,13 @@ describe("formatter:junit", () => { it("should return 2 's", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed multiple files should print even if no errors", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [{ message: "Unexpected foo.", severity: 1, @@ -195,20 +199,20 @@ describe("formatter:junit", () => { it("should return 2 ", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); describe("when passed a file with no errors", () => { const code = [{ - filePath: "foo.js", + filePath: suppliedFilePath, messages: [] }]; it("should print a passing ", () => { const result = formatter(code); - assert.strictEqual(result.replace(/\n/gu, ""), ""); + assert.strictEqual(result.replace(/\n/gu, ""), ``); }); }); });