-
Notifications
You must be signed in to change notification settings - Fork 81
fix(bundle): atomic re-commit via per-entry aside (compatible with #162) #196
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
Open
SahilRakhaiya05
wants to merge
2
commits into
TestSprite:main
Choose a base branch
from
SahilRakhaiya05:fix/bundle-atomic-recommit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,124 @@ | ||
| import type * as NodeFsPromises from 'node:fs/promises'; | ||
| import { | ||
| existsSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| readdirSync, | ||
| readFileSync, | ||
| writeFileSync, | ||
| } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const renameMock = vi.hoisted(() => vi.fn()); | ||
| const rmMock = vi.hoisted(() => vi.fn()); | ||
|
|
||
| vi.mock('node:fs/promises', async importOriginal => { | ||
| const actual = (await importOriginal()) as typeof NodeFsPromises; | ||
| return { | ||
| ...actual, | ||
| rename: renameMock, | ||
| rm: rmMock, | ||
| }; | ||
| }); | ||
|
|
||
| const { commitBundle } = await import('./bundle.js'); | ||
|
|
||
| describe('commitBundle', () => { | ||
| let realRename: typeof NodeFsPromises.rename; | ||
| let realRm: typeof NodeFsPromises.rm; | ||
|
|
||
| beforeEach(async () => { | ||
| const actual = (await vi.importActual('node:fs/promises')) as typeof NodeFsPromises; | ||
| realRename = actual.rename; | ||
| realRm = actual.rm; | ||
| renameMock.mockImplementation(realRename); | ||
| rmMock.mockImplementation(realRm); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| renameMock.mockReset(); | ||
| rmMock.mockReset(); | ||
| }); | ||
|
|
||
| async function withTempParent(run: (parent: string) => Promise<void>): Promise<void> { | ||
| const parent = mkdtempSync(join(tmpdir(), 'bundle-commit-parent-')); | ||
| try { | ||
| await run(parent); | ||
| } finally { | ||
| await realRm(parent, { recursive: true, force: true }).catch(() => undefined); | ||
| } | ||
| } | ||
|
|
||
| function seedBundleDirs(parent: string): { dir: string; tmpDir: string; files: string[] } { | ||
| const dir = join(parent, 'bundle'); | ||
| const tmpDir = join(dir, '.tmp'); | ||
|
|
||
| mkdirSync(dir, { recursive: true }); | ||
| writeFileSync(join(dir, 'notes.txt'), 'foreign notes\n', 'utf8'); | ||
| mkdirSync(join(dir, 'steps'), { recursive: true }); | ||
| writeFileSync(join(dir, 'meta.json'), '{"snapshotId":"snap_old"}\n', 'utf8'); | ||
| writeFileSync(join(dir, 'steps', '01-evidence.json'), '{"step":1}\n', 'utf8'); | ||
|
|
||
| mkdirSync(join(tmpDir, 'steps'), { recursive: true }); | ||
| writeFileSync(join(tmpDir, 'meta.json'), '{"snapshotId":"snap_new"}\n', 'utf8'); | ||
| writeFileSync(join(tmpDir, 'result.json'), '{}\n', 'utf8'); | ||
| writeFileSync(join(tmpDir, 'steps', '01-evidence.json'), '{"step":9}\n', 'utf8'); | ||
|
|
||
| return { dir, tmpDir, files: ['result.json', 'meta.json', 'steps/01-evidence.json'] }; | ||
| } | ||
|
|
||
| it('rolls back to the prior complete bundle when a staged rename fails', async () => { | ||
| await withTempParent(async parent => { | ||
| const { dir, tmpDir, files } = seedBundleDirs(parent); | ||
|
|
||
| renameMock.mockImplementation(async (oldPath, newPath) => { | ||
| const dest = String(newPath); | ||
| if (dest.endsWith('result.json') && !dest.includes('.aside.')) { | ||
| throw Object.assign(new Error('simulated install failure'), { code: 'EACCES' }); | ||
| } | ||
| return realRename(oldPath, newPath); | ||
| }); | ||
|
|
||
| await expect(commitBundle(tmpDir, dir, files)).rejects.toThrow('simulated install failure'); | ||
|
|
||
| expect(readFileSync(join(dir, 'meta.json'), 'utf8')).toBe('{"snapshotId":"snap_old"}\n'); | ||
| expect(readFileSync(join(dir, 'steps', '01-evidence.json'), 'utf8')).toBe('{"step":1}\n'); | ||
| expect(readFileSync(join(dir, 'notes.txt'), 'utf8')).toBe('foreign notes\n'); | ||
| const leftovers = readdirSync(parent).filter(name => name.includes('.aside.')); | ||
| expect(leftovers).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it('preserves foreign files while installing the new bundle on success', async () => { | ||
| await withTempParent(async parent => { | ||
| const { dir, tmpDir, files } = seedBundleDirs(parent); | ||
|
|
||
| await expect(commitBundle(tmpDir, dir, files)).resolves.toBeUndefined(); | ||
|
|
||
| expect(readFileSync(join(dir, 'meta.json'), 'utf8')).toBe('{"snapshotId":"snap_new"}\n'); | ||
| expect(readFileSync(join(dir, 'steps', '01-evidence.json'), 'utf8')).toBe('{"step":9}\n'); | ||
| expect(readFileSync(join(dir, 'notes.txt'), 'utf8')).toBe('foreign notes\n'); | ||
| expect(existsSync(join(dir, 'result.json'))).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps the new bundle when post-commit aside cleanup fails', async () => { | ||
| await withTempParent(async parent => { | ||
| const { dir, tmpDir, files } = seedBundleDirs(parent); | ||
|
|
||
| rmMock.mockImplementation(async (path, options) => { | ||
| if (String(path).includes('.aside.')) { | ||
| throw Object.assign(new Error('simulated aside cleanup failure'), { code: 'EACCES' }); | ||
| } | ||
| return realRm(path, options); | ||
| }); | ||
|
|
||
| await expect(commitBundle(tmpDir, dir, files)).resolves.toBeUndefined(); | ||
|
|
||
| expect(readFileSync(join(dir, 'meta.json'), 'utf8')).toBe('{"snapshotId":"snap_new"}\n'); | ||
| expect(readFileSync(join(dir, 'notes.txt'), 'utf8')).toBe('foreign notes\n'); | ||
| }); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: TestSprite/testsprite-cli
Length of output: 50381
🏁 Script executed:
Repository: TestSprite/testsprite-cli
Length of output: 50381
🏁 Script executed:
Repository: TestSprite/testsprite-cli
Length of output: 50382
Rollback must clean up newly installed bundle files (
src/lib/bundle.ts:578-627)If a later rename fails after
result.jsonorsteps/has already been moved in,rollback()restores the aside entries but leaves those new files behind, which can mix two runs in the same bundle. Track installed destinations and delete them during rollback.🤖 Prompt for AI Agents
Source: Path instructions