Files
Reims-Go/internal/archtest/file_replace_test.go
T
NwflowerandClaude Opus 5.5 90fbce8b47 refactor: write small files through one atomic writer
Fifteen call sites each had their own temp-file-and-rename code. Some
used a fixed temporary name, so two writers could mix their content, and
most did not flush before the rename. They now share atomicfile.Write.

An architecture test keeps new os.Rename calls out of other packages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:59:56 -07:00

59 lines
2.3 KiB
Go

package archtest
import (
"go/ast"
"testing"
)
// renameOwners move something atomicfile cannot write: a directory, an image or
// download too large for memory, a rotated log, the running executable, or
// several files that must change together.
var renameOwners = map[string]bool{
"internal/app/guesttools_flow.go": true, // file copied into the shared folder
"internal/app/ui_health.go": true, // log rotation
"internal/atomicfile/atomicfile.go": true,
"internal/fetch/fetch.go": true, // download
"internal/macos/installer/accelerated.go": true, // download
"internal/macos/installer/hfs_image.go": true, // disk image
"internal/macos/installer/oc_cache.go": true, // download
"internal/macos/installer/recovery.go": true, // download
"internal/macos/installguide/progress.go": true, // macOS may not import atomicfile, see macosHostImports
"internal/macos/reims/network_record.go": true, // same
"internal/macos/opencore/image.go": true, // disk image with rollback
"internal/macos/reims/disk_import.go": true, // disk image with rollback
"internal/macos/reims/suspend_transaction.go": true, // state and metadata commit together
"internal/plugin/broker.go": true, // log rotation
"internal/plugin/pack.go": true, // archive
"internal/plugin/package.go": true, // plugin directory
"internal/runtime/runtime.go": true, // runtime directory
"internal/selfupdate/selfupdate.go": true, // running executable
}
func TestSmallFilesAreReplacedThroughAtomicfile(t *testing.T) {
seen := map[string]bool{}
for _, s := range sources(t) {
if s.isTest() {
continue
}
ast.Inspect(s.file, func(n ast.Node) bool {
sel, ok := n.(*ast.SelectorExpr)
if !ok || sel.Sel.Name != "Rename" {
return true
}
if pkg, ok := sel.X.(*ast.Ident); !ok || pkg.Name != "os" {
return true
}
seen[s.rel] = true
if !renameOwners[s.rel] {
t.Errorf("%s calls os.Rename: write small files with atomicfile.Write or atomicfile.WriteJSON", s.rel)
}
return true
})
}
for rel := range renameOwners {
if !seen[rel] {
t.Errorf("%s no longer calls os.Rename: remove it from renameOwners", rel)
}
}
}