ship test: scan a private TMPDIR, not the shared one (#645)
TestShipAPIStaticCleansUpTempFileOnServerError diffed the shared temp dir for new carlos-ship-* files, so any concurrent writer failed it (main job 4995). It now uses its own TMPDIR, and the fake server checks the temp file existed during the upload. With a background writer the old test failed 153/200 and the new one 0/200; removing the cleanup turns it red. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from fix-ship-tmp-flake into main.
1 file changed,
+33
−20
apimode_test.go+33 −20
CI passed — run details
diff --git a/apimode_test.go b/apimode_test.go| index 74aa45bda..3a2c682ce 100644 |
| --- a/apimode_test.go |
| +++ b/apimode_test.go |
| @@ -1651,39 +1651,52 @@ func TestShipAPIStaticCleansUpTempFileOnServerError(t *testing.T) { |
| t.Fatal(err) |
| } |
| + // A private TMPDIR: os.CreateTemp("", ...) in runShipAPI lands here, and |
| + // nothing else (a concurrent package run, another CI job on the runner) |
| + // can drop its own carlos-ship-* file into it. Scanning the shared temp |
| + // dir made this test flaky (#645). |
| + tmpParent := t.TempDir() |
| + t.Setenv("TMPDIR", tmpParent) |
| + |
| + shipTemps := func() []string { |
| + entries, err := os.ReadDir(tmpParent) |
| + if err != nil { |
| + t.Error(err) |
| + return nil |
| + } |
| + var names []string |
| + for _, e := range entries { |
| + if strings.HasPrefix(e.Name(), "carlos-ship-") { |
| + names = append(names, e.Name()) |
| + } |
| + } |
| + return names |
| + } |
| + |
| + // The server notes whether the temp file existed while a request was in |
| + // flight, so a pass below means "created, then cleaned up", never "the |
| + // ship failed before it made one" or "it wrote somewhere else". |
| + var sawTemp atomic.Bool |
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + if len(shipTemps()) > 0 { |
| + sawTemp.Store(true) |
| + } |
| io.Copy(io.Discard, r.Body) |
| w.WriteHeader(http.StatusInternalServerError) |
| })) |
| defer ts.Close() |
| - tmpParent := os.TempDir() |
| - before, err := os.ReadDir(tmpParent) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - beforeNames := map[string]bool{} |
| - for _, e := range before { |
| - beforeNames[e.Name()] = true |
| - } |
| - |
| sess := newTestSession(ts.URL) |
| fs, acctRaw, acctIDRaw := newShipFlagSet(t, apiTestAccount, siteDir) |
| if _, err := runShipAPI(sess, apiAccount(acctRaw, acctIDRaw), fs, "titesite", "abc1234", "static", "", "", "", nil, true, false); err == nil { |
| t.Fatal("runShipAPI against a failing server must error") |
| } |
| - after, err := os.ReadDir(tmpParent) |
| - if err != nil { |
| - t.Fatal(err) |
| + if !sawTemp.Load() { |
| + t.Fatalf("no carlos-ship-* temp file in %s while the ship was in flight; the test no longer exercises the cleanup", tmpParent) |
| } |
| - for _, e := range after { |
| - if beforeNames[e.Name()] { |
| - continue |
| - } |
| - if strings.HasPrefix(e.Name(), "carlos-ship-") { |
| - t.Fatalf("temp file %s leaked after a failed ship", e.Name()) |
| - } |
| + if leaked := shipTemps(); len(leaked) > 0 { |
| + t.Fatalf("temp file %s leaked after a failed ship", leaked[0]) |
| } |
| checkNoRegistry() |
| } |