oh-my-posh/src/segment_path_test.go

514 lines
15 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import (
"testing"
"github.com/distatus/battery"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type MockedEnvironment struct {
mock.Mock
}
func (env *MockedEnvironment) getenv(key string) string {
args := env.Called(key)
return args.String(0)
}
func (env *MockedEnvironment) getcwd() string {
2019-03-13 04:14:30 -07:00
args := env.Called(nil)
return args.String(0)
}
func (env *MockedEnvironment) homeDir() string {
args := env.Called(nil)
return args.String(0)
2019-03-13 04:14:30 -07:00
}
2020-10-01 11:57:02 -07:00
func (env *MockedEnvironment) hasFiles(pattern string) bool {
args := env.Called(pattern)
return args.Bool(0)
}
func (env *MockedEnvironment) hasFilesInDir(dir, pattern string) bool {
args := env.Called(dir, pattern)
return args.Bool(0)
}
2020-10-07 04:32:42 -07:00
func (env *MockedEnvironment) hasFolder(folder string) bool {
args := env.Called(folder)
return args.Bool(0)
}
func (env *MockedEnvironment) getFileContent(file string) string {
args := env.Called(file)
return args.String(0)
}
2019-03-13 04:14:30 -07:00
func (env *MockedEnvironment) getPathSeperator() string {
args := env.Called(nil)
return args.String(0)
}
func (env *MockedEnvironment) getCurrentUser() string {
2019-03-13 04:14:30 -07:00
args := env.Called(nil)
return args.String(0)
2019-03-13 04:14:30 -07:00
}
func (env *MockedEnvironment) getHostName() (string, error) {
args := env.Called(nil)
return args.String(0), args.Error(1)
}
func (env *MockedEnvironment) getRuntimeGOOS() string {
args := env.Called(nil)
return args.String(0)
}
2020-10-21 19:49:14 -07:00
func (env *MockedEnvironment) getPlatform() string {
args := env.Called(nil)
return args.String(0)
}
func (env *MockedEnvironment) hasCommand(command string) bool {
2019-03-13 04:14:30 -07:00
args := env.Called(command)
return args.Bool(0)
2019-03-13 04:14:30 -07:00
}
func (env *MockedEnvironment) runCommand(command string, args ...string) (string, error) {
2019-03-13 04:14:30 -07:00
arguments := env.Called(command, args)
return arguments.String(0), arguments.Error(1)
2019-03-13 04:14:30 -07:00
}
func (env *MockedEnvironment) runShellCommand(shell, command string) string {
2019-03-13 04:14:30 -07:00
args := env.Called(shell, command)
return args.String(0)
}
func (env *MockedEnvironment) lastErrorCode() int {
args := env.Called(nil)
return args.Int(0)
}
2020-12-06 13:03:40 -08:00
func (env *MockedEnvironment) executionTime() float64 {
args := env.Called(nil)
return float64(args.Int(0))
}
2019-03-13 04:14:30 -07:00
func (env *MockedEnvironment) isRunningAsRoot() bool {
args := env.Called(nil)
return args.Bool(0)
}
func (env *MockedEnvironment) getArgs() *args {
arguments := env.Called(nil)
return arguments.Get(0).(*args)
}
func (env *MockedEnvironment) getBatteryInfo() (*battery.Battery, error) {
args := env.Called(nil)
return args.Get(0).(*battery.Battery), args.Error(1)
}
2020-09-24 10:11:56 -07:00
func (env *MockedEnvironment) getShellName() string {
2020-09-15 04:44:53 -07:00
args := env.Called(nil)
2020-09-24 10:11:56 -07:00
return args.String(0)
2020-09-15 04:44:53 -07:00
}
func (env *MockedEnvironment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
2020-11-04 23:56:12 -08:00
args := env.Called(imageName)
return args.String(0), args.Error(1)
}
func (env *MockedEnvironment) doGet(url string) ([]byte, error) {
args := env.Called(url)
return args.Get(0).([]byte), args.Error(1)
}
func (env *MockedEnvironment) hasParentFilePath(path string) (*fileInfo, error) {
args := env.Called(path)
return args.Get(0).(*fileInfo), args.Error(1)
}
const (
homeBill = "/home/bill"
homeJan = "/usr/home/jan"
homeBillWindows = "C:\\Users\\Bill"
levelDir = "/level"
)
2019-03-13 04:14:30 -07:00
func TestIsInHomeDirTrue(t *testing.T) {
home := homeBill
2019-03-13 04:14:30 -07:00
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
2019-03-13 04:14:30 -07:00
path := &path{
env: env,
}
got := path.inHomeDir(home)
assert.True(t, got)
}
func TestIsInHomeDirLevelTrue(t *testing.T) {
home := homeBill
2019-03-13 04:14:30 -07:00
pwd := home
2020-12-24 13:17:00 -08:00
for i := 0; i < 99; i++ {
pwd += levelDir
2019-03-13 04:14:30 -07:00
}
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
2019-03-13 04:14:30 -07:00
path := &path{
env: env,
}
got := path.inHomeDir(pwd)
assert.True(t, got)
}
func TestRootLocationHome(t *testing.T) {
cases := []struct {
Expected string
HomePath string
Pswd string
Pwd string
PathSeperator string
HomeIcon string
RegistryIcon string
}{
{Expected: "~", HomeIcon: "~", HomePath: "/home/bill/", Pwd: "/home/bill/", PathSeperator: "/"},
{Expected: "usr", HomePath: "/home/bill/", Pwd: "/usr/error/what", PathSeperator: "/"},
{Expected: "C:", HomePath: "C:\\Users\\Bill", Pwd: "C:\\Program Files\\Go", PathSeperator: "\\"},
{Expected: "REG", RegistryIcon: "REG", HomePath: "C:\\Users\\Bill", Pwd: "HKCU:\\Program Files\\Go", PathSeperator: "\\"},
{Expected: "~", HomeIcon: "~", HomePath: "C:\\Users\\Bill", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", PathSeperator: "\\"},
{Expected: "C:", HomePath: "C:\\Users\\Jack", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", PathSeperator: "\\"},
{Expected: "", HomePath: "C:\\Users\\Jack", Pwd: "", PathSeperator: "\\"},
{Expected: "DRIVE:", HomePath: "/home/bill/", Pwd: "/usr/error/what", Pswd: "DRIVE:", PathSeperator: "/"},
2019-03-13 04:14:30 -07:00
}
for _, tc := range cases {
props := &properties{
values: map[Property]interface{}{
HomeIcon: tc.HomeIcon,
WindowsRegistryIcon: tc.RegistryIcon,
},
}
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(tc.HomePath)
env.On("getcwd", nil).Return(tc.Pwd)
args := &args{
PSWD: &tc.Pswd,
}
env.On("getArgs", nil).Return(args)
env.On("getPathSeperator", nil).Return(tc.PathSeperator)
path := &path{
env: env,
props: props,
}
got := path.rootLocation()
assert.EqualValues(t, tc.Expected, got)
2019-03-13 04:14:30 -07:00
}
}
func TestIsInHomeDirFalse(t *testing.T) {
home := homeBill
2019-03-13 04:14:30 -07:00
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
2019-03-13 04:14:30 -07:00
path := &path{
env: env,
}
got := path.inHomeDir("/usr/error")
assert.False(t, got)
}
2020-12-24 13:17:00 -08:00
func TestPathDepthMultipleLevelsDeep(t *testing.T) {
2019-03-13 04:14:30 -07:00
pwd := "/usr"
2020-12-24 13:17:00 -08:00
for i := 0; i < 99; i++ {
pwd += levelDir
2019-03-13 04:14:30 -07:00
}
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
}
got := path.pathDepth(pwd)
2020-12-24 13:17:00 -08:00
assert.Equal(t, 99, got)
2019-03-13 04:14:30 -07:00
}
2020-12-24 13:17:00 -08:00
func TestPathDepthZeroLevelsDeep(t *testing.T) {
2019-03-13 04:14:30 -07:00
pwd := "/usr/"
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
}
got := path.pathDepth(pwd)
assert.Equal(t, 0, got)
}
2020-12-24 13:17:00 -08:00
func TestPathDepthOneLevelDeep(t *testing.T) {
2019-03-13 04:14:30 -07:00
pwd := "/usr/location"
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
path := &path{
env: env,
}
got := path.pathDepth(pwd)
assert.Equal(t, 1, got)
}
func TestAgnosterPathStyles(t *testing.T) {
cases := []struct {
Expected string
HomePath string
Pswd string
Pwd string
PathSeperator string
HomeIcon string
FolderSeparatorIcon string
Style string
}{
{Style: AgnosterFull, Expected: "usr > location > whatever", HomePath: "/usr/home", Pwd: "/usr/location/whatever", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "usr > .. > man", HomePath: "/usr/home", Pwd: "/usr/location/whatever/man", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "~ > .. > man", HomePath: "/usr/home", Pwd: "/usr/home/whatever/man", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "~ > projects", HomePath: "/usr/home", Pwd: "/usr/home/projects", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "C:", HomePath: homeBillWindows, Pwd: "C:", PathSeperator: "\\", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "", HomePath: homeBillWindows, Pwd: "/", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "foo", HomePath: homeBillWindows, Pwd: "/foo", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterFull, Expected: "PSDRIVE: | src", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src", PathSeperator: "/", FolderSeparatorIcon: " | "},
{Style: AgnosterShort, Expected: "PSDRIVE: | .. | init", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src/init", PathSeperator: "/", FolderSeparatorIcon: " | "},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return(tc.PathSeperator)
env.On("homeDir", nil).Return(tc.HomePath)
env.On("getcwd", nil).Return(tc.Pwd)
args := &args{
PSWD: &tc.Pswd,
}
env.On("getArgs", nil).Return(args)
path := &path{
env: env,
props: &properties{
values: map[Property]interface{}{
FolderSeparatorIcon: tc.FolderSeparatorIcon,
Style: tc.Style,
},
},
}
got := path.string()
assert.Equal(t, tc.Expected, got)
}
}
func TestGetFullPath(t *testing.T) {
cases := []struct {
Style string
FolderSeparatorIcon string
Pwd string
Pswd string
Expected string
}{
{Style: Full, Pwd: "", Expected: ""},
{Style: Full, Pwd: "/", Expected: "/"},
{Style: Full, Pwd: "/usr/home", Expected: "~"},
{Style: Full, Pwd: "/usr/home/abc", Expected: "~/abc"},
{Style: Full, Pwd: "/a/b/c/d", Expected: "/a/b/c/d"},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "", Expected: ""},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", Expected: "|"},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/usr/home", Expected: "~"},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/usr/home/abc", Expected: "~|abc"},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/a/b/c/d", Expected: "|a|b|c|d"},
{Style: Folder, Pwd: "", Expected: "."},
{Style: Folder, Pwd: "/", Expected: "/"},
{Style: Folder, Pwd: "/usr/home", Expected: "~"},
{Style: Folder, Pwd: "/usr/home/abc", Expected: "abc"},
{Style: Folder, Pwd: "/a/b/c/d", Expected: "d"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "", Expected: "."},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/", Expected: "|"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/usr/home", Expected: "~"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/usr/home/abc", Expected: "abc"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/a/b/c/d", Expected: "d"},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
env.On("homeDir", nil).Return("/usr/home")
env.On("getcwd", nil).Return(tc.Pwd)
args := &args{
PSWD: &tc.Pswd,
}
env.On("getArgs", nil).Return(args)
props := &properties{
values: map[Property]interface{}{
Style: tc.Style,
},
}
if tc.FolderSeparatorIcon != "" {
props.values[FolderSeparatorIcon] = tc.FolderSeparatorIcon
}
path := &path{
env: env,
props: props,
}
got := path.string()
assert.Equal(t, tc.Expected, got)
}
}
func TestGetFolderPathCustomMappedLocations(t *testing.T) {
pwd := "/a/b/c/d"
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
env.On("homeDir", nil).Return("/usr/home")
env.On("getcwd", nil).Return(pwd)
args := &args{
PSWD: &pwd,
}
env.On("getArgs", nil).Return(args)
path := &path{
env: env,
props: &properties{
values: map[Property]interface{}{
MappedLocations: map[string]string{
"/a/b/c/d": "#",
},
},
},
}
got := path.getFolderPath()
assert.Equal(t, "#", got)
}
func testWritePathInfo(home, pwd, pathSeparator string) string {
2019-03-13 04:14:30 -07:00
props := &properties{
values: map[Property]interface{}{
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
},
}
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
2019-03-13 04:14:30 -07:00
env.On("getPathSeperator", nil).Return(pathSeparator)
env.On("getcwd", nil).Return(pwd)
args := &args{
PSWD: &pwd,
}
env.On("getArgs", nil).Return(args)
2019-03-13 04:14:30 -07:00
path := &path{
env: env,
props: props,
}
return path.getAgnosterPath()
}
func TestWritePathInfoWindowsOutsideHome(t *testing.T) {
home := homeBillWindows
2019-03-13 04:14:30 -07:00
want := "C: > f > f > location"
got := testWritePathInfo(home, "C:\\Program Files\\Go\\location", "\\")
assert.Equal(t, want, got)
}
func TestWritePathInfoWindowsInsideHome(t *testing.T) {
home := homeBillWindows
2019-03-13 04:14:30 -07:00
location := home + "\\Documents\\Bill\\location"
want := "~ > f > f > location"
got := testWritePathInfo(home, location, "\\")
assert.Equal(t, want, got)
}
func TestWritePathInfoWindowsOutsideHomeZeroLevels(t *testing.T) {
home := homeBillWindows
2019-03-13 04:14:30 -07:00
want := "C: > location"
got := testWritePathInfo(home, "C:\\location", "\\")
assert.Equal(t, want, got)
}
func TestWritePathInfoWindowsOutsideHomeOneLevels(t *testing.T) {
home := homeBillWindows
2019-03-13 04:14:30 -07:00
want := "C: > f > location"
got := testWritePathInfo(home, "C:\\Program Files\\location", "\\")
assert.Equal(t, want, got)
}
func TestWritePathInfoUnixOutsideHome(t *testing.T) {
home := homeJan
2019-03-13 04:14:30 -07:00
want := "mnt > f > f > location"
got := testWritePathInfo(home, "/mnt/go/test/location", "/")
assert.Equal(t, want, got)
}
func TestWritePathInfoUnixInsideHome(t *testing.T) {
home := homeJan
2019-03-13 04:14:30 -07:00
location := home + "/docs/jan/location"
want := "~ > f > f > location"
got := testWritePathInfo(home, location, "/")
assert.Equal(t, want, got)
}
func TestWritePathInfoUnixOutsideHomeZeroLevels(t *testing.T) {
home := homeJan
2019-03-13 04:14:30 -07:00
want := "mnt > location"
got := testWritePathInfo(home, "/mnt/location", "/")
assert.Equal(t, want, got)
}
func TestWritePathInfoUnixOutsideHomeOneLevels(t *testing.T) {
home := homeJan
2019-03-13 04:14:30 -07:00
want := "mnt > f > location"
got := testWritePathInfo(home, "/mnt/folder/location", "/")
assert.Equal(t, want, got)
}
2020-12-24 13:17:00 -08:00
func TestGetPwd(t *testing.T) {
cases := []struct {
MappedLocationsEnabled bool
Pwd string
Pswd string
2020-12-24 13:17:00 -08:00
Expected string
}{
{MappedLocationsEnabled: true, Pwd: "", Expected: ""},
{MappedLocationsEnabled: true, Pwd: "/usr", Expected: "/usr"},
{MappedLocationsEnabled: true, Pwd: "/usr/home", Expected: "~"},
{MappedLocationsEnabled: true, Pwd: "/usr/home/abc", Expected: "~/abc"},
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d", Expected: "#"},
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d/e/f/g", Expected: "#/e/f/g"},
{MappedLocationsEnabled: true, Pwd: "/z/y/x/w", Expected: "/z/y/x/w"},
{MappedLocationsEnabled: false, Pwd: "", Expected: ""},
{MappedLocationsEnabled: false, Pwd: "/usr/home/abc", Expected: "/usr/home/abc"},
{MappedLocationsEnabled: false, Pwd: "/a/b/c/d/e/f/g", Expected: "/a/b/c/d/e/f/g"},
{MappedLocationsEnabled: true, Pwd: "/w/d/x/w", Pswd: "/z/y/x/w", Expected: "/z/y/x/w"},
{MappedLocationsEnabled: false, Pwd: "/f/g/k/d/e/f/g", Pswd: "/a/b/c/d/e/f/g", Expected: "/a/b/c/d/e/f/g"},
2020-12-24 13:17:00 -08:00
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
env.On("homeDir", nil).Return("/usr/home")
env.On("getcwd", nil).Return(tc.Pwd)
args := &args{
PSWD: &tc.Pswd,
}
env.On("getArgs", nil).Return(args)
2020-12-24 13:17:00 -08:00
path := &path{
env: env,
props: &properties{
values: map[Property]interface{}{
MappedLocationsEnabled: tc.MappedLocationsEnabled,
MappedLocations: map[string]string{
"/a/b/c/d": "#",
},
},
},
}
got := path.getPwd()
assert.Equal(t, tc.Expected, got)
}
}