From a0ddb2df94cbe5bceaa22ec3a06543264fccafdd Mon Sep 17 00:00:00 2001 From: thewalla07 Date: Tue, 29 Oct 2019 17:28:54 +0000 Subject: [PATCH] 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. --- lib/cli-engine/formatters/junit.js | 2 +- tests/lib/cli-engine/formatters/junit.js | 65 ++++++++++++++++-------- 2 files changed, 46 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..0f0f534ed0b 100644 --- a/tests/lib/cli-engine/formatters/junit.js +++ b/tests/lib/cli-engine/formatters/junit.js @@ -12,12 +12,37 @@ //------------------------------------------------------------------------------ const assert = require("chai").assert, - formatter = require("../../../../lib/cli-engine/formatters/junit"); + formatter = require("../../../../lib/cli-engine/formatters/junit"), + process = require("process"); //------------------------------------------------------------------------------ // Tests //------------------------------------------------------------------------------ +/** + * Returns the suppliedFilePath for each test + * @returns {string} the filepath in a format that would be expected on either a win32 system or POSIX system + * @private + */ +function suppliedFilePath() { + if (process.platform === "win32") { + return "C:\\path\\to\\foo.js"; + } + return "/path/to/foo.js"; +} + +/** + * Returns the expectedClassName for each test + * @returns {string} the expected classname in a win32 system or POSIX system path format + * @private + */ +function expectedClassName() { + if (process.platform === "win32") { + return "C:\\path\\to\\foo"; + } + return "/path/to/foo"; +} + describe("formatter:junit", () => { describe("when there are no problems", () => { const code = []; @@ -31,7 +56,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 +69,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 +95,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 +111,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 +126,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 +151,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 +170,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 +198,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 +220,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, ""), ``); }); }); });