fix(core): Return default tags-mappings and folders value when file not found (#16747)

This commit is contained in:
Guillaume Jacquart
2025-06-27 09:36:01 +02:00
committed by GitHub
parent a715f53eb6
commit e163141b72
2 changed files with 64 additions and 7 deletions

View File

@@ -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: [],
});
});
});

View File

@@ -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<ExportedFolders> {
return jsonParse<ExportedFolders>(await fsReadFile(file, { encoding: 'utf8' }), {
fallbackValue: { folders: [] },
});
try {
return jsonParse<ExportedFolders>(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(