Skip to content

fix(test): use atomic writes for code get --out to prevent file truncation#7

Open
crypticsaiyan wants to merge 1 commit into
TestSprite:mainfrom
crypticsaiyan:fix/code-get-out-truncation
Open

fix(test): use atomic writes for code get --out to prevent file truncation#7
crypticsaiyan wants to merge 1 commit into
TestSprite:mainfrom
crypticsaiyan:fix/code-get-out-truncation

Conversation

@crypticsaiyan

Copy link
Copy Markdown

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.ts command already correctly mitigates this using an atomic write pattern (writing to a .tmp file and renaming it only upon success); code get --out had skipped this safety mechanism.

The Fix

Implemented the atomic write pattern to ensure safe file operations. openOutputFile now 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 --out path 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.ts to simulate failure and empty states. Now, file content is preserved in the following cases (previously emptied):

  • Failing fetch: Simulated a network error with a pre-existing --out file.
  • Empty content: Simulated a "No code generated yet" response with a pre-existing --out file.

CI Gates: All local checks pass.

  • npm run lint & npm run format:check
  • npm run typecheck
  • npm test (1449 tests passing)
  • npm run test:coverage (≥80% threshold met)

Files Changed:

  • src/commands/test.ts
  • src/commands/test.test.ts

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant