From e163141b72d9926ede92714aadb2d1218cbcf34b Mon Sep 17 00:00:00 2001 From: Guillaume Jacquart Date: Fri, 27 Jun 2025 09:36:01 +0200 Subject: [PATCH] fix(core): Return default tags-mappings and folders value when file not found (#16747) --- .../source-control-helper.ee.test.ts | 41 +++++++++++++++++++ .../source-control-helper.ee.ts | 30 ++++++++++---- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/environments.ee/source-control/__tests__/source-control-helper.ee.test.ts b/packages/cli/src/environments.ee/source-control/__tests__/source-control-helper.ee.test.ts index 5748bf2321..036ed8fecb 100644 --- a/packages/cli/src/environments.ee/source-control/__tests__/source-control-helper.ee.test.ts +++ b/packages/cli/src/environments.ee/source-control/__tests__/source-control-helper.ee.test.ts @@ -313,3 +313,44 @@ describe('isWorkflowModified', () => { expect(isWorkflowModified(local, remote)).toBe(false); }); }); + +describe('readTagAndMappingsFromSourceControlFile', () => { + beforeEach(() => { + // Reset module registry so we can unmock properly + jest.resetModules(); + jest.unmock('node:fs/promises'); + }); + + it('should return default mapping if the file path is not valid', async () => { + const filePath = 'invalid/path/tags-and-mappings.json'; + // Import the function after resetting modules + const { readTagAndMappingsFromSourceControlFile } = await import( + '@/environments.ee/source-control/source-control-helper.ee' + ); + const result = await readTagAndMappingsFromSourceControlFile(filePath); + expect(result).toEqual({ + tags: [], + mappings: [], + }); + }); +}); + +describe('readFoldersFromSourceControlFile', () => { + beforeEach(() => { + // Reset module registry so we can unmock properly + jest.resetModules(); + jest.unmock('node:fs/promises'); + }); + + it('should return default folders if the file path is not valid', async () => { + const filePath = 'invalid/path/folders.json'; + // Import the function after resetting modules + const { readFoldersFromSourceControlFile } = await import( + '@/environments.ee/source-control/source-control-helper.ee' + ); + const result = await readFoldersFromSourceControlFile(filePath); + expect(result).toEqual({ + folders: [], + }); + }); +}); diff --git a/packages/cli/src/environments.ee/source-control/source-control-helper.ee.ts b/packages/cli/src/environments.ee/source-control/source-control-helper.ee.ts index b611a40033..8d95bb0512 100644 --- a/packages/cli/src/environments.ee/source-control/source-control-helper.ee.ts +++ b/packages/cli/src/environments.ee/source-control/source-control-helper.ee.ts @@ -54,16 +54,32 @@ export async function readTagAndMappingsFromSourceControlFile(file: string): Pro tags: TagEntity[]; mappings: WorkflowTagMapping[]; }> { - return jsonParse<{ tags: TagEntity[]; mappings: WorkflowTagMapping[] }>( - await fsReadFile(file, { encoding: 'utf8' }), - { fallbackValue: { tags: [], mappings: [] } }, - ); + try { + return jsonParse<{ tags: TagEntity[]; mappings: WorkflowTagMapping[] }>( + await fsReadFile(file, { encoding: 'utf8' }), + { fallbackValue: { tags: [], mappings: [] } }, + ); + } catch (error) { + // Return fallback if file not found + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { + return { tags: [], mappings: [] }; + } + throw error; + } } export async function readFoldersFromSourceControlFile(file: string): Promise { - return jsonParse(await fsReadFile(file, { encoding: 'utf8' }), { - fallbackValue: { folders: [] }, - }); + try { + return jsonParse(await fsReadFile(file, { encoding: 'utf8' }), { + fallbackValue: { folders: [] }, + }); + } catch (error) { + // Return fallback if file not found + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { + return { folders: [] }; + } + throw error; + } } export function sourceControlFoldersExistCheck(