| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // TestResolveWorkspaceRootExplicitAndGitFallback proves the --dir contract: |
| 10 | // an explicit workspace root is honored even inside a git repository, while an |
| 11 | // empty root still falls back to the nearest git root from the working directory. |
| 12 | func TestResolveWorkspaceRootExplicitAndGitFallback(t *testing.T) { |
| 13 | repo := t.TempDir() |
| 14 | if err := os.Mkdir(filepath.Join(repo, ".git"), 0o755); err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | sub := filepath.Join(repo, "a", "b") |
| 18 | if err := os.MkdirAll(sub, 0o755); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | |
| 22 | // Explicit --dir pins the workspace root, not the git root of the repo. |
| 23 | if got := resolveWorkspaceRoot(sub); got != sub { |
| 24 | t.Fatalf("resolveWorkspaceRoot(%q) = %q, want the explicit dir", sub, got) |
| 25 | } |
| 26 | |
| 27 | // No explicit root: fall back to the nearest git root from the CWD. |
| 28 | t.Chdir(sub) |
| 29 | got := resolveWorkspaceRoot("") |
| 30 | wantRoot, err := filepath.EvalSymlinks(repo) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | gotResolved, err := filepath.EvalSymlinks(got) |
| 35 | if err != nil { |
| 36 | t.Fatalf("resolveWorkspaceRoot(%q) returned unusable path %q: %v", "", got, err) |
| 37 | } |
| 38 | if gotResolved != wantRoot { |
| 39 | t.Fatalf("resolveWorkspaceRoot(\"\") = %q, want git root %q", got, repo) |
| 40 | } |
| 41 | } |
| 42 |