check-safety/workspace_test.go
2026-08-09 10:16:13 +08:00

65 lines
2.0 KiB
Go

package main
import (
"os"
"path/filepath"
"slices"
"testing"
)
func TestRepoRootsFollowConsolidatedWorkspaceContainingCWD(t *testing.T) {
workspace := scaffoldConsolidatedWorkspace(t)
t.Chdir(filepath.Join(workspace, "check-safety"))
if got, want := blockNinjaRepoRoot(), filepath.Join(workspace, "cms"); got != want {
t.Fatalf("blockNinjaRepoRoot() = %q, want %q", got, want)
}
if got, want := orchestratorRepoRoot(), filepath.Join(workspace, "orchestrator"); got != want {
t.Fatalf("orchestratorRepoRoot() = %q, want %q", got, want)
}
orchestratorBackend := filepath.Join(workspace, "orchestrator", "backend")
if got := inferAllowedPackagePrefixes(filepath.Join(workspace, "orchestrator"), orchestratorBackend); !slices.Equal(got, []string{"orchestrator."}) {
t.Fatalf("orchestrator prefixes = %v, want [orchestrator.]", got)
}
}
func TestSamePathRecognizesFilesystemAliases(t *testing.T) {
realDir := filepath.Join(t.TempDir(), "real")
if err := os.Mkdir(realDir, 0755); err != nil {
t.Fatalf("mkdir real directory: %v", err)
}
aliasDir := filepath.Join(t.TempDir(), "alias")
if err := os.Symlink(realDir, aliasDir); err != nil {
t.Fatalf("symlink alias: %v", err)
}
if !samePath(realDir, aliasDir) {
t.Fatalf("samePath(%q, %q) = false, want true", realDir, aliasDir)
}
}
func scaffoldConsolidatedWorkspace(t *testing.T) string {
t.Helper()
workspace := t.TempDir()
for _, dir := range []string{
filepath.Join(workspace, "cms", "backend"),
filepath.Join(workspace, "orchestrator", "backend"),
filepath.Join(workspace, "check-safety"),
} {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("mkdir %s: %v", dir, err)
}
}
for _, path := range []string{
filepath.Join(workspace, "cms", "backend", "go.mod"),
filepath.Join(workspace, "orchestrator", "backend", "go.mod"),
filepath.Join(workspace, "check-safety", "go.mod"),
} {
if err := os.WriteFile(path, []byte("module example.com/test\n"), 0644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
return workspace
}