fix: correct cross platform home location

resolves #24
This commit is contained in:
Jan De Dobbeleer 2020-10-02 21:50:13 +02:00 committed by Jan De Dobbeleer
parent 9ffa132174
commit 66941ddb43
5 changed files with 59 additions and 86 deletions

View file

@ -8,7 +8,7 @@
"type": "session",
"style": "diamond",
"foreground": "#ffffff",
"background": "#ffb300",
"background": "#a8216b",
"leading_diamond": "",
"trailing_diamond": "",
"properties": {
@ -33,7 +33,7 @@
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#61AFEF",
"background": "#ff479c",
"properties": {
"prefix": "  ",
"style": "folder"
@ -44,7 +44,7 @@
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#ffeb3b",
"background": "#fffb38",
"properties": {
"branch_icon": "",
"branch_identical_icon": "≡",
@ -60,7 +60,7 @@
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#ffeb3b",
"background": "#f36943",
"properties": {
"battery_icon": "",
"discharging_icon": " ",
@ -97,13 +97,13 @@
"type": "exit",
"style": "diamond",
"foreground": "#ffffff",
"background": "#00897b",
"background": "#2e9599",
"leading_diamond": "",
"trailing_diamond": "",
"properties": {
"display_exit_code": false,
"always_enabled": true,
"error_color": "#e91e63",
"error_color": "#f1184c",
"color_background": true,
"prefix": "<#193549></> "
}

View file

@ -83,7 +83,7 @@ func (e *engine) renderSegmentText(text string) {
func (e *engine) renderBlockSegments(block *Block) string {
defer e.reset()
e.activeBlock = block
cwd, _ := e.env.getwd()
cwd := e.env.getcwd()
for _, segment := range block.Segments {
if segment.hasValue(IgnoreFolders, cwd) {
continue

View file

@ -15,7 +15,8 @@ import (
type environmentInfo interface {
getenv(key string) string
getwd() (string, error)
getcwd() string
homeDir() string
hasFiles(pattern string) bool
getPathSeperator() string
getCurrentUser() (*user.User, error)
@ -39,12 +40,27 @@ func (env *environment) getenv(key string) string {
return os.Getenv(key)
}
func (env *environment) getwd() (string, error) {
return os.Getwd()
func (env *environment) getcwd() string {
dir, err := os.Getwd()
if err != nil {
return ""
}
// on Windows, and being case sentisitive and not consistent and all, this gives silly issues
return strings.Replace(dir, "c:", "C:", 1)
}
func (env *environment) homeDir() string {
usr, err := user.Current()
if err != nil {
return ""
}
homeDir := usr.HomeDir
// on Windows, and being case sentisitive and not consistent and all, this gives silly issues
return strings.Replace(homeDir, "c:", "C:", 1)
}
func (env *environment) hasFiles(pattern string) bool {
cwd, _ := env.getwd()
cwd := env.getcwd()
pattern = cwd + env.getPathSeperator() + pattern
matches, err := filepath.Glob(pattern)
if err != nil {

View file

@ -42,9 +42,9 @@ func (pt *path) string() string {
case Short:
return pt.getShortPath()
case Full:
return pt.workingDir()
return pt.env.getcwd()
case Folder:
return base(pt.workingDir(), pt.env)
return base(pt.env.getcwd(), pt.env)
default:
return fmt.Sprintf("Path style: %s is not available", style)
}
@ -56,11 +56,11 @@ func (pt *path) init(props *properties, env environmentInfo) {
}
func (pt *path) getShortPath() string {
pwd := pt.workingDir()
pwd := pt.env.getcwd()
mappedLocations := map[string]string{
"HKCU:": pt.props.getString(WindowsRegistryIcon, "HK:"),
"Microsoft.PowerShell.Core\\FileSystem::": "",
pt.homeDir(): pt.props.getString(HomeIcon, "~"),
pt.env.homeDir(): pt.props.getString(HomeIcon, "~"),
}
for location, value := range mappedLocations {
if strings.HasPrefix(pwd, location) {
@ -71,7 +71,7 @@ func (pt *path) getShortPath() string {
}
func (pt *path) getAgnosterPath() string {
pwd := pt.workingDir()
pwd := pt.env.getcwd()
buffer := new(bytes.Buffer)
buffer.WriteString(pt.rootLocation(pwd))
pathDepth := pt.pathDepth(pwd)
@ -84,22 +84,8 @@ func (pt *path) getAgnosterPath() string {
return buffer.String()
}
func (pt *path) workingDir() string {
dir, err := pt.env.getwd()
if err != nil {
return ""
}
return dir
}
func (pt *path) homeDir() string {
// On Unix systems, $HOME comes with a trailing slash, unlike the Windows variant
home := pt.env.getenv("HOME")
return home
}
func (pt *path) inHomeDir(pwd string) bool {
return strings.HasPrefix(pwd, pt.homeDir())
return strings.HasPrefix(pwd, pt.env.homeDir())
}
func (pt *path) rootLocation(pwd string) string {
@ -124,7 +110,7 @@ func (pt *path) rootLocation(pwd string) string {
func (pt *path) pathDepth(pwd string) int {
if pt.inHomeDir(pwd) {
pwd = strings.Replace(pwd, pt.homeDir(), "root", 1)
pwd = strings.Replace(pwd, pt.env.homeDir(), "root", 1)
}
splitted := strings.Split(pwd, pt.env.getPathSeperator())
var validParts []string

View file

@ -1,7 +1,6 @@
package main
import (
"errors"
"math/rand"
"os/user"
"testing"
@ -20,9 +19,14 @@ func (env *MockedEnvironment) getenv(key string) string {
return args.String(0)
}
func (env *MockedEnvironment) getwd() (string, error) {
func (env *MockedEnvironment) getcwd() string {
args := env.Called(nil)
return args.String(0), args.Error(1)
return args.String(0)
}
func (env *MockedEnvironment) homeDir() string {
args := env.Called(nil)
return args.String(0)
}
func (env *MockedEnvironment) hasFiles(pattern string) bool {
@ -90,43 +94,10 @@ func (env *MockedEnvironment) getShellName() string {
return args.String(0)
}
func TestWorkingDir(t *testing.T) {
want := "/usr/err"
env := new(MockedEnvironment)
env.On("getwd", nil).Return(want, nil)
path := &path{
env: env,
}
got := path.workingDir()
assert.EqualValues(t, want, got)
}
func TestWorkingDirError(t *testing.T) {
env := new(MockedEnvironment)
expectedError := errors.New("emit macho dwarf: elf header corrupted")
env.On("getwd", nil).Return("random", expectedError)
path := &path{
env: env,
}
got := path.workingDir()
assert.EqualValues(t, "", got)
}
func TestHomeDir(t *testing.T) {
want := "/mnt/Users/Bill"
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(want)
path := &path{
env: env,
}
got := path.homeDir()
assert.EqualValues(t, want, got)
}
func TestIsInHomeDirTrue(t *testing.T) {
home := "/home/bill"
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
path := &path{
env: env,
}
@ -142,7 +113,7 @@ func TestIsInHomeDirLevelTrue(t *testing.T) {
pwd += "/level"
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
path := &path{
env: env,
}
@ -157,7 +128,7 @@ func TestRootLocationHome(t *testing.T) {
}
home := "/home/bill/"
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return("/home/bill")
env.On("homeDir", nil).Return("/home/bill")
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -172,7 +143,7 @@ func TestRootLocationOutsideHome(t *testing.T) {
values: map[Property]interface{}{HomeIcon: "~"},
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return("/home/bill")
env.On("homeDir", nil).Return("/home/bill")
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -187,7 +158,7 @@ func TestRootLocationWindowsDrive(t *testing.T) {
values: map[Property]interface{}{HomeIcon: "~"},
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return("C:\\Users\\Bill")
env.On("homeDir", nil).Return("C:\\Users\\Bill")
env.On("getPathSeperator", nil).Return("\\")
path := &path{
env: env,
@ -203,7 +174,7 @@ func TestRootLocationWindowsRegistry(t *testing.T) {
values: map[Property]interface{}{WindowsRegistryIcon: expected},
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return("C:\\Users\\Bill")
env.On("homeDir", nil).Return("C:\\Users\\Bill")
env.On("getPathSeperator", nil).Return("\\")
path := &path{
env: env,
@ -219,7 +190,7 @@ func TestRootLocationWindowsPowerShellHome(t *testing.T) {
values: map[Property]interface{}{HomeIcon: expected},
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return("C:\\Users\\Bill")
env.On("homeDir", nil).Return("C:\\Users\\Bill")
env.On("getPathSeperator", nil).Return("\\")
path := &path{
env: env,
@ -234,7 +205,7 @@ func TestRootLocationWindowsPowerShellOutsideHome(t *testing.T) {
values: map[Property]interface{}{WindowsRegistryIcon: "REG"},
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return("C:\\Program Files\\Go")
env.On("homeDir", nil).Return("C:\\Program Files\\Go")
env.On("getPathSeperator", nil).Return("\\")
path := &path{
env: env,
@ -249,7 +220,7 @@ func TestRootLocationEmptyDir(t *testing.T) {
values: map[Property]interface{}{WindowsRegistryIcon: "REG"},
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return("/home/bill")
env.On("homeDir", nil).Return("/home/bill")
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -262,7 +233,7 @@ func TestRootLocationEmptyDir(t *testing.T) {
func TestIsInHomeDirFalse(t *testing.T) {
home := "/home/bill"
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
path := &path{
env: env,
}
@ -274,7 +245,7 @@ func TestPathDepthInHome(t *testing.T) {
home := "/home/bill"
pwd := home
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -287,7 +258,7 @@ func TestPathDepthInHomeTrailing(t *testing.T) {
home := "/home/bill/"
pwd := home + "/"
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -304,7 +275,7 @@ func TestPathDepthInHomeMultipleLevelsDeep(t *testing.T) {
pwd += "/level"
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -321,7 +292,7 @@ func TestPathDepthOutsideHomeMultipleLevelsDeep(t *testing.T) {
pwd += "/level"
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -334,7 +305,7 @@ func TestPathDepthOutsideHomeZeroLevelsDeep(t *testing.T) {
home := "/home/gates"
pwd := "/usr/"
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -347,7 +318,7 @@ func TestPathDepthOutsideHomeOneLevelDeep(t *testing.T) {
home := "/home/gates"
pwd := "/usr/location"
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
@ -365,9 +336,9 @@ func testWritePathInfo(home string, pwd string, pathSeparator string) string {
},
}
env := new(MockedEnvironment)
env.On("getenv", "HOME").Return(home)
env.On("homeDir", nil).Return(home)
env.On("getPathSeperator", nil).Return(pathSeparator)
env.On("getwd", nil).Return(pwd, nil)
env.On("getcwd", nil).Return(pwd)
path := &path{
env: env,
props: props,