Skip to content

Commit

Permalink
refactor: user env for user name
Browse files Browse the repository at this point in the history
relates to #55
  • Loading branch information
JanDeDobbeleer committed Oct 12, 2020
1 parent 1ef2575 commit 56f4bcf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
10 changes: 7 additions & 3 deletions environment.go
Expand Up @@ -22,7 +22,7 @@ type environmentInfo interface {
hasFolder(folder string) bool
getFileContent(file string) string
getPathSeperator() string
getCurrentUser() (*user.User, error)
getCurrentUser() string
isRunningAsRoot() bool
getHostName() (string, error)
getRuntimeGOOS() string
Expand Down Expand Up @@ -101,8 +101,12 @@ func (env *environment) getPathSeperator() string {
return string(os.PathSeparator)
}

func (env *environment) getCurrentUser() (*user.User, error) {
return user.Current()
func (env *environment) getCurrentUser() string {
user := os.Getenv("USER")
if user == "" {
user = os.Getenv("USERNAME")
}
return user
}

func (env *environment) getHostName() (string, error) {
Expand Down
5 changes: 2 additions & 3 deletions segment_path_test.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"math/rand"
"os/user"
"testing"

"github.com/distatus/battery"
Expand Down Expand Up @@ -49,9 +48,9 @@ func (env *MockedEnvironment) getPathSeperator() string {
return args.String(0)
}

func (env *MockedEnvironment) getCurrentUser() (*user.User, error) {
func (env *MockedEnvironment) getCurrentUser() string {
args := env.Called(nil)
return args.Get(0).(*user.User), args.Error(1)
return args.String(0)
}

func (env *MockedEnvironment) getHostName() (string, error) {
Expand Down
7 changes: 2 additions & 5 deletions segment_session.go
Expand Up @@ -61,11 +61,8 @@ func (s *session) getUserName() string {
if !s.props.getBool(DisplayUser, true) {
return ""
}
user, err := s.env.getCurrentUser()
if err != nil {
return "unknown"
}
username := strings.TrimSpace(user.Username)
user := s.env.getCurrentUser()
username := strings.TrimSpace(user)
if s.env.getRuntimeGOOS() == "windows" && strings.Contains(username, "\\") {
username = strings.Split(username, "\\")[1]
}
Expand Down
13 changes: 8 additions & 5 deletions segment_session_test.go
@@ -1,18 +1,14 @@
package main

import (
"os/user"
"testing"

"github.com/stretchr/testify/assert"
)

func setupSession(userInfoSeparator string, username string, hostname string, goos string) session {
env := new(MockedEnvironment)
user := user.User{
Username: username,
}
env.On("getCurrentUser", nil).Return(&user, nil)
env.On("getCurrentUser", nil).Return(username)
env.On("getHostName", nil).Return(hostname, nil)
env.On("getRuntimeGOOS", nil).Return(goos)
props := &properties{
Expand Down Expand Up @@ -61,3 +57,10 @@ func TestWriteOnlyHostname(t *testing.T) {
got := s.getFormattedText()
assert.EqualValues(t, want, got)
}

func TestSession(t *testing.T) {
s := &session{
env: &environment{},
}
assert.NotEmpty(t, s.getUserName())
}

0 comments on commit 56f4bcf

Please sign in to comment.