mirror of
https://github.com/golang/go
synced 2024-11-26 17:16:54 -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
|
||||
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user