fix(test): use atomic writes for code get --out to prevent file truncation#7
Open
crypticsaiyan wants to merge 1 commit into
Open
fix(test): use atomic writes for code get --out to prevent file truncation#7crypticsaiyan wants to merge 1 commit into
crypticsaiyan wants to merge 1 commit into
Conversation
test code get --out <path> opened (truncated) the destination file before the network request. If the GET then failed, or hit the "no code generated yet" branch which writes nothing, the user's pre-existing --out file was left emptied with no way to recover it. Write to a sibling temp file instead and rename it onto the real path only after a successful, complete write. Mirrors the atomic rename contract bundle.ts already uses for multi-file bundles. Add regression tests for both failure modes: a failing fetch and the no-code-yet branch. Both reproduce the truncation on the old code and pass on the fix.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Previously, running
test code get --out <path>would open and immediately truncate the destination file before issuing the network request. If the request subsequently failed (e.g., network error, 4xx,NOT_FOUND) or hit the "no code generated yet" branch (which writes nothing and exits 0), the user's pre-existing file was left completely empty, with no way to recover the original contents.Note: The
bundle.tscommand already correctly mitigates this using an atomic write pattern (writing to a.tmpfile and renaming it only upon success);code get --outhad skipped this safety mechanism.The Fix
Implemented the atomic write pattern to ensure safe file operations.
openOutputFilenow writes to a sibling temporary file (.{name}.tmp-{uuid}) instead of directly targeting the user's requested path.This ensures
closeOutputFile(sink, commit)renames the temporary file onto the real--outpath only when content is successfully downloaded and written. On any failure, or if the test has no code yet, the temporary file is safely discarded, and the user's original file is left completely untouched.Testing & Validation
Regression Test: Added new test cases in
src/commands/test.test.tsto simulate failure and empty states. Now, file content is preserved in the following cases (previously emptied):--outfile.--outfile.CI Gates: All local checks pass.
npm run lint&npm run format:checknpm run typechecknpm test(1449 tests passing)npm run test:coverage(≥80% threshold met)Files Changed:
src/commands/test.tssrc/commands/test.test.ts