From: Bryan C. Mills Date: Thu, 20 Apr 2023 17:56:20 +0000 (-0400) Subject: os/user: skip tests that invoke Current if it returns an expected error X-Git-Tag: go1.21rc1~819 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9027e5d2b486906f1fce862be295defe44cea213;p=gostls13.git os/user: skip tests that invoke Current if it returns an expected error Today Current may fail if the binary is not built with cgo and USER and/or HOME is not set in the environment. That should not cause the test to fail. After this change, GOCACHE=$(go env GOCACHE) CGO_ENABLED=0 USER= HOME= go test os/user now passes on linux/amd64. For #59583. Change-Id: Id290cd1088051e930d73f0dd554177124796e8f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/487015 Reviewed-by: Michael Pratt Run-TryBot: Bryan Mills Reviewed-by: Johan Brandhorst-Satzkorn Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot --- diff --git a/src/os/user/cgo_user_test.go b/src/os/user/cgo_user_test.go new file mode 100644 index 0000000000..0458495a3f --- /dev/null +++ b/src/os/user/cgo_user_test.go @@ -0,0 +1,11 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build cgo && !osusergo + +package user + +func init() { + hasCgo = true +} diff --git a/src/os/user/user_test.go b/src/os/user/user_test.go index 0fa963dae0..fa597b78ec 100644 --- a/src/os/user/user_test.go +++ b/src/os/user/user_test.go @@ -5,9 +5,16 @@ package user import ( + "os" "testing" ) +var ( + hasCgo = false + hasUSER = os.Getenv("USER") != "" + hasHOME = os.Getenv("HOME") != "" +) + func checkUser(t *testing.T) { t.Helper() if !userImplemented { @@ -23,7 +30,11 @@ func TestCurrent(t *testing.T) { userBuffer = 1 // force use of retry code u, err := Current() if err != nil { - t.Fatalf("Current: %v (got %#v)", err, u) + if hasCgo || (hasUSER && hasHOME) { + t.Fatalf("Current: %v (got %#v)", err, u) + } else { + t.Skipf("skipping: %v", err) + } } if u.HomeDir == "" { t.Errorf("didn't get a HomeDir") @@ -62,8 +73,13 @@ func TestLookup(t *testing.T) { want, err := Current() if err != nil { - t.Fatalf("Current: %v", err) + if hasCgo || (hasUSER && hasHOME) { + t.Fatalf("Current: %v", err) + } else { + t.Skipf("skipping: %v", err) + } } + // TODO: Lookup() has a fast path that calls Current() and returns if the // usernames match, so this test does not exercise very much. It would be // good to try and test finding a different user than the current user. @@ -79,8 +95,13 @@ func TestLookupId(t *testing.T) { want, err := Current() if err != nil { - t.Fatalf("Current: %v", err) + if hasCgo || (hasUSER && hasHOME) { + t.Fatalf("Current: %v", err) + } else { + t.Skipf("skipping: %v", err) + } } + got, err := LookupId(want.Uid) if err != nil { t.Fatalf("LookupId: %v", err) @@ -102,9 +123,14 @@ func TestLookupGroup(t *testing.T) { }() groupBuffer = 1 // force use of retry code checkGroup(t) + user, err := Current() if err != nil { - t.Fatalf("Current(): %v", err) + if hasCgo || (hasUSER && hasHOME) { + t.Fatalf("Current: %v", err) + } else { + t.Skipf("skipping: %v", err) + } } g1, err := LookupGroupId(user.Gid) @@ -137,10 +163,16 @@ func checkGroupList(t *testing.T) { func TestGroupIds(t *testing.T) { checkGroupList(t) + user, err := Current() if err != nil { - t.Fatalf("Current(): %v", err) + if hasCgo || (hasUSER && hasHOME) { + t.Fatalf("Current: %v", err) + } else { + t.Skipf("skipping: %v", err) + } } + gids, err := user.GroupIds() if err != nil { t.Fatalf("%+v.GroupIds(): %v", user, err)