oh-my-posh/src/segment_git_test.go

1078 lines
27 KiB
Go
Raw Normal View History

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"
)
const (
changesColor = "#BD8BDE"
)
2019-03-13 04:14:30 -07:00
func TestEnabledGitNotFound(t *testing.T) {
env := new(MockedEnvironment)
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)
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)
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())
assert.Equal(t, fileInfo.path, g.repo.gitWorkingFolder)
}
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)
fileInfo := &fileInfo{
path: "/dir/hello",
parentFolder: "/dir",
isDir: false,
}
env.On("hasParentFilePath", ".git").Return(fileInfo, nil)
env.On("getFileContent", "/dir/hello").Return("gitdir: /dir/hello/burp/burp")
2019-03-13 04:14:30 -07:00
g := &git{
env: env,
}
assert.True(t, g.enabled())
assert.Equal(t, "/dir/hello/burp/burp", g.repo.gitWorkingFolder)
2019-03-13 04:14:30 -07:00
}
func TestGetGitOutputForCommand(t *testing.T) {
args := []string{"--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)
env.On("isWsl", nil).Return(false)
env.On("runCommand", "git", append(args, commandArgs...)).Return(want, nil)
env.On("getRuntimeGOOS", nil).Return("unix")
2019-03-13 04:14:30 -07:00
g := &git{
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
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
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)
env.On("isWsl", nil).Return(false)
env.On("hasFolder", "/rebase-merge").Return(context.rebaseMerge)
env.On("hasFolder", "/rebase-apply").Return(context.rebaseApply)
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)
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)
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))
env.On("getFileContent", "/sequencer/todo").Return(context.sequencerTodo)
env.On("getFileContent", "/HEAD").Return(context.branchName)
env.On("hasFilesInDir", "", "CHERRY_PICK_HEAD").Return(context.cherryPick)
env.On("hasFilesInDir", "", "REVERT_HEAD").Return(context.revert)
2021-01-07 10:29:34 -08:00
env.On("hasFilesInDir", "", "MERGE_MSG").Return(context.merge)
env.On("hasFilesInDir", "", "MERGE_HEAD").Return(context.merge)
env.On("hasFilesInDir", "", "sequencer/todo").Return(context.sequencer)
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)
env.mockGitCommand(context.branchName, "branch", "--show-current")
env.mockGitCommand(context.status, "status", "-unormal", "--short", "--branch")
env.On("getRuntimeGOOS", nil).Return("unix")
2020-10-13 01:00:17 -07:00
g := &git{
env: env,
repo: &Repo{
gitWorkingFolder: "",
Working: &GitStatus{},
Staging: &GitStatus{},
2020-10-13 01:00:17 -07:00
},
}
return g
2020-10-07 04:32:42 -07:00
}
func (m *MockedEnvironment) mockGitCommand(returnValue string, args ...string) {
args = append([]string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
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)
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)
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)
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)
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)
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)
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)
got := g.getGitHEADContext("")
2019-03-13 04:14:30 -07:00
assert.Equal(t, want, got)
}
func TestGetGitHEADContextRevertOnBranch(t *testing.T) {
want := "\uf0e2 012345 onto \ue0a0main"
context := &detachedContext{
currentCommit: "whatever",
branchName: "main",
revert: true,
revertSHA: "01234567",
}
g := setupHEADContextEnv(context)
got := g.getGitHEADContext("main")
assert.Equal(t, want, got)
}
func TestGetGitHEADContextRevertOnTag(t *testing.T) {
want := "\uf0e2 012345 onto \uf412v3.4.6"
context := &detachedContext{
currentCommit: "whatever",
tagName: "v3.4.6",
revert: true,
revertSHA: "01234567",
}
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,
sequencerTodo: "pick pickme message\npick notme message",
}
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,
sequencerTodo: "pick pickme message\npick notme message",
}
g := setupHEADContextEnv(context)
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}
func TestGetGitHEADContextSequencerRevertOnBranch(t *testing.T) {
want := "\uf0e2 012345 onto \ue0a0main"
context := &detachedContext{
currentCommit: "whatever",
branchName: "main",
sequencer: true,
sequencerTodo: "revert 01234567 message\nrevert notme message",
}
g := setupHEADContextEnv(context)
got := g.getGitHEADContext("main")
assert.Equal(t, want, got)
}
func TestGetGitHEADContextSequencerRevertOnTag(t *testing.T) {
want := "\uf0e2 012345 onto \uf412v3.4.6"
context := &detachedContext{
currentCommit: "whatever",
tagName: "v3.4.6",
sequencer: true,
sequencerTodo: "revert 01234567 message\nrevert notme message",
}
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) {
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
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getFileContent", "/logs/refs/stash").Return(tc.StashContent)
g := &git{
repo: &Repo{
gitWorkingFolder: "",
},
env: env,
}
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
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) {
expected := " x1"
status := &GitStatus{
Unmerged: 1,
2020-10-11 04:32:21 -07:00
}
assert.Equal(t, expected, status.String())
2020-10-11 04:32:21 -07:00
}
func TestGitStatusUnmergedModified(t *testing.T) {
expected := " ~3 x1"
status := &GitStatus{
Unmerged: 1,
Modified: 3,
2020-10-11 04:32:21 -07:00
}
assert.Equal(t, expected, status.String())
2020-10-11 04:32:21 -07:00
}
func TestGitStatusEmpty(t *testing.T) {
expected := ""
status := &GitStatus{}
assert.Equal(t, expected, status.String())
2020-10-11 04:32:21 -07:00
}
func TestParseGitStatsWorking(t *testing.T) {
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",
}
status.parse(output, true)
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) {
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",
}
status.parse(output, false)
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) {
status := &GitStatus{}
expected := &GitStatus{}
2020-10-11 04:32:21 -07:00
output := []string{
"## amazing-feat",
}
status.parse(output, false)
2020-10-11 04:32:21 -07:00
assert.Equal(t, expected, status)
assert.False(t, status.Changed)
2020-10-11 04:32:21 -07:00
}
func TestParseGitStatsInvalidLine(t *testing.T) {
status := &GitStatus{}
expected := &GitStatus{}
2020-10-11 04:32:21 -07:00
output := []string{
"## amazing-feat",
"#",
}
status.parse(output, false)
2020-10-11 04:32:21 -07:00
assert.Equal(t, expected, status)
assert.False(t, status.Changed)
2020-10-11 04:32:21 -07:00
}
func bootstrapUpstreamTest(upstream string) *git {
env := &MockedEnvironment{}
env.On("isWsl", nil).Return(false)
env.On("runCommand", "git", []string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false", "remote", "get-url", "origin"}).Return(upstream, nil)
env.On("getRuntimeGOOS", nil).Return("unix")
props := &properties{
values: map[Property]interface{}{
GithubIcon: "GH",
GitlabIcon: "GL",
BitbucketIcon: "BB",
AzureDevOpsIcon: "AD",
GitIcon: "G",
},
}
g := &git{
env: env,
repo: &Repo{
Upstream: "origin/main",
},
props: props,
}
return g
}
func TestGetUpstreamSymbolGitHub(t *testing.T) {
g := bootstrapUpstreamTest("github.com/test")
upstreamIcon := g.getUpstreamSymbol()
assert.Equal(t, "GH", upstreamIcon)
}
func TestGetUpstreamSymbolGitLab(t *testing.T) {
g := bootstrapUpstreamTest("gitlab.com/test")
upstreamIcon := g.getUpstreamSymbol()
assert.Equal(t, "GL", upstreamIcon)
}
func TestGetUpstreamSymbolBitBucket(t *testing.T) {
g := bootstrapUpstreamTest("bitbucket.org/test")
upstreamIcon := g.getUpstreamSymbol()
assert.Equal(t, "BB", upstreamIcon)
}
func TestGetUpstreamSymbolAzureDevOps(t *testing.T) {
g := bootstrapUpstreamTest("dev.azure.com/test")
upstreamIcon := g.getUpstreamSymbol()
assert.Equal(t, "AD", upstreamIcon)
g = bootstrapUpstreamTest("test.visualstudio.com")
upstreamIcon = g.getUpstreamSymbol()
assert.Equal(t, "AD", upstreamIcon)
}
func TestGetUpstreamSymbolGit(t *testing.T) {
g := bootstrapUpstreamTest("gitstash.com/test")
upstreamIcon := g.getUpstreamSymbol()
assert.Equal(t, "G", upstreamIcon)
}
2020-10-18 10:27:20 -07:00
func TestGetStatusColorLocalChangesStaging(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{
Changed: true,
2020-10-18 10:27:20 -07:00
},
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
LocalChangesColor: expected,
},
},
}
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
}
func TestGetStatusColorLocalChangesWorking(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{},
Working: &GitStatus{
Changed: true,
2020-10-18 10:27:20 -07:00
},
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
LocalChangesColor: expected,
},
},
}
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
}
func TestGetStatusColorAheadAndBehind(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{},
Working: &GitStatus{},
Ahead: 1,
Behind: 3,
2020-10-18 10:27:20 -07:00
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
AheadAndBehindColor: expected,
},
},
}
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
}
func TestGetStatusColorAhead(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{},
Working: &GitStatus{},
Ahead: 1,
Behind: 0,
2020-10-18 10:27:20 -07:00
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
AheadColor: expected,
},
},
}
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
}
func TestGetStatusColorBehind(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{},
Working: &GitStatus{},
Ahead: 0,
Behind: 5,
2020-10-18 10:27:20 -07:00
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
BehindColor: expected,
},
},
}
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
}
func TestGetStatusColorDefault(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{},
Working: &GitStatus{},
Ahead: 0,
Behind: 0,
2020-10-18 10:27:20 -07:00
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
BehindColor: changesColor,
2020-10-18 10:27:20 -07:00
},
},
}
assert.Equal(t, expected, g.getStatusColor(expected))
}
func TestSetStatusColorForeground(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{
Changed: true,
2020-10-18 10:27:20 -07:00
},
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
LocalChangesColor: changesColor,
2020-10-18 10:27:20 -07:00
ColorBackground: false,
},
foreground: "#ffffff",
background: "#111111",
},
}
g.SetStatusColor()
assert.Equal(t, expected, g.props.foreground)
}
func TestSetStatusColorBackground(t *testing.T) {
expected := changesColor
repo := &Repo{
Staging: &GitStatus{
Changed: true,
2020-10-18 10:27:20 -07:00
},
}
g := &git{
repo: repo,
props: &properties{
values: map[Property]interface{}{
LocalChangesColor: changesColor,
2020-10-18 10:27:20 -07:00
ColorBackground: true,
},
foreground: "#ffffff",
background: "#111111",
},
}
g.SetStatusColor()
assert.Equal(t, expected, g.props.background)
}
func TestStatusColorsWithoutDisplayStatus(t *testing.T) {
expected := changesColor
context := &detachedContext{
status: "## main...origin/main [ahead 33]\n M myfile",
}
g := setupHEADContextEnv(context)
g.props = &properties{
values: map[Property]interface{}{
DisplayStatus: false,
StatusColorsEnabled: true,
LocalChangesColor: expected,
},
}
g.string()
assert.Equal(t, expected, g.props.background)
}
func TestGetStatusDetailStringDefault(t *testing.T) {
expected := "icon +1"
status := &GitStatus{
Changed: true,
Added: 1,
}
g := &git{
props: &properties{
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}
func TestGetStatusDetailStringDefaultColorOverride(t *testing.T) {
expected := "<#123456>icon +1</>"
status := &GitStatus{
Changed: true,
Added: 1,
}
g := &git{
props: &properties{
values: map[Property]interface{}{
WorkingColor: "#123456",
},
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}
func TestGetStatusDetailStringDefaultColorOverrideAndIconColorOverride(t *testing.T) {
expected := "<#789123>work</><#123456> +1</>"
status := &GitStatus{
Changed: true,
Added: 1,
}
g := &git{
props: &properties{
values: map[Property]interface{}{
WorkingColor: "#123456",
LocalWorkingIcon: "<#789123>work</>",
},
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}
func TestGetStatusDetailStringDefaultColorOverrideNoIconColorOverride(t *testing.T) {
expected := "<#123456>work +1</>"
status := &GitStatus{
Changed: true,
Added: 1,
}
g := &git{
props: &properties{
values: map[Property]interface{}{
WorkingColor: "#123456",
LocalWorkingIcon: "work",
},
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}
func TestGetStatusDetailStringNoStatus(t *testing.T) {
expected := "icon"
status := &GitStatus{
Changed: true,
Added: 1,
}
g := &git{
props: &properties{
values: map[Property]interface{}{
DisplayStatusDetail: false,
},
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}
func TestGetStatusDetailStringNoStatusColorOverride(t *testing.T) {
expected := "<#123456>icon</>"
status := &GitStatus{
Changed: true,
Added: 1,
}
g := &git{
props: &properties{
values: map[Property]interface{}{
DisplayStatusDetail: false,
WorkingColor: "#123456",
},
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}
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 {
g := &git{
props: &properties{
values: map[Property]interface{}{
BranchAheadIcon: "up",
BranchBehindIcon: "down",
BranchIdenticalIcon: "equal",
BranchGoneIcon: "gone",
},
},
repo: &Repo{
Ahead: tc.Ahead,
Behind: tc.Behind,
Upstream: tc.Upstream,
},
}
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 {
props := map[Property]interface{}{
ExcludeFolders: []string{
"/home/bill",
"/home/gates.*",
},
}
env := new(MockedEnvironment)
env.On("homeDir", nil).Return("/home/bill")
env.On("getRuntimeGOOS", nil).Return(windowsPlatform)
git := &git{
props: &properties{
values: props,
},
env: env,
}
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"},
{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"},
{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 {
g := &git{
props: &properties{
values: map[Property]interface{}{
BranchMaxLength: tc.MaxLength,
},
},
}
assert.Equal(t, tc.Expected, g.truncateBranch(tc.Branch), tc.Case)
}
}
2021-10-20 13:20:47 -07:00
func TestGetGitCommand(t *testing.T) {
cases := []struct {
Case string
2021-10-20 13:20:47 -07:00
Expected string
IsWSL bool
GOOS string
CWD string
}{
2021-10-20 13:20:47 -07:00
{Case: "On Windows", Expected: "git.exe", GOOS: windowsPlatform},
{Case: "Non Windows", Expected: "git"},
{Case: "Iside WSL, non shared", IsWSL: true, Expected: "git"},
{Case: "Iside WSL, shared", Expected: "git.exe", IsWSL: true, CWD: "/mnt/bill"},
}
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)
g := &git{
env: env,
}
2021-10-20 13:20:47 -07:00
assert.Equal(t, tc.Expected, g.getGitCommand(), tc.Case)
}
}
func TestGitTemplate(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
Repo *Repo
}{
{
Case: "Only HEAD name",
Expected: "main",
Template: "{{ .HEAD }}",
Repo: &Repo{
HEAD: "main",
Behind: 2,
},
},
{
Case: "Working area changes",
Expected: "main \uF044 +2 ~3",
Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
Repo: &Repo{
HEAD: "main",
Working: &GitStatus{
Added: 2,
Modified: 3,
Changed: true,
},
},
},
{
Case: "No working area changes",
Expected: "main",
Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
Repo: &Repo{
HEAD: "main",
Working: &GitStatus{
Changed: false,
},
},
},
{
Case: "Working and staging area changes",
Expected: "main \uF046 +5 ~1 \uF044 +2 ~3",
Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
Repo: &Repo{
HEAD: "main",
Working: &GitStatus{
Added: 2,
Modified: 3,
Changed: true,
},
Staging: &GitStatus{
Added: 5,
Modified: 1,
Changed: true,
},
},
},
{
Case: "No local changes",
Expected: "main",
Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
Repo: &Repo{
HEAD: "main",
Staging: &GitStatus{},
Working: &GitStatus{},
},
},
}
for _, tc := range cases {
g := &git{
props: &properties{
values: map[Property]interface{}{
SegmentTemplate: tc.Template,
},
},
repo: tc.Repo,
}
assert.Equal(t, tc.Expected, g.string(), tc.Case)
}
}