From de77be2278c71dfbca0e772c2d7b8ae27274ed8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Aubr=C3=A9e?= Date: Wed, 2 Dec 2026 01:05:15 +0000 Subject: [PATCH] test(cli): never start the background update check in package tests The macOS race job on main (bc1c1ee) failed TestDoctorOutdatedFailsOnlyWhenALookupCouldNotRun with "race detected during execution of test". Every command runs the root PersistentPreRun, whose update check starts a goroutine that calls the real release origin, writes under HOME and reads version.Version after the command returns; a later test that set version.Version raced with it. TestMain now turns the check off, and a test proves no command reaches the release origin during package tests (it failed before this change). Co-Authored-By: Claude Opus 5.5 --- app/go-cli/internal/cli/tmpdir_main_test.go | 3 ++ app/go-cli/internal/cli/update_check.go | 7 +++- app/go-cli/internal/cli/update_check_test.go | 34 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/go-cli/internal/cli/update_check_test.go diff --git a/app/go-cli/internal/cli/tmpdir_main_test.go b/app/go-cli/internal/cli/tmpdir_main_test.go index de8abf3..6c3a8ba 100644 --- a/app/go-cli/internal/cli/tmpdir_main_test.go +++ b/app/go-cli/internal/cli/tmpdir_main_test.go @@ -29,5 +29,8 @@ func TestMain(m *testing.M) { "registry:3": "sha256:" + strings.Repeat("e", 64), "caddy:2": "sha256:" + strings.Repeat("1", 64), } + // Commands run their background update check from the root PersistentPreRun; + // in tests it would call the real release origin and race with later tests. + updateCheckEnabled = false os.Exit(m.Run()) } diff --git a/app/go-cli/internal/cli/update_check.go b/app/go-cli/internal/cli/update_check.go index baf9e59..eaead37 100644 --- a/app/go-cli/internal/cli/update_check.go +++ b/app/go-cli/internal/cli/update_check.go @@ -14,10 +14,15 @@ import ( const updateCheckInterval = 24 * time.Hour +// updateCheckEnabled is turned off by the package TestMain: the check calls the +// release origin, writes under HOME and reads globals that tests change, from a +// goroutine that outlives the command. +var updateCheckEnabled = true + func checkForUpdateAsync() { // Without a release origin there is nothing to ask; do not start a network // call that can only fail. - if siteBaseURL() == "" { + if !updateCheckEnabled || siteBaseURL() == "" { return } go func() { diff --git a/app/go-cli/internal/cli/update_check_test.go b/app/go-cli/internal/cli/update_check_test.go new file mode 100644 index 0000000..9649b0a --- /dev/null +++ b/app/go-cli/internal/cli/update_check_test.go @@ -0,0 +1,34 @@ +package cli + +import ( + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" +) + +// Every command runs the root PersistentPreRun. Its background update check +// called the release origin and read version.Version from a goroutine that +// outlived the test, so a later test that set version.Version raced with it +// (macOS race job on main, bc1c1ee). Package tests must never start it. +func TestPackageTestsNeverStartTheBackgroundUpdateCheck(t *testing.T) { + var hits atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hits.Add(1) + http.NotFound(w, r) + })) + defer server.Close() + allowInsecureUpdateForTests = true + t.Cleanup(func() { allowInsecureUpdateForTests = false }) + t.Setenv("SHUTTLE_SITE_URL", server.URL) + t.Setenv("HOME", t.TempDir()) + + cmd := NewRootCommand() + cmd.PersistentPreRun(cmd, nil) + + time.Sleep(300 * time.Millisecond) + if got := hits.Load(); got != 0 { + t.Fatalf("background update check called the release origin %d time(s) during package tests", got) + } +}