Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/cmd/testmain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ func TestMain(m *testing.M) {
_ = os.Setenv("HOME", home)
_ = os.Setenv("XDG_CONFIG_HOME", xdg)

// Ambient GOG_* path overrides and XDG data/state/cache directories escape
// this sandbox entirely: the layout resolver (internal/config/layout.go)
// honors them ahead of the HOME- and XDG_CONFIG_HOME-derived defaults set
// above, pointing tests at shared real directories. Unset rather than
// redirect: a single shared override directory still cross-contaminates
// tests. Per-test t.Setenv values are unaffected.
oldPathEnv := map[string]string{}
for _, name := range []string{
"GOG_HOME", "GOG_CONFIG_DIR", "GOG_DATA_DIR", "GOG_STATE_DIR", "GOG_CACHE_DIR",
"XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME",
} {
if value, ok := os.LookupEnv(name); ok {
oldPathEnv[name] = value
}
_ = os.Unsetenv(name)
}

code := m.Run()

if oldHome == "" {
Expand All @@ -38,6 +55,10 @@ func TestMain(m *testing.M) {
} else {
_ = os.Setenv("XDG_CONFIG_HOME", oldXDG)
}

for name, value := range oldPathEnv {
_ = os.Setenv(name, value)
}
_ = os.RemoveAll(root)
os.Exit(code)
}
24 changes: 24 additions & 0 deletions internal/config/testmain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
"os"
"testing"
)

func TestMain(m *testing.M) {
// Tests isolate storage via per-test temp directories (HOME by default),
// but the layout resolver (internal/config/layout.go) honors ambient GOG_*
// path overrides and XDG base directories ahead of HOME-derived defaults,
// leaking state across tests and into the developer's real gogcli
// directories. Unset rather than redirect: a single shared override
// directory still cross-contaminates tests. Per-test t.Setenv values are
// unaffected.
for _, name := range []string{
"GOG_HOME", "GOG_CONFIG_DIR", "GOG_DATA_DIR", "GOG_STATE_DIR", "GOG_CACHE_DIR",
"XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME",
} {
_ = os.Unsetenv(name)
}

os.Exit(m.Run())
}
7 changes: 7 additions & 0 deletions internal/googleapi/service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func TestTokenSourceForServiceAccountScopesUsesInjectedStore(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", filepath.Join(ambientHome, "xdg-config"))
t.Setenv("XDG_DATA_HOME", filepath.Join(ambientHome, "xdg-data"))

// GOG_* path overrides outrank HOME and XDG in the layout resolver; clear
// them so the ambient file below lands in this test's sandbox rather than
// a real gogcli data directory.
for _, name := range []string{"GOG_HOME", "GOG_CONFIG_DIR", "GOG_DATA_DIR", "GOG_STATE_DIR", "GOG_CACHE_DIR"} {
t.Setenv(name, "")
}

ambientLayout, err := config.NewSystemResolver("").Resolve(config.PathKindData)
if err != nil {
t.Fatalf("resolve ambient layout: %v", err)
Expand Down
24 changes: 24 additions & 0 deletions internal/secrets/testmain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package secrets

import (
"os"
"testing"
)

func TestMain(m *testing.M) {
// Tests isolate storage via per-test temp directories (HOME by default),
// but the layout resolver (internal/config/layout.go) honors ambient GOG_*
// path overrides and XDG base directories ahead of HOME-derived defaults,
// leaking state across tests and into the developer's real gogcli
// directories. Unset rather than redirect: a single shared override
// directory still cross-contaminates tests. Per-test t.Setenv values are
// unaffected.
for _, name := range []string{
"GOG_HOME", "GOG_CONFIG_DIR", "GOG_DATA_DIR", "GOG_STATE_DIR", "GOG_CACHE_DIR",
"XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME",
} {
_ = os.Unsetenv(name)
}

os.Exit(m.Run())
}