From f91d539b04055624bf0956855836a4d5e84f4ea7 Mon Sep 17 00:00:00 2001 From: webwarrior-ws Date: Tue, 19 Mar 2024 03:22:05 +0100 Subject: [PATCH] Fix regression when using --last flag (#3982) * test(cli): add regression test for --last flag Added regression test for running with --last flag. See [1]. [1] https://github.com/conventional-changelog/commitlint/issues/3981 * fix(cli): fixed regression when using --last flag Fixed regression when using --last flag. Fixes https://github.com/conventional-changelog/commitlint/issues/3981 --- .../fixtures/last-flag-regression/commitlint.config.js | 9 +++++++++ @commitlint/cli/src/cli.test.ts | 10 ++++++++++ @commitlint/read/src/read.ts | 10 +++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 @commitlint/cli/fixtures/last-flag-regression/commitlint.config.js diff --git a/@commitlint/cli/fixtures/last-flag-regression/commitlint.config.js b/@commitlint/cli/fixtures/last-flag-regression/commitlint.config.js new file mode 100644 index 0000000000..3230d4c57e --- /dev/null +++ b/@commitlint/cli/fixtures/last-flag-regression/commitlint.config.js @@ -0,0 +1,9 @@ +module.exports = { + rules: { + 'subject-empty': [2, 'never'], + 'type-empty': [2, 'never'], + 'type-enum': [2, 'always', [ + 'test' + ]] + } +}; diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index b7b83ea401..b30add24d0 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -62,6 +62,16 @@ test('should produce last commit and success output with --verbose flag', async expect(actual.stderr).toEqual(''); }); +test('regression test for running with --last flag', async () => { + const cwd = await gitBootstrap('fixtures/last-flag-regression'); + await execa('git', ['add', 'commitlint.config.js'], {cwd}); + await execa('git', ['commit', '-m', '"test: this should work"'], {cwd}); + const actual = await cli(['--last', '--verbose'], {cwd})(); + expect(actual.stdout).toContain('0 problems, 0 warnings'); + expect(actual.stdout).toContain('test: this should work'); + expect(actual.stderr).toEqual(''); +}); + test('should produce no output with --quiet flag', async () => { const cwd = await gitBootstrap('fixtures/default'); const actual = await cli(['--quiet'], {cwd})('foo: bar'); diff --git a/@commitlint/read/src/read.ts b/@commitlint/read/src/read.ts index d0ffdb9387..0972c8b0d9 100644 --- a/@commitlint/read/src/read.ts +++ b/@commitlint/read/src/read.ts @@ -26,12 +26,16 @@ export default async function getCommitMessages( } if (last) { - const executeGitCommand = await execa('git', [ + const gitCommandResult = await execa('git', [ 'log', '-1', - '--pretty=format:"%B"', + '--pretty=format:%B', ]); - return [executeGitCommand.stdout]; + let output = gitCommandResult.stdout; + // strip output of extra quotation marks ("") + if (output[0] == '"' && output[output.length - 1] == '"') + output = output.slice(1, -1); + return [output]; } let gitOptions: GitOptions = {from, to};