From b144489a838feb343c7ce90074d9a12f6842c6b6 Mon Sep 17 00:00:00 2001 From: itchyny Date: Mon, 28 Oct 2024 21:20:25 +0900 Subject: [PATCH] handle error of setting environment variables in tests --- cli/cli_test.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cli/cli_test.go b/cli/cli_test.go index 8f0b6429..ee104ec8 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -12,8 +12,6 @@ import ( func init() { addDefaultModulePaths = false - os.Setenv("NO_COLOR", "") - os.Setenv("GOJQ_COLORS", "") } func setLocation(loc *time.Location) func() { @@ -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) @@ -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, @@ -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 == "" {