fix: 固定开发环境后端数据目录,避免 cwd 不同导致头像 404
从 cwd 向上解析 backend 根,并写入本地开发启动约定。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
90
backend/config/config_test.go
Normal file
90
backend/config/config_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func backendRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root, err := filepath.Abs(filepath.Join(wd, ".."))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func assertPath(t *testing.T, got, want string) {
|
||||
t.Helper()
|
||||
g, err := filepath.Abs(got)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w, err := filepath.Abs(want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if filepath.Clean(g) != filepath.Clean(w) {
|
||||
t.Fatalf("路径不符:\n got %s\n want %s", g, w)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWorkPathFromOfficialCwd(t *testing.T) {
|
||||
root := backendRoot(t)
|
||||
t.Chdir(root)
|
||||
got, err := resolveWorkPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertPath(t, got, root)
|
||||
}
|
||||
|
||||
func TestResolveWorkPathFromCmdJiang13(t *testing.T) {
|
||||
root := backendRoot(t)
|
||||
t.Chdir(filepath.Join(root, "cmd", "jiang13"))
|
||||
got, err := resolveWorkPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertPath(t, got, root)
|
||||
}
|
||||
|
||||
func TestResolveWorkPathFromRepoRoot(t *testing.T) {
|
||||
root := backendRoot(t)
|
||||
t.Chdir(filepath.Join(root, ".."))
|
||||
got, err := resolveWorkPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertPath(t, got, root)
|
||||
}
|
||||
|
||||
func TestResolveWorkPathEnvOverride(t *testing.T) {
|
||||
root := backendRoot(t)
|
||||
t.Setenv("JIANG13_WORK_PATH", root)
|
||||
t.Chdir(t.TempDir())
|
||||
got, err := resolveWorkPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertPath(t, got, root)
|
||||
}
|
||||
|
||||
func TestRejectStrayDataDir(t *testing.T) {
|
||||
err := rejectStrayDataDir(`C:\proj\backend\cmd\jiang13\data`)
|
||||
if err == nil {
|
||||
t.Fatal("期望拒绝 cmd/jiang13/data")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "拒绝使用数据目录") {
|
||||
t.Fatalf("错误文案不符: %v", err)
|
||||
}
|
||||
if err := rejectStrayDataDir(`C:\proj\backend\data`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user