1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-16 11:37:01 +02:00
lazygit/pkg/commands/git_test.go

304 lines
6.9 KiB
Go
Raw Normal View History

package commands
import (
2018-08-29 22:55:57 +02:00
"fmt"
"os"
"testing"
"time"
2019-05-12 09:04:32 +02:00
"github.com/go-errors/errors"
2020-10-06 11:50:54 +02:00
gogit "github.com/jesseduffield/go-git/v5"
2021-10-23 00:52:19 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sasha-s/go-deadlock"
"github.com/stretchr/testify/assert"
)
type fileInfoMock struct {
name string
size int64
fileMode os.FileMode
fileModTime time.Time
isDir bool
sys interface{}
}
2018-11-30 02:47:14 +02:00
// Name is a function.
func (f fileInfoMock) Name() string {
return f.name
}
2018-11-30 02:47:14 +02:00
// Size is a function.
func (f fileInfoMock) Size() int64 {
return f.size
}
2018-11-30 02:47:14 +02:00
// Mode is a function.
func (f fileInfoMock) Mode() os.FileMode {
return f.fileMode
}
2018-11-30 02:47:14 +02:00
// ModTime is a function.
func (f fileInfoMock) ModTime() time.Time {
return f.fileModTime
}
2018-11-30 02:47:14 +02:00
// IsDir is a function.
func (f fileInfoMock) IsDir() bool {
return f.isDir
}
2018-11-30 02:47:14 +02:00
// Sys is a function.
func (f fileInfoMock) Sys() interface{} {
return f.sys
}
2018-11-30 02:47:14 +02:00
// TestNavigateToRepoRootDirectory is a function.
func TestNavigateToRepoRootDirectory(t *testing.T) {
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
stat func(string) (os.FileInfo, error)
chdir func(string) error
test func(error)
}
scenarios := []scenario{
2018-08-29 22:55:57 +02:00
{
2018-09-05 22:16:26 +02:00
"Navigate to git repository",
func(string) (os.FileInfo, error) {
return fileInfoMock{isDir: true}, nil
},
func(string) error {
return nil
},
func(err error) {
assert.NoError(t, err)
},
},
{
2022-04-03 22:19:15 +02:00
"An error occurred when getting path information",
func(string) (os.FileInfo, error) {
return nil, fmt.Errorf("An error occurred")
2018-08-29 22:55:57 +02:00
},
func(string) error {
return nil
2018-08-29 22:55:57 +02:00
},
func(err error) {
assert.Error(t, err)
assert.EqualError(t, err, "An error occurred")
2018-08-29 22:55:57 +02:00
},
},
{
2018-09-05 22:16:26 +02:00
"An error occurred when trying to move one path backward",
func(string) (os.FileInfo, error) {
return nil, os.ErrNotExist
2018-08-29 22:55:57 +02:00
},
func(string) error {
return fmt.Errorf("An error occurred")
2018-08-29 22:55:57 +02:00
},
func(err error) {
assert.Error(t, err)
assert.EqualError(t, err, "An error occurred")
2018-08-29 22:55:57 +02:00
},
},
}
for _, s := range scenarios {
2022-01-08 06:46:35 +02:00
s := s
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
s.test(navigateToRepoRootDirectory(s.stat, s.chdir))
})
}
}
2020-09-27 07:36:04 +02:00
// TestSetupRepository is a function.
func TestSetupRepository(t *testing.T) {
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
2022-04-07 18:45:08 +02:00
openGitRepository func(string, *gogit.PlainOpenOptions) (*gogit.Repository, error)
2020-10-04 02:00:48 +02:00
errorStr string
2022-04-07 18:45:08 +02:00
options gogit.PlainOpenOptions
2020-09-27 07:36:04 +02:00
test func(*gogit.Repository, error)
}
2018-08-29 22:55:57 +02:00
scenarios := []scenario{
{
2018-09-05 22:16:26 +02:00
"A gitconfig parsing error occurred",
2022-04-07 18:45:08 +02:00
func(string, *gogit.PlainOpenOptions) (*gogit.Repository, error) {
return nil, fmt.Errorf(`unquoted '\' must be followed by new line`)
},
2020-10-04 02:00:48 +02:00
"error translated",
2022-04-07 18:45:08 +02:00
gogit.PlainOpenOptions{},
2020-09-27 07:36:04 +02:00
func(r *gogit.Repository, err error) {
assert.Error(t, err)
assert.EqualError(t, err, "error translated")
},
},
{
2018-09-05 22:16:26 +02:00
"A gogit error occurred",
2022-04-07 18:45:08 +02:00
func(string, *gogit.PlainOpenOptions) (*gogit.Repository, error) {
return nil, fmt.Errorf("Error from inside gogit")
},
2020-10-04 02:00:48 +02:00
"",
2022-04-07 18:45:08 +02:00
gogit.PlainOpenOptions{},
2020-09-27 07:36:04 +02:00
func(r *gogit.Repository, err error) {
assert.Error(t, err)
assert.EqualError(t, err, "Error from inside gogit")
},
},
{
2018-09-05 22:16:26 +02:00
"Setup done properly",
2022-04-07 18:45:08 +02:00
func(string, *gogit.PlainOpenOptions) (*gogit.Repository, error) {
assert.NoError(t, os.RemoveAll("/tmp/lazygit-test"))
r, err := gogit.PlainInit("/tmp/lazygit-test", false)
assert.NoError(t, err)
return r, nil
},
2020-10-04 02:00:48 +02:00
"",
2022-04-07 18:45:08 +02:00
gogit.PlainOpenOptions{},
2020-09-27 07:36:04 +02:00
func(r *gogit.Repository, err error) {
assert.NoError(t, err)
2018-09-05 22:16:26 +02:00
assert.NotNil(t, r)
},
},
}
for _, s := range scenarios {
2022-01-08 06:46:35 +02:00
s := s
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
2022-04-07 18:45:08 +02:00
s.test(setupRepository(s.openGitRepository, s.options, s.errorStr))
2018-09-05 22:16:26 +02:00
})
}
}
2018-11-30 02:47:14 +02:00
// TestNewGitCommand is a function.
2018-09-02 17:18:33 +02:00
func TestNewGitCommand(t *testing.T) {
actual, err := os.Getwd()
assert.NoError(t, err)
defer func() {
assert.NoError(t, os.Chdir(actual))
}()
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
setup func()
test func(*GitCommand, error)
2018-09-02 17:18:33 +02:00
}
scenarios := []scenario{
{
2018-09-05 22:16:26 +02:00
"An error occurred, folder doesn't contains a git repository",
2018-09-02 17:18:33 +02:00
func() {
assert.NoError(t, os.Chdir("/tmp"))
},
func(gitCmd *GitCommand, err error) {
assert.Error(t, err)
2021-03-30 13:17:42 +02:00
assert.Regexp(t, `Must open lazygit in a git repository`, err.Error())
2018-09-02 17:18:33 +02:00
},
},
{
2018-09-05 22:16:26 +02:00
"New GitCommand object created",
2018-09-02 17:18:33 +02:00
func() {
assert.NoError(t, os.RemoveAll("/tmp/lazygit-test"))
_, err := gogit.PlainInit("/tmp/lazygit-test", false)
assert.NoError(t, err)
assert.NoError(t, os.Chdir("/tmp/lazygit-test"))
},
func(gitCmd *GitCommand, err error) {
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
2022-01-08 06:46:35 +02:00
s := s
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
s.setup()
s.test(
NewGitCommand(utils.NewDummyCommon(),
oscommands.NewDummyOSCommand(),
git_config.NewFakeGitConfig(nil),
&deadlock.Mutex{},
))
2018-09-05 22:16:26 +02:00
})
2018-09-02 17:18:33 +02:00
}
}
2019-05-12 09:04:32 +02:00
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 {
2022-01-08 06:46:35 +02:00
s := s
2019-05-12 09:04:32 +02:00
t.Run(s.testName, func(t *testing.T) {
s.test(findDotGitDir(s.stat, s.readFile))
})
}
}