Files
RemoteCompilation/server/workspace_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

137 lines
4.8 KiB
Go

package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestManifestRechecksActualContent(t *testing.T) {
s := testServer(t)
p := project(t, s)
call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "r", Files: map[string]File{"a.txt": file("source")}})
os.WriteFile(filepath.Join(s.work(p), "a.txt"), []byte("changed by build"), 0600)
w := call(t, s, "GET", "/projects/"+p+"/manifest", nil)
var actual Project
json.Unmarshal(w.Body.Bytes(), &actual)
if len(actual.Files) != 0 {
t.Fatal("advertised stale hash, causing missing content")
}
if w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "r2", Files: map[string]File{"a.txt": file("source")}}); w.Code != 200 {
t.Fatal(w.Body.String())
}
}
func TestExistingWorkspaceBoundaryAndGitProtection(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, "中文 project")
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")
os.WriteFile(filepath.Join(work, "a.txt"), []byte("base"), 0600)
os.WriteFile(filepath.Join(work, ".gitignore"), []byte("ignored/\n"), 0600)
git("add", ".")
git("-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-qm", "base")
snapshot, e := gitSnapshot(work)
if e != nil {
t.Fatal(e)
}
req := WorkspaceRequest{WorkspacePath: work, Git: snapshot}
w := call(t, s, "POST", "/projects/check", req)
if w.Code != 200 || !strings.Contains(w.Body.String(), `"canSync":true`) {
t.Fatal(w.Body.String())
}
if len(s.projects) != 0 {
t.Fatal("manual check mutated server projects")
}
mismatch := *snapshot
mismatch.Head = "different"
req.Git = &mismatch
w = call(t, s, "POST", "/projects/check", req)
if !strings.Contains(w.Body.String(), `"canSync":false`) {
t.Fatal("mismatched HEAD accepted")
}
req.Git = snapshot
w = call(t, s, "POST", "/projects", req)
if w.Code != 200 {
t.Fatal(w.Body.String())
}
var allocated map[string]string
json.Unmarshal(w.Body.Bytes(), &allocated)
p := allocated["id"]
if s.work(p) != work {
t.Fatal("selected existing path not used")
}
// Local dirty content is intentional; the server must accept subsequent syncs of its own snapshot.
files := map[string]File{}
for path, f := range s.projects[p].Files {
files[path] = f
}
files["a.txt"] = file("local edit")
w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "new", Files: files, Git: snapshot})
if w.Code != 200 {
t.Fatal(w.Body.String())
}
req.ProjectID = p
w = call(t, s, "POST", "/projects/check", req)
if !strings.Contains(w.Body.String(), `"canSync":true`) {
t.Fatal(w.Body.String())
}
os.WriteFile(filepath.Join(work, "a.txt"), []byte("remote edit"), 0600)
w = call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "next", Files: files, Git: snapshot})
if w.Code != 409 {
t.Fatal("remote edit overwritten", w.Body.String())
}
if w = call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"revision": "new", "task": ":app:assembleDebug"}); w.Code != 409 {
t.Fatal("built changed source under stale revision", w.Body.String())
}
req.WorkspacePath = t.TempDir()
if w = call(t, s, "POST", "/projects/check", req); w.Code != 400 {
t.Fatal("allowed root escaped")
}
req.WorkspacePath = filepath.Join(s.workspaceRoot, "missing")
if w = call(t, s, "POST", "/projects/check", req); w.Code != 400 {
t.Fatal("missing folder accepted")
}
// Directory browsing lists folders within the configured root without binding them.
w = call(t, s, "GET", "/directories", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), "中文 project") {
t.Fatal(w.Body.String())
}
if w = call(t, s, "GET", "/directories?path="+filepath.ToSlash(t.TempDir()), nil); w.Code != 400 {
t.Fatal("directory browser escaped root")
}
}
func TestNativeBuildCachesCannotBeSyncedOrDeleted(t *testing.T) {
s := testServer(t)
p := project(t, s)
path := "app/.cxx/Debug/arm64-v8a/.ninja_deps"
if validPath(path) {
t.Fatal("native cache accepted")
}
os.MkdirAll(filepath.Dir(filepath.Join(s.work(p), filepath.FromSlash(path))), 0700)
os.WriteFile(filepath.Join(s.work(p), filepath.FromSlash(path)), []byte("server cache"), 0600)
s.projects[p].Files[path] = File{Hash: digest([]byte("old upload"))}
w := call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "clean", Files: map[string]File{}})
if w.Code != 200 {
t.Fatal(w.Body.String())
}
if _, e := os.Stat(filepath.Join(s.work(p), filepath.FromSlash(path))); e != nil {
t.Fatal("server cache deleted", e)
}
if len(s.projects[p].Files) != 0 {
t.Fatal("cache stayed in source manifest")
}
}