2019-03-13 04:14:30 -07:00
package main
import (
2021-01-07 10:29:34 -08:00
"fmt"
2019-03-13 04:14:30 -07:00
"testing"
"github.com/stretchr/testify/assert"
)
2020-11-12 00:43:32 -08:00
const (
changesColor = "#BD8BDE"
)
2019-03-13 04:14:30 -07:00
func TestEnabledGitNotFound ( t * testing . T ) {
env := new ( MockedEnvironment )
2021-01-05 04:05:37 -08:00
env . On ( "hasCommand" , "git" ) . Return ( false )
2021-10-20 13:20:47 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
env . On ( "isWsl" , nil ) . Return ( false )
2019-03-13 04:14:30 -07:00
g := & git {
env : env ,
}
assert . False ( t , g . enabled ( ) )
}
func TestEnabledInWorkingDirectory ( t * testing . T ) {
env := new ( MockedEnvironment )
2021-01-05 04:05:37 -08:00
env . On ( "hasCommand" , "git" ) . Return ( true )
2021-10-20 13:20:47 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
env . On ( "isWsl" , nil ) . Return ( false )
2021-01-05 11:12:52 -08:00
fileInfo := & fileInfo {
path : "/dir/hello" ,
parentFolder : "/dir" ,
isDir : true ,
}
env . On ( "hasParentFilePath" , ".git" ) . Return ( fileInfo , nil )
g := & git {
env : env ,
}
assert . True ( t , g . enabled ( ) )
2021-11-09 08:13:47 -08:00
assert . Equal ( t , fileInfo . path , g . gitWorkingFolder )
2021-01-05 11:12:52 -08:00
}
func TestEnabledInWorkingTree ( t * testing . T ) {
env := new ( MockedEnvironment )
env . On ( "hasCommand" , "git" ) . Return ( true )
2021-10-20 13:20:47 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "" )
env . On ( "isWsl" , nil ) . Return ( false )
2021-01-05 11:12:52 -08:00
fileInfo := & fileInfo {
2021-11-11 23:59:03 -08:00
path : "/dev/folder_worktree/.git" ,
parentFolder : "/dev/folder_worktree" ,
2021-01-05 11:12:52 -08:00
isDir : false ,
}
env . On ( "hasParentFilePath" , ".git" ) . Return ( fileInfo , nil )
2021-11-11 23:59:03 -08:00
env . On ( "getFileContent" , "/dev/folder_worktree/.git" ) . Return ( "gitdir: /dev/real_folder/.git/worktrees/folder_worktree" )
env . On ( "getFileContent" , "/dev/real_folder/.git/worktrees/folder_worktree/gitdir" ) . Return ( "/dev/folder_worktree.git\n" )
2019-03-13 04:14:30 -07:00
g := & git {
env : env ,
}
assert . True ( t , g . enabled ( ) )
2021-11-11 23:59:03 -08:00
assert . Equal ( t , "/dev/real_folder/.git/worktrees/folder_worktree" , g . gitWorkingFolder )
assert . Equal ( t , "/dev/folder_worktree" , g . gitWorktreeFolder )
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"
env := new ( MockedEnvironment )
2021-10-11 00:02:51 -07:00
env . On ( "isWsl" , nil ) . Return ( false )
2020-10-16 08:43:02 -07:00
env . On ( "runCommand" , "git" , append ( args , commandArgs ... ) ) . Return ( want , nil )
2021-03-07 08:05:10 -08:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "unix" )
2019-03-13 04:14:30 -07:00
g := & git {
2021-11-10 23:45:46 -08:00
env : env ,
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 )
}
2020-10-07 04:32:42 -07:00
type detachedContext struct {
currentCommit string
rebase string
rebaseMerge bool
rebaseApply bool
origin string
onto string
step string
total string
branchName string
tagName string
2020-10-07 11:44:22 -07:00
cherryPick bool
cherryPickSHA string
2021-07-29 13:06:56 -07:00
revert bool
revertSHA string
sequencer bool
sequencerTodo string
2020-10-11 02:56:33 -07:00
merge bool
mergeHEAD string
2021-09-13 08:29:19 -07:00
mergeMsgStart string
2021-03-13 06:58:17 -08:00
status string
2020-10-07 04:32:42 -07:00
}
2020-10-13 01:00:17 -07:00
func setupHEADContextEnv ( context * detachedContext ) * git {
2019-03-13 04:14:30 -07:00
env := new ( MockedEnvironment )
2021-10-11 00:02:51 -07:00
env . On ( "isWsl" , nil ) . Return ( false )
2021-01-05 11:12:52 -08:00
env . On ( "hasFolder" , "/rebase-merge" ) . Return ( context . rebaseMerge )
env . On ( "hasFolder" , "/rebase-apply" ) . Return ( context . rebaseApply )
2021-07-29 13:25:54 -07:00
env . On ( "hasFolder" , "/sequencer" ) . Return ( context . sequencer )
2021-01-07 10:29:34 -08:00
env . On ( "getFileContent" , "/rebase-merge/head-name" ) . Return ( context . origin )
2021-01-05 11:12:52 -08:00
env . On ( "getFileContent" , "/rebase-merge/onto" ) . Return ( context . onto )
env . On ( "getFileContent" , "/rebase-merge/msgnum" ) . Return ( context . step )
env . On ( "getFileContent" , "/rebase-apply/next" ) . Return ( context . step )
env . On ( "getFileContent" , "/rebase-merge/end" ) . Return ( context . total )
env . On ( "getFileContent" , "/rebase-apply/last" ) . Return ( context . total )
env . On ( "getFileContent" , "/rebase-apply/head-name" ) . Return ( context . origin )
env . On ( "getFileContent" , "/CHERRY_PICK_HEAD" ) . Return ( context . cherryPickSHA )
2021-07-29 13:06:56 -07:00
env . On ( "getFileContent" , "/REVERT_HEAD" ) . Return ( context . revertSHA )
2021-09-13 08:29:19 -07:00
env . On ( "getFileContent" , "/MERGE_MSG" ) . Return ( fmt . Sprintf ( "%s '%s' into %s" , context . mergeMsgStart , context . mergeHEAD , context . onto ) )
2021-07-29 13:25:54 -07:00
env . On ( "getFileContent" , "/sequencer/todo" ) . Return ( context . sequencerTodo )
2021-08-04 21:43:12 -07:00
env . On ( "getFileContent" , "/HEAD" ) . Return ( context . branchName )
2021-01-05 11:12:52 -08:00
env . On ( "hasFilesInDir" , "" , "CHERRY_PICK_HEAD" ) . Return ( context . cherryPick )
2021-07-29 13:06:56 -07:00
env . On ( "hasFilesInDir" , "" , "REVERT_HEAD" ) . Return ( context . revert )
2021-01-07 10:29:34 -08:00
env . On ( "hasFilesInDir" , "" , "MERGE_MSG" ) . Return ( context . merge )
2021-01-05 11:12:52 -08:00
env . On ( "hasFilesInDir" , "" , "MERGE_HEAD" ) . Return ( context . merge )
2021-07-29 13:25:54 -07:00
env . On ( "hasFilesInDir" , "" , "sequencer/todo" ) . Return ( context . sequencer )
2020-11-12 00:43:32 -08:00
env . mockGitCommand ( context . currentCommit , "rev-parse" , "--short" , "HEAD" )
env . mockGitCommand ( context . tagName , "describe" , "--tags" , "--exact-match" )
env . mockGitCommand ( context . origin , "name-rev" , "--name-only" , "--exclude=tags/*" , context . origin )
env . mockGitCommand ( context . onto , "name-rev" , "--name-only" , "--exclude=tags/*" , context . onto )
2021-03-09 10:42:47 -08:00
env . mockGitCommand ( context . branchName , "branch" , "--show-current" )
2021-03-13 06:58:17 -08:00
env . mockGitCommand ( context . status , "status" , "-unormal" , "--short" , "--branch" )
2021-03-07 08:05:10 -08:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "unix" )
2020-10-13 01:00:17 -07:00
g := & git {
2021-11-09 08:13:47 -08:00
env : env ,
gitWorkingFolder : "" ,
Working : & GitStatus { } ,
Staging : & GitStatus { } ,
2020-10-13 01:00:17 -07:00
}
return g
2020-10-07 04:32:42 -07:00
}
2020-11-12 00:43:32 -08:00
func ( m * MockedEnvironment ) mockGitCommand ( returnValue string , args ... string ) {
2021-11-11 23:59:03 -08:00
args = append ( [ ] string { "-C" , "" , "--no-optional-locks" , "-c" , "core.quotepath=false" , "-c" , "color.status=false" } , args ... )
2020-11-12 00:43:32 -08:00
m . On ( "runCommand" , "git" , args ) . Return ( returnValue , nil )
}
2020-10-07 04:32:42 -07:00
func TestGetGitDetachedCommitHash ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\uf417lalasha1"
2020-10-07 04:32:42 -07:00
context := & detachedContext {
2020-10-07 11:44:22 -07:00
currentCommit : "lalasha1" ,
2020-10-07 04:32:42 -07:00
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-10 07:38:57 -07:00
got := g . getGitHEADContext ( "" )
2019-03-13 04:14:30 -07:00
assert . Equal ( t , want , got )
}
2020-10-07 11:44:22 -07:00
func TestGetGitHEADContextTagName ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\uf412lalasha1"
2020-10-07 04:32:42 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
2020-10-07 11:44:22 -07:00
tagName : "lalasha1" ,
2020-10-07 04:32:42 -07:00
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-10 07:38:57 -07:00
got := g . getGitHEADContext ( "" )
2020-10-07 04:32:42 -07:00
assert . Equal ( t , want , got )
}
2020-10-07 11:44:22 -07:00
func TestGetGitHEADContextRebaseMerge ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\ue728 \ue0a0cool-feature-bro onto \ue0a0main (2/3) at \uf417whatever"
2020-10-07 04:32:42 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
rebase : "true" ,
rebaseMerge : true ,
origin : "cool-feature-bro" ,
onto : "main" ,
step : "2" ,
total : "3" ,
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-10 07:38:57 -07:00
got := g . getGitHEADContext ( "" )
2020-10-07 04:32:42 -07:00
assert . Equal ( t , want , got )
}
2020-10-07 11:44:22 -07:00
func TestGetGitHEADContextRebaseApply ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\ue728 \ue0a0cool-feature-bro (2/3) at \uf417whatever"
2020-10-07 04:32:42 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
rebase : "true" ,
rebaseApply : true ,
origin : "cool-feature-bro" ,
step : "2" ,
total : "3" ,
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-10 07:38:57 -07:00
got := g . getGitHEADContext ( "" )
2020-10-07 04:32:42 -07:00
assert . Equal ( t , want , got )
}
2020-10-07 11:44:22 -07:00
func TestGetGitHEADContextRebaseUnknown ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\uf417whatever"
2020-10-07 04:32:42 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
rebase : "true" ,
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-10 07:38:57 -07:00
got := g . getGitHEADContext ( "" )
2020-10-07 11:44:22 -07:00
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextCherryPickOnBranch ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\ue29b pickme onto \ue0a0main"
2020-10-07 11:44:22 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
branchName : "main" ,
cherryPick : true ,
cherryPickSHA : "pickme" ,
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-10 07:38:57 -07:00
got := g . getGitHEADContext ( "main" )
2020-10-07 11:44:22 -07:00
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextCherryPickOnTag ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\ue29b pickme onto \uf412v3.4.6"
2020-10-07 11:44:22 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
tagName : "v3.4.6" ,
cherryPick : true ,
cherryPickSHA : "pickme" ,
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-10 07:38:57 -07:00
got := g . getGitHEADContext ( "" )
2019-03-13 04:14:30 -07:00
assert . Equal ( t , want , got )
}
2021-07-29 13:06:56 -07:00
func TestGetGitHEADContextRevertOnBranch ( t * testing . T ) {
2021-07-29 14:35:58 -07:00
want := "\uf0e2 012345 onto \ue0a0main"
2021-07-29 13:06:56 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
branchName : "main" ,
revert : true ,
2021-07-29 14:35:58 -07:00
revertSHA : "01234567" ,
2021-07-29 13:06:56 -07:00
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "main" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextRevertOnTag ( t * testing . T ) {
2021-07-29 14:35:58 -07:00
want := "\uf0e2 012345 onto \uf412v3.4.6"
2021-07-29 13:06:56 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
tagName : "v3.4.6" ,
revert : true ,
2021-07-29 14:35:58 -07:00
revertSHA : "01234567" ,
2021-07-29 13:06:56 -07:00
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextSequencerCherryPickOnBranch ( t * testing . T ) {
want := "\ue29b pickme onto \ue0a0main"
context := & detachedContext {
currentCommit : "whatever" ,
branchName : "main" ,
sequencer : true ,
2021-07-29 13:25:54 -07:00
sequencerTodo : "pick pickme message\npick notme message" ,
2021-07-29 13:06:56 -07:00
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "main" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextSequencerCherryPickOnTag ( t * testing . T ) {
want := "\ue29b pickme onto \uf412v3.4.6"
context := & detachedContext {
currentCommit : "whatever" ,
tagName : "v3.4.6" ,
sequencer : true ,
2021-07-29 14:35:58 -07:00
sequencerTodo : "pick pickme message\npick notme message" ,
2021-07-29 13:06:56 -07:00
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextSequencerRevertOnBranch ( t * testing . T ) {
2021-07-29 14:35:58 -07:00
want := "\uf0e2 012345 onto \ue0a0main"
2021-07-29 13:06:56 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
branchName : "main" ,
sequencer : true ,
2021-07-29 14:35:58 -07:00
sequencerTodo : "revert 01234567 message\nrevert notme message" ,
2021-07-29 13:06:56 -07:00
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "main" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextSequencerRevertOnTag ( t * testing . T ) {
2021-07-29 14:35:58 -07:00
want := "\uf0e2 012345 onto \uf412v3.4.6"
2021-07-29 13:06:56 -07:00
context := & detachedContext {
currentCommit : "whatever" ,
tagName : "v3.4.6" ,
sequencer : true ,
2021-07-29 14:35:58 -07:00
sequencerTodo : "revert 01234567 message\nrevert notme message" ,
2021-07-29 13:06:56 -07:00
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "" )
assert . Equal ( t , want , got )
}
2020-10-11 02:56:33 -07:00
func TestGetGitHEADContextMerge ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\ue727 \ue0a0feat into \ue0a0main"
2020-10-11 02:56:33 -07:00
context := & detachedContext {
2021-09-13 08:29:19 -07:00
merge : true ,
mergeHEAD : "feat" ,
mergeMsgStart : "Merge branch" ,
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "main" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextMergeRemote ( t * testing . T ) {
want := "\ue727 \ue0a0feat into \ue0a0main"
context := & detachedContext {
merge : true ,
mergeHEAD : "feat" ,
mergeMsgStart : "Merge remote-tracking branch" ,
2020-10-11 02:56:33 -07:00
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-11 02:56:33 -07:00
got := g . getGitHEADContext ( "main" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextMergeTag ( t * testing . T ) {
2021-09-13 15:00:59 -07:00
want := "\ue727 \uf412v7.8.9 into \ue0a0main"
context := & detachedContext {
merge : true ,
mergeHEAD : "v7.8.9" ,
mergeMsgStart : "Merge tag" ,
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "main" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextMergeCommit ( t * testing . T ) {
want := "\ue727 \uf4178d7e869 into \ue0a0main"
context := & detachedContext {
merge : true ,
mergeHEAD : "8d7e869" ,
mergeMsgStart : "Merge commit" ,
}
g := setupHEADContextEnv ( context )
got := g . getGitHEADContext ( "main" )
assert . Equal ( t , want , got )
}
func TestGetGitHEADContextMergeIntoTag ( t * testing . T ) {
2020-10-16 04:28:54 -07:00
want := "\ue727 \ue0a0feat into \uf412v3.4.6"
2020-10-11 02:56:33 -07:00
context := & detachedContext {
2021-09-13 08:29:19 -07:00
tagName : "v3.4.6" ,
merge : true ,
mergeHEAD : "feat" ,
mergeMsgStart : "Merge branch" ,
2020-10-11 02:56:33 -07:00
}
2020-10-13 01:00:17 -07:00
g := setupHEADContextEnv ( context )
2020-10-11 02:56:33 -07:00
got := g . getGitHEADContext ( "" )
assert . Equal ( t , want , got )
}
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 {
env := new ( MockedEnvironment )
env . On ( "getFileContent" , "/logs/refs/stash" ) . Return ( tc . StashContent )
g := & git {
2021-11-09 08:13:47 -08:00
env : env ,
gitWorkingFolder : "" ,
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
}
}
func TestParseGitBranchInfoEqual ( t * testing . T ) {
g := git { }
branchInfo := "## master...origin/master"
2020-10-07 11:44:22 -07:00
got := g . parseGitStatusInfo ( branchInfo )
2019-03-13 04:14:30 -07:00
assert . Equal ( t , "master" , got [ "local" ] )
assert . Equal ( t , "origin/master" , got [ "upstream" ] )
assert . Empty ( t , got [ "ahead" ] )
assert . Empty ( t , got [ "behind" ] )
}
func TestParseGitBranchInfoAhead ( t * testing . T ) {
g := git { }
branchInfo := "## master...origin/master [ahead 1]"
2020-10-07 11:44:22 -07:00
got := g . parseGitStatusInfo ( branchInfo )
2019-03-13 04:14:30 -07:00
assert . Equal ( t , "master" , got [ "local" ] )
assert . Equal ( t , "origin/master" , got [ "upstream" ] )
assert . Equal ( t , "1" , got [ "ahead" ] )
assert . Empty ( t , got [ "behind" ] )
}
func TestParseGitBranchInfoBehind ( t * testing . T ) {
g := git { }
branchInfo := "## master...origin/master [behind 1]"
2020-10-07 11:44:22 -07:00
got := g . parseGitStatusInfo ( branchInfo )
2019-03-13 04:14:30 -07:00
assert . Equal ( t , "master" , got [ "local" ] )
assert . Equal ( t , "origin/master" , got [ "upstream" ] )
assert . Equal ( t , "1" , got [ "behind" ] )
assert . Empty ( t , got [ "ahead" ] )
}
func TestParseGitBranchInfoBehindandAhead ( t * testing . T ) {
g := git { }
branchInfo := "## master...origin/master [ahead 1, behind 2]"
2020-10-07 11:44:22 -07:00
got := g . parseGitStatusInfo ( branchInfo )
2019-03-13 04:14:30 -07:00
assert . Equal ( t , "master" , got [ "local" ] )
assert . Equal ( t , "origin/master" , got [ "upstream" ] )
assert . Equal ( t , "2" , got [ "behind" ] )
assert . Equal ( t , "1" , got [ "ahead" ] )
}
2020-10-10 10:16:58 -07:00
func TestParseGitBranchInfoNoRemote ( t * testing . T ) {
g := git { }
branchInfo := "## master"
got := g . parseGitStatusInfo ( branchInfo )
assert . Equal ( t , "master" , got [ "local" ] )
assert . Empty ( t , got [ "upstream" ] )
}
2020-10-11 04:32:21 -07:00
2020-11-10 12:35:08 -08:00
func TestParseGitBranchInfoRemoteGone ( t * testing . T ) {
g := git { }
branchInfo := "## test-branch...origin/test-branch [gone]"
got := g . parseGitStatusInfo ( branchInfo )
assert . Equal ( t , "test-branch" , got [ "local" ] )
assert . Equal ( t , "gone" , got [ "upstream_status" ] )
}
2020-10-11 04:32:21 -07:00
func TestGitStatusUnmerged ( t * testing . T ) {
2021-10-31 13:10:18 -07:00
expected := "x1"
2021-10-31 11:52:12 -07:00
status := & GitStatus {
Unmerged : 1 ,
2020-10-11 04:32:21 -07:00
}
2021-10-31 12:34:13 -07:00
assert . Equal ( t , expected , status . String ( ) )
2020-10-11 04:32:21 -07:00
}
func TestGitStatusUnmergedModified ( t * testing . T ) {
2021-10-31 13:10:18 -07:00
expected := "~3 x1"
2021-10-31 11:52:12 -07:00
status := & GitStatus {
Unmerged : 1 ,
Modified : 3 ,
2020-10-11 04:32:21 -07:00
}
2021-10-31 12:34:13 -07:00
assert . Equal ( t , expected , status . String ( ) )
2020-10-11 04:32:21 -07:00
}
func TestGitStatusEmpty ( t * testing . T ) {
expected := ""
2021-10-31 11:52:12 -07:00
status := & GitStatus { }
2021-10-31 12:34:13 -07:00
assert . Equal ( t , expected , status . String ( ) )
2020-10-11 04:32:21 -07:00
}
func TestParseGitStatsWorking ( t * testing . T ) {
2021-10-31 12:34:13 -07:00
status := & GitStatus { }
2020-10-11 04:32:21 -07:00
output := [ ] string {
"## amazing-feat" ,
" M change.go" ,
"DD change.go" ,
" ? change.go" ,
" ? change.go" ,
" A change.go" ,
" U change.go" ,
" R change.go" ,
" C change.go" ,
}
2021-10-31 12:34:13 -07:00
status . parse ( output , true )
2021-10-31 11:52:12 -07:00
assert . Equal ( t , 3 , status . Modified )
assert . Equal ( t , 1 , status . Unmerged )
assert . Equal ( t , 3 , status . Added )
assert . Equal ( t , 1 , status . Deleted )
assert . True ( t , status . Changed )
2020-10-11 04:32:21 -07:00
}
func TestParseGitStatsStaging ( t * testing . T ) {
2021-10-31 12:34:13 -07:00
status := & GitStatus { }
2020-10-11 04:32:21 -07:00
output := [ ] string {
"## amazing-feat" ,
" M change.go" ,
"DD change.go" ,
" ? change.go" ,
"?? change.go" ,
" A change.go" ,
"DU change.go" ,
"MR change.go" ,
"AC change.go" ,
}
2021-10-31 12:34:13 -07:00
status . parse ( output , false )
2021-10-31 11:52:12 -07:00
assert . Equal ( t , 1 , status . Modified )
assert . Equal ( t , 0 , status . Unmerged )
assert . Equal ( t , 1 , status . Added )
assert . Equal ( t , 2 , status . Deleted )
assert . True ( t , status . Changed )
2020-10-11 04:32:21 -07:00
}
func TestParseGitStatsNoChanges ( t * testing . T ) {
2021-10-31 12:34:13 -07:00
status := & GitStatus { }
2021-10-31 11:52:12 -07:00
expected := & GitStatus { }
2020-10-11 04:32:21 -07:00
output := [ ] string {
"## amazing-feat" ,
}
2021-10-31 12:34:13 -07:00
status . parse ( output , false )
2020-10-11 04:32:21 -07:00
assert . Equal ( t , expected , status )
2021-10-31 11:52:12 -07:00
assert . False ( t , status . Changed )
2020-10-11 04:32:21 -07:00
}
func TestParseGitStatsInvalidLine ( t * testing . T ) {
2021-10-31 12:34:13 -07:00
status := & GitStatus { }
2021-10-31 11:52:12 -07:00
expected := & GitStatus { }
2020-10-11 04:32:21 -07:00
output := [ ] string {
"## amazing-feat" ,
"#" ,
}
2021-10-31 12:34:13 -07:00
status . parse ( output , false )
2020-10-11 04:32:21 -07:00
assert . Equal ( t , expected , status )
2021-10-31 11:52:12 -07:00
assert . False ( t , status . Changed )
2020-10-11 04:32:21 -07:00
}
2020-10-11 10:28:29 -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 {
env := & MockedEnvironment { }
env . On ( "isWsl" , nil ) . Return ( false )
2021-11-11 23:59:03 -08:00
env . On ( "runCommand" , "git" , [ ] string { "-C" , "" , "--no-optional-locks" , "-c" , "core.quotepath=false" ,
"-c" , "color.status=false" , "remote" , "get-url" , "origin" } ) . Return ( tc . Upstream , nil )
2021-10-31 13:10:18 -07:00
env . On ( "getRuntimeGOOS" , nil ) . Return ( "unix" )
2021-11-26 01:37:33 -08:00
var props properties = map [ Property ] interface { } {
GithubIcon : "GH" ,
GitlabIcon : "GL" ,
BitbucketIcon : "BB" ,
AzureDevOpsIcon : "AD" ,
GitIcon : "G" ,
2021-10-31 13:10:18 -07:00
}
g := & git {
2021-11-09 08:13:47 -08:00
env : env ,
props : props ,
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 {
Case string
Expected string
Ahead int
Behind int
Upstream string
} {
{ Case : "Equal with remote" , Expected : " equal" , Upstream : "main" } ,
{ Case : "Ahead" , Expected : " up2" , Ahead : 2 } ,
{ Case : "Behind" , Expected : " down8" , Behind : 8 } ,
{ Case : "Behind and ahead" , Expected : " up7 down8" , Behind : 8 , Ahead : 7 } ,
{ Case : "Gone" , Expected : " gone" } ,
{ Case : "Default (bug)" , Expected : "" , Behind : - 8 , Upstream : "wonky" } ,
}
for _ , tc := range cases {
2021-11-26 01:37:33 -08:00
var props properties = map [ Property ] interface { } {
BranchAheadIcon : "up" ,
BranchBehindIcon : "down" ,
BranchIdenticalIcon : "equal" ,
BranchGoneIcon : "gone" ,
}
2021-02-19 10:31:58 -08:00
g := & git {
2021-11-26 01:37:33 -08:00
props : props ,
2021-11-09 08:13:47 -08:00
Ahead : tc . Ahead ,
Behind : tc . Behind ,
Upstream : tc . Upstream ,
2021-02-19 10:31:58 -08:00
}
assert . Equal ( t , tc . Expected , g . getBranchStatus ( ) , tc . Case )
}
}
2021-05-29 04:37:05 -07:00
2021-10-20 13:20:47 -07:00
func TestShouldIgnoreRootRepository ( t * testing . T ) {
cases := [ ] struct {
Case string
Dir string
Expected bool
} {
{ Case : "inside excluded" , Dir : "/home/bill/repo" } ,
{ Case : "oustide excluded" , Dir : "/home/melinda" } ,
{ Case : "excluded exact match" , Dir : "/home/gates" , Expected : true } ,
{ Case : "excluded inside match" , Dir : "/home/gates/bill" , Expected : true } ,
}
for _ , tc := range cases {
2021-11-26 01:37:33 -08:00
var props properties = map [ Property ] interface { } {
2021-10-20 13:20:47 -07:00
ExcludeFolders : [ ] string {
"/home/bill" ,
"/home/gates.*" ,
} ,
}
env := new ( MockedEnvironment )
env . On ( "homeDir" , nil ) . Return ( "/home/bill" )
env . On ( "getRuntimeGOOS" , nil ) . Return ( windowsPlatform )
git := & git {
2021-11-26 01:37:33 -08:00
props : props ,
env : env ,
2021-10-20 13:20:47 -07:00
}
got := git . shouldIgnoreRootRepository ( tc . Dir )
assert . Equal ( t , tc . Expected , got , tc . Case )
}
}
2021-05-29 04:37:05 -07:00
func TestTruncateBranch ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Branch string
MaxLength interface { }
} {
{ Case : "No limit" , Expected : "all-your-base-are-belong-to-us" , Branch : "all-your-base-are-belong-to-us" } ,
2021-06-23 12:50:05 -07:00
{ Case : "No limit - larger" , Expected : "all-your-base" , Branch : "all-your-base-are-belong-to-us" , MaxLength : 13.0 } ,
{ Case : "No limit - smaller" , Expected : "all-your-base" , Branch : "all-your-base" , MaxLength : 13.0 } ,
2021-05-29 04:37:05 -07:00
{ Case : "Invalid setting" , Expected : "all-your-base" , Branch : "all-your-base" , MaxLength : "burp" } ,
2021-06-23 12:50:05 -07:00
{ Case : "Lower than limit" , Expected : "all-your-base" , Branch : "all-your-base" , MaxLength : 20.0 } ,
2021-05-29 04:37:05 -07:00
}
for _ , tc := range cases {
2021-11-26 01:37:33 -08:00
var props properties = map [ Property ] interface { } {
BranchMaxLength : tc . MaxLength ,
}
2021-05-29 04:37:05 -07:00
g := & git {
2021-11-26 01:37:33 -08:00
props : props ,
2021-05-29 04:37:05 -07:00
}
assert . Equal ( t , tc . Expected , g . truncateBranch ( tc . Branch ) , tc . Case )
2021-11-18 10:58:57 -08:00
}
}
func TestTruncateBranchWithSymbol ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Branch string
MaxLength interface { }
TruncateSymbol interface { }
} {
{ Case : "No limit" , Expected : "all-your-base-are-belong-to-us" , Branch : "all-your-base-are-belong-to-us" , TruncateSymbol : "..." } ,
{ Case : "No limit - larger" , Expected : "all-your-base..." , Branch : "all-your-base-are-belong-to-us" , MaxLength : 13.0 , TruncateSymbol : "..." } ,
{ Case : "No limit - smaller" , Expected : "all-your-base" , Branch : "all-your-base" , MaxLength : 16.0 , TruncateSymbol : "..." } ,
{ Case : "Invalid setting" , Expected : "all-your-base" , Branch : "all-your-base" , MaxLength : "burp" , TruncateSymbol : "..." } ,
{ Case : "Lower than limit" , Expected : "all-your-base" , Branch : "all-your-base" , MaxLength : 20.0 , TruncateSymbol : "..." } ,
}
for _ , tc := range cases {
2021-11-26 01:37:33 -08:00
var props properties = map [ Property ] interface { } {
BranchMaxLength : tc . MaxLength ,
TruncateSymbol : tc . TruncateSymbol ,
}
2021-11-18 10:58:57 -08:00
g := & git {
2021-11-26 01:37:33 -08:00
props : props ,
2021-11-18 10:58:57 -08:00
}
assert . Equal ( t , tc . Expected , g . truncateBranch ( tc . Branch ) , tc . Case )
2021-05-29 04:37:05 -07:00
}
}
2021-10-20 08:01:19 -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 {
Case string
2021-10-20 13:20:47 -07:00
Expected string
IsWSL bool
2021-11-25 05:19:23 -08:00
IsWSL1 bool
2021-10-20 13:20:47 -07:00
GOOS string
CWD string
2021-10-20 08:01:19 -07:00
} {
2021-10-20 13:20:47 -07:00
{ Case : "On Windows" , Expected : "git.exe" , GOOS : windowsPlatform } ,
{ Case : "Non Windows" , Expected : "git" } ,
2021-11-25 05:19:23 -08:00
{ Case : "Iside WSL2, non shared" , IsWSL : true , Expected : "git" } ,
{ Case : "Iside WSL2, shared" , Expected : "git.exe" , IsWSL : true , CWD : "/mnt/bill" } ,
{ 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 {
env := new ( MockedEnvironment )
2021-10-20 13:20:47 -07:00
env . On ( "isWsl" , nil ) . Return ( tc . IsWSL )
env . On ( "getRuntimeGOOS" , nil ) . Return ( tc . GOOS )
env . On ( "getcwd" , nil ) . 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"
}
env . On ( "runCommand" , "uname" , [ ] string { "-r" } ) . Return ( wslUname , nil )
2021-10-20 13:20:47 -07:00
g := & git {
2021-10-20 08:01:19 -07:00
env : env ,
}
2021-10-20 13:20:47 -07:00
assert . Equal ( t , tc . Expected , g . getGitCommand ( ) , 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
2021-11-09 08:13:47 -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-10-31 12:34:13 -07:00
Expected : "main" ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}" ,
Git : & git {
2021-10-31 12:59:35 -07:00
HEAD : "main" ,
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 }}" ,
Git : & git {
2021-10-31 12:34:13 -07:00
HEAD : "main" ,
Working : & GitStatus {
2021-10-31 12:59:35 -07:00
Added : 2 ,
2021-10-31 12:34:13 -07:00
Modified : 3 ,
2021-10-31 12:59:35 -07:00
Changed : true ,
2021-10-31 12:34:13 -07:00
} ,
} ,
} ,
{
2021-10-31 12:59:35 -07:00
Case : "No working area changes" ,
2021-10-31 12:34:13 -07:00
Expected : "main" ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}" ,
Git : & git {
2021-10-31 12:34:13 -07:00
HEAD : "main" ,
Working : & GitStatus {
Changed : false ,
} ,
} ,
} ,
{
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 }}" ,
Git : & git {
2021-10-31 12:34:13 -07:00
HEAD : "main" ,
Working : & GitStatus {
2021-10-31 12:59:35 -07:00
Added : 2 ,
2021-10-31 12:34:13 -07:00
Modified : 3 ,
2021-10-31 12:59:35 -07:00
Changed : true ,
2021-10-31 12:34:13 -07:00
} ,
Staging : & GitStatus {
2021-10-31 12:59:35 -07:00
Added : 5 ,
2021-10-31 12:34:13 -07:00
Modified : 1 ,
2021-10-31 12:59:35 -07:00
Changed : true ,
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
Git : & git {
2021-11-06 12:32:24 -07:00
HEAD : "main" ,
Working : & GitStatus {
Added : 2 ,
Modified : 3 ,
Changed : true ,
} ,
Staging : & GitStatus {
Added : 5 ,
Modified : 1 ,
Changed : true ,
} ,
} ,
} ,
{
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
Git : & git {
2021-11-06 12:32:24 -07:00
HEAD : "main" ,
Working : & GitStatus {
Added : 2 ,
Modified : 3 ,
Changed : true ,
} ,
Staging : & GitStatus {
Added : 5 ,
Modified : 1 ,
Changed : true ,
} ,
StashCount : 3 ,
} ,
} ,
2021-10-31 12:34:13 -07:00
{
2021-10-31 12:59:35 -07:00
Case : "No local changes" ,
2021-10-31 12:34:13 -07:00
Expected : "main" ,
2021-11-09 08:13:47 -08:00
Template : "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}" ,
Git : & git {
2021-10-31 12:59:35 -07:00
HEAD : "main" ,
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 }}" ,
Git : & git {
2021-10-31 13:10:18 -07:00
HEAD : "main" ,
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 {
2021-11-26 01:37:33 -08:00
var props properties = map [ Property ] interface { } {
FetchStatus : true ,
2021-10-31 12:34:13 -07:00
}
2021-11-26 01:37:33 -08:00
tc . Git . props = props
2021-11-09 08:13:47 -08:00
assert . Equal ( t , tc . Expected , tc . Git . templateString ( tc . Template ) , tc . Case )
2021-10-31 12:34:13 -07:00
}
}