| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Both ways of reading processes, over real ones: /proc, which Linux has, |
| 13 | // and ps with lsof, which macOS reads them with. Each runs wherever it can. |
| 14 | // The worktree's name holds what lsof prints escaped. |
| 15 | func TestLingeringProcessesFindsWorkInsideDir(t *testing.T) { |
| 16 | for _, r := range []struct { |
| 17 | name string |
| 18 | ready func() error |
| 19 | find func(worktree string) ([]string, error) |
| 20 | }{ |
| 21 | { |
| 22 | name: "proc", |
| 23 | ready: func() error { |
| 24 | _, err := os.Stat("/proc/self/cwd") |
| 25 | return err |
| 26 | }, |
| 27 | find: lingeringProcesses, |
| 28 | }, |
| 29 | { |
| 30 | name: "ps", |
| 31 | ready: func() error { |
| 32 | for _, tool := range []string{"ps", "lsof"} { |
| 33 | if _, err := exec.LookPath(tool); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | } |
| 37 | return nil |
| 38 | }, |
| 39 | find: func(worktree string) ([]string, error) { |
| 40 | root, err := filepath.EvalSymlinks(worktree) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | procs, err := psProcesses() |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | return lingering(root, os.Getpid(), procs) |
| 49 | }, |
| 50 | }, |
| 51 | } { |
| 52 | t.Run(r.name, func(t *testing.T) { |
| 53 | if err := r.ready(); err != nil { |
| 54 | if r.name == "ps" && os.Getenv("KLAUS_TEST_TOOLS") == "required" { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | t.Skip(err) |
| 58 | } |
| 59 | worktree := filepath.Join(t.TempDir(), "caf\u00e9 a\\b\nrun") |
| 60 | sub := filepath.Join(worktree, "nested") |
| 61 | if err := os.Mkdir(worktree, 0o755); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if err := os.Mkdir(sub, 0o755); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | // A sibling sharing the worktree's name as a prefix is not inside it. |
| 68 | sibling := worktree + "-other" |
| 69 | if err := os.Mkdir(sibling, 0o755); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | t.Cleanup(func() { os.RemoveAll(sibling) }) |
| 73 | |
| 74 | startSleep := func(dir string) { |
| 75 | c := exec.Command("sleep", "60") |
| 76 | c.Dir = dir |
| 77 | if err := c.Start(); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | t.Cleanup(func() { c.Process.Kill(); c.Wait() }) |
| 81 | } |
| 82 | |
| 83 | find := func() []string { |
| 84 | got, err := r.find(worktree) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | return got |
| 89 | } |
| 90 | if got := find(); len(got) != 0 { |
| 91 | t.Fatalf("idle worktree reported %v", got) |
| 92 | } |
| 93 | startSleep(sibling) |
| 94 | if got := find(); len(got) != 0 { |
| 95 | t.Fatalf("process in sibling dir reported %v", got) |
| 96 | } |
| 97 | startSleep(sub) |
| 98 | if got := find(); !slices.Equal(got, []string{"sleep"}) { |
| 99 | t.Fatalf("lingeringProcesses = %v, want [sleep]", got) |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // ps and lsof as macOS prints them: "??" for no terminal and a command name |
| 106 | // with a space in it. This process's ancestors, a process on another |
| 107 | // terminal, and one beside the worktree are not lingering work. |
| 108 | func TestLingeringFromMacOSListing(t *testing.T) { |
| 109 | ps := ` 1 0 ?? launchd |
| 110 | 400 1 ttys001 login |
| 111 | 401 400 ttys001 zsh |
| 112 | 500 401 ttys001 klaus |
| 113 | 600 1 ?? nix daemon |
| 114 | 601 500 ttys001 go |
| 115 | 700 1 ttys002 vim |
| 116 | 900 1 ?? sleep |
| 117 | ` |
| 118 | lsof := "p401\nfcwd\nn/work/run\np500\nfcwd\nn/work/run\np600\nfcwd\nn/work/run/sub\n" + |
| 119 | "p601\nfcwd\nn/work/run\np700\nfcwd\nn/work/run\np900\nfcwd\nn/work/run-other\n" |
| 120 | got, err := lingering("/work/run", 500, fromPSAndLsof(ps, lsof)) |
| 121 | if want := []string{"go", "nix daemon"}; err != nil || !slices.Equal(got, want) { |
| 122 | t.Fatalf("lingering = %v, %v, want %v", got, err, want) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Names as lsof prints them, in the C locale psProcesses runs it in. |
| 127 | func TestLsofName(t *testing.T) { |
| 128 | for _, c := range []struct { |
| 129 | printed, name string |
| 130 | part bool |
| 131 | }{ |
| 132 | {printed: `/work/caf\xc3\xa9/run`, name: "/work/caf\u00e9/run"}, |
| 133 | {printed: `/work/a\\b`, name: `/work/a\b`}, |
| 134 | {printed: `/work/my run`, name: "/work/my run"}, |
| 135 | {printed: `/work/a\nb`, name: "/work/a\nb"}, |
| 136 | // ^ is printed as itself: ^H is literal, as a backspace prints as \b, |
| 137 | // but ^A is either ^ and A or 0x01. |
| 138 | {printed: `/work/^H`, name: "/work/^H"}, |
| 139 | {printed: `/work/^Arun`, name: "/work/", part: true}, |
| 140 | // lsof prints no \q, nor a backslash at the end. |
| 141 | {printed: `/work/a\qb`, name: "/work/a", part: true}, |
| 142 | {printed: `/work/a\`, name: "/work/a", part: true}, |
| 143 | } { |
| 144 | name, part := lsofName(c.printed) |
| 145 | if name != c.name || part != c.part { |
| 146 | t.Errorf("lsofName(%q) = %q, %v, want %q, %v", c.printed, name, part, c.name, c.part) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // A name lsofName reads only the start of is under the worktree when that |
| 152 | // start is, outside it when that start leaves it, and otherwise unknown: an |
| 153 | // error, beside the commands that are known, even one of the same name. |
| 154 | func TestLingeringWithPartialNames(t *testing.T) { |
| 155 | ps := ` 500 1 ttys001 klaus |
| 156 | 600 1 ?? make |
| 157 | 601 1 ?? sleep |
| 158 | 602 1 ?? make |
| 159 | 603 1 ?? vim |
| 160 | ` |
| 161 | lsof := "p600\nfcwd\nn/work/run/^Abuild\np601\nfcwd\nn/work/ru^An\n" + |
| 162 | "p602\nfcwd\nn/work/r\\qun\np603\nfcwd\nn/home/^A/run\n" |
| 163 | got, err := lingering("/work/run", 500, fromPSAndLsof(ps, lsof)) |
| 164 | if !slices.Equal(got, []string{"make"}) { |
| 165 | t.Fatalf("lingering = %v, want [make]", got) |
| 166 | } |
| 167 | if err == nil { |
| 168 | t.Fatal("working directories that may be under the worktree read as known") |
| 169 | } |
| 170 | for _, unknown := range []string{"pid 601", "pid 602"} { |
| 171 | if !strings.Contains(err.Error(), unknown) { |
| 172 | t.Errorf("error %q does not name %s", err, unknown) |
| 173 | } |
| 174 | } |
| 175 | if strings.Contains(err.Error(), "pid 603") { |
| 176 | t.Errorf("error %q names a process outside the worktree", err) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Without ps or lsof the processes cannot be read, and that is an error, not |
| 181 | // an empty list. |
| 182 | func TestPSProcessesFailsWithoutTheTools(t *testing.T) { |
| 183 | ps, err := exec.LookPath("ps") |
| 184 | if err != nil { |
| 185 | if os.Getenv("KLAUS_TEST_TOOLS") == "required" { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | t.Skip(err) |
| 189 | } |
| 190 | t.Setenv("PATH", t.TempDir()) |
| 191 | if _, err := psProcesses(); err == nil { |
| 192 | t.Fatal("psProcesses with no ps on PATH read processes") |
| 193 | } |
| 194 | onlyPS := t.TempDir() |
| 195 | if err := os.Symlink(ps, filepath.Join(onlyPS, "ps")); err != nil { |
| 196 | t.Fatal(err) |
| 197 | } |
| 198 | t.Setenv("PATH", onlyPS) |
| 199 | if _, err := psProcesses(); err == nil { |
| 200 | t.Fatal("psProcesses with no lsof on PATH read processes") |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // A worktree that is gone holds nothing; one that cannot be resolved is an |
| 205 | // error. |
| 206 | func TestLingeringProcessesResolvesTheWorktree(t *testing.T) { |
| 207 | dir := t.TempDir() |
| 208 | if got, err := lingeringProcesses(filepath.Join(dir, "gone")); err != nil || len(got) != 0 { |
| 209 | t.Fatalf("a missing worktree: %v, %v", got, err) |
| 210 | } |
| 211 | loop := filepath.Join(dir, "loop") |
| 212 | if err := os.Symlink(loop, loop); err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | if _, err := lingeringProcesses(loop); err == nil { |
| 216 | t.Fatal("a worktree in a symlink loop read as holding nothing") |
| 217 | } |
| 218 | } |