From 9194f45ac7d521119a53773bf02b81670bad526e Mon Sep 17 00:00:00 2001 From: Gabriel Cousin <7649529+GabrielCousin@users.noreply.github.com> Date: Wed, 16 Jan 2019 16:38:58 +0100 Subject: [PATCH] Fix: Manage severity of 1 with TAP reporter (fixes #11110) (#11221) When using TAP reporter, both messages with severity of 1 or 2 were listed as errors (`not ok`). This changes aims at reporting warnings as `ok` messages. --- lib/formatters/tap.js | 11 +++--- tests/lib/formatters/tap.js | 70 +++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/lib/formatters/tap.js b/lib/formatters/tap.js index 9651a2bcf12..354872a0c92 100644 --- a/lib/formatters/tap.js +++ b/lib/formatters/tap.js @@ -20,7 +20,6 @@ function getMessageType(message) { return "error"; } return "warning"; - } /** @@ -50,12 +49,11 @@ module.exports = function(results) { let diagnostics = {}; if (messages.length > 0) { - testResult = "not ok"; - messages.forEach(message => { + const severity = getMessageType(message); const diagnostic = { message: message.message, - severity: getMessageType(message), + severity, data: { line: message.line || 0, column: message.column || 0, @@ -63,6 +61,11 @@ module.exports = function(results) { } }; + // This ensures a warning message is not flagged as error + if (severity === "error") { + testResult = "not ok"; + } + /* * If we have multiple messages place them under a messages key * The first error will be logged as message key diff --git a/tests/lib/formatters/tap.js b/tests/lib/formatters/tap.js index 7fc080a5338..903af6a8bbc 100644 --- a/tests/lib/formatters/tap.js +++ b/tests/lib/formatters/tap.js @@ -72,7 +72,7 @@ describe("formatter:tap", () => { }] }]; - it("should return a an error string", () => { + it("should return an error string", () => { const result = formatter(code); assert.include(result, "not ok"); @@ -80,7 +80,70 @@ describe("formatter:tap", () => { }); }); - describe("when passed multiple messages", () => { + describe("when passed a message with a severity of 1", () => { + const code = [{ + filePath: "foo.js", + messages: [{ + message: "Unexpected foo.", + severity: 1, + line: 5, + column: 10, + ruleId: "foo" + }] + }]; + + it("should return a warning string", () => { + const result = formatter(code); + + assert.include(result, "ok"); + assert.notInclude(result, "not ok"); + assert.include(result, "warning"); + }); + }); + + describe("when passed multiple messages with a severity of 1", () => { + const code = [{ + filePath: "foo.js", + messages: [{ + message: "Foo.", + severity: 1, + line: 5, + column: 10, + ruleId: "foo" + }, { + message: "Bar.", + severity: 1, + line: 6, + column: 11, + ruleId: "bar" + }, { + message: "Baz.", + severity: 1, + line: 7, + column: 12, + ruleId: "baz" + }] + }]; + + it("should return a string with multiple entries", () => { + const result = formatter(code); + + assert.include(result, "ok"); + assert.notInclude(result, "not ok"); + assert.include(result, "messages"); + assert.include(result, "Foo."); + assert.include(result, "line: 5"); + assert.include(result, "column: 10"); + assert.include(result, "Bar."); + assert.include(result, "line: 6"); + assert.include(result, "column: 11"); + assert.include(result, "Baz."); + assert.include(result, "line: 7"); + assert.include(result, "column: 12"); + }); + }); + + describe("when passed multiple messages with different error severity", () => { const code = [{ filePath: "foo.js", messages: [{ @@ -146,7 +209,8 @@ describe("formatter:tap", () => { const result = formatter(code); assert.include(result, "not ok 1"); - assert.include(result, "not ok 2"); + assert.include(result, "ok 2"); + assert.notInclude(result, "not ok 2"); }); });