|
5 | 5 | "fmt" |
6 | 6 | "os" |
7 | 7 | "path" |
| 8 | + "strings" |
8 | 9 | "testing" |
9 | 10 | "time" |
10 | 11 |
|
@@ -144,4 +145,44 @@ func TestSelfUpgrade(t *testing.T) { |
144 | 145 | t.Errorf("binary must not been updated") |
145 | 146 | } |
146 | 147 | }) |
| 148 | + |
| 149 | + t.Run("should not self-upgrade homebrew-managed binary", func(t *testing.T) { |
| 150 | + currentVersion := "v0.0.1" |
| 151 | + latestVersion := "v0.0.2" |
| 152 | + |
| 153 | + tempDir := t.TempDir() |
| 154 | + binaryPath := path.Join(tempDir, "Cellar", "lets", currentVersion, "bin", "lets") |
| 155 | + if err := os.MkdirAll(path.Dir(binaryPath), 0o755); err != nil { |
| 156 | + t.Fatalf("failed to create homebrew binary dir: %s", err) |
| 157 | + } |
| 158 | + |
| 159 | + if err := os.WriteFile(binaryPath, []byte(currentVersion), 0o755); err != nil { |
| 160 | + t.Fatalf("failed to write homebrew binary: %s", err) |
| 161 | + } |
| 162 | + |
| 163 | + upgrader := &BinaryUpgrader{ |
| 164 | + registry: &MockRegistry{latestVersion: latestVersion}, |
| 165 | + currentVersion: currentVersion, |
| 166 | + binaryPath: binaryPath, |
| 167 | + downloadPath: path.Join(tempDir, "lets.download"), |
| 168 | + backupPath: path.Join(tempDir, "lets.backup"), |
| 169 | + } |
| 170 | + |
| 171 | + err := upgrader.Upgrade(context.Background()) |
| 172 | + if err == nil { |
| 173 | + t.Fatal("expected homebrew upgrade error") |
| 174 | + } |
| 175 | + |
| 176 | + if !strings.Contains(err.Error(), "brew upgrade lets-cli/tap/lets") { |
| 177 | + t.Fatalf("expected homebrew upgrade command in error, got %q", err.Error()) |
| 178 | + } |
| 179 | + |
| 180 | + if !testVersion(binaryPath, currentVersion) { |
| 181 | + t.Errorf("expected version %s", currentVersion) |
| 182 | + } |
| 183 | + |
| 184 | + if _, err := os.Stat(upgrader.downloadPath); !os.IsNotExist(err) { |
| 185 | + t.Fatalf("expected no downloaded binary, got err %v", err) |
| 186 | + } |
| 187 | + }) |
147 | 188 | } |
0 commit comments