|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// --- extractMPRFromArgs --- |
| 13 | + |
| 14 | +func TestExtractMPRFromArgs_ShortFlag(t *testing.T) { |
| 15 | + got := extractMPRFromArgs([]string{"-p", "app.mpr", "-c", "show entities"}) |
| 16 | + if got != "app.mpr" { |
| 17 | + t.Errorf("got %q, want app.mpr", got) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestExtractMPRFromArgs_LongFlag(t *testing.T) { |
| 22 | + got := extractMPRFromArgs([]string{"--project", "app.mpr"}) |
| 23 | + if got != "app.mpr" { |
| 24 | + t.Errorf("got %q, want app.mpr", got) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestExtractMPRFromArgs_EqualForm(t *testing.T) { |
| 29 | + got := extractMPRFromArgs([]string{"--project=app.mpr", "-c", "show"}) |
| 30 | + if got != "app.mpr" { |
| 31 | + t.Errorf("got %q, want app.mpr", got) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestExtractMPRFromArgs_None(t *testing.T) { |
| 36 | + got := extractMPRFromArgs([]string{"--help"}) |
| 37 | + if got != "" { |
| 38 | + t.Errorf("got %q, want empty", got) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestExtractMPRFromArgs_DanglingFlag(t *testing.T) { |
| 43 | + // -p at end of slice (no value follows) |
| 44 | + got := extractMPRFromArgs([]string{"-p"}) |
| 45 | + if got != "" { |
| 46 | + t.Errorf("got %q, want empty", got) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// --- mprDaemonSocketPath --- |
| 51 | + |
| 52 | +func TestMPRDaemonSocketPath_Deterministic(t *testing.T) { |
| 53 | + e := newTestEnv(t) |
| 54 | + a := e.mprDaemonSocketPath("/home/user/project.mpr") |
| 55 | + b := e.mprDaemonSocketPath("/home/user/project.mpr") |
| 56 | + if a != b { |
| 57 | + t.Errorf("same input produced different paths: %q vs %q", a, b) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestMPRDaemonSocketPath_DifferentMPR(t *testing.T) { |
| 62 | + e := newTestEnv(t) |
| 63 | + a := e.mprDaemonSocketPath("/home/user/project-a.mpr") |
| 64 | + b := e.mprDaemonSocketPath("/home/user/project-b.mpr") |
| 65 | + if a == b { |
| 66 | + t.Errorf("different MPR paths produced same socket: %q", a) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestMPRDaemonSocketPath_HasMprPrefix(t *testing.T) { |
| 71 | + e := newTestEnv(t) |
| 72 | + p := e.mprDaemonSocketPath("/some/project.mpr") |
| 73 | + base := filepath.Base(p) |
| 74 | + if !strings.HasPrefix(base, "mpr-") { |
| 75 | + t.Errorf("socket name %q does not start with mpr-", base) |
| 76 | + } |
| 77 | + if !strings.HasSuffix(base, ".sock") { |
| 78 | + t.Errorf("socket name %q does not end with .sock", base) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestMPRDaemonSocketPath_InDaemonDir(t *testing.T) { |
| 83 | + e := newTestEnv(t) |
| 84 | + p := e.mprDaemonSocketPath("/some/project.mpr") |
| 85 | + if !strings.HasPrefix(p, e.daemonDir()) { |
| 86 | + t.Errorf("socket path %q is not under daemonDir %q", p, e.daemonDir()) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// --- mprSocketPrefix --- |
| 91 | + |
| 92 | +func TestMPRSocketPrefix_SameForSameMPR(t *testing.T) { |
| 93 | + e := newTestEnv(t) |
| 94 | + a := e.mprSocketPrefix("/home/user/project.mpr") |
| 95 | + b := e.mprSocketPrefix("/home/user/project.mpr") |
| 96 | + if a != b { |
| 97 | + t.Errorf("same MPR produced different prefix: %q vs %q", a, b) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestMPRSocketPrefix_DifferentForDifferentMPR(t *testing.T) { |
| 102 | + e := newTestEnv(t) |
| 103 | + a := e.mprSocketPrefix("/home/user/a.mpr") |
| 104 | + b := e.mprSocketPrefix("/home/user/b.mpr") |
| 105 | + if a == b { |
| 106 | + t.Errorf("different MPR produced same prefix: %q", a) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// --- cleanupStaleMPRSockets --- |
| 111 | + |
| 112 | +func TestCleanupStaleMPRSockets_RemovesSameMPROldSocket(t *testing.T) { |
| 113 | + e := newTestEnv(t) |
| 114 | + if err := os.MkdirAll(e.daemonDir(), 0755); err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + mprPath := "/home/user/project.mpr" |
| 118 | + |
| 119 | + // Create a fake stale socket with the same MPR prefix but different binary hash suffix. |
| 120 | + prefix := e.mprSocketPrefix(mprPath) |
| 121 | + stale := filepath.Join(e.daemonDir(), prefix+"aabbcc.sock") |
| 122 | + if err := os.WriteFile(stale, []byte{}, 0644); err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + |
| 126 | + // current socket should NOT be deleted. |
| 127 | + current := e.mprDaemonSocketPath(mprPath) |
| 128 | + if err := os.WriteFile(current, []byte{}, 0644); err != nil { |
| 129 | + t.Fatal(err) |
| 130 | + } |
| 131 | + |
| 132 | + e.cleanupStaleMPRSockets(mprPath, current) |
| 133 | + |
| 134 | + if _, err := os.Stat(stale); !os.IsNotExist(err) { |
| 135 | + t.Error("stale same-MPR socket should have been removed") |
| 136 | + } |
| 137 | + if _, err := os.Stat(current); os.IsNotExist(err) { |
| 138 | + t.Error("current socket should NOT have been removed") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestCleanupStaleMPRSockets_RemovesDeadOrphanSocket(t *testing.T) { |
| 143 | + e := newTestEnv(t) |
| 144 | + if err := os.MkdirAll(e.daemonDir(), 0755); err != nil { |
| 145 | + t.Fatal(err) |
| 146 | + } |
| 147 | + |
| 148 | + // Orphan socket from a different MPR (different prefix) that is NOT alive. |
| 149 | + orphan := filepath.Join(e.daemonDir(), "mpr-deadbeef-0a0b.sock") |
| 150 | + if err := os.WriteFile(orphan, []byte{}, 0644); err != nil { |
| 151 | + t.Fatal(err) |
| 152 | + } |
| 153 | + |
| 154 | + current := e.mprDaemonSocketPath("/home/user/project.mpr") |
| 155 | + e.cleanupStaleMPRSockets("/home/user/project.mpr", current) |
| 156 | + |
| 157 | + if _, err := os.Stat(orphan); !os.IsNotExist(err) { |
| 158 | + t.Error("dead orphan socket should have been removed") |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestCleanupStaleMPRSockets_KeepsNonSocketFiles(t *testing.T) { |
| 163 | + e := newTestEnv(t) |
| 164 | + if err := os.MkdirAll(e.daemonDir(), 0755); err != nil { |
| 165 | + t.Fatal(err) |
| 166 | + } |
| 167 | + |
| 168 | + // Non-.sock file in daemon dir should be untouched. |
| 169 | + other := filepath.Join(e.daemonDir(), "version") |
| 170 | + if err := os.WriteFile(other, []byte("v1"), 0644); err != nil { |
| 171 | + t.Fatal(err) |
| 172 | + } |
| 173 | + |
| 174 | + current := e.mprDaemonSocketPath("/some/project.mpr") |
| 175 | + e.cleanupStaleMPRSockets("/some/project.mpr", current) |
| 176 | + |
| 177 | + if _, err := os.Stat(other); os.IsNotExist(err) { |
| 178 | + t.Error("non-socket file should not have been removed") |
| 179 | + } |
| 180 | +} |
0 commit comments