From a96ad42e02586265387e85534489201a6ebb4aee Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 9 Mar 2026 06:12:01 +0000 Subject: [PATCH] test(ci): fix windows tracking and capture flakes --- internal/cmd/docs_write_update_test.go | 6 ++++-- internal/cmd/testutil_test.go | 23 +++++++++++++++++++---- internal/tracking/config.go | 4 ++++ internal/tracking/config_more_test.go | 13 +++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/internal/cmd/docs_write_update_test.go b/internal/cmd/docs_write_update_test.go index 3bbe769..05ca0e7 100644 --- a/internal/cmd/docs_write_update_test.go +++ b/internal/cmd/docs_write_update_test.go @@ -3,6 +3,8 @@ package cmd import ( "context" "encoding/json" + "errors" + "io/fs" "net/http" "os" "path/filepath" @@ -320,8 +322,8 @@ func TestDocsWriteUpdate_FileInputErrors(t *testing.T) { if err == nil { t.Fatal("expected error for non-existent file, got nil") } - if !strings.Contains(err.Error(), "no such file") { - t.Fatalf("expected 'no such file' error, got: %v", err) + if !errors.Is(err, fs.ErrNotExist) { + t.Fatalf("expected fs.ErrNotExist, got: %v", err) } // Test with empty file diff --git a/internal/cmd/testutil_test.go b/internal/cmd/testutil_test.go index a9b8bc0..ef0a533 100644 --- a/internal/cmd/testutil_test.go +++ b/internal/cmd/testutil_test.go @@ -1,6 +1,7 @@ package cmd import ( + "bytes" "context" "encoding/json" "errors" @@ -80,13 +81,20 @@ func captureStdout(t *testing.T, fn func()) string { } os.Stdout = w + var buf bytes.Buffer + done := make(chan struct{}) + go func() { + _, _ = io.Copy(&buf, r) + close(done) + }() + fn() _ = w.Close() os.Stdout = orig - b, _ := io.ReadAll(r) + <-done _ = r.Close() - return string(b) + return buf.String() } func captureStderr(t *testing.T, fn func()) string { @@ -99,13 +107,20 @@ func captureStderr(t *testing.T, fn func()) string { } os.Stderr = w + var buf bytes.Buffer + done := make(chan struct{}) + go func() { + _, _ = io.Copy(&buf, r) + close(done) + }() + fn() _ = w.Close() os.Stderr = orig - b, _ := io.ReadAll(r) + <-done _ = r.Close() - return string(b) + return buf.String() } func withStdin(t *testing.T, input string, fn func()) { diff --git a/internal/tracking/config.go b/internal/tracking/config.go index 13f90da..2b9ff9e 100644 --- a/internal/tracking/config.go +++ b/internal/tracking/config.go @@ -45,6 +45,10 @@ func ConfigPath() (string, error) { } func legacyConfigPath() (string, error) { + if xdg := strings.TrimSpace(os.Getenv("XDG_CONFIG_HOME")); xdg != "" { + return filepath.Join(xdg, "gog", "tracking.json"), nil + } + configDir, err := os.UserConfigDir() if err != nil { return "", fmt.Errorf("user config dir: %w", err) diff --git a/internal/tracking/config_more_test.go b/internal/tracking/config_more_test.go index a60be71..af552eb 100644 --- a/internal/tracking/config_more_test.go +++ b/internal/tracking/config_more_test.go @@ -109,6 +109,19 @@ func TestLoadConfigLegacyFallback(t *testing.T) { } } +func TestLegacyConfigPathUsesXDGConfigHome(t *testing.T) { + setupTrackingConfigEnv(t) + + path, err := legacyConfigPath() + if err != nil { + t.Fatalf("legacyConfigPath: %v", err) + } + + if !strings.Contains(path, filepath.Join("xdg", "gog", "tracking.json")) { + t.Fatalf("expected XDG-based legacy path, got %q", path) + } +} + func TestSaveConfigMissingAccount(t *testing.T) { setupTrackingConfigEnv(t)