From d4dd4a33b71dc3e591bc33affb73400e3e24b7b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Mon, 18 Nov 2024 00:46:33 +0100 Subject: [PATCH] Format, fix error and add tests --- bin/cli.js | 2 +- src/cli/gui.js | 15 +++++++++++---- test/cli.spec.js | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 9260c9b..d7145b4 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -66,7 +66,7 @@ const cli = meow( }, ); -const gui = new GUI({ printSummary: cli.flags.summary }); +const gui = new GUI({printSummary: cli.flags.summary}); process.on('exit', () => { gui.finish(); diff --git a/src/cli/gui.js b/src/cli/gui.js index 0ae1617..2c21d63 100644 --- a/src/cli/gui.js +++ b/src/cli/gui.js @@ -15,7 +15,7 @@ const logHistory = Logger.cliConsole; /** @typedef {import("../lib/linting.js")} Linting */ export default class GUI { - constructor({ printSummary = true } = {}) { + constructor({printSummary = true} = {}) { // Subscribe to global logs Logger.setCLI(true); logHistory.on('msg', () => this.update()); @@ -40,8 +40,11 @@ export default class GUI { */ finish() { if (this.ci) { - // eslint-disable-next-line no-console - console.log(this.render()); + const output = this.render(); + if (output) { + // eslint-disable-next-line no-console + console.log(output); + } } else { this.update(true); } @@ -107,6 +110,7 @@ export default class GUI { if (this.$summary) { outp.push('', this.$titles.summary, this.$summary); } + if (outp[0] === '') { outp.shift(); } @@ -129,7 +133,10 @@ export default class GUI { */ addLinting(linting) { this.$lintings.push(new LintingDisplay(linting)); - this.$summary?.addLinting(linting); + if (this.$summary) { + this.$summary.addLinting(linting); + } + linting.on('rule', () => this.update()); linting.on('done', () => this.update()); } diff --git a/test/cli.spec.js b/test/cli.spec.js index 564097c..bf768c4 100644 --- a/test/cli.spec.js +++ b/test/cli.spec.js @@ -144,3 +144,21 @@ describe('Configuration files', function () { expect(stdout).not.toMatch('Failed to lint'); }); }); + +describe('--no-summary', function () { + it('should not print summary with valid SVG', async function () { + const {stdout} = await execCli([VALID_SVG, '--no-summary']); + expect(stdout).not.toMatch('Summary'); + }); + + it('should print the summary not passing --no-summary', async function () { + const {stdout} = await execCli([VALID_SVG]); + expect(stdout).toMatch('Summary'); + }); + + it('should print errors with invalid SVG', async function () { + const {stdout} = await execCli([INVALID_SVG, '--no-summary']); + expect(stdout).toMatch('Files'); + expect(stdout).not.toMatch('Summary'); + }); +});