Skip to content

Commit 9027e5d

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
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 <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Johan Brandhorst-Satzkorn <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent ed832ed commit 9027e5d

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/os/user/cgo_user_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build cgo && !osusergo
6+
7+
package user
8+
9+
func init() {
10+
hasCgo = true
11+
}

src/os/user/user_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
package user
66

77
import (
8+
"os"
89
"testing"
910
)
1011

12+
var (
13+
hasCgo = false
14+
hasUSER = os.Getenv("USER") != ""
15+
hasHOME = os.Getenv("HOME") != ""
16+
)
17+
1118
func checkUser(t *testing.T) {
1219
t.Helper()
1320
if !userImplemented {
@@ -23,7 +30,11 @@ func TestCurrent(t *testing.T) {
2330
userBuffer = 1 // force use of retry code
2431
u, err := Current()
2532
if err != nil {
26-
t.Fatalf("Current: %v (got %#v)", err, u)
33+
if hasCgo || (hasUSER && hasHOME) {
34+
t.Fatalf("Current: %v (got %#v)", err, u)
35+
} else {
36+
t.Skipf("skipping: %v", err)
37+
}
2738
}
2839
if u.HomeDir == "" {
2940
t.Errorf("didn't get a HomeDir")
@@ -62,8 +73,13 @@ func TestLookup(t *testing.T) {
6273

6374
want, err := Current()
6475
if err != nil {
65-
t.Fatalf("Current: %v", err)
76+
if hasCgo || (hasUSER && hasHOME) {
77+
t.Fatalf("Current: %v", err)
78+
} else {
79+
t.Skipf("skipping: %v", err)
80+
}
6681
}
82+
6783
// TODO: Lookup() has a fast path that calls Current() and returns if the
6884
// usernames match, so this test does not exercise very much. It would be
6985
// good to try and test finding a different user than the current user.
@@ -79,8 +95,13 @@ func TestLookupId(t *testing.T) {
7995

8096
want, err := Current()
8197
if err != nil {
82-
t.Fatalf("Current: %v", err)
98+
if hasCgo || (hasUSER && hasHOME) {
99+
t.Fatalf("Current: %v", err)
100+
} else {
101+
t.Skipf("skipping: %v", err)
102+
}
83103
}
104+
84105
got, err := LookupId(want.Uid)
85106
if err != nil {
86107
t.Fatalf("LookupId: %v", err)
@@ -102,9 +123,14 @@ func TestLookupGroup(t *testing.T) {
102123
}()
103124
groupBuffer = 1 // force use of retry code
104125
checkGroup(t)
126+
105127
user, err := Current()
106128
if err != nil {
107-
t.Fatalf("Current(): %v", err)
129+
if hasCgo || (hasUSER && hasHOME) {
130+
t.Fatalf("Current: %v", err)
131+
} else {
132+
t.Skipf("skipping: %v", err)
133+
}
108134
}
109135

110136
g1, err := LookupGroupId(user.Gid)
@@ -137,10 +163,16 @@ func checkGroupList(t *testing.T) {
137163

138164
func TestGroupIds(t *testing.T) {
139165
checkGroupList(t)
166+
140167
user, err := Current()
141168
if err != nil {
142-
t.Fatalf("Current(): %v", err)
169+
if hasCgo || (hasUSER && hasHOME) {
170+
t.Fatalf("Current: %v", err)
171+
} else {
172+
t.Skipf("skipping: %v", err)
173+
}
143174
}
175+
144176
gids, err := user.GroupIds()
145177
if err != nil {
146178
t.Fatalf("%+v.GroupIds(): %v", user, err)

0 commit comments

Comments
 (0)