升级版本并改用内部账号存储
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package seckill
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAccountStoreMigratesLegacyFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
legacy := filepath.Join(dir, "data", "user.txt")
|
||||
target := filepath.Join(dir, "app-data", "accounts.txt")
|
||||
content := []byte("# 旧账号\r\nfoo----password----cookie-a\r\n")
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(legacy), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(legacy, content, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store := NewAccountStore(target, legacy)
|
||||
store.Load()
|
||||
accounts := store.Snapshot()
|
||||
if len(accounts) != 1 || accounts[0].Name != "foo" || accounts[0].Cookie != "cookie-a" {
|
||||
t.Fatalf("迁移后的账号 = %#v", accounts)
|
||||
}
|
||||
if _, err := os.Stat(legacy); err != nil {
|
||||
t.Fatalf("旧账号文件不应被删除: %v", err)
|
||||
}
|
||||
info, err := os.Stat(target)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Mode().Perm() != 0600 {
|
||||
t.Fatalf("账号库权限 = %o, want 0600", info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user