2019-03-13 04:14:30 -07:00
package main
import (
"testing"
"github.com/distatus/battery"
2021-05-02 05:32:48 -07:00
"github.com/gookit/config/v2"
2021-06-08 11:16:55 -07:00
"github.com/mitchellh/mapstructure"
2019-03-13 04:14:30 -07:00
"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 )
}
2020-10-02 12:50:13 -07:00
func ( env * MockedEnvironment ) getcwd ( ) string {
2019-03-13 04:14:30 -07:00
args := env . Called ( nil )
2020-10-02 12:50:13 -07:00
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 )
}
2020-12-01 11:43:30 -08:00
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 )
}
2021-09-04 11:32:55 -07:00
func ( env * MockedEnvironment ) getFoldersList ( path string ) [ ] string {
args := env . Called ( path )
return args . Get ( 0 ) . ( [ ] string )
}
2019-03-13 04:14:30 -07:00
func ( env * MockedEnvironment ) getPathSeperator ( ) string {
args := env . Called ( nil )
return args . String ( 0 )
}
2020-10-12 03:53:54 -07:00
func ( env * MockedEnvironment ) getCurrentUser ( ) string {
2019-03-13 04:14:30 -07:00
args := env . Called ( nil )
2020-10-12 03:53:54 -07:00
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 )
}
2021-01-05 04:05:37 -08:00
func ( env * MockedEnvironment ) hasCommand ( command string ) bool {
2019-03-13 04:14:30 -07:00
args := env . Called ( command )
2021-01-05 04:05:37 -08:00
return args . Bool ( 0 )
2019-03-13 04:14:30 -07:00
}
2020-10-16 08:43:02 -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 )
2020-10-16 08:43:02 -07:00
return arguments . String ( 0 ) , arguments . Error ( 1 )
2019-03-13 04:14:30 -07:00
}
2020-11-12 00:43:32 -08: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 )
}
2021-04-15 02:04:11 -07:00
func ( env * MockedEnvironment ) getBatteryInfo ( ) ( [ ] * battery . Battery , error ) {
2019-03-13 04:14:30 -07:00
args := env . Called ( nil )
2021-04-15 02:04:11 -07:00
return args . Get ( 0 ) . ( [ ] * battery . Battery ) , args . Error ( 1 )
2019-03-13 04:14:30 -07:00
}
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
}
2020-11-12 00:43:32 -08: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 )
}
2021-11-24 04:47:30 -08:00
func ( env * MockedEnvironment ) getWindowsRegistryKeyValue ( regPath , regKey string ) ( string , error ) {
args := env . Called ( regPath , regKey )
return args . String ( 0 ) , args . Error ( 1 )
}
2021-08-17 23:21:55 -07:00
func ( env * MockedEnvironment ) doGet ( url string , timeout int ) ( [ ] byte , error ) {
2020-11-19 19:12:20 -08:00
args := env . Called ( url )
return args . Get ( 0 ) . ( [ ] byte ) , args . Error ( 1 )
}
2021-01-05 11:12:52 -08:00
func ( env * MockedEnvironment ) hasParentFilePath ( path string ) ( * fileInfo , error ) {
args := env . Called ( path )
return args . Get ( 0 ) . ( * fileInfo ) , args . Error ( 1 )
}
2021-04-12 01:58:03 -07:00
func ( env * MockedEnvironment ) stackCount ( ) int {
args := env . Called ( nil )
return args . Int ( 0 )
}
2021-02-14 23:26:52 -08:00
func ( env * MockedEnvironment ) isWsl ( ) bool {
2021-10-11 00:02:51 -07:00
args := env . Called ( nil )
return args . Bool ( 0 )
2021-02-14 23:26:52 -08:00
}
2021-05-22 07:50:34 -07:00
func ( env * MockedEnvironment ) getTerminalWidth ( ) ( int , error ) {
args := env . Called ( nil )
return args . Int ( 0 ) , args . Error ( 1 )
}
2021-10-02 22:27:18 -07:00
func ( env * MockedEnvironment ) getCachePath ( ) string {
args := env . Called ( nil )
return args . String ( 0 )
}
2021-09-21 11:22:59 -07:00
func ( env * MockedEnvironment ) cache ( ) cache {
args := env . Called ( nil )
return args . Get ( 0 ) . ( cache )
}
func ( env * MockedEnvironment ) close ( ) {
_ = env . Called ( nil )
}
2021-11-16 10:59:42 -08:00
func ( env * MockedEnvironment ) logs ( ) string {
args := env . Called ( nil )
return args . String ( 0 )
}
2020-11-12 00:43:32 -08:00
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 ) {
2020-11-12 00:43:32 -08:00
home := homeBill
2019-03-13 04:14:30 -07:00
env := new ( MockedEnvironment )
2020-10-02 12:50:13 -07:00
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 ) {
2020-11-12 00:43:32 -08:00
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 ++ {
2020-11-12 00:43:32 -08:00
pwd += levelDir
2019-03-13 04:14:30 -07:00
}
env := new ( MockedEnvironment )
2020-10-02 12:50:13 -07:00
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 ) {
2021-01-09 04:48:54 -08:00
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
}
2021-01-09 04:48:54 -08:00
for _ , tc := range cases {
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 )
2021-09-18 13:03:00 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
2021-01-09 04:48:54 -08:00
path := & path {
2021-11-26 01:37:33 -08:00
env : env ,
props : map [ Property ] interface { } {
HomeIcon : tc . HomeIcon ,
WindowsRegistryIcon : tc . RegistryIcon ,
} ,
2021-01-09 04:48:54 -08:00
}
got := path . rootLocation ( )
assert . EqualValues ( t , tc . Expected , got )
2019-03-13 04:14:30 -07:00
}
}
func TestIsInHomeDirFalse ( t * testing . T ) {
2020-11-12 00:43:32 -08:00
home := homeBill
2019-03-13 04:14:30 -07:00
env := new ( MockedEnvironment )
2020-10-02 12:50:13 -07:00
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 ++ {
2020-11-12 00:43:32 -08:00
pwd += levelDir
2019-03-13 04:14:30 -07:00
}
env := new ( MockedEnvironment )
env . On ( "getPathSeperator" , nil ) . Return ( "/" )
2021-09-18 13:03:00 -07:00
env . On ( "getRunteGOOS" , nil ) . Return ( "" )
2019-03-13 04:14:30 -07:00
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 )
}
2021-01-09 04:48:54 -08:00
func TestAgnosterPathStyles ( t * testing . T ) {
2020-12-25 19:33:51 -08:00
cases := [ ] struct {
2021-01-09 04:48:54 -08:00
Expected string
HomePath string
Pswd string
Pwd string
PathSeperator string
HomeIcon string
FolderSeparatorIcon string
Style string
2021-03-17 04:49:30 -07:00
GOOS string
2021-08-20 08:06:37 -07:00
MaxDepth int
2020-12-25 19:33:51 -08:00
} {
2021-01-09 04:48:54 -08:00
{ 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 : " > " } ,
2021-09-30 01:29:17 -07:00
{ Style : AgnosterShort , Expected : "/" , HomePath : homeBillWindows , Pwd : "/" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
2021-01-09 04:48:54 -08:00
{ Style : AgnosterShort , Expected : "foo" , HomePath : homeBillWindows , Pwd : "/foo" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
2020-12-25 19:33:51 -08:00
2021-08-20 08:06:37 -07:00
{ Style : AgnosterShort , Expected : "usr > .. > bar > man" , HomePath : "/usr/home" , Pwd : "/usr/foo/bar/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " , MaxDepth : 2 } ,
{ Style : AgnosterShort , Expected : "usr > foo > bar > man" , HomePath : "/usr/home" , Pwd : "/usr/foo/bar/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " , MaxDepth : 3 } ,
{ Style : AgnosterShort , Expected : "~ > .. > bar > man" , HomePath : "/usr/home" , Pwd : "/usr/home/foo/bar/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " , MaxDepth : 2 } ,
{ Style : AgnosterShort , Expected : "~ > foo > bar > man" , HomePath : "/usr/home" , Pwd : "/usr/home/foo/bar/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " , MaxDepth : 3 } ,
{
Style : AgnosterShort ,
Expected : "C: > .. > bar > man" ,
HomePath : homeBillWindows ,
Pwd : "C:\\usr\\foo\\bar\\man" ,
PathSeperator : "\\" ,
FolderSeparatorIcon : " > " ,
MaxDepth : 2 ,
} ,
{
Style : AgnosterShort ,
Expected : "C: > .. > foo > bar > man" ,
HomePath : homeBillWindows ,
Pwd : "C:\\usr\\foo\\bar\\man" ,
PathSeperator : "\\" ,
FolderSeparatorIcon : " > " ,
MaxDepth : 3 ,
} ,
{
Style : AgnosterShort ,
Expected : "~ > .. > bar > man" ,
HomePath : homeBillWindows ,
Pwd : "C:\\Users\\Bill\\foo\\bar\\man" ,
PathSeperator : "\\" ,
FolderSeparatorIcon : " > " ,
MaxDepth : 2 ,
} ,
{
Style : AgnosterShort ,
Expected : "~ > foo > bar > man" ,
HomePath : homeBillWindows ,
Pwd : "C:\\Users\\Bill\\foo\\bar\\man" ,
PathSeperator : "\\" ,
FolderSeparatorIcon : " > " ,
MaxDepth : 3 ,
} ,
2021-01-09 04:48:54 -08:00
{ 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 : " | " } ,
2021-02-09 02:57:32 -08:00
{ Style : Mixed , Expected : "~ > .. > man" , HomePath : "/usr/home" , Pwd : "/usr/home/whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Mixed , Expected : "~ > ab > .. > man" , HomePath : "/usr/home" , Pwd : "/usr/home/ab/whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
2021-07-13 11:15:53 -07:00
{ Style : Letter , Expected : "~ > a > w > man" , HomePath : "/usr/home" , Pwd : "/usr/home/ab/whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "u > b > a > w > man" , HomePath : "/usr/home" , Pwd : "/usr/burp/ab/whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
2021-08-09 11:36:26 -07:00
{ Style : Letter , Expected : "u > .b > a > w > man" , HomePath : "/usr/home" , Pwd : "/usr/.burp/ab/whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "u > .b > a > .w > man" , HomePath : "/usr/home" , Pwd : "/usr/.burp/ab/.whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
2021-10-15 17:14:34 -07:00
{ Style : Letter , Expected : "u > .b > a > ._w > man" , HomePath : "/usr/home" , Pwd : "/usr/.burp/ab/._whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "u > .ä > ū > .w > man" , HomePath : "/usr/home" , Pwd : "/usr/.äufbau/ūmgebung/.whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "u > .b > 1 > .w > man" , HomePath : "/usr/home" , Pwd : "/usr/.burp/12345/.whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "u > .b > 1 > .w > man" , HomePath : "/usr/home" , Pwd : "/usr/.burp/12345abc/.whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "u > .b > __p > .w > man" , HomePath : "/usr/home" , Pwd : "/usr/.burp/__pycache__/.whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "➼ > .w > man" , HomePath : "/usr/home" , Pwd : "➼/.whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
{ Style : Letter , Expected : "➼ s > .w > man" , HomePath : "/usr/home" , Pwd : "➼ something/.whatever/man" , PathSeperator : "/" , FolderSeparatorIcon : " > " } ,
2021-01-09 04:48:54 -08:00
}
2020-12-25 19:33:51 -08:00
for _ , tc := range cases {
env := new ( MockedEnvironment )
2021-01-09 04:48:54 -08:00
env . On ( "getPathSeperator" , nil ) . Return ( tc . PathSeperator )
env . On ( "homeDir" , nil ) . Return ( tc . HomePath )
2020-12-25 19:33:51 -08:00
env . On ( "getcwd" , nil ) . Return ( tc . Pwd )
2021-03-17 04:49:30 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( tc . GOOS )
2021-01-09 04:48:54 -08:00
args := & args {
PSWD : & tc . Pswd ,
2020-12-25 19:33:51 -08:00
}
2021-01-09 04:48:54 -08:00
env . On ( "getArgs" , nil ) . Return ( args )
2020-12-25 19:33:51 -08:00
path := & path {
env : env ,
2021-11-26 01:37:33 -08:00
props : map [ Property ] interface { } {
FolderSeparatorIcon : tc . FolderSeparatorIcon ,
Style : tc . Style ,
MaxDepth : tc . MaxDepth ,
2020-12-25 19:33:51 -08:00
} ,
}
2021-01-09 04:48:54 -08:00
got := path . string ( )
2020-12-25 19:33:51 -08:00
assert . Equal ( t , tc . Expected , got )
}
}
2021-01-09 04:48:54 -08:00
func TestGetFullPath ( t * testing . T ) {
2021-01-03 08:21:17 -08:00
cases := [ ] struct {
2021-03-05 08:11:06 -08:00
Style string
FolderSeparatorIcon string
Pwd string
Pswd string
Expected string
DisableMappedLocations bool
2021-03-17 04:49:30 -07:00
GOOS string
PathSeparator string
2021-04-12 01:58:03 -07:00
StackCount int
StackCountEnabled bool
2021-01-03 08:21:17 -08:00
} {
2021-03-17 04:49:30 -07:00
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/" , Expected : "/" } ,
2021-01-09 04:48:54 -08:00
{ Style : Full , Pwd : "" , Expected : "" } ,
{ Style : Full , Pwd : "/" , Expected : "/" } ,
{ Style : Full , Pwd : "/usr/home" , Expected : "~" } ,
{ Style : Full , Pwd : "/usr/home/abc" , Expected : "~/abc" } ,
2021-03-05 08:11:06 -08:00
{ Style : Full , Pwd : "/usr/home/abc" , Expected : "/usr/home/abc" , DisableMappedLocations : true } ,
2021-01-09 04:48:54 -08:00
{ Style : Full , Pwd : "/a/b/c/d" , Expected : "/a/b/c/d" } ,
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "" , Expected : "" } ,
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/usr/home" , Expected : "~" } ,
2021-03-05 08:11:06 -08:00
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/usr/home" , Expected : "|usr|home" , DisableMappedLocations : true } ,
2021-01-09 04:48:54 -08:00
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/usr/home/abc" , Expected : "~|abc" } ,
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/a/b/c/d" , Expected : "|a|b|c|d" } ,
2021-03-17 04:49:30 -07:00
{ Style : Folder , Pwd : "" , Expected : "" } ,
2021-01-09 04:48:54 -08:00
{ Style : Folder , Pwd : "/" , Expected : "/" } ,
{ Style : Folder , Pwd : "/usr/home" , Expected : "~" } ,
2021-03-05 08:11:06 -08:00
{ Style : Folder , Pwd : "/usr/home" , Expected : "home" , DisableMappedLocations : true } ,
2021-01-09 04:48:54 -08:00
{ Style : Folder , Pwd : "/usr/home/abc" , Expected : "abc" } ,
{ Style : Folder , Pwd : "/a/b/c/d" , Expected : "d" } ,
2021-03-17 04:49:30 -07:00
{ Style : Folder , FolderSeparatorIcon : "|" , Pwd : "" , Expected : "" } ,
{ Style : Folder , FolderSeparatorIcon : "|" , Pwd : "/" , Expected : "/" } ,
2021-01-09 04:48:54 -08:00
{ Style : Folder , FolderSeparatorIcon : "|" , Pwd : "/usr/home" , Expected : "~" } ,
2021-03-05 08:11:06 -08:00
{ Style : Folder , FolderSeparatorIcon : "|" , Pwd : "/usr/home" , Expected : "home" , DisableMappedLocations : true } ,
2021-01-09 04:48:54 -08:00
{ Style : Folder , FolderSeparatorIcon : "|" , Pwd : "/usr/home/abc" , Expected : "abc" } ,
{ Style : Folder , FolderSeparatorIcon : "|" , Pwd : "/a/b/c/d" , Expected : "d" } ,
2021-03-17 04:49:30 -07:00
{ Style : Folder , FolderSeparatorIcon : "\\" , Pwd : "C:\\" , Expected : "C:\\" , PathSeparator : "\\" , GOOS : windowsPlatform } ,
{ Style : Full , FolderSeparatorIcon : "\\" , Pwd : "C:\\Users\\Jan" , Expected : "C:\\Users\\Jan" , PathSeparator : "\\" , GOOS : windowsPlatform } ,
2021-04-12 01:58:03 -07:00
// StackCountEnabled=true and StackCount=2
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/" , StackCountEnabled : true , StackCount : 2 , Expected : "2 /" } ,
{ Style : Full , Pwd : "" , StackCountEnabled : true , StackCount : 2 , Expected : "2 " } ,
{ Style : Full , Pwd : "/" , StackCountEnabled : true , StackCount : 2 , Expected : "2 /" } ,
{ Style : Full , Pwd : "/usr/home" , StackCountEnabled : true , StackCount : 2 , Expected : "2 ~" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , StackCount : 2 , Expected : "2 ~/abc" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , StackCount : 2 , Expected : "2 /usr/home/abc" , DisableMappedLocations : true } ,
{ Style : Full , Pwd : "/a/b/c/d" , StackCountEnabled : true , StackCount : 2 , Expected : "2 /a/b/c/d" } ,
// StackCountEnabled=false and StackCount=2
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/" , StackCountEnabled : false , StackCount : 2 , Expected : "/" } ,
{ Style : Full , Pwd : "" , StackCountEnabled : false , StackCount : 2 , Expected : "" } ,
{ Style : Full , Pwd : "/" , StackCountEnabled : false , StackCount : 2 , Expected : "/" } ,
{ Style : Full , Pwd : "/usr/home" , StackCountEnabled : false , StackCount : 2 , Expected : "~" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : false , StackCount : 2 , Expected : "~/abc" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : false , StackCount : 2 , Expected : "/usr/home/abc" , DisableMappedLocations : true } ,
{ Style : Full , Pwd : "/a/b/c/d" , StackCountEnabled : false , StackCount : 2 , Expected : "/a/b/c/d" } ,
// StackCountEnabled=true and StackCount=0
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/" , StackCountEnabled : true , StackCount : 0 , Expected : "/" } ,
{ Style : Full , Pwd : "" , StackCountEnabled : true , StackCount : 0 , Expected : "" } ,
{ Style : Full , Pwd : "/" , StackCountEnabled : true , StackCount : 0 , Expected : "/" } ,
{ Style : Full , Pwd : "/usr/home" , StackCountEnabled : true , StackCount : 0 , Expected : "~" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , StackCount : 0 , Expected : "~/abc" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , StackCount : 0 , Expected : "/usr/home/abc" , DisableMappedLocations : true } ,
{ Style : Full , Pwd : "/a/b/c/d" , StackCountEnabled : true , StackCount : 0 , Expected : "/a/b/c/d" } ,
// StackCountEnabled=true and StackCount<0
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/" , StackCountEnabled : true , StackCount : - 1 , Expected : "/" } ,
{ Style : Full , Pwd : "" , StackCountEnabled : true , StackCount : - 1 , Expected : "" } ,
{ Style : Full , Pwd : "/" , StackCountEnabled : true , StackCount : - 1 , Expected : "/" } ,
{ Style : Full , Pwd : "/usr/home" , StackCountEnabled : true , StackCount : - 1 , Expected : "~" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , StackCount : - 1 , Expected : "~/abc" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , StackCount : - 1 , Expected : "/usr/home/abc" , DisableMappedLocations : true } ,
{ Style : Full , Pwd : "/a/b/c/d" , StackCountEnabled : true , StackCount : - 1 , Expected : "/a/b/c/d" } ,
// StackCountEnabled=true and StackCount not set
{ Style : Full , FolderSeparatorIcon : "|" , Pwd : "/" , StackCountEnabled : true , Expected : "/" } ,
{ Style : Full , Pwd : "" , StackCountEnabled : true , Expected : "" } ,
{ Style : Full , Pwd : "/" , StackCountEnabled : true , Expected : "/" } ,
{ Style : Full , Pwd : "/usr/home" , StackCountEnabled : true , Expected : "~" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , Expected : "~/abc" } ,
{ Style : Full , Pwd : "/usr/home/abc" , StackCountEnabled : true , Expected : "/usr/home/abc" , DisableMappedLocations : true } ,
{ Style : Full , Pwd : "/a/b/c/d" , StackCountEnabled : true , Expected : "/a/b/c/d" } ,
2020-12-24 06:19:15 -08:00
}
2021-01-03 08:21:17 -08:00
for _ , tc := range cases {
env := new ( MockedEnvironment )
2021-03-17 04:49:30 -07:00
if len ( tc . PathSeparator ) == 0 {
tc . PathSeparator = "/"
}
env . On ( "getPathSeperator" , nil ) . Return ( tc . PathSeparator )
2021-01-03 08:21:17 -08:00
env . On ( "homeDir" , nil ) . Return ( "/usr/home" )
env . On ( "getcwd" , nil ) . Return ( tc . Pwd )
2021-03-17 04:49:30 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( tc . GOOS )
2021-04-12 01:58:03 -07:00
env . On ( "stackCount" , nil ) . Return ( tc . StackCount )
2021-01-09 04:48:54 -08:00
args := & args {
PSWD : & tc . Pswd ,
2021-01-03 08:21:17 -08:00
}
2021-01-09 04:48:54 -08:00
env . On ( "getArgs" , nil ) . Return ( args )
2021-11-26 01:37:33 -08:00
var props properties = map [ Property ] interface { } {
Style : tc . Style ,
StackCountEnabled : tc . StackCountEnabled ,
2021-01-03 08:21:17 -08:00
}
2021-01-09 04:48:54 -08:00
if tc . FolderSeparatorIcon != "" {
2021-11-26 01:37:33 -08:00
props [ FolderSeparatorIcon ] = tc . FolderSeparatorIcon
2021-01-09 04:48:54 -08:00
}
2021-03-05 08:11:06 -08:00
if tc . DisableMappedLocations {
2021-11-26 01:37:33 -08:00
props [ MappedLocationsEnabled ] = false
2021-03-05 08:11:06 -08:00
}
2021-01-09 04:48:54 -08:00
path := & path {
env : env ,
props : props ,
}
got := path . string ( )
2021-01-03 08:21:17 -08:00
assert . Equal ( t , tc . Expected , got )
2020-12-24 06:19:15 -08:00
}
}
2021-09-18 13:03:00 -07:00
func TestGetFullPathCustomMappedLocations ( t * testing . T ) {
cases := [ ] struct {
Pwd string
MappedLocations map [ string ] string
Expected string
} {
{ Pwd : "/a/b/c/d" , MappedLocations : map [ string ] string { "/a/b/c/d" : "#" } , Expected : "#" } ,
{ Pwd : "/a/b/c/d" , MappedLocations : map [ string ] string { "\\a\\b" : "#" } , Expected : "#/c/d" } ,
{ Pwd : "\\a\\b\\c\\d" , MappedLocations : map [ string ] string { "\\a\\b" : "#" } , Expected : "#\\c\\d" } ,
{ Pwd : "/a/b/c/d" , MappedLocations : map [ string ] string { "/a/b" : "#" } , Expected : "#/c/d" } ,
{ Pwd : "/a/b/c/d" , MappedLocations : map [ string ] string { "/a/b" : "/e/f" } , Expected : "/e/f/c/d" } ,
{ Pwd : "/usr/home/a/b/c/d" , MappedLocations : map [ string ] string { "~\\a\\b" : "#" } , Expected : "#/c/d" } ,
{ Pwd : "/usr/home/a/b/c/d" , MappedLocations : map [ string ] string { "~/a/b" : "#" } , Expected : "#/c/d" } ,
{ Pwd : "/a/usr/home/b/c/d" , MappedLocations : map [ string ] string { "/a~" : "#" } , Expected : "/a/usr/home/b/c/d" } ,
{ Pwd : "/usr/home/a/b/c/d" , MappedLocations : map [ string ] string { "/a/b" : "#" } , Expected : "/usr/home/a/b/c/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 )
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
args := & args {
PSWD : & tc . Pwd ,
}
env . On ( "getArgs" , nil ) . Return ( args )
path := & path {
env : env ,
2021-11-26 01:37:33 -08:00
props : map [ Property ] interface { } {
MappedLocationsEnabled : false ,
MappedLocations : tc . MappedLocations ,
2021-09-18 13:03:00 -07:00
} ,
}
got := path . getFullPath ( )
assert . Equal ( t , tc . Expected , got )
}
}
func TestNormalizePath ( t * testing . T ) {
cases := [ ] struct {
Input string
GOOS string
Expected string
} {
{ Input : "C:\\Users\\Bob\\Foo" , GOOS : linuxPlatform , Expected : "C:/Users/Bob/Foo" } ,
{ Input : "C:\\Users\\Bob\\Foo" , GOOS : windowsPlatform , Expected : "c:/users/bob/foo" } ,
{ Input : "~\\Bob\\Foo" , GOOS : linuxPlatform , Expected : "/usr/home/Bob/Foo" } ,
{ Input : "~\\Bob\\Foo" , GOOS : windowsPlatform , Expected : "/usr/home/bob/foo" } ,
{ Input : "/foo/~/bar" , GOOS : linuxPlatform , Expected : "/foo/~/bar" } ,
{ Input : "/foo/~/bar" , GOOS : windowsPlatform , Expected : "/foo/~/bar" } ,
{ Input : "~/baz" , GOOS : linuxPlatform , Expected : "/usr/home/baz" } ,
{ Input : "~/baz" , GOOS : windowsPlatform , Expected : "/usr/home/baz" } ,
}
for _ , tc := range cases {
env := new ( MockedEnvironment )
env . On ( "homeDir" , nil ) . Return ( "/usr/home" )
env . On ( "getRuntimeGOOS" , nil ) . Return ( tc . GOOS )
pt := & path {
env : env ,
}
got := pt . normalize ( tc . Input )
assert . Equal ( t , tc . Expected , got )
}
}
2020-12-24 06:19:15 -08:00
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 )
2021-09-18 13:03:00 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
2021-01-09 04:48:54 -08:00
args := & args {
PSWD : & pwd ,
}
env . On ( "getArgs" , nil ) . Return ( args )
2020-12-24 06:19:15 -08:00
path := & path {
env : env ,
2021-11-26 01:37:33 -08:00
props : map [ Property ] interface { } {
MappedLocations : map [ string ] string {
"/a/b/c/d" : "#" ,
2020-12-24 06:19:15 -08:00
} ,
} ,
}
got := path . getFolderPath ( )
assert . Equal ( t , "#" , got )
}
2021-11-05 01:02:21 -07:00
func TestAgnosterPath ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Home string
PWD string
PathSeparator string
} {
{ Case : "Windows outside home" , Expected : "C: > f > f > location" , Home : homeBillWindows , PWD : "C:\\Program Files\\Go\\location" , PathSeparator : "\\" } ,
{ Case : "Windows oustide home" , Expected : "~ > f > f > location" , Home : homeBillWindows , PWD : homeBillWindows + "\\Documents\\Bill\\location" , PathSeparator : "\\" } ,
{ Case : "Windows inside home zero levels" , Expected : "C: > location" , Home : homeBillWindows , PWD : "C:\\location" , PathSeparator : "\\" } ,
{ Case : "Windows inside home one level" , Expected : "C: > f > location" , Home : homeBillWindows , PWD : "C:\\Program Files\\location" , PathSeparator : "\\" } ,
{ Case : "Windows lower case drive letter" , Expected : "C: > Windows" , Home : homeBillWindows , PWD : "C:\\Windows\\" , PathSeparator : "\\" } ,
{ Case : "Windows lower case drive letter (other)" , Expected : "P: > Other" , Home : homeBillWindows , PWD : "P:\\Other\\" , PathSeparator : "\\" } ,
{ Case : "Windows lower word drive" , Expected : "some: > some" , Home : homeBillWindows , PWD : "some:\\some\\" , PathSeparator : "\\" } ,
{ Case : "Windows lower word drive (ending with c)" , Expected : "src: > source" , Home : homeBillWindows , PWD : "src:\\source\\" , PathSeparator : "\\" } ,
{ Case : "Windows lower word drive (arbitrary cases)" , Expected : "sRc: > source" , Home : homeBillWindows , PWD : "sRc:\\source\\" , PathSeparator : "\\" } ,
{ Case : "Windows registry drive" , Expected : "\uf013 > f > magnetic:test" , Home : homeBillWindows , PWD : "HKLM:\\SOFTWARE\\magnetic:test\\" , PathSeparator : "\\" } ,
{ Case : "Windows registry drive case sensitive" , Expected : "\uf013 > f > magnetic:TOAST" , Home : homeBillWindows , PWD : "HKLM:\\SOFTWARE\\magnetic:TOAST\\" , PathSeparator : "\\" } ,
{ Case : "Unix outside home" , Expected : "mnt > f > f > location" , Home : homeJan , PWD : "/mnt/go/test/location" , PathSeparator : "/" } ,
{ Case : "Unix inside home" , Expected : "~ > f > f > location" , Home : homeJan , PWD : homeJan + "/docs/jan/location" , PathSeparator : "/" } ,
{ Case : "Unix outside home zero levels" , Expected : "mnt > location" , Home : homeJan , PWD : "/mnt/location" , PathSeparator : "/" } ,
{ Case : "Unix outside home one level" , Expected : "mnt > f > location" , Home : homeJan , PWD : "/mnt/folder/location" , PathSeparator : "/" } ,
2019-03-13 04:14:30 -07:00
}
2021-11-05 01:02:21 -07:00
for _ , tc := range cases {
env := new ( MockedEnvironment )
env . On ( "homeDir" , nil ) . Return ( tc . Home )
env . On ( "getPathSeperator" , nil ) . Return ( tc . PathSeparator )
env . On ( "getcwd" , nil ) . Return ( tc . PWD )
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
args := & args {
PSWD : & tc . PWD ,
}
env . On ( "getArgs" , nil ) . Return ( args )
path := & path {
2021-11-26 01:37:33 -08:00
env : env ,
props : map [ Property ] interface { } {
FolderSeparatorIcon : " > " ,
FolderIcon : "f" ,
HomeIcon : "~" ,
} ,
2021-11-05 01:02:21 -07:00
}
got := path . getAgnosterPath ( )
assert . Equal ( t , tc . Expected , got , tc . Case )
}
2019-03-13 04:14:30 -07:00
}
2020-12-24 13:17:00 -08:00
func TestGetPwd ( t * testing . T ) {
cases := [ ] struct {
MappedLocationsEnabled bool
Pwd string
2021-01-09 04:48:54 -08:00
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" } ,
2021-07-24 10:49:01 -07:00
{ MappedLocationsEnabled : false , Pwd : "/a/b/c/d/e/f/g" , Expected : "#/e/f/g" } ,
{ MappedLocationsEnabled : false , Pwd : "/usr/home/c/d/e/f/g" , Expected : "/usr/home/c/d/e/f/g" } ,
{ MappedLocationsEnabled : true , Pwd : "/usr/home/c/d/e/f/g" , Expected : "~/c/d/e/f/g" } ,
2021-01-09 04:48:54 -08:00
{ MappedLocationsEnabled : true , Pwd : "/w/d/x/w" , Pswd : "/z/y/x/w" , Expected : "/z/y/x/w" } ,
2021-07-24 10:49:01 -07:00
{ MappedLocationsEnabled : false , Pwd : "/f/g/k/d/e/f/g" , Pswd : "/a/b/c/d/e/f/g" , Expected : "#/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 )
2021-09-18 13:03:00 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
2021-01-09 04:48:54 -08:00
args := & args {
PSWD : & tc . Pswd ,
}
env . On ( "getArgs" , nil ) . Return ( args )
2020-12-24 13:17:00 -08:00
path := & path {
env : env ,
2021-11-26 01:37:33 -08:00
props : map [ Property ] interface { } {
MappedLocationsEnabled : tc . MappedLocationsEnabled ,
MappedLocations : map [ string ] string {
"/a/b/c/d" : "#" ,
2020-12-24 13:17:00 -08:00
} ,
} ,
}
got := path . getPwd ( )
assert . Equal ( t , tc . Expected , got )
}
}
2021-05-02 05:32:48 -07:00
func TestParseMappedLocations ( t * testing . T ) {
2021-11-22 06:25:56 -08:00
defer testClearDefaultConfig ( )
2021-05-02 05:32:48 -07:00
cases := [ ] struct {
Case string
JSON string
} {
{ Case : "new format" , JSON : ` { "properties": { "mapped_locations": { "folder1": "one","folder2": "two"} } } ` } ,
{ Case : "old format" , JSON : ` { "properties": { "mapped_locations": [["folder1", "one"], ["folder2", "two"]] } } ` } ,
}
for _ , tc := range cases {
config . ClearAll ( )
config . WithOptions ( func ( opt * config . Options ) {
2021-06-08 11:16:55 -07:00
opt . DecoderConfig = & mapstructure . DecoderConfig {
TagName : "config" ,
}
2021-05-02 05:32:48 -07:00
} )
err := config . LoadStrings ( config . JSON , tc . JSON )
assert . NoError ( t , err )
var segment Segment
err = config . BindStruct ( "" , & segment )
assert . NoError ( t , err )
2021-11-26 01:37:33 -08:00
mappedLocations := segment . Properties . getKeyValueMap ( MappedLocations , make ( map [ string ] string ) )
2021-05-02 05:32:48 -07:00
assert . Equal ( t , "two" , mappedLocations [ "folder2" ] )
}
}