Skip to content

Commit

Permalink
handle error of setting environment variables in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 28, 2024
1 parent 086bd2b commit b144489
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

func init() {
addDefaultModulePaths = false
os.Setenv("NO_COLOR", "")
os.Setenv("GOJQ_COLORS", "")
}

func setLocation(loc *time.Location) func() {
Expand All @@ -23,6 +21,12 @@ func setLocation(loc *time.Location) func() {
}

func TestCliRun(t *testing.T) {
if err := os.Setenv("NO_COLOR", ""); err != nil {
t.Fatal(err)
}
if err := os.Setenv("GOJQ_COLORS", ""); err != nil {
t.Fatal(err)
}
f, err := os.Open("test.yaml")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -64,7 +68,11 @@ func TestCliRun(t *testing.T) {
for _, env := range tc.Env {
xs := strings.SplitN(env, "=", 2)
k, v := xs[0], xs[1]
defer func(v string) { os.Setenv(k, v) }(os.Getenv(k))
defer func(v string) {
if err := os.Setenv(k, v); err != nil {
t.Fatal(err)
}
}(os.Getenv(k))
if k == "GOJQ_COLORS" {
defer func(colors [][]byte) {
nullColor, falseColor, trueColor, numberColor,
Expand All @@ -76,7 +84,9 @@ func TestCliRun(t *testing.T) {
stringColor, objectKeyColor, arrayColor, objectColor,
})
}
os.Setenv(k, v)
if err := os.Setenv(k, v); err != nil {
t.Fatal(err)
}
}
code := cli.run(tc.Args)
if tc.Error == "" {
Expand Down

0 comments on commit b144489

Please sign in to comment.