-
Notifications
You must be signed in to change notification settings - Fork 66
fix(generator-adp): write headless generation result for BAS orchestrator #4973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sap-ux/generator-adp': patch | ||
| --- | ||
|
|
||
| FIX: Write headless generation result for BAS orchestrator when JSON input provides a correlation id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { existsSync, readFileSync, writeFileSync } from 'node:fs'; | ||
|
|
||
| /** | ||
| * Absolute path of the file the SAP Business Application Studio orchestrator polls for | ||
| * headless generation results. Each result is stored under the correlation `id` supplied | ||
| * in the generator JSON input. | ||
| */ | ||
| export const RESULT_FILE_PATH = '/home/user/tmpProjectTemplate.json'; | ||
|
testojs marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Writes the outcome of a headless generation run to the orchestrator result file, keyed by | ||
| * the correlation `id` from the JSON input. Existing entries are preserved; a missing or | ||
| * malformed result file is treated as empty. The result file is a best-effort side channel: | ||
| * a write failure is swallowed so it never masks the actual generation outcome. | ||
| * | ||
| * @param {string} id - The correlation key supplied by the orchestrator in the JSON input. | ||
| * @param {string} result - The generated project path on success, or `Failure: <message>` on error. | ||
| */ | ||
| export function writeResult(id: string, result: string): void { | ||
| let fileContent: Record<string, string> = {}; | ||
|
|
||
| if (existsSync(RESULT_FILE_PATH)) { | ||
| try { | ||
| const parsed: unknown = JSON.parse(readFileSync(RESULT_FILE_PATH, 'utf-8')); | ||
| if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { | ||
| // Safe after the runtime object check; the file only ever holds a flat id -> result map. | ||
| fileContent = parsed as Record<string, string>; | ||
| } | ||
| } catch { | ||
| fileContent = {}; | ||
| } | ||
| } | ||
|
|
||
| fileContent[id] = result; | ||
| try { | ||
| writeFileSync(RESULT_FILE_PATH, JSON.stringify(fileContent)); | ||
| } catch { | ||
| // Best-effort side channel; never let a failed result write break generation. | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/generator-adp/test/unit/utils/write-result.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { jest } from '@jest/globals'; | ||
|
|
||
| const mockExistsSync = jest.fn<typeof realFs.existsSync>(); | ||
| const mockReadFileSync = jest.fn<typeof realFs.readFileSync>(); | ||
| const mockWriteFileSync = jest.fn<typeof realFs.writeFileSync>(); | ||
|
|
||
| const realFs = await import('node:fs'); | ||
| jest.unstable_mockModule('node:fs', () => ({ | ||
| ...realFs, | ||
| existsSync: mockExistsSync, | ||
| readFileSync: mockReadFileSync, | ||
| writeFileSync: mockWriteFileSync | ||
| })); | ||
|
|
||
| const { writeResult, RESULT_FILE_PATH } = await import('../../../src/utils/write-result.js'); | ||
|
|
||
| describe('writeResult', () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should write the project path under the given id when the file does not exist', () => { | ||
| mockExistsSync.mockReturnValue(false); | ||
|
|
||
| writeResult('id-1', '/home/user/projects/app.variant'); | ||
|
|
||
| expect(mockReadFileSync).not.toHaveBeenCalled(); | ||
| expect(mockWriteFileSync).toHaveBeenCalledWith( | ||
| RESULT_FILE_PATH, | ||
| JSON.stringify({ 'id-1': '/home/user/projects/app.variant' }) | ||
| ); | ||
| }); | ||
|
|
||
| it('should write a failure result under the given id', () => { | ||
| mockExistsSync.mockReturnValue(false); | ||
|
|
||
| writeResult('id-1', 'Failure: something went wrong'); | ||
|
|
||
| expect(mockWriteFileSync).toHaveBeenCalledWith( | ||
| RESULT_FILE_PATH, | ||
| JSON.stringify({ 'id-1': 'Failure: something went wrong' }) | ||
| ); | ||
| }); | ||
|
|
||
| it('should merge the new result into existing entries', () => { | ||
| mockExistsSync.mockReturnValue(true); | ||
| mockReadFileSync.mockReturnValue(JSON.stringify({ existing: '/path/existing' })); | ||
|
|
||
| writeResult('id-2', '/path/new'); | ||
|
|
||
| expect(mockWriteFileSync).toHaveBeenCalledWith( | ||
| RESULT_FILE_PATH, | ||
| JSON.stringify({ existing: '/path/existing', 'id-2': '/path/new' }) | ||
| ); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['malformed JSON', 'not-json'], | ||
| ['a non-object value', '42'], | ||
| ['an array', '[1,2]'] | ||
| ])('should treat %s result file as empty', (_label, fileContent) => { | ||
| mockExistsSync.mockReturnValue(true); | ||
| mockReadFileSync.mockReturnValue(fileContent); | ||
|
|
||
| writeResult('id-3', '/path/new'); | ||
|
|
||
| expect(mockWriteFileSync).toHaveBeenCalledWith(RESULT_FILE_PATH, JSON.stringify({ 'id-3': '/path/new' })); | ||
| }); | ||
|
|
||
| it('should swallow a write failure instead of throwing', () => { | ||
| mockExistsSync.mockReturnValue(false); | ||
| mockWriteFileSync.mockImplementation(() => { | ||
| throw new Error('EACCES'); | ||
| }); | ||
|
|
||
| expect(() => writeResult('id-6', '/path/new')).not.toThrow(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.