TestApplyCleansTempOnFailure: a failed download (server returns 500) must not leave a half-written temp file in the install directory.
(t *testing.T)
| 245 | // TestApplyCleansTempOnFailure: a failed download (server returns 500) |
| 246 | // must not leave a half-written temp file in the install directory. |
| 247 | func TestApplyCleansTempOnFailure(t *testing.T) { |
| 248 | asset := platformAsset(t) |
| 249 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 250 | switch { |
| 251 | case strings.HasSuffix(req.URL.Path, "checksums.txt"): |
| 252 | _, _ = w.Write([]byte(hashOf([]byte{}) + " " + asset + "\n")) |
| 253 | default: |
| 254 | http.Error(w, "boom", http.StatusInternalServerError) |
| 255 | } |
| 256 | })) |
| 257 | t.Cleanup(srv.Close) |
| 258 | withReleaseURLs(t, srv.URL) |
| 259 | |
| 260 | tmpDir := t.TempDir() |
| 261 | exec := filepath.Join(tmpDir, "codehamr") |
| 262 | if err := os.WriteFile(exec, []byte("o\n"), 0o755); err != nil { |
| 263 | t.Fatal(err) |
| 264 | } |
| 265 | if err := Apply(context.Background(), exec); err == nil { |
| 266 | t.Fatal("Apply must error on download failure") |
| 267 | } |
| 268 | matches, _ := filepath.Glob(filepath.Join(tmpDir, ".codehamr-update-*")) |
| 269 | if len(matches) != 0 { |
| 270 | t.Fatalf("temp file leaked after failed Apply: %+v", matches) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // TestFetchHashHandlesCorruptManifest: a manifest not in `<hash> <name>` |
| 275 | // form must not crash fetchHash; it just yields an empty hash. |
nothing calls this directly
no test coverage detected