gateway: TestFetchPrivateEntries puts settings.json's bytes back, not a Save over "{not json"

ubuntu CI at 836d074f1 (run 43242922324, race) failed
TestRemoteMagpieAskedToFollow at remote_update_test.go:156: "could not
read settings ... invalid character 'n' looking for beginning of object
key string". The 'n' is TestFetchPrivateEntries' "{not json". That test
shares the package's HOME and restored it with allowPrivate's
t.Cleanup(settings.Save(was)), but Save refuses to write over a file it
can't parse (unless a last good generation is there), and the cleanup
dropped the error. The broken file was left to whichever later test
saved settings next without fresh(t).

It now reads the file's bytes before garbling it and writes them back in
a cleanup that runs before allowPrivate's, and reports a failed write.

Run (mote): go test -tags nogui -count=1 -run
'TestFetchPrivateEntries$|TestRemoteMagpieAskedToFollow$'
./internal/gateway: red on origin/main (the CI message, word for word),
ok with this change. gofmt -l empty.
Siblings checked: allow_test's garble runs under fresh(t);
backup's and davsync's unreadable-settings tests use their own HOMEs;
request_settings_once and the cli update tests write the bytes back.
Whole-package run: after the push (below in the loop's record).
This commit is contained in:
yetone
2026-11-13 00:06:43 +08:00
parent bbce1fc1ab
commit aa744b9ed2
@@ -222,6 +222,17 @@ func TestFetchPrivateEntries(t *testing.T) {
t.Errorf("%s: %v", ip, !ok)
}
}
// the package's tests share one HOME: the file's own bytes go back
// before allowPrivate's Save, which refuses to write over "{not json"
raw, err := os.ReadFile(settings.Path())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.WriteFile(settings.Path(), raw, 0o600); err != nil {
t.Error(err)
}
})
if err := os.WriteFile(settings.Path(), []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}