diff --git a/commands/api/http_test.go b/commands/api/http_test.go index c47f2e65..e9c502d6 100644 --- a/commands/api/http_test.go +++ b/commands/api/http_test.go @@ -11,6 +11,7 @@ import ( "gitlab.com/gitlab-org/cli/api" "gitlab.com/gitlab-org/cli/internal/config" + "gitlab.com/gitlab-org/cli/test" ) func Test_groupGraphQLVariables(t *testing.T) { @@ -96,7 +97,7 @@ hosts: username: monalisa token: OTOKEN `, "")() - t.Setenv("GITLAB_TOKEN", "") + test.ClearEnvironmentVariables(t) api.SetUserAgent("v1.2.3", "darwin", "arm64") versionString := "glab/v1.2.3 (darwin, arm64)" diff --git a/internal/config/config_file_test.go b/internal/config/config_file_test.go index b1ab1776..9bddc252 100644 --- a/internal/config/config_file_test.go +++ b/internal/config/config_file_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "gitlab.com/gitlab-org/cli/test" "gopkg.in/yaml.v3" ) @@ -16,13 +17,6 @@ func eq(t *testing.T, got interface{}, expected interface{}) { } } -func clearEnvironmentVariables(t *testing.T) { - // prevent using environment variables for test - t.Setenv("GITLAB_TOKEN", "") - t.Setenv("VISUAL", "") - t.Setenv("EDITOR", "") -} - func Test_parseConfig(t *testing.T) { defer StubConfig(`--- hosts: @@ -31,7 +25,7 @@ hosts: token: OTOKEN aliases: `, "")() - clearEnvironmentVariables(t) + test.ClearEnvironmentVariables(t) config, err := ParseConfig("config.yml") eq(t, err, nil) @@ -53,7 +47,7 @@ hosts: username: monalisa token: OTOKEN `, "")() - clearEnvironmentVariables(t) + test.ClearEnvironmentVariables(t) config, err := ParseConfig("config.yml") eq(t, err, nil) @@ -73,7 +67,7 @@ hosts: token: OTOKEN `, ` `)() - clearEnvironmentVariables(t) + test.ClearEnvironmentVariables(t) config, err := ParseConfig("config.yml") eq(t, err, nil) @@ -86,6 +80,8 @@ hosts: } func Test_parseConfig_Local(t *testing.T) { + test.ClearEnvironmentVariables(t) + defer StubConfig(`--- git_protocol: ssh editor: vim @@ -105,6 +101,8 @@ local: } func Test_Get_configReadSequence(t *testing.T) { + test.ClearEnvironmentVariables(t) + defer StubConfig(`--- git_protocol: ssh editor: vim diff --git a/internal/config/config_test.go b/internal/config/config_test.go index c385d3e8..626b0686 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -2,6 +2,7 @@ package config import ( "bytes" + "os" "path/filepath" "testing" @@ -93,7 +94,7 @@ func Test_defaultConfig(t *testing.T) { editor, err := cfg.Get("", "editor") assert.Nil(t, err) - assert.Equal(t, "", editor) + assert.Equal(t, os.Getenv("EDITOR"), editor) aliases, err := cfg.Aliases() assert.Nil(t, err) diff --git a/pkg/iostreams/iostreams_test.go b/pkg/iostreams/iostreams_test.go index 749eeca7..51b878f3 100644 --- a/pkg/iostreams/iostreams_test.go +++ b/pkg/iostreams/iostreams_test.go @@ -10,7 +10,7 @@ import ( ) func Test_HelperFunctions(t *testing.T) { - // Base ios object that is modifiede as required + // Base ios object that is modified as required ios := &IOStreams{ In: os.Stdin, StdOut: NewColorable(os.Stdout), diff --git a/test/helpers.go b/test/helpers.go index 826be6ec..cf03429b 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -106,6 +106,15 @@ func ExpectLines(t T, output string, lines ...string) { } } +func ClearEnvironmentVariables(t *testing.T) { + // prevent using environment variables for test + t.Setenv("GITLAB_TOKEN", "") + t.Setenv("VISUAL", "") + t.Setenv("EDITOR", "") + t.Setenv("GITLAB_ACCESS_TOKEN", "") + t.Setenv("OAUTH_TOKEN", "") +} + func GetHostOrSkip(t testing.TB) string { t.Helper() glTestHost := os.Getenv("GITLAB_TEST_HOST")