mirror of
https://github.com/golang/go
synced 2024-11-26 21:41:33 -07:00
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 <mpratt@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
ed832ed353
commit
9027e5d2b4
11
src/os/user/cgo_user_test.go
Normal file
11
src/os/user/cgo_user_test.go
Normal file
@ -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
|
||||||
|
}
|
@ -5,9 +5,16 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
hasCgo = false
|
||||||
|
hasUSER = os.Getenv("USER") != ""
|
||||||
|
hasHOME = os.Getenv("HOME") != ""
|
||||||
|
)
|
||||||
|
|
||||||
func checkUser(t *testing.T) {
|
func checkUser(t *testing.T) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if !userImplemented {
|
if !userImplemented {
|
||||||
@ -23,7 +30,11 @@ func TestCurrent(t *testing.T) {
|
|||||||
userBuffer = 1 // force use of retry code
|
userBuffer = 1 // force use of retry code
|
||||||
u, err := Current()
|
u, err := Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if hasCgo || (hasUSER && hasHOME) {
|
||||||
t.Fatalf("Current: %v (got %#v)", err, u)
|
t.Fatalf("Current: %v (got %#v)", err, u)
|
||||||
|
} else {
|
||||||
|
t.Skipf("skipping: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if u.HomeDir == "" {
|
if u.HomeDir == "" {
|
||||||
t.Errorf("didn't get a HomeDir")
|
t.Errorf("didn't get a HomeDir")
|
||||||
@ -62,8 +73,13 @@ func TestLookup(t *testing.T) {
|
|||||||
|
|
||||||
want, err := Current()
|
want, err := Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if hasCgo || (hasUSER && hasHOME) {
|
||||||
t.Fatalf("Current: %v", err)
|
t.Fatalf("Current: %v", err)
|
||||||
|
} else {
|
||||||
|
t.Skipf("skipping: %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Lookup() has a fast path that calls Current() and returns if the
|
// 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
|
// 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.
|
// 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()
|
want, err := Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if hasCgo || (hasUSER && hasHOME) {
|
||||||
t.Fatalf("Current: %v", err)
|
t.Fatalf("Current: %v", err)
|
||||||
|
} else {
|
||||||
|
t.Skipf("skipping: %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
got, err := LookupId(want.Uid)
|
got, err := LookupId(want.Uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("LookupId: %v", err)
|
t.Fatalf("LookupId: %v", err)
|
||||||
@ -102,9 +123,14 @@ func TestLookupGroup(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
groupBuffer = 1 // force use of retry code
|
groupBuffer = 1 // force use of retry code
|
||||||
checkGroup(t)
|
checkGroup(t)
|
||||||
|
|
||||||
user, err := Current()
|
user, err := Current()
|
||||||
if err != nil {
|
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)
|
g1, err := LookupGroupId(user.Gid)
|
||||||
@ -137,10 +163,16 @@ func checkGroupList(t *testing.T) {
|
|||||||
|
|
||||||
func TestGroupIds(t *testing.T) {
|
func TestGroupIds(t *testing.T) {
|
||||||
checkGroupList(t)
|
checkGroupList(t)
|
||||||
|
|
||||||
user, err := Current()
|
user, err := Current()
|
||||||
if err != nil {
|
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()
|
gids, err := user.GroupIds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("%+v.GroupIds(): %v", user, err)
|
t.Fatalf("%+v.GroupIds(): %v", user, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user