Files
magpie/internal/steady/steady_windows_test.go
T
yetone 7dd809002b accounts: on Linux an account put first is read first at once, and on Windows a file being replaced isn't read as none (goal: 插件订阅 provider 跟内置的所有逻辑都要一模一样, run on Windows and Linux) — the provider tests run on the Windows test box and in WSL found two bugs of magpie's own files. Linux stamps a file's time from a clock a tick (a few milliseconds) coarse, and filememo took same time and size for the same file: two accounts swapped (Devin, Grok, WorkBuddy, ZCode, Claude…) read back in the old order, so magpie kept using the account the user had just moved down, and a moved-back plugin's accounts read as before. A file written within the last two seconds is now also compared by its bytes. On Windows a file being renamed over refuses to open for a moment ("Access is denied"), and one held open refuses to be renamed over: plugin-auth.json, plugins.json and the plugin providers read as nothing (a usage fetch right after a move showed no cards), logins.json failed to save, and host.js, reading plugin-auth.json as {}, would have written the other accounts away on the next sign-in. New internal/steady waits such a read or rename out (half a second at most, Windows only); filememo, edit.WriteAtomic, writePrivate, Kiro's writes and the plugin store go through it, host.js retries its read and rename, and the plugin store writes its files by a rename. Tests: USERPROFILE beside HOME in the test homes, the fake devin (a shell script) skipped on Windows, plugin-auth.json's mode bits unchecked there. TestReadSameSizeSameTick fails on Linux and TestRenameOverAFileHeldOpen/TestReadAFileHeldOpen on Windows without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; on Linux (WSL) the provider and plugin packages pass whole (13 failed before), on Windows the plugin package and the move/plugin/Grok/Devin/usage tests pass 10 times over (TestMoveToPlugin failed one run in a few before); 15 built-in tests that assume Unix (mode bits, Keychain, shell-script CLIs) still fail there, as before.
2026-10-01 13:27:12 +08:00

61 lines
1.8 KiB
Go

package steady
import (
"os"
"path/filepath"
"syscall"
"testing"
"time"
)
// a reader holding the file without FILE_SHARE_DELETE, as other programs
// open it, for a moment: os.Rename over it is refused, Rename waits it out
func TestRenameOverAFileHeldOpen(t *testing.T) {
dir := t.TempDir()
p, tmp := filepath.Join(dir, "logins.json"), filepath.Join(dir, "logins.json.tmp")
os.WriteFile(p, []byte("old"), 0o600)
os.WriteFile(tmp, []byte("new"), 0o600)
name, _ := syscall.UTF16PtrFromString(p)
h, err := syscall.CreateFile(name, syscall.GENERIC_READ, syscall.FILE_SHARE_READ, nil, syscall.OPEN_EXISTING, 0, 0)
if err != nil {
t.Fatal(err)
}
if err := os.Rename(tmp, p); err == nil {
t.Fatal("renamed over a file held open: nothing to wait out")
}
go func() {
time.Sleep(100 * time.Millisecond)
syscall.CloseHandle(h)
}()
if err := Rename(tmp, p); err != nil {
t.Fatal(err)
}
if b, err := ReadFile(p); err != nil || string(b) != "new" {
t.Fatalf("read %q, %v", b, err)
}
}
// and a file held so refuses to be read until let go
func TestReadAFileHeldOpen(t *testing.T) {
p := filepath.Join(t.TempDir(), "plugin-auth.json")
os.WriteFile(p, []byte("accounts"), 0o600)
name, _ := syscall.UTF16PtrFromString(p)
h, err := syscall.CreateFile(name, syscall.GENERIC_WRITE, 0, nil, syscall.OPEN_EXISTING, 0, 0)
if err != nil {
t.Fatal(err)
}
if _, err := os.ReadFile(p); err == nil {
t.Fatal("read a file held unshared: nothing to wait out")
}
go func() {
time.Sleep(100 * time.Millisecond)
syscall.CloseHandle(h)
}()
if b, err := ReadFile(p); err != nil || string(b) != "accounts" {
t.Fatalf("read %q, %v", b, err)
}
if _, err := ReadFile(filepath.Join(filepath.Dir(p), "none")); !os.IsNotExist(err) {
t.Fatalf("a file not there: %v", err)
}
}