Files
magpie/internal/library/backup_fresh_test.go
T
yetone a43fa6809b library: two changes in one millisecond each get a backup folder of their own (#1743)
liubing reported on #1743 that TestRemoveFoundEntryEveryFormat failed
in a full run on Linux, the backup read holding another format's
content, on an unmodified main too. The backup folder was named by
the millisecond and taken as this change's own: a second change in
the same millisecond wrote into the first's folder, its file kept
under its folder's name since the first's held the name, and the
copy at the name the change wrote was the other change's. The test's
loop runs one change per format, all named config.json, and on a fast
machine two land in one millisecond. A user's two quick changes did
the same: the backup the second change named wasn't its own.

freshBackup makes the folder with os.Mkdir, adding -2, -3... when the
millisecond's is taken. pruneBackups' sort keeps them in order.
Siblings checked: the skill backups (keepLibrarySkill, keepReplaced,
setAside, carry's and RemoveSkills' aside) use per-skill paths under
the stamp; setAside already steps past a taken name, and the others
only collide on the same skill in the same millisecond, where move
fails loudly rather than mixing; they are left as they were, since
a folder each would split one change's skills over many folders.

Run: go test -tags nogui ./internal/library/ ok on macOS and in
Docker golang:1.26.8 GOTOOLCHAIN=local (also -count=30 of the two
tests). The original red didn't show in Docker in 100 runs; it was
reproduced by injecting a minute-wide stamp, which turned
TestRemoveFoundEntryEveryFormat red on the old code and leaves it
green with the fix. Broken by hand: os.Mkdir -> os.MkdirAll (no
claim on the folder) turns TestBackupsInOneMillisecond red.
2026-10-13 06:43:50 +08:00

35 lines
1.0 KiB
Go

package library
import (
"path/filepath"
"testing"
"time"
)
// Two changes in one millisecond each keep their own copy at the name
// they wrote, not the other's (TestRemoveFoundEntryEveryFormat on a fast
// Linux machine, #1743).
func TestBackupsInOneMillisecond(t *testing.T) {
sandbox(t)
at := time.Date(2026, 10, 13, 12, 0, 0, 0, time.Local)
was := backupNow
backupNow = func() time.Time { return at }
t.Cleanup(func() { backupNow = was })
var runs []*backups
for _, body := range []string{"first", "second", "third"} {
p := filepath.Join(t.TempDir(), "config.json")
write(t, p, body)
b := newBackups()
if err := b.keep("x", p); err != nil {
t.Fatal(err)
}
runs = append(runs, b)
if got := read(t, filepath.Join(b.dir, "x", "config.json")); got != body {
t.Fatalf("the %s change's backup holds %q", body, got)
}
}
if runs[0].dir == runs[1].dir || runs[1].dir == runs[2].dir || runs[0].dir == runs[2].dir {
t.Fatalf("changes share a folder: %s %s %s", runs[0].dir, runs[1].dir, runs[2].dir)
}
}