| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func requireTestPathWithin(t *testing.T, root, path string) { |
| 11 | t.Helper() |
| 12 | rel, err := filepath.Rel(root, path) |
| 13 | if err != nil || filepath.IsAbs(rel) || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) { |
| 14 | t.Fatalf("test path %q escapes isolated root %q", path, root) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | func TestProcessTestEnvironmentContainsAllUserStatePaths(t *testing.T) { |
| 19 | home := os.Getenv("HOME") |
| 20 | paths := map[string]string{ |
| 21 | "operating system cache": osUserCacheDir(), |
| 22 | "Reasonix cache": CacheDir(), |
| 23 | "workspace leases": WorkspaceLeaseDir(), |
| 24 | "delivery worktrees": DeliveryWorktreeDir(), |
| 25 | } |
| 26 | for name, path := range paths { |
| 27 | t.Run(name, func(t *testing.T) { |
| 28 | requireTestPathWithin(t, home, path) |
| 29 | }) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // TestStatsDirResolution pins the stats data root: <state home>/stats, where |
| 34 | // the state home is REASONIX_STATE_HOME when set and REASONIX_HOME otherwise. |
| 35 | // Usage records live under the state root — not the install directory, which |
| 36 | // is replaced on upgrade — so a wrong resolution would silently lose stats. |
| 37 | func TestStatsDirResolution(t *testing.T) { |
| 38 | home := t.TempDir() |
| 39 | state := t.TempDir() |
| 40 | t.Setenv("REASONIX_HOME", home) |
| 41 | if got := StatsDir(); got != filepath.Join(home, "stats") { |
| 42 | t.Fatalf("REASONIX_HOME stats dir: want %q, got %q", filepath.Join(home, "stats"), got) |
| 43 | } |
| 44 | t.Setenv("REASONIX_STATE_HOME", state) |
| 45 | if got := StatsDir(); got != filepath.Join(state, "stats") { |
| 46 | t.Fatalf("REASONIX_STATE_HOME stats dir: want %q, got %q", filepath.Join(state, "stats"), got) |
| 47 | } |
| 48 | } |
| 49 |