2022-01-26 06:54:36 -08:00
package segments
2019-03-13 04:14:30 -07:00
import (
2021-01-07 10:29:34 -08:00
"fmt"
2022-01-26 01:23:18 -08:00
"oh-my-posh/environment"
"oh-my-posh/mock"
2022-01-26 04:53:35 -08:00
"oh-my-posh/properties"
2022-07-13 23:00:18 -07:00
"os"
"path/filepath"
2021-12-05 09:51:10 -08:00
"strings"
2019-03-13 04:14:30 -07:00
"testing"
"github.com/stretchr/testify/assert"
)
2020-11-12 00:43:32 -08:00
const (
2022-01-23 11:24:02 -08:00
branchName = "main"
2020-11-12 00:43:32 -08:00
)
2019-03-13 04:14:30 -07:00
func TestEnabledGitNotFound ( t * testing . T ) {
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "InWSLSharedDrive" ) . Return ( false )
env . On ( "HasCommand" , "git" ) . Return ( false )
env . On ( "GOOS" ) . Return ( "" )
env . On ( "IsWsl" ) . Return ( false )
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
2022-01-01 11:08:08 -08:00
env : env ,
2022-01-26 04:53:35 -08:00
props : properties . Map { } ,
2021-12-11 13:08:47 -08:00
} ,
2019-03-13 04:14:30 -07:00
}
2022-01-26 05:26:56 -08:00
assert . False ( t , g . Enabled ( ) )
2019-03-13 04:14:30 -07:00
}
func TestEnabledInWorkingDirectory ( t * testing . T ) {
2022-01-26 01:23:18 -08:00
fileInfo := & environment . FileInfo {
2022-01-23 12:37:51 -08:00
Path : "/dir/hello" ,
ParentFolder : "/dir" ,
IsDir : true ,
2021-01-05 11:12:52 -08:00
}
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "InWSLSharedDrive" ) . Return ( false )
env . On ( "HasCommand" , "git" ) . Return ( true )
env . On ( "GOOS" ) . Return ( "" )
env . On ( "FileContent" , "/dir/hello/HEAD" ) . Return ( "" )
2022-01-26 01:23:18 -08:00
env . MockGitCommand ( fileInfo . Path , "" , "describe" , "--tags" , "--exact-match" )
2022-01-23 12:37:51 -08:00
env . On ( "IsWsl" ) . Return ( false )
env . On ( "HasParentFilePath" , ".git" ) . Return ( fileInfo , nil )
2022-07-01 04:14:25 -07:00
env . On ( "PathSeparator" ) . Return ( "/" )
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
2022-01-01 11:08:08 -08:00
env : env ,
2022-01-26 04:53:35 -08:00
props : properties . Map { } ,
2021-12-11 13:08:47 -08:00
} ,
2021-01-05 11:12:52 -08:00
}
2022-01-26 05:26:56 -08:00
assert . True ( t , g . Enabled ( ) )
2022-03-30 15:33:15 -07:00
assert . Equal ( t , fileInfo . Path , g . workingFolder )
2021-01-05 11:12:52 -08:00
}
2022-08-02 11:49:15 -07:00
func TestResolveEmptyGitPath ( t * testing . T ) {
base := "base"
assert . Equal ( t , base , resolveGitPath ( base , "" ) )
}
2022-07-13 23:00:18 -07:00
func TestEnabledInWorktree ( t * testing . T ) {
cases := [ ] struct {
Case string
ExpectedEnabled bool
WorkingFolder string
WorkingFolderAddon string
WorkingFolderContent string
ExpectedRealFolder string
ExpectedWorkingFolder string
ExpectedRootFolder string
} {
{
Case : "worktree" ,
ExpectedEnabled : true ,
2022-08-02 11:49:15 -07:00
WorkingFolder : TestRootPath + "dev/.git/worktrees/folder_worktree" ,
2022-07-13 23:00:18 -07:00
WorkingFolderAddon : "gitdir" ,
2022-08-02 11:49:15 -07:00
WorkingFolderContent : TestRootPath + "dev/worktree.git\n" ,
ExpectedWorkingFolder : TestRootPath + "dev/.git/worktrees/folder_worktree" ,
ExpectedRealFolder : TestRootPath + "dev/worktree" ,
ExpectedRootFolder : TestRootPath + "dev/.git" ,
2021-12-11 13:08:47 -08:00
} ,
2022-07-13 23:00:18 -07:00
{
Case : "submodule" ,
ExpectedEnabled : true ,
WorkingFolder : "./.git/modules/submodule" ,
2022-08-02 11:49:15 -07:00
ExpectedWorkingFolder : TestRootPath + "dev/.git/modules/submodule" ,
ExpectedRealFolder : TestRootPath + "dev/.git/modules/submodule" ,
ExpectedRootFolder : TestRootPath + "dev/.git/modules/submodule" ,
2022-07-13 23:00:18 -07:00
} ,
{
Case : "submodule with root working folder" ,
ExpectedEnabled : true ,
2022-08-02 11:49:15 -07:00
WorkingFolder : TestRootPath + "repo/.git/modules/submodule" ,
ExpectedWorkingFolder : TestRootPath + "repo/.git/modules/submodule" ,
ExpectedRealFolder : TestRootPath + "repo/.git/modules/submodule" ,
ExpectedRootFolder : TestRootPath + "repo/.git/modules/submodule" ,
2022-07-13 23:00:18 -07:00
} ,
{
Case : "submodule with worktrees" ,
ExpectedEnabled : true ,
2022-08-02 11:49:15 -07:00
WorkingFolder : TestRootPath + "dev/.git/modules/module/path/worktrees/location" ,
2022-07-20 09:37:22 -07:00
WorkingFolderAddon : "gitdir" ,
2022-08-02 11:49:15 -07:00
WorkingFolderContent : TestRootPath + "dev/worktree.git\n" ,
ExpectedWorkingFolder : TestRootPath + "dev/.git/modules/module/path" ,
ExpectedRealFolder : TestRootPath + "dev/worktree" ,
ExpectedRootFolder : TestRootPath + "dev/.git/modules/module/path" ,
2022-07-13 23:00:18 -07:00
} ,
{
Case : "separate git dir" ,
ExpectedEnabled : true ,
2022-08-02 11:49:15 -07:00
WorkingFolder : TestRootPath + "dev/separate/.git/posh" ,
ExpectedWorkingFolder : TestRootPath + "dev/" ,
ExpectedRealFolder : TestRootPath + "dev/" ,
ExpectedRootFolder : TestRootPath + "dev/separate/.git/posh" ,
2021-12-24 06:19:59 -08:00
} ,
}
2022-04-11 11:54:30 -07:00
fileInfo := & environment . FileInfo {
2022-08-02 11:49:15 -07:00
Path : TestRootPath + "dev/.git" ,
ParentFolder : TestRootPath + "dev" ,
2022-04-11 11:54:30 -07:00
}
2022-07-13 23:00:18 -07:00
for _ , tc := range cases {
env := new ( mock . MockedEnvironment )
2022-08-02 11:49:15 -07:00
env . On ( "FileContent" , TestRootPath + "dev/.git" ) . Return ( fmt . Sprintf ( "gitdir: %s" , tc . WorkingFolder ) )
2022-07-13 23:00:18 -07:00
env . On ( "FileContent" , filepath . Join ( tc . WorkingFolder , tc . WorkingFolderAddon ) ) . Return ( tc . WorkingFolderContent )
2022-07-20 09:37:22 -07:00
env . On ( "HasFilesInDir" , tc . WorkingFolder , tc . WorkingFolderAddon ) . Return ( true )
2022-07-13 23:00:18 -07:00
env . On ( "HasFilesInDir" , tc . WorkingFolder , "HEAD" ) . Return ( true )
env . On ( "PathSeparator" ) . Return ( string ( os . PathSeparator ) )
g := & Git {
scm : scm {
env : env ,
props : properties . Map { } ,
} ,
}
assert . Equal ( t , tc . ExpectedEnabled , g . hasWorktree ( fileInfo ) , tc . Case )
assert . Equal ( t , tc . ExpectedWorkingFolder , g . workingFolder , tc . Case )
assert . Equal ( t , tc . ExpectedRealFolder , g . realFolder , tc . Case )
assert . Equal ( t , tc . ExpectedRootFolder , g . rootFolder , tc . Case )
2022-04-11 11:54:30 -07:00
}
}
2022-07-13 23:00:18 -07:00
2019-03-13 04:14:30 -07:00
func TestGetGitOutputForCommand ( t * testing . T ) {
2021-11-11 23:59:03 -08:00
args := [ ] string { "-C" , "" , "--no-optional-locks" , "-c" , "core.quotepath=false" , "-c" , "color.status=false" }
2019-03-13 04:14:30 -07:00
commandArgs := [ ] string { "symbolic-ref" , "--short" , "HEAD" }
want := "je suis le output"
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "IsWsl" ) . Return ( false )
env . On ( "RunCommand" , "git" , append ( args , commandArgs ... ) ) . Return ( want , nil )
env . On ( "GOOS" ) . Return ( "unix" )
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
2022-01-01 11:08:08 -08:00
env : env ,
2022-01-26 04:53:35 -08:00
props : properties . Map { } ,
2021-12-11 13:08:47 -08:00
} ,
2019-03-13 04:14:30 -07:00
}
2020-10-07 04:32:42 -07:00
got := g . getGitCommandOutput ( commandArgs ... )
2019-03-13 04:14:30 -07:00
assert . Equal ( t , want , got )
}
2021-12-05 09:51:10 -08:00
func TestSetGitHEADContextClean ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Ref string
RebaseMerge bool
RebaseApply bool
Merge bool
CherryPick bool
Revert bool
Sequencer bool
Ours string
Theirs string
Step string
Total string
} {
{ Case : "detached on commit" , Ref : DETACHED , Expected : "branch detached at commit 1234567" } ,
{ Case : "not detached, clean" , Ref : "main" , Expected : "branch main" } ,
{
Case : "rebase merge" ,
Ref : DETACHED ,
Expected : "rebase branch origin/main onto branch main (1/2) at commit 1234567" ,
RebaseMerge : true ,
Ours : "refs/heads/origin/main" ,
Theirs : "main" ,
Step : "1" ,
Total : "2" ,
} ,
{
Case : "rebase apply" ,
Ref : DETACHED ,
Expected : "rebase branch origin/main (1/2) at commit 1234567" ,
RebaseApply : true ,
Ours : "refs/heads/origin/main" ,
Step : "1" ,
Total : "2" ,
} ,
{
Case : "merge branch" ,
Ref : "main" ,
Expected : "merge branch feat-1 into branch main" ,
Merge : true ,
Theirs : "branch 'feat-1'" ,
Ours : "main" ,
} ,
{
Case : "merge commit" ,
Ref : "main" ,
Expected : "merge commit 1234567 into branch main" ,
Merge : true ,
Theirs : "commit '123456789101112'" ,
Ours : "main" ,
} ,
{
Case : "merge tag" ,
Ref : "main" ,
Expected : "merge tag 1.2.4 into branch main" ,
Merge : true ,
Theirs : "tag '1.2.4'" ,
Ours : "main" ,
} ,
{
Case : "cherry pick" ,
Ref : "main" ,
Expected : "pick commit 1234567 onto branch main" ,
CherryPick : true ,
Theirs : "123456789101012" ,
Ours : "main" ,
} ,
{
Case : "revert" ,
Ref : "main" ,
Expected : "revert commit 1234567 onto branch main" ,
Revert : true ,
Theirs : "123456789101012" ,
Ours : "main" ,
} ,
{
Case : "sequencer cherry" ,
Ref : "main" ,
Expected : "pick commit 1234567 onto branch main" ,
Sequencer : true ,
Theirs : "pick 123456789101012" ,
Ours : "main" ,
} ,
{
Case : "sequencer cherry p" ,
Ref : "main" ,
Expected : "pick commit 1234567 onto branch main" ,
Sequencer : true ,
Theirs : "p 123456789101012" ,
Ours : "main" ,
} ,
{
Case : "sequencer revert" ,
Ref : "main" ,
Expected : "revert commit 1234567 onto branch main" ,
Sequencer : true ,
Theirs : "revert 123456789101012" ,
Ours : "main" ,
} ,
2021-07-29 13:06:56 -07:00
}
2021-12-05 09:51:10 -08:00
for _ , tc := range cases {
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "InWSLSharedDrive" ) . Return ( false )
env . On ( "GOOS" ) . Return ( "unix" )
env . On ( "IsWsl" ) . Return ( false )
2022-01-26 01:23:18 -08:00
env . MockGitCommand ( "" , "" , "describe" , "--tags" , "--exact-match" )
env . MockGitCommand ( "" , tc . Theirs , "name-rev" , "--name-only" , "--exclude=tags/*" , tc . Theirs )
env . MockGitCommand ( "" , tc . Ours , "name-rev" , "--name-only" , "--exclude=tags/*" , tc . Ours )
2021-12-05 09:51:10 -08:00
// rebase merge
2022-01-23 12:37:51 -08:00
env . On ( "HasFolder" , "/rebase-merge" ) . Return ( tc . RebaseMerge )
env . On ( "FileContent" , "/rebase-merge/head-name" ) . Return ( tc . Ours )
env . On ( "FileContent" , "/rebase-merge/onto" ) . Return ( tc . Theirs )
env . On ( "FileContent" , "/rebase-merge/msgnum" ) . Return ( tc . Step )
env . On ( "FileContent" , "/rebase-merge/end" ) . Return ( tc . Total )
2021-12-05 09:51:10 -08:00
// rebase apply
2022-01-23 12:37:51 -08:00
env . On ( "HasFolder" , "/rebase-apply" ) . Return ( tc . RebaseApply )
env . On ( "FileContent" , "/rebase-apply/head-name" ) . Return ( tc . Ours )
env . On ( "FileContent" , "/rebase-apply/next" ) . Return ( tc . Step )
env . On ( "FileContent" , "/rebase-apply/last" ) . Return ( tc . Total )
2021-12-05 09:51:10 -08:00
// merge
2022-01-23 12:37:51 -08:00
env . On ( "HasFilesInDir" , "" , "MERGE_MSG" ) . Return ( tc . Merge )
env . On ( "FileContent" , "/MERGE_MSG" ) . Return ( fmt . Sprintf ( "Merge %s into %s" , tc . Theirs , tc . Ours ) )
2021-12-05 09:51:10 -08:00
// cherry pick
2022-01-23 12:37:51 -08:00
env . On ( "HasFilesInDir" , "" , "CHERRY_PICK_HEAD" ) . Return ( tc . CherryPick )
env . On ( "FileContent" , "/CHERRY_PICK_HEAD" ) . Return ( tc . Theirs )
2021-12-05 09:51:10 -08:00
// revert
2022-01-23 12:37:51 -08:00
env . On ( "HasFilesInDir" , "" , "REVERT_HEAD" ) . Return ( tc . Revert )
env . On ( "FileContent" , "/REVERT_HEAD" ) . Return ( tc . Theirs )
2021-12-05 09:51:10 -08:00
// sequencer
2022-01-23 12:37:51 -08:00
env . On ( "HasFilesInDir" , "" , "sequencer/todo" ) . Return ( tc . Sequencer )
env . On ( "FileContent" , "/sequencer/todo" ) . Return ( tc . Theirs )
2021-07-29 13:06:56 -07:00
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
env : env ,
2022-01-26 04:53:35 -08:00
props : properties . Map {
2021-12-11 13:08:47 -08:00
BranchIcon : "branch " ,
CommitIcon : "commit " ,
RebaseIcon : "rebase " ,
MergeIcon : "merge " ,
CherryPickIcon : "pick " ,
TagIcon : "tag " ,
RevertIcon : "revert " ,
} ,
2021-12-05 09:51:10 -08:00
} ,
Hash : "1234567" ,
Ref : tc . Ref ,
}
g . setGitHEADContext ( )
assert . Equal ( t , tc . Expected , g . HEAD , tc . Case )
2021-09-13 08:29:19 -07:00
}
}
2021-12-05 09:51:10 -08:00
func TestSetPrettyHEADName ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Hash string
Tag string
HEAD string
} {
{ Case : "main" , Expected : "branch main" , HEAD : BRANCHPREFIX + "main" } ,
{ Case : "no hash" , Expected : "commit 1234567" , HEAD : "12345678910" } ,
{ Case : "hash on tag" , Hash : "132312322321" , Expected : "tag tag-1" , HEAD : "12345678910" , Tag : "tag-1" } ,
{ Case : "no hash on tag" , Expected : "tag tag-1" , Tag : "tag-1" } ,
{ Case : "hash on commit" , Hash : "1234567" , Expected : "commit 1234567" } ,
{ Case : "no hash on commit" , Expected : "commit 1234567" , HEAD : "12345678910" } ,
2020-10-11 02:56:33 -07:00
}
2021-12-05 09:51:10 -08:00
for _ , tc := range cases {
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "FileContent" , "/HEAD" ) . Return ( tc . HEAD )
env . On ( "GOOS" ) . Return ( "unix" )
env . On ( "IsWsl" ) . Return ( false )
2022-01-26 01:23:18 -08:00
env . MockGitCommand ( "" , tc . Tag , "describe" , "--tags" , "--exact-match" )
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
env : env ,
2022-01-26 04:53:35 -08:00
props : properties . Map {
2021-12-11 13:08:47 -08:00
BranchIcon : "branch " ,
CommitIcon : "commit " ,
TagIcon : "tag " ,
} ,
2021-12-05 09:51:10 -08:00
} ,
Hash : tc . Hash ,
}
g . setPrettyHEADName ( )
assert . Equal ( t , tc . Expected , g . HEAD , tc . Case )
2021-09-13 15:00:59 -07:00
}
}
2021-12-05 09:51:10 -08:00
func TestSetGitStatus ( t * testing . T ) {
cases := [ ] struct {
2022-01-25 14:59:27 -08:00
Case string
Output string
ExpectedWorking * GitStatus
ExpectedStaging * GitStatus
ExpectedHash string
ExpectedRef string
ExpectedUpstream string
ExpectedUpstreamGone bool
ExpectedAhead int
ExpectedBehind int
2021-12-05 09:51:10 -08:00
} {
{
Case : "all different options on working and staging, no remote" ,
Output : `
# branch . oid 1234567891011121314
# branch . head rework - git - status
1 . R N ...
1 . C N ...
1 . M N ...
1 . m N ...
2022-05-29 23:51:39 -07:00
1 . A N ...
2021-12-05 09:51:10 -08:00
1 . D N ...
1 . A N ...
1 . U N ...
1 A . N ...
` ,
2022-04-12 23:00:49 -07:00
ExpectedWorking : & GitStatus { ScmStatus : ScmStatus { Modified : 4 , Added : 2 , Deleted : 1 , Unmerged : 1 } } ,
ExpectedStaging : & GitStatus { ScmStatus : ScmStatus { Added : 1 } } ,
ExpectedHash : "1234567" ,
ExpectedRef : "rework-git-status" ,
ExpectedUpstreamGone : true ,
2021-12-05 09:51:10 -08:00
} ,
{
Case : "all different options on working and staging, with remote" ,
Output : `
# branch . oid 1234567891011121314
# branch . head rework - git - status
# branch . upstream origin / rework - git - status
2022-01-25 14:59:27 -08:00
# branch . ab + 0 - 0
2021-12-05 09:51:10 -08:00
1 . R N ...
1 . C N ...
1 . M N ...
1 . m N ...
2022-05-29 23:51:39 -07:00
1 . A N ...
2021-12-05 09:51:10 -08:00
1 . D N ...
1 . A N ...
1 . U N ...
1 A . N ...
` ,
2021-12-11 13:08:47 -08:00
ExpectedWorking : & GitStatus { ScmStatus : ScmStatus { Modified : 4 , Added : 2 , Deleted : 1 , Unmerged : 1 } } ,
ExpectedStaging : & GitStatus { ScmStatus : ScmStatus { Added : 1 } } ,
2021-12-05 09:51:10 -08:00
ExpectedUpstream : "origin/rework-git-status" ,
ExpectedHash : "1234567" ,
ExpectedRef : "rework-git-status" ,
} ,
{
Case : "remote with equal branch" ,
Output : `
# branch . oid 1234567891011121314
# branch . head rework - git - status
# branch . upstream origin / rework - git - status
2022-01-25 14:59:27 -08:00
# branch . ab + 0 - 0
2021-12-05 09:51:10 -08:00
` ,
ExpectedUpstream : "origin/rework-git-status" ,
ExpectedHash : "1234567" ,
ExpectedRef : "rework-git-status" ,
} ,
{
Case : "remote with branch status" ,
Output : `
# branch . oid 1234567891011121314
# branch . head rework - git - status
# branch . upstream origin / rework - git - status
# branch . ab + 2 - 1
` ,
ExpectedUpstream : "origin/rework-git-status" ,
ExpectedHash : "1234567" ,
ExpectedRef : "rework-git-status" ,
ExpectedAhead : 2 ,
ExpectedBehind : 1 ,
} ,
2021-12-13 08:37:06 -08:00
{
Case : "untracked files" ,
Output : `
# branch . oid 1234567891011121314
# branch . head main
# branch . upstream origin / main
# branch . ab + 0 - 0
? q
? qq
? qqq
` ,
ExpectedUpstream : "origin/main" ,
ExpectedHash : "1234567" ,
ExpectedRef : "main" ,
2022-05-29 23:51:39 -07:00
ExpectedWorking : & GitStatus { ScmStatus : ScmStatus { Untracked : 3 } } ,
2021-12-13 08:37:06 -08:00
} ,
2022-01-25 14:59:27 -08:00
{
Case : "remote branch was deleted" ,
Output : `
# branch . oid 1234567891011121314
# branch . head branch - is - gone
# branch . upstream origin / branch - is - gone
` ,
ExpectedUpstream : "origin/branch-is-gone" ,
ExpectedHash : "1234567" ,
ExpectedRef : "branch-is-gone" ,
ExpectedUpstreamGone : true ,
} ,
2021-09-13 15:00:59 -07:00
}
2021-12-05 09:51:10 -08:00
for _ , tc := range cases {
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "GOOS" ) . Return ( "unix" )
env . On ( "IsWsl" ) . Return ( false )
2022-01-26 01:23:18 -08:00
env . MockGitCommand ( "" , strings . ReplaceAll ( tc . Output , "\t" , "" ) , "status" , "-unormal" , "--branch" , "--porcelain=2" )
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
2022-04-21 23:11:20 -07:00
env : env ,
props : properties . Map { } ,
2021-12-11 13:08:47 -08:00
} ,
2021-12-05 09:51:10 -08:00
}
if tc . ExpectedWorking == nil {
tc . ExpectedWorking = & GitStatus { }
}
if tc . ExpectedStaging == nil {
tc . ExpectedStaging = & GitStatus { }
}
g . setGitStatus ( )
assert . Equal ( t , tc . ExpectedStaging , g . Staging , tc . Case )
assert . Equal ( t , tc . ExpectedWorking , g . Working , tc . Case )
assert . Equal ( t , tc . ExpectedHash , g . Hash , tc . Case )
assert . Equal ( t , tc . ExpectedRef , g . Ref , tc . Case )
assert . Equal ( t , tc . ExpectedUpstream , g . Upstream , tc . Case )
2022-01-25 14:59:27 -08:00
assert . Equal ( t , tc . ExpectedUpstreamGone , g . UpstreamGone , tc . Case )
2021-12-05 09:51:10 -08:00
assert . Equal ( t , tc . ExpectedAhead , g . Ahead , tc . Case )
assert . Equal ( t , tc . ExpectedBehind , g . Behind , tc . Case )
2020-10-11 02:56:33 -07:00
}
}
2019-03-13 04:14:30 -07:00
func TestGetStashContextZeroEntries ( t * testing . T ) {
2021-01-09 12:06:59 -08:00
cases := [ ] struct {
Expected int
StashContent string
} {
{ Expected : 0 , StashContent : "" } ,
{ Expected : 2 , StashContent : "1\n2\n" } ,
{ Expected : 4 , StashContent : "1\n2\n3\n4\n\n" } ,
2019-03-13 04:14:30 -07:00
}
2021-01-09 12:06:59 -08:00
for _ , tc := range cases {
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "FileContent" , "/logs/refs/stash" ) . Return ( tc . StashContent )
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
2022-03-30 15:33:15 -07:00
env : env ,
workingFolder : "" ,
2021-12-11 13:08:47 -08:00
} ,
2021-01-09 12:06:59 -08:00
}
got := g . getStashContext ( )
assert . Equal ( t , tc . Expected , got )
2019-03-13 04:14:30 -07:00
}
}
2021-10-31 13:10:18 -07:00
func TestGitUpstream ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Upstream string
} {
{ Case : "GitHub" , Expected : "GH" , Upstream : "github.com/test" } ,
{ Case : "Gitlab" , Expected : "GL" , Upstream : "gitlab.com/test" } ,
{ Case : "Bitbucket" , Expected : "BB" , Upstream : "bitbucket.org/test" } ,
{ Case : "Azure DevOps" , Expected : "AD" , Upstream : "dev.azure.com/test" } ,
{ Case : "Azure DevOps Dos" , Expected : "AD" , Upstream : "test.visualstudio.com" } ,
{ Case : "Gitstash" , Expected : "G" , Upstream : "gitstash.com/test" } ,
2020-10-24 10:26:26 -07:00
}
2021-10-31 13:10:18 -07:00
for _ , tc := range cases {
2022-01-26 01:23:18 -08:00
env := & mock . MockedEnvironment { }
2022-01-23 12:37:51 -08:00
env . On ( "IsWsl" ) . Return ( false )
env . On ( "RunCommand" , "git" , [ ] string { "-C" , "" , "--no-optional-locks" , "-c" , "core.quotepath=false" ,
2021-11-11 23:59:03 -08:00
"-c" , "color.status=false" , "remote" , "get-url" , "origin" } ) . Return ( tc . Upstream , nil )
2022-01-23 12:37:51 -08:00
env . On ( "GOOS" ) . Return ( "unix" )
2022-01-26 04:53:35 -08:00
props := properties . Map {
2021-11-26 01:37:33 -08:00
GithubIcon : "GH" ,
GitlabIcon : "GL" ,
BitbucketIcon : "BB" ,
AzureDevOpsIcon : "AD" ,
GitIcon : "G" ,
2021-10-31 13:10:18 -07:00
}
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
env : env ,
props : props ,
} ,
2021-11-09 08:13:47 -08:00
Upstream : "origin/main" ,
2021-10-31 13:10:18 -07:00
}
upstreamIcon := g . getUpstreamIcon ( )
assert . Equal ( t , tc . Expected , upstreamIcon , tc . Case )
2020-10-24 10:26:26 -07:00
}
}
2021-02-19 10:31:58 -08:00
func TestGetBranchStatus ( t * testing . T ) {
cases := [ ] struct {
2022-01-25 14:59:27 -08:00
Case string
Expected string
Ahead int
Behind int
Upstream string
UpstreamGone bool
2021-02-19 10:31:58 -08:00
} {
2021-12-05 09:51:10 -08:00
{ Case : "Equal with remote" , Expected : " equal" , Upstream : branchName } ,
2021-02-19 10:31:58 -08:00
{ Case : "Ahead" , Expected : " up2" , Ahead : 2 } ,
{ Case : "Behind" , Expected : " down8" , Behind : 8 } ,
{ Case : "Behind and ahead" , Expected : " up7 down8" , Behind : 8 , Ahead : 7 } ,
2022-01-25 14:59:27 -08:00
{ Case : "Gone" , Expected : " gone" , Upstream : branchName , UpstreamGone : true } ,
{ Case : "No remote" , Expected : "" , Upstream : "" } ,
2021-02-19 10:31:58 -08:00
{ Case : "Default (bug)" , Expected : "" , Behind : - 8 , Upstream : "wonky" } ,
}
for _ , tc := range cases {
2022-01-26 04:53:35 -08:00
props := properties . Map {
2021-11-26 01:37:33 -08:00
BranchAheadIcon : "up" ,
BranchBehindIcon : "down" ,
BranchIdenticalIcon : "equal" ,
BranchGoneIcon : "gone" ,
}
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
props : props ,
} ,
2022-01-25 14:59:27 -08:00
Ahead : tc . Ahead ,
Behind : tc . Behind ,
Upstream : tc . Upstream ,
UpstreamGone : tc . UpstreamGone ,
2021-02-19 10:31:58 -08:00
}
2021-12-08 04:36:44 -08:00
g . setBranchStatus ( )
assert . Equal ( t , tc . Expected , g . BranchStatus , tc . Case )
2021-02-19 10:31:58 -08:00
}
}
2021-05-29 04:37:05 -07:00
2021-10-20 13:20:47 -07:00
func TestGetGitCommand ( t * testing . T ) {
2021-10-20 08:01:19 -07:00
cases := [ ] struct {
2021-12-22 08:28:15 -08:00
Case string
Expected string
IsWSL bool
IsWSL1 bool
GOOS string
CWD string
IsWslSharedPath bool
2021-10-20 08:01:19 -07:00
} {
2022-08-02 22:25:59 -07:00
{ Case : "On Windows" , Expected : "git.exe" , GOOS : environment . WINDOWS } ,
2021-10-20 13:20:47 -07:00
{ Case : "Non Windows" , Expected : "git" } ,
2021-11-25 05:19:23 -08:00
{ Case : "Iside WSL2, non shared" , IsWSL : true , Expected : "git" } ,
2021-12-22 08:28:15 -08:00
{ Case : "Iside WSL2, shared" , Expected : "git.exe" , IsWSL : true , IsWslSharedPath : true , CWD : "/mnt/bill" } ,
2021-11-25 05:19:23 -08:00
{ Case : "Iside WSL1, shared" , Expected : "git" , IsWSL : true , IsWSL1 : true , CWD : "/mnt/bill" } ,
2021-10-20 08:01:19 -07:00
}
for _ , tc := range cases {
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-23 12:37:51 -08:00
env . On ( "IsWsl" ) . Return ( tc . IsWSL )
env . On ( "GOOS" ) . Return ( tc . GOOS )
env . On ( "Pwd" ) . Return ( tc . CWD )
2021-11-25 05:19:23 -08:00
wslUname := "5.10.60.1-microsoft-standard-WSL2"
if tc . IsWSL1 {
wslUname = "4.4.0-19041-Microsoft"
}
2022-01-23 12:37:51 -08:00
env . On ( "RunCommand" , "uname" , [ ] string { "-r" } ) . Return ( wslUname , nil )
2022-01-26 05:10:18 -08:00
g := & Git {
2021-12-11 13:08:47 -08:00
scm : scm {
env : env ,
} ,
2021-10-20 08:01:19 -07:00
}
2021-12-22 08:28:15 -08:00
if tc . IsWslSharedPath {
2022-01-23 12:37:51 -08:00
env . On ( "InWSLSharedDrive" ) . Return ( true )
2021-12-22 08:28:15 -08:00
g . IsWslSharedPath = tc . IsWslSharedPath
} else {
2022-01-23 12:37:51 -08:00
env . On ( "InWSLSharedDrive" ) . Return ( false )
2021-12-22 08:28:15 -08:00
}
2022-03-30 15:33:15 -07:00
assert . Equal ( t , tc . Expected , g . getCommand ( GITCOMMAND ) , tc . Case )
2021-10-20 08:01:19 -07:00
}
}
2021-10-31 12:34:13 -07:00
2021-10-31 12:59:35 -07:00
func TestGitTemplateString ( t * testing . T ) {
2021-10-31 12:34:13 -07:00
cases := [ ] struct {
Case string
Expected string
Template string
2022-01-26 05:10:18 -08:00
Git * Git
2021-10-31 12:34:13 -07:00
} {
{
2021-10-31 12:59:35 -07:00
Case : "Only HEAD name" ,
2021-12-05 09:51:10 -08:00
Expected : branchName ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}" ,
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
2021-10-31 12:34:13 -07:00
Behind : 2 ,
} ,
} ,
{
2021-10-31 12:59:35 -07:00
Case : "Working area changes" ,
2021-10-31 12:34:13 -07:00
Expected : "main \uF044 +2 ~3" ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}" ,
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
2021-10-31 12:34:13 -07:00
Working : & GitStatus {
2021-12-11 13:08:47 -08:00
ScmStatus : ScmStatus {
Added : 2 ,
Modified : 3 ,
} ,
2021-10-31 12:34:13 -07:00
} ,
} ,
} ,
{
2021-10-31 12:59:35 -07:00
Case : "No working area changes" ,
2021-12-05 09:51:10 -08:00
Expected : branchName ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}" ,
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
Working : & GitStatus { } ,
2021-10-31 12:34:13 -07:00
} ,
} ,
{
2021-10-31 12:59:35 -07:00
Case : "Working and staging area changes" ,
2021-10-31 12:34:13 -07:00
Expected : "main \uF046 +5 ~1 \uF044 +2 ~3" ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}" ,
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
2021-10-31 12:34:13 -07:00
Working : & GitStatus {
2021-12-11 13:08:47 -08:00
ScmStatus : ScmStatus {
Added : 2 ,
Modified : 3 ,
} ,
2021-10-31 12:34:13 -07:00
} ,
Staging : & GitStatus {
2021-12-11 13:08:47 -08:00
ScmStatus : ScmStatus {
Added : 5 ,
Modified : 1 ,
} ,
2021-10-31 12:34:13 -07:00
} ,
} ,
} ,
2021-11-06 12:32:24 -07:00
{
Case : "Working and staging area changes with separator" ,
Expected : "main \uF046 +5 ~1 | \uF044 +2 ~3" ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}" , //nolint:lll
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
2021-11-06 12:32:24 -07:00
Working : & GitStatus {
2021-12-11 13:08:47 -08:00
ScmStatus : ScmStatus {
Added : 2 ,
Modified : 3 ,
} ,
2021-11-06 12:32:24 -07:00
} ,
Staging : & GitStatus {
2021-12-11 13:08:47 -08:00
ScmStatus : ScmStatus {
Added : 5 ,
Modified : 1 ,
} ,
2021-11-06 12:32:24 -07:00
} ,
} ,
} ,
{
Case : "Working and staging area changes with separator and stash count" ,
Expected : "main \uF046 +5 ~1 | \uF044 +2 ~3 \uf692 3" ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if gt .StashCount 0 }} \uF692 {{ .StashCount }}{{ end }}" , //nolint:lll
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
2021-11-06 12:32:24 -07:00
Working : & GitStatus {
2021-12-11 13:08:47 -08:00
ScmStatus : ScmStatus {
Added : 2 ,
Modified : 3 ,
} ,
2021-11-06 12:32:24 -07:00
} ,
Staging : & GitStatus {
2021-12-11 13:08:47 -08:00
ScmStatus : ScmStatus {
Added : 5 ,
Modified : 1 ,
} ,
2021-11-06 12:32:24 -07:00
} ,
StashCount : 3 ,
} ,
} ,
2021-10-31 12:34:13 -07:00
{
2021-10-31 12:59:35 -07:00
Case : "No local changes" ,
2021-12-05 09:51:10 -08:00
Expected : branchName ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}" ,
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
2021-10-31 12:59:35 -07:00
Staging : & GitStatus { } ,
Working : & GitStatus { } ,
} ,
} ,
{
Case : "Upstream Icon" ,
Expected : "from GitHub on main" ,
2021-11-09 08:13:47 -08:00
Template : "from {{ .UpstreamIcon }} on {{ .HEAD }}" ,
2022-01-26 05:10:18 -08:00
Git : & Git {
2021-12-05 09:51:10 -08:00
HEAD : branchName ,
2021-10-31 13:10:18 -07:00
Staging : & GitStatus { } ,
Working : & GitStatus { } ,
2021-10-31 12:59:35 -07:00
UpstreamIcon : "GitHub" ,
} ,
} ,
2021-10-31 12:34:13 -07:00
}
for _ , tc := range cases {
2022-01-26 04:53:35 -08:00
props := properties . Map {
2021-11-26 01:37:33 -08:00
FetchStatus : true ,
2021-10-31 12:34:13 -07:00
}
2022-01-26 01:23:18 -08:00
env := new ( mock . MockedEnvironment )
2022-01-18 00:48:47 -08:00
tc . Git . env = env
2021-11-26 01:37:33 -08:00
tc . Git . props = props
2022-01-23 12:37:51 -08:00
assert . Equal ( t , tc . Expected , renderTemplate ( env , tc . Template , tc . Git ) , tc . Case )
2021-10-31 12:34:13 -07:00
}
}
2022-04-21 23:11:20 -07:00
func TestGitUntrackedMode ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
UntrackedModes map [ string ] string
} {
{
Case : "Default mode - no map" ,
Expected : "-unormal" ,
} ,
{
Case : "Default mode - no match" ,
Expected : "-unormal" ,
UntrackedModes : map [ string ] string {
"bar" : "no" ,
} ,
} ,
{
Case : "No mode - match" ,
Expected : "-uno" ,
UntrackedModes : map [ string ] string {
"foo" : "no" ,
"bar" : "normal" ,
} ,
} ,
2022-06-29 21:57:39 -07:00
{
Case : "Global mode" ,
Expected : "-uno" ,
UntrackedModes : map [ string ] string {
"*" : "no" ,
} ,
} ,
2022-04-21 23:11:20 -07:00
}
for _ , tc := range cases {
g := & Git {
scm : scm {
props : properties . Map {
UntrackedModes : tc . UntrackedModes ,
} ,
2022-03-30 15:33:15 -07:00
realFolder : "foo" ,
2022-04-21 23:11:20 -07:00
} ,
}
got := g . getUntrackedFilesMode ( )
assert . Equal ( t , tc . Expected , got , tc . Case )
}
}
2022-06-30 09:01:53 -07:00
func TestGitIgnoreSubmodules ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
IgnoreSubmodules map [ string ] string
} {
{
Case : "Overide" ,
Expected : "--ignore-submodules=all" ,
IgnoreSubmodules : map [ string ] string {
"foo" : "all" ,
} ,
} ,
{
Case : "Default mode - empty" ,
IgnoreSubmodules : map [ string ] string {
"bar" : "no" ,
} ,
} ,
{
Case : "Global mode" ,
Expected : "--ignore-submodules=dirty" ,
IgnoreSubmodules : map [ string ] string {
"*" : "dirty" ,
} ,
} ,
}
for _ , tc := range cases {
g := & Git {
scm : scm {
props : properties . Map {
IgnoreSubmodules : tc . IgnoreSubmodules ,
} ,
realFolder : "foo" ,
} ,
}
got := g . getIgnoreSubmodulesMode ( )
assert . Equal ( t , tc . Expected , got , tc . Case )
}
}