}
func current() (*User, error) {
- u := &User{
- Uid: currentUID(),
+ uid := currentUID()
+ // $USER and /etc/passwd may disagree; prefer the latter if we can get it.
+ // See issue 27524 for more information.
+ u, err := lookupUserId(uid)
+ if err == nil {
+ return u, nil
+ }
+ u = &User{
+ Uid: uid,
Gid: currentGID(),
Username: os.Getenv("USER"),
Name: "", // ignored
package user
import (
- "internal/testenv"
- "os"
"runtime"
"testing"
)
func checkUser(t *testing.T) {
+ t.Helper()
if !userImplemented {
t.Skip("user: not implemented; skipping tests")
}
}
func TestCurrent(t *testing.T) {
- // The Go builders (in particular the ones using containers)
- // often have minimal environments without $HOME or $USER set,
- // which breaks Current which relies on those working as a
- // fallback.
- // TODO: we should fix that (Issue 24884) and remove these
- // workarounds.
- if testenv.Builder() != "" && runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
- if os.Getenv("HOME") == "" {
- os.Setenv("HOME", "/tmp")
- }
- if os.Getenv("USER") == "" {
- os.Setenv("USER", "gobuilder")
- }
- }
u, err := Current()
if err != nil {
t.Fatalf("Current: %v (got %#v)", err, u)
}
func checkGroup(t *testing.T) {
+ t.Helper()
if !groupImplemented {
t.Skip("user: group not implemented; skipping test")
}