code.gitea.io/gitea@v1.19.3/modules/user/user_test.go (about) 1 // Copyright 2020 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package user 5 6 import ( 7 "os" 8 "os/exec" 9 "runtime" 10 "strings" 11 "testing" 12 ) 13 14 func getWhoamiOutput() (string, error) { 15 output, err := exec.Command("whoami").Output() 16 if err != nil { 17 return "", err 18 } 19 return strings.TrimSpace(string(output)), nil 20 } 21 22 func TestCurrentUsername(t *testing.T) { 23 user := CurrentUsername() 24 if len(user) == 0 { 25 t.Errorf("expected non-empty user, got: %s", user) 26 } 27 // Windows whoami is weird, so just skip remaining tests 28 if runtime.GOOS == "windows" { 29 t.Skip("skipped test because of weird whoami on Windows") 30 } 31 whoami, err := getWhoamiOutput() 32 if err != nil { 33 t.Errorf("failed to run whoami to test current user: %f", err) 34 } 35 user = CurrentUsername() 36 if user != whoami { 37 t.Errorf("expected %s as user, got: %s", whoami, user) 38 } 39 os.Setenv("USER", "spoofed") 40 user = CurrentUsername() 41 if user != whoami { 42 t.Errorf("expected %s as user, got: %s", whoami, user) 43 } 44 }