Merge branch 'gmh-fix-environment-variable-tests' into 'main'

test: fix issue with environment variables being set

See merge request https://gitlab.com/gitlab-org/cli/-/merge_requests/1449

Merged-by: Shekhar Patnaik <spatnaik@gitlab.com>
Approved-by: Vitali Tatarintev <vtatarintev@gitlab.com>
Approved-by: Shekhar Patnaik <spatnaik@gitlab.com>
Co-authored-by: Gary Holtz <gholtz@gitlab.com>
This commit is contained in:
Shekhar Patnaik 2024-04-24 14:51:44 +00:00
commit 7953c99e8e
5 changed files with 22 additions and 13 deletions

View File

@ -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)"

View File

@ -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

View File

@ -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)

View File

@ -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),

View File

@ -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")