From 5b1dc08ab368afc5ff5d72e758ec5a9f75d6228e Mon Sep 17 00:00:00 2001 From: Patrick Taylor Date: Thu, 18 Jul 2024 13:05:06 +0100 Subject: [PATCH] =?UTF-8?q?Code=20improvements=20=F0=9F=99=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../markdownlint/__tests__/index.spec.ts | 14 +++++++------- src/linters/markdownlint/index.ts | 4 ++-- src/utils/__tests__/colourLog.spec.ts | 6 +++--- src/utils/colourLog.ts | 17 +++++++++-------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/linters/markdownlint/__tests__/index.spec.ts b/src/linters/markdownlint/__tests__/index.spec.ts index d5e0db4..4b4c7e3 100644 --- a/src/linters/markdownlint/__tests__/index.spec.ts +++ b/src/linters/markdownlint/__tests__/index.spec.ts @@ -3,7 +3,7 @@ import markdownlint from 'markdownlint' import colourLog from '@Utils/colourLog' import loadConfig from '../loadConfig' -import markdownLib from '..' +import markdownlintLib from '..' jest.mock('markdownlint') jest.mock('@Utils/colourLog') @@ -26,7 +26,7 @@ describe('markdownlint', () => { callback(null, {}) }) - await markdownLib.lintFiles(testFiles) + await markdownlintLib.lintFiles(testFiles) expect(loadConfig).toHaveBeenCalledTimes(1) expect(colourLog.configDebug).toHaveBeenCalledWith('Using default markdownlint config:', mockedConfig) @@ -37,7 +37,7 @@ describe('markdownlint', () => { callback(null, {}) }) - await markdownLib.lintFiles(testFiles) + await markdownlintLib.lintFiles(testFiles) expect(markdownlint).toHaveBeenCalledOnceWith({ config: mockedConfig, @@ -52,7 +52,7 @@ describe('markdownlint', () => { callback(error, undefined) }) - await expect(markdownLib.lintFiles(testFiles)).rejects.toThrow(error) + await expect(markdownlintLib.lintFiles(testFiles)).rejects.toThrow(error) expect(colourLog.error).toHaveBeenCalledOnceWith('An error occurred while running markdownlint', error) }) @@ -62,7 +62,7 @@ describe('markdownlint', () => { callback(null, undefined) }) - await expect(markdownLib.lintFiles(testFiles)).rejects.toThrow('No results') + await expect(markdownlintLib.lintFiles(testFiles)).rejects.toThrow('No results') expect(colourLog.error).toHaveBeenCalledOnceWith('An error occurred while running markdownlint: no results') }) @@ -74,7 +74,7 @@ describe('markdownlint', () => { }) }) - expect(await markdownLib.lintFiles(testFiles)).toStrictEqual({ + expect(await markdownlintLib.lintFiles(testFiles)).toStrictEqual({ results: {}, summary: { deprecatedRules: [], @@ -149,7 +149,7 @@ describe('markdownlint', () => { callback(null, markdownlintResult) }) - expect(await markdownLib.lintFiles(testFiles)).toStrictEqual({ + expect(await markdownlintLib.lintFiles(testFiles)).toStrictEqual({ results: { 'CHANGELOG.md': [{ ...commonResult, diff --git a/src/linters/markdownlint/index.ts b/src/linters/markdownlint/index.ts index 708afd9..e227f44 100644 --- a/src/linters/markdownlint/index.ts +++ b/src/linters/markdownlint/index.ts @@ -72,8 +72,8 @@ const lintFiles = (files: Array): Promise => new Promise((re }) }) -const markdownLib = { +const markdownlintLib = { lintFiles, } -export default markdownLib +export default markdownlintLib diff --git a/src/utils/__tests__/colourLog.spec.ts b/src/utils/__tests__/colourLog.spec.ts index d6d6aee..e7e5566 100644 --- a/src/utils/__tests__/colourLog.spec.ts +++ b/src/utils/__tests__/colourLog.spec.ts @@ -97,7 +97,7 @@ describe('colourLog', () => { expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\nAn error occurred.') }) - it('logs additional debug information if there is an error', () => { + it('logs additional debug information if there is an error and global.debug is false', () => { colourLog.error('An error occurred', error) expect(chalk.red).toHaveBeenCalledOnceWith('\nAn error occurred. Run with --debug for more information.') @@ -119,9 +119,9 @@ describe('colourLog', () => { colourLog.error('An error occurred', error) - expect(chalk.red).toHaveBeenCalledOnceWith('\nAn error occurred. Run with --debug for more information.') + expect(chalk.red).toHaveBeenCalledOnceWith('\nAn error occurred.') expect(mockedConsoleLog).toHaveBeenCalledTimes(3) - expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nAn error occurred. Run with --debug for more information.') + expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nAn error occurred.') expect(mockedConsoleLog).toHaveBeenNthCalledWith(2) expect(mockedConsoleLog).toHaveBeenNthCalledWith(3, error) }) diff --git a/src/utils/colourLog.ts b/src/utils/colourLog.ts index 45543cd..a5967cf 100644 --- a/src/utils/colourLog.ts +++ b/src/utils/colourLog.ts @@ -22,14 +22,15 @@ const colourLog = { }, error: (text: string, error?: Error | unknown) => { - if (error) { - console.log(chalk.red(`\n${text}. Run with --debug for more information.`)) - if (global.debug === true) { - console.log() - console.log(error) - } - } else { - console.log(chalk.red(`\n${text}.`)) + let errorMessage = `\n${text}.` + if (error && global.debug === false) { + errorMessage += ' Run with --debug for more information.' + } + + console.log(chalk.red(errorMessage)) + if (error && global.debug === true) { + console.log() + console.log(error) } },