]> Cypherpunks.ru repositories - gostls13.git/commitdiff
os/user: skip tests that invoke Current if it returns an expected error
authorBryan C. Mills <bcmills@google.com>
Thu, 20 Apr 2023 17:56:20 +0000 (13:56 -0400)
committerGopher Robot <gobot@golang.org>
Fri, 21 Apr 2023 15:59:43 +0000 (15:59 +0000)
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>

src/os/user/cgo_user_test.go [new file with mode: 0644]
src/os/user/user_test.go

diff --git a/src/os/user/cgo_user_test.go b/src/os/user/cgo_user_test.go
new file mode 100644 (file)
index 0000000..0458495
--- /dev/null
@@ -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
+}
index 0fa963dae07d9db1a9f799111fcef8cd97c907c4..fa597b78ece7c836595ce6b6cbdec039613d4e4b 100644 (file)
@@ -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)