From 6081edff8ab3eb0874f2f667cb9f26a346bbde28 Mon Sep 17 00:00:00 2001 From: Sergio Padrino Date: Wed, 13 Mar 2024 13:52:23 +0100 Subject: [PATCH 1/2] Add parseBadConfigValueErrorInfo function --- lib/git-process.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/git-process.ts b/lib/git-process.ts index def68479..8789fe50 100644 --- a/lib/git-process.ts +++ b/lib/git-process.ts @@ -291,6 +291,30 @@ export class GitProcess { return null } + + public static parseBadConfigValueErrorInfo( + stderr: string + ): { key: string; value: string } | null { + const errorEntry = Object.entries(GitErrorRegexes).find( + ([_, v]) => v === GitError.BadConfigValue + ) + + if (errorEntry === undefined) { + return null + } + + const m = stderr.match(errorEntry[0]) + + if (m === null) { + return null + } + + if (!m[1] || !m[2]) { + return null + } + + return { key: m[2], value: m[1] } + } } /** From eb1d295e0bc95853b0ddb20dddd0a3d365803721 Mon Sep 17 00:00:00 2001 From: Sergio Padrino Date: Wed, 13 Mar 2024 13:52:30 +0100 Subject: [PATCH 2/2] Use parseBadConfigValueErrorInfo in tests --- test/fast/errors-test.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/fast/errors-test.ts b/test/fast/errors-test.ts index b4e6a406..eeca6120 100644 --- a/test/fast/errors-test.ts +++ b/test/fast/errors-test.ts @@ -82,15 +82,10 @@ describe('detects errors', () => { expect(result).toHaveGitError(GitError.BadConfigValue) - const errorEntry = Object.entries(GitErrorRegexes).find( - ([_, v]) => v === GitError.BadConfigValue - ) - - expect(errorEntry).not.toBe(null) - const m = result.stderr.match(errorEntry![0]) - - expect(m![1]).toBe('nab') - expect(m![2]).toBe('core.autocrlf') + const errorInfo = GitProcess.parseBadConfigValueErrorInfo(result.stderr) + expect(errorInfo).not.toBe(null) + expect(errorInfo!.value).toBe('nab') + expect(errorInfo!.key).toBe('core.autocrlf') }) it('detects bad numeric config value', async () => { const repoPath = await initialize('bad-config-repo')