mirror of https://gitlab.com/gitlab-org/cli.git
refactor(test): replace os.Setenv with t.Setenv in tests
This commit is contained in:
parent
588960cebd
commit
b5711554c1
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -96,11 +95,7 @@ hosts:
|
|||
username: monalisa
|
||||
token: OTOKEN
|
||||
`, "")()
|
||||
|
||||
originalEnvVarToken, envIsSet := os.LookupEnv("GITLAB_TOKEN")
|
||||
if envIsSet && originalEnvVarToken != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", "")
|
||||
}
|
||||
t.Setenv("GITLAB_TOKEN", "")
|
||||
|
||||
api.SetUserAgent("v1.2.3", "darwin", "arm64")
|
||||
versionString := "glab/v1.2.3 (darwin, arm64)"
|
||||
|
@ -275,10 +270,6 @@ hosts:
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
if envIsSet && originalEnvVarToken != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", originalEnvVarToken)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_addQuery(t *testing.T) {
|
||||
|
|
|
@ -3,7 +3,6 @@ package clone
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -153,10 +152,8 @@ hosts:
|
|||
gitlab.com:
|
||||
token: qRC87Xg9Wd46RhB8J8sp
|
||||
`, "")()
|
||||
token := os.Getenv("GITLAB_TOKEN")
|
||||
if token != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", "")
|
||||
}
|
||||
t.Setenv("GITLAB_TOKEN", "")
|
||||
|
||||
io, stdin, stdout, stderr := iostreams.Test()
|
||||
fac := &cmdutils.Factory{
|
||||
IO: io,
|
||||
|
@ -183,9 +180,6 @@ hosts:
|
|||
assert.Equal(t, "", out.Stderr())
|
||||
assert.Equal(t, 1, cs.Count)
|
||||
assert.Equal(t, "git clone git@gitlab.com:clemsbot/test.git", strings.Join(cs.Calls[0].Args, " "))
|
||||
if token != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", token)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_repoClone_group(t *testing.T) {
|
||||
|
@ -214,10 +208,8 @@ hosts:
|
|||
gitlab.com:
|
||||
token: qRC87Xg9Wd46RhB8J8sp
|
||||
`, "")()
|
||||
token := os.Getenv("GITLAB_TOKEN")
|
||||
if token != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", "")
|
||||
}
|
||||
t.Setenv("GITLAB_TOKEN", "")
|
||||
|
||||
io, stdin, stdout, stderr := iostreams.Test()
|
||||
fac := &cmdutils.Factory{
|
||||
IO: io,
|
||||
|
@ -256,8 +248,4 @@ hosts:
|
|||
for i := 0; i < len(expectedRepoUrls); i++ {
|
||||
assert.Equal(t, fmt.Sprintf("git clone %s", expectedRepoUrls[i]), strings.Join(cs.Calls[i].Args, " "))
|
||||
}
|
||||
|
||||
if token != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", token)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package config
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
|
@ -17,21 +16,11 @@ func eq(t *testing.T, got interface{}, expected interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func clearEnvironmentVariables() (string, string, string) {
|
||||
func clearEnvironmentVariables(t *testing.T) {
|
||||
// prevent using environment variables for test
|
||||
envToken := os.Getenv("GITLAB_TOKEN")
|
||||
if envToken != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", "")
|
||||
}
|
||||
envVisual := os.Getenv("VISUAL")
|
||||
if envVisual != "" {
|
||||
_ = os.Setenv("VISUAL", "")
|
||||
}
|
||||
envEditor := os.Getenv("EDITOR")
|
||||
if envEditor != "" {
|
||||
_ = os.Setenv("EDITOR", "")
|
||||
}
|
||||
return envToken, envVisual, envEditor
|
||||
t.Setenv("GITLAB_TOKEN", "")
|
||||
t.Setenv("VISUAL", "")
|
||||
t.Setenv("EDITOR", "")
|
||||
}
|
||||
|
||||
func Test_parseConfig(t *testing.T) {
|
||||
|
@ -42,8 +31,7 @@ hosts:
|
|||
token: OTOKEN
|
||||
aliases:
|
||||
`, "")()
|
||||
|
||||
envToken, _, _ := clearEnvironmentVariables()
|
||||
clearEnvironmentVariables(t)
|
||||
|
||||
config, err := ParseConfig("config.yml")
|
||||
eq(t, err, nil)
|
||||
|
@ -53,9 +41,6 @@ aliases:
|
|||
token, err := config.Get("gitlab.com", "token")
|
||||
eq(t, err, nil)
|
||||
eq(t, token, "OTOKEN")
|
||||
if envToken != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", "")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseConfig_multipleHosts(t *testing.T) {
|
||||
|
@ -68,8 +53,7 @@ hosts:
|
|||
username: monalisa
|
||||
token: OTOKEN
|
||||
`, "")()
|
||||
|
||||
envToken, _, _ := clearEnvironmentVariables()
|
||||
clearEnvironmentVariables(t)
|
||||
|
||||
config, err := ParseConfig("config.yml")
|
||||
eq(t, err, nil)
|
||||
|
@ -79,9 +63,6 @@ hosts:
|
|||
token, err := config.Get("gitlab.com", "token")
|
||||
eq(t, err, nil)
|
||||
eq(t, token, "OTOKEN")
|
||||
if envToken != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", envToken)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseConfig_Hosts(t *testing.T) {
|
||||
|
@ -92,8 +73,7 @@ hosts:
|
|||
token: OTOKEN
|
||||
`, `
|
||||
`)()
|
||||
|
||||
envToken, _, _ := clearEnvironmentVariables()
|
||||
clearEnvironmentVariables(t)
|
||||
|
||||
config, err := ParseConfig("config.yml")
|
||||
eq(t, err, nil)
|
||||
|
@ -103,10 +83,6 @@ hosts:
|
|||
token, err := config.Get("gitlab.com", "token")
|
||||
eq(t, err, nil)
|
||||
eq(t, token, "OTOKEN")
|
||||
|
||||
if envToken != "" {
|
||||
_ = os.Setenv("GITLAB_TOKEN", envToken)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseConfig_Local(t *testing.T) {
|
||||
|
@ -139,9 +115,7 @@ local:
|
|||
browser: chrome
|
||||
`, `
|
||||
`)()
|
||||
|
||||
envVar := os.Getenv("BROWSER")
|
||||
_ = os.Setenv("BROWSER", "opera")
|
||||
t.Setenv("BROWSER", "opera")
|
||||
|
||||
config, err := ParseConfig("config.yml")
|
||||
eq(t, err, nil)
|
||||
|
@ -156,9 +130,6 @@ local:
|
|||
eq(t, browser, "opera")
|
||||
l, _ := config.Local()
|
||||
t.Log(l.All())
|
||||
if envVar != "" {
|
||||
_ = os.Setenv("BROWSER", envVar)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseConfig_AliasesFile(t *testing.T) {
|
||||
|
@ -241,8 +212,7 @@ func Test_parseConfigFile(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_parseConfigHostEnv(t *testing.T) {
|
||||
os.Setenv("GITLAB_URI", "https://gitlab.mycompany.env")
|
||||
defer os.Unsetenv("GITLAB_URI")
|
||||
t.Setenv("GITLAB_URI", "https://gitlab.mycompany.env")
|
||||
|
||||
defer StubConfig(`---
|
||||
host: https://gitlab.mycompany.global
|
||||
|
|
|
@ -22,12 +22,11 @@ func TestLookPath(t *testing.T) {
|
|||
if wderr != nil {
|
||||
t.Fatal(wderr)
|
||||
}
|
||||
defaultPath := os.Getenv("PATH")
|
||||
paths := []string{
|
||||
filepath.Join(root, "testdata", "nonexist"),
|
||||
filepath.Join(root, "testdata", "PATH"),
|
||||
}
|
||||
os.Setenv("PATH", strings.Join(paths, string(filepath.ListSeparator)))
|
||||
t.Setenv("PATH", strings.Join(paths, string(filepath.ListSeparator)))
|
||||
|
||||
if err := os.Chdir(filepath.Join(root, "testdata", "cwd")); err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -127,5 +126,4 @@ func TestLookPath(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
_ = os.Setenv("PATH", defaultPath)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package iostreams
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
|
@ -11,8 +10,6 @@ import (
|
|||
|
||||
func Test_isColorEnabled(t *testing.T) {
|
||||
preRun := func() {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
os.Unsetenv("COLOR_ENABLED")
|
||||
checkedNoColor = false // Reset it before each run
|
||||
}
|
||||
|
||||
|
@ -26,7 +23,7 @@ func Test_isColorEnabled(t *testing.T) {
|
|||
t.Run("NO_COLOR", func(t *testing.T) {
|
||||
preRun()
|
||||
|
||||
_ = os.Setenv("NO_COLOR", "")
|
||||
t.Setenv("NO_COLOR", "")
|
||||
|
||||
got := isColorEnabled()
|
||||
assert.False(t, got)
|
||||
|
@ -35,8 +32,8 @@ func Test_isColorEnabled(t *testing.T) {
|
|||
t.Run("COLOR_ENABLED == 1", func(t *testing.T) {
|
||||
preRun()
|
||||
|
||||
_ = os.Setenv("NO_COLOR", "")
|
||||
_ = os.Setenv("COLOR_ENABLED", "1")
|
||||
t.Setenv("NO_COLOR", "")
|
||||
t.Setenv("COLOR_ENABLED", "1")
|
||||
|
||||
got := isColorEnabled()
|
||||
assert.True(t, got)
|
||||
|
@ -45,8 +42,8 @@ func Test_isColorEnabled(t *testing.T) {
|
|||
t.Run("COLOR_ENABLED == true", func(t *testing.T) {
|
||||
preRun()
|
||||
|
||||
_ = os.Setenv("NO_COLOR", "")
|
||||
_ = os.Setenv("COLOR_ENABLED", "true")
|
||||
t.Setenv("NO_COLOR", "")
|
||||
t.Setenv("COLOR_ENABLED", "true")
|
||||
|
||||
got := isColorEnabled()
|
||||
assert.True(t, got)
|
||||
|
|
|
@ -38,7 +38,7 @@ func Test_HelperFunctions(t *testing.T) {
|
|||
assert.Equal(t, ios.pagerCommand, got.pagerCommand)
|
||||
})
|
||||
t.Run("GLAB_PAGER=", func(t *testing.T) {
|
||||
os.Setenv("GLAB_PAGER", "more")
|
||||
t.Setenv("GLAB_PAGER", "more")
|
||||
|
||||
got := Init()
|
||||
|
||||
|
|
Loading…
Reference in New Issue