@@ -395,41 +395,29 @@ func TestFix_IsIgnored_Continue(t *testing.T) {
395395
396396// --- atomicWriteFile error paths ---
397397
398- // TestAtomicWriteFile_RenameFailure causes os.Rename to fail by making the
399- // target path a directory. On Linux, renaming a regular file onto a directory
400- // returns EISDIR .
401- func TestAtomicWriteFile_RenameFailure (t * testing.T ) {
398+ // TestAtomicWriteFile_TargetNotWritable verifies that atomicWriteFile returns
399+ // an error when the target exists but is not writable ( directory). The
400+ // preflight OpenFile(O_WRONLY) check fails before any temp file is created .
401+ func TestAtomicWriteFile_TargetNotWritable (t * testing.T ) {
402402 if runtime .GOOS == "windows" {
403- t .Skip ("rename-over-directory test not reliable on Windows" )
403+ t .Skip ("not reliable on Windows" )
404404 }
405405
406406 dir := t .TempDir ()
407- // Create a directory at the target path.
407+ // Create a directory at the target path — OpenFile(O_WRONLY) on a
408+ // directory returns EISDIR on Linux, failing the preflight check.
408409 targetDir := filepath .Join (dir , "target" )
409410 require .NoError (t , os .Mkdir (targetDir , 0o755 ))
410411
411- // atomicWriteFile: the target exists, stat will succeed, OpenFile on a
412- // directory for O_WRONLY will fail on Linux (EISDIR).
413412 err := atomicWriteFile (targetDir , []byte ("data" ), 0o644 )
414413 require .Error (t , err , "expected error when target is a directory" )
415414}
416415
417- // TestAtomicWriteFile_WriteFailure causes tmp.Write to fail by filling the
418- // target directory's filesystem quota. Since we cannot easily exhaust disk
419- // space, we instead point the temp file into a read-only directory — but that
420- // would prevent CreateTemp too. Instead, we use a pipe trick: replace the
421- // temp dir with a read-only mount is not portable. The most portable approach
422- // is to write a very large file to a tmpfs of limited size. As an alternative,
423- // we test the Rename failure path (different from read-only target) by
424- // arranging the Chmod to fail.
425- //
426- // On Linux, Chmod of a temp file we own always succeeds, so the only reliably
427- // injectable post-CreateTemp failure is Rename. TestAtomicWriteFile_RenameFailure
428- // covers that. The write/sync/close paths are exercised implicitly by the
429- // successful write tests; their error returns are straightforward OS wrappers
430- // with no logic to branch on, so they do not contribute to the branch-coverage
431- // gap targeted here.
432- func TestAtomicWriteFile_TmpFileCleanedUpOnRenameFailure (t * testing.T ) {
416+ // TestAtomicWriteFile_NoTempFilesOnEarlyFailure verifies that no temp files
417+ // are created when atomicWriteFile fails at the preflight writability check
418+ // (before reaching CreateTemp). Using a directory as the target triggers the
419+ // same early-exit path as TestAtomicWriteFile_TargetNotWritable.
420+ func TestAtomicWriteFile_NoTempFilesOnEarlyFailure (t * testing.T ) {
433421 if runtime .GOOS == "windows" {
434422 t .Skip ("not reliable on Windows" )
435423 }
@@ -441,7 +429,7 @@ func TestAtomicWriteFile_TmpFileCleanedUpOnRenameFailure(t *testing.T) {
441429 err := atomicWriteFile (targetDir , []byte ("data" ), 0o644 )
442430 require .Error (t , err )
443431
444- // Verify no orphaned temp files remain in the directory after the failure .
432+ // No temp files should exist because the error occurred before CreateTemp .
445433 entries , err := os .ReadDir (dir )
446434 require .NoError (t , err )
447435 for _ , e := range entries {
0 commit comments