summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/commands/git_test.go')
-rw-r--r--pkg/commands/git_test.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index 0d59c30b7..be9f8c883 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -8,6 +8,7 @@ import (
"testing"
"time"
+ "github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/test"
"github.com/stretchr/testify/assert"
@@ -2122,3 +2123,78 @@ func TestGitCommandCreateFixupCommit(t *testing.T) {
})
}
}
+
+func TestFindDotGitDir(t *testing.T) {
+ type scenario struct {
+ testName string
+ stat func(string) (os.FileInfo, error)
+ readFile func(filename string) ([]byte, error)
+ test func(string, error)
+ }
+
+ scenarios := []scenario{
+ {
+ ".git is a directory",
+ func(dotGit string) (os.FileInfo, error) {
+ assert.Equal(t, ".git", dotGit)
+ return os.Stat("testdata/a_dir")
+ },
+ func(dotGit string) ([]byte, error) {
+ assert.Fail(t, "readFile should not be called if .git is a directory")
+ return nil, nil
+ },
+ func(gitDir string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, ".git", gitDir)
+ },
+ },
+ {
+ ".git is a file",
+ func(dotGit string) (os.FileInfo, error) {
+ assert.Equal(t, ".git", dotGit)
+ return os.Stat("testdata/a_file")
+ },
+ func(dotGit string) ([]byte, error) {
+ assert.Equal(t, ".git", dotGit)
+ return []byte("gitdir: blah\n"), nil
+ },
+ func(gitDir string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, "blah", gitDir)
+ },
+ },
+ {
+ "os.Stat returns an error",
+ func(dotGit string) (os.FileInfo, error) {
+ assert.Equal(t, ".git", dotGit)
+ return nil, errors.New("error")
+ },
+ func(dotGit string) ([]byte, error) {
+ assert.Fail(t, "readFile should not be called os.Stat returns an error")
+ return nil, nil
+ },
+ func(gitDir string, err error) {
+ assert.Error(t, err)
+ },
+ },
+ {
+ "readFile returns an error",
+ func(dotGit string) (os.FileInfo, error) {
+ assert.Equal(t, ".git", dotGit)
+ return os.Stat("testdata/a_file")
+ },
+ func(dotGit string) ([]byte, error) {
+ return nil, errors.New("error")
+ },
+ func(gitDir string, err error) {
+ assert.Error(t, err)
+ },
+ },
+ }
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ s.test(findDotGitDir(s.stat, s.readFile))
+ })
+ }
+}