From 2ccc6863504cd9901afb3dd27e7d11a63bf23fb6 Mon Sep 17 00:00:00 2001 From: d068777 Date: Mon, 17 Aug 2020 17:31:39 +0200 Subject: [PATCH 1/3] check error for os.Setenv in parse_test.go --- mockgen/parse_test.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/mockgen/parse_test.go b/mockgen/parse_test.go index 28fb5d57..6a77ce55 100644 --- a/mockgen/parse_test.go +++ b/mockgen/parse_test.go @@ -167,7 +167,10 @@ func TestParsePackageImport(t *testing.T) { } { t.Run(testCase.name, func(t *testing.T) { for key, value := range testCase.envs { - os.Setenv(key, value) + err = os.Setenv(key, value) + if err != nil { + t.Errorf("unexpected error: %s", err.Error()) + } } pkgPath, err := parsePackageImport(filepath.Clean(testCase.dir)) if err != testCase.err { @@ -195,8 +198,14 @@ func TestParsePackageImport_FallbackGoPath(t *testing.T) { if err != nil { t.Error(err) } - os.Setenv("GOPATH", goPath) - os.Setenv("GO111MODULE", "on") + err = os.Setenv("GOPATH", goPath) + if err != nil { + t.Errorf("unexpected error: %s", err.Error()) + } + err = os.Setenv("GO111MODULE", "on") + if err != nil { + t.Errorf("unexpected error: %s", err.Error()) + } pkgPath, err := parsePackageImport(srcDir) expected := "example.com/foo" if pkgPath != expected { @@ -237,8 +246,14 @@ func TestParsePackageImport_FallbackMultiGoPath(t *testing.T) { }() goPaths := strings.Join(goPathList, string(os.PathListSeparator)) - os.Setenv("GOPATH", goPaths) - os.Setenv("GO111MODULE", "on") + err = os.Setenv("GOPATH", goPaths) + if err != nil { + t.Errorf("unexpected error: %s", err.Error()) + } + err = os.Setenv("GO111MODULE", "on") + if err != nil { + t.Errorf("unexpected error: %s", err.Error()) + } pkgPath, err := parsePackageImport(srcDir) expected := "example.com/foo" if pkgPath != expected { From 25d1a55a071c25d1b9738104b8ad0ec122d882c7 Mon Sep 17 00:00:00 2001 From: d068777 Date: Tue, 18 Aug 2020 09:44:31 +0200 Subject: [PATCH 2/3] print key, value and error in t.Errorf() --- mockgen/parse_test.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/mockgen/parse_test.go b/mockgen/parse_test.go index 6a77ce55..625f0287 100644 --- a/mockgen/parse_test.go +++ b/mockgen/parse_test.go @@ -169,7 +169,7 @@ func TestParsePackageImport(t *testing.T) { for key, value := range testCase.envs { err = os.Setenv(key, value) if err != nil { - t.Errorf("unexpected error: %s", err.Error()) + t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } } pkgPath, err := parsePackageImport(filepath.Clean(testCase.dir)) @@ -198,13 +198,17 @@ func TestParsePackageImport_FallbackGoPath(t *testing.T) { if err != nil { t.Error(err) } - err = os.Setenv("GOPATH", goPath) + key := "GOPATH" + value := goPath + err = os.Setenv(key, value) if err != nil { - t.Errorf("unexpected error: %s", err.Error()) + t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } - err = os.Setenv("GO111MODULE", "on") + key = "GO111MODULE" + value = "on" + err = os.Setenv(key, value) if err != nil { - t.Errorf("unexpected error: %s", err.Error()) + t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } pkgPath, err := parsePackageImport(srcDir) expected := "example.com/foo" @@ -246,13 +250,17 @@ func TestParsePackageImport_FallbackMultiGoPath(t *testing.T) { }() goPaths := strings.Join(goPathList, string(os.PathListSeparator)) - err = os.Setenv("GOPATH", goPaths) + key := "GOPATH" + value := goPaths + err = os.Setenv(key, value) if err != nil { - t.Errorf("unexpected error: %s", err.Error()) + t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } - err = os.Setenv("GO111MODULE", "on") + key = "GO111MODULE" + value = "on" + err = os.Setenv(key, value) if err != nil { - t.Errorf("unexpected error: %s", err.Error()) + t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } pkgPath, err := parsePackageImport(srcDir) expected := "example.com/foo" From 3d69240ebbd28c0c566321fd66a497554d5836ca Mon Sep 17 00:00:00 2001 From: d068777 Date: Tue, 18 Aug 2020 16:04:36 +0200 Subject: [PATCH 3/3] error assignment and handling in one line --- mockgen/parse_test.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/mockgen/parse_test.go b/mockgen/parse_test.go index 625f0287..b0cdbb13 100644 --- a/mockgen/parse_test.go +++ b/mockgen/parse_test.go @@ -167,8 +167,7 @@ func TestParsePackageImport(t *testing.T) { } { t.Run(testCase.name, func(t *testing.T) { for key, value := range testCase.envs { - err = os.Setenv(key, value) - if err != nil { + if err := os.Setenv(key, value); err != nil { t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } } @@ -200,14 +199,12 @@ func TestParsePackageImport_FallbackGoPath(t *testing.T) { } key := "GOPATH" value := goPath - err = os.Setenv(key, value) - if err != nil { + if err := os.Setenv(key, value); err != nil { t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } key = "GO111MODULE" value = "on" - err = os.Setenv(key, value) - if err != nil { + if err := os.Setenv(key, value); err != nil { t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } pkgPath, err := parsePackageImport(srcDir) @@ -252,14 +249,12 @@ func TestParsePackageImport_FallbackMultiGoPath(t *testing.T) { goPaths := strings.Join(goPathList, string(os.PathListSeparator)) key := "GOPATH" value := goPaths - err = os.Setenv(key, value) - if err != nil { + if err := os.Setenv(key, value); err != nil { t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } key = "GO111MODULE" value = "on" - err = os.Setenv(key, value) - if err != nil { + if err := os.Setenv(key, value); err != nil { t.Fatalf("unable to set environment variable %q to %q: %v", key, value, err) } pkgPath, err := parsePackageImport(srcDir)