File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -1316,6 +1316,9 @@ func TestCheckShellEnvironment(t *testing.T) {
13161316}
13171317
13181318func TestOfferShellEnvironmentFix (t * testing.T ) {
1319+ // Clear CI environment to ensure consistent test behavior
1320+ defer testutil .ClearCIEnvironment (t )()
1321+
13191322 tmpDir := t .TempDir ()
13201323 cfg := & config.Config {
13211324 Root : tmpDir ,
Original file line number Diff line number Diff line change @@ -5,10 +5,14 @@ import (
55 "strings"
66 "testing"
77
8+ "github.com/go-nv/goenv/testing/testutil"
89 "github.com/spf13/cobra"
910)
1011
1112func TestNewInteractiveContext (t * testing.T ) {
13+ // Clear CI environment to ensure consistent test behavior
14+ defer testutil .ClearCIEnvironment (t )()
15+
1216 tests := []struct {
1317 name string
1418 setupCmd func () * cobra.Command
Original file line number Diff line number Diff line change @@ -77,3 +77,49 @@ func StripDeprecationWarning(output string) string {
7777 // Only trim trailing whitespace to preserve formatting of the output
7878 return strings .TrimRight (result , " \t \n \r " )
7979}
80+
81+ // ClearCIEnvironment temporarily clears CI-related environment variables for testing.
82+ // This ensures tests behave consistently regardless of whether they run locally or in CI.
83+ //
84+ // The function saves all CI environment variables, unsets them, and returns a cleanup
85+ // function that should be deferred to restore the original state.
86+ //
87+ // Accepts testing.TB so it works with both *testing.T and *testing.B.
88+ //
89+ // Example:
90+ //
91+ // func TestSomething(t *testing.T) {
92+ // cleanup := testutil.ClearCIEnvironment(t)
93+ // defer cleanup()
94+ // // Test code here will run in non-CI mode
95+ // }
96+ func ClearCIEnvironment (tb testing.TB ) func () {
97+ tb .Helper ()
98+
99+ ciEnvVars := []string {
100+ "GITHUB_ACTIONS" ,
101+ "CI" ,
102+ "GITLAB_CI" ,
103+ "CIRCLECI" ,
104+ "TRAVIS" ,
105+ "BUILD_ID" ,
106+ "BUILDKITE" ,
107+ "DRONE" ,
108+ "TEAMCITY_VERSION" ,
109+ }
110+
111+ savedEnv := make (map [string ]string )
112+ for _ , envVar := range ciEnvVars {
113+ savedEnv [envVar ] = os .Getenv (envVar )
114+ os .Unsetenv (envVar )
115+ }
116+
117+ return func () {
118+ tb .Helper ()
119+ for envVar , value := range savedEnv {
120+ if value != "" {
121+ os .Setenv (envVar , value )
122+ }
123+ }
124+ }
125+ }
You can’t perform that action at this time.
0 commit comments