Files
RemoteCompilation/server/snapshot_rebase_test.go
T
tongtong 2cb70beb69 feat: 完善远程构建恢复、工程同步与变体选择
增加持久化构建记录、历史 APK 恢复、断点下载、Gradle 缓存复用、系统代理与按工程选择服务端 JDK。

支持已有工程目录、Git 状态检查、原生忽略规则和文本换行等价比较,修复干净检出与历史同步快照不一致时的误拦截。

优化插件设备可选构建、侧栏滚动、左右日志布局、时间显示和构建变体快速选择,并补充部署与协议文档。

验证:17 项 Go 测试、22 项插件测试及 HTTP/HTTPS 两条端到端测试通过;Linux/macOS 服务端交叉编译通过。
2026-11-25 02:10:52 +08:00

90 lines
3.3 KiB
Go

package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestCleanSameHeadCanResyncWithoutAcceptingStaleBuild(t *testing.T) {
if _, e := exec.LookPath("git"); e != nil {
t.Skip("Git unavailable")
}
s := testServer(t)
s.workspaceRoot = t.TempDir()
work := filepath.Join(s.workspaceRoot, "checkout")
os.MkdirAll(work, 0700)
git := func(args ...string) {
t.Helper()
if out, e := exec.Command("git", append([]string{"-C", work}, args...)...).CombinedOutput(); e != nil {
t.Fatalf("git: %v %s", e, out)
}
}
git("init", "-q")
git("config", "core.autocrlf", "false")
os.WriteFile(filepath.Join(work, "a.txt"), []byte("original\n"), 0600)
os.WriteFile(filepath.Join(work, "restored.txt"), []byte("tracked\n"), 0600)
git("add", ".")
git("-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-qm", "base")
g, e := gitSnapshot(work)
if e != nil {
t.Fatal(e)
}
w := call(t, s, "POST", "/projects", WorkspaceRequest{WorkspacePath: work, Git: g})
if w.Code != 200 {
t.Fatal(w.Body.String())
}
var out map[string]string
json.Unmarshal(w.Body.Bytes(), &out)
p := out["id"]
// A successful sync intentionally removed a tracked file; later checkout restores it.
w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "old", Files: map[string]File{"a.txt": file("original\n")}, Git: g})
if w.Code != 200 {
t.Fatal(w.Body.String())
}
git("restore", "--worktree", "--", "restored.txt")
// CRLF checkout is Git-clean, even though raw bytes no longer match the old snapshot.
git("config", "core.autocrlf", "true")
os.WriteFile(filepath.Join(work, "a.txt"), []byte("original\r\n"), 0600)
git("add", "--renormalize", ".")
g, e = gitSnapshot(work)
if e != nil || g.Dirty {
out, _ := gitCommand(work, nil, "status", "--porcelain")
diff, _ := gitCommand(work, nil, "diff", "--numstat")
t.Fatalf("fixture not Git-clean: %v %v status=%s diff=%s", g, e, out, diff)
}
originalRevision := s.projects[p].Revision
if e = s.checkGit(p, g); e != nil {
t.Fatal("clean same-HEAD checkout blocked", e)
}
manifest := s.manifest(p)
if manifest.Files["a.txt"].Hash != digest([]byte("original\r\n")) || manifest.Files["restored.txt"].Hash == "" || manifest.Revision != "" {
t.Fatal("manifest did not expose current checkout", manifest)
}
if s.projects[p].Revision != originalRevision || len(s.projects[p].Files) != 1 {
t.Fatal("read-only check rebased persistent snapshot")
}
w = call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"revision": "old", "task": ":app:assembleDebug"})
if w.Code != 409 {
t.Fatal("stale revision built", w.Body.String())
}
// Reuse the remote raw hash, preserving CRLF, while applying local deletion again.
w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "new", Files: map[string]File{"a.txt": {Hash: manifest.Files["a.txt"].Hash}}, Git: g})
if w.Code != 200 {
t.Fatal(w.Body.String())
}
if _, e = os.Stat(filepath.Join(work, "restored.txt")); !os.IsNotExist(e) {
t.Fatal("restored tracked deletion not synchronized")
}
if s.projects[p].Files["a.txt"].TextHash == "" {
t.Fatal("normalized baseline not persisted")
}
// A later real remote edit must remain protected.
os.WriteFile(filepath.Join(work, "a.txt"), []byte("remote real edit\n"), 0600)
if e = s.checkGit(p, g); e == nil {
t.Fatal("real remote edit was accepted")
}
}