| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestLingeringProcessesFindsWorkInsideDir(t *testing.T) { |
| 12 | if _, err := os.Stat("/proc/self/cwd"); err != nil { |
| 13 | t.Skip("no /proc on this platform") |
| 14 | } |
| 15 | worktree := t.TempDir() |
| 16 | sub := filepath.Join(worktree, "nested") |
| 17 | if err := os.Mkdir(sub, 0o755); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | // A sibling sharing the worktree's name as a prefix is not inside it. |
| 21 | sibling := worktree + "-other" |
| 22 | if err := os.Mkdir(sibling, 0o755); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | t.Cleanup(func() { os.RemoveAll(sibling) }) |
| 26 | |
| 27 | startSleep := func(dir string) { |
| 28 | c := exec.Command("sleep", "60") |
| 29 | c.Dir = dir |
| 30 | if err := c.Start(); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | t.Cleanup(func() { c.Process.Kill(); c.Wait() }) |
| 34 | } |
| 35 | |
| 36 | if got := lingeringProcesses(worktree); len(got) != 0 { |
| 37 | t.Fatalf("idle worktree reported %v", got) |
| 38 | } |
| 39 | startSleep(sibling) |
| 40 | if got := lingeringProcesses(worktree); len(got) != 0 { |
| 41 | t.Fatalf("process in sibling dir reported %v", got) |
| 42 | } |
| 43 | startSleep(sub) |
| 44 | if got := lingeringProcesses(worktree); !slices.Equal(got, []string{"sleep"}) { |
| 45 | t.Fatalf("lingeringProcesses = %v, want [sleep]", got) |
| 46 | } |
| 47 | } |