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

2178 lines
51 KiB
Go
Raw Normal View History

package commands
import (
2018-08-29 22:55:57 +02:00
"fmt"
"io/ioutil"
"os"
2018-08-27 22:41:46 +02:00
"os/exec"
"testing"
"time"
2019-05-12 09:04:32 +02:00
"github.com/go-errors/errors"
2018-08-29 22:55:57 +02:00
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/test"
"github.com/stretchr/testify/assert"
2018-08-29 22:55:57 +02:00
gogit "gopkg.in/src-d/go-git.v4"
)
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
// TestVerifyInGitRepo is a function.
func TestVerifyInGitRepo(t *testing.T) {
2018-08-29 22:55:57 +02:00
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
2019-11-21 12:45:18 +02:00
runCmd func(string, ...interface{}) error
2018-09-05 22:16:26 +02:00
test func(error)
2018-08-29 22:55:57 +02:00
}
scenarios := []scenario{
{
2018-09-05 22:16:26 +02:00
"Valid git repository",
2019-11-21 12:45:18 +02:00
func(string, ...interface{}) error {
2018-09-04 06:16:19 +02:00
return nil
2018-08-29 22:55:57 +02:00
},
func(err error) {
assert.NoError(t, err)
},
},
{
2018-09-05 22:16:26 +02:00
"Not a valid git repository",
2019-11-21 12:45:18 +02:00
func(string, ...interface{}) error {
2018-09-04 06:16:19 +02:00
return fmt.Errorf("fatal: Not a git repository (or any of the parent directories): .git")
2018-08-29 22:55:57 +02:00
},
func(err error) {
assert.Error(t, err)
2018-10-31 18:36:20 +02:00
assert.Regexp(t, `fatal: .ot a git repository \(or any of the parent directories\s?\/?\): \.git`, err.Error())
2018-08-29 22:55:57 +02:00
},
},
}
for _, s := range scenarios {
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
s.test(verifyInGitRepo(s.runCmd))
})
}
}
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)
},
},
{
2018-09-05 22:16:26 +02:00
"An error occurred when getting path informations",
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 {
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
s.test(navigateToRepoRootDirectory(s.stat, s.chdir))
})
}
}
2018-11-30 02:47:14 +02:00
// TestSetupRepositoryAndWorktree is a function.
func TestSetupRepositoryAndWorktree(t *testing.T) {
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
openGitRepository func(string) (*gogit.Repository, error)
sLocalize func(string) string
test func(*gogit.Repository, *gogit.Worktree, error)
}
2018-08-29 22:55:57 +02:00
scenarios := []scenario{
{
2018-09-05 22:16:26 +02:00
"A gitconfig parsing error occurred",
func(string) (*gogit.Repository, error) {
return nil, fmt.Errorf(`unquoted '\' must be followed by new line`)
},
func(string) string {
return "error translated"
},
func(r *gogit.Repository, w *gogit.Worktree, err error) {
assert.Error(t, err)
assert.EqualError(t, err, "error translated")
},
},
{
2018-09-05 22:16:26 +02:00
"A gogit error occurred",
func(string) (*gogit.Repository, error) {
return nil, fmt.Errorf("Error from inside gogit")
},
func(string) string { return "" },
func(r *gogit.Repository, w *gogit.Worktree, err error) {
assert.Error(t, err)
assert.EqualError(t, err, "Error from inside gogit")
},
},
{
2018-09-05 22:16:26 +02:00
"An error occurred cause git repository is a bare repository",
func(string) (*gogit.Repository, error) {
return &gogit.Repository{}, nil
},
func(string) string { return "" },
func(r *gogit.Repository, w *gogit.Worktree, err error) {
assert.Error(t, err)
assert.Equal(t, gogit.ErrIsBareRepository, err)
},
},
{
2018-09-05 22:16:26 +02:00
"Setup done properly",
func(string) (*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
},
func(string) string { return "" },
func(r *gogit.Repository, w *gogit.Worktree, err error) {
assert.NoError(t, err)
2018-09-05 22:16:26 +02:00
assert.NotNil(t, w)
assert.NotNil(t, r)
},
},
}
for _, s := range scenarios {
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
s.test(setupRepositoryAndWorktree(s.openGitRepository, s.sLocalize))
})
}
}
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)
2018-10-31 18:55:02 +02:00
assert.Regexp(t, `fatal: .ot a git repository ((\(or any of the parent directories\): \.git)|(\(or any parent up to mount point \/\)))`, 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 {
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
s.setup()
s.test(NewGitCommand(NewDummyLog(), NewDummyOSCommand(), i18n.NewLocalizer(NewDummyLog()), NewDummyAppConfig()))
2018-09-05 22:16:26 +02:00
})
2018-09-02 17:18:33 +02:00
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandGetStashEntries is a function.
2018-08-27 22:41:46 +02:00
func TestGitCommandGetStashEntries(t *testing.T) {
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
command func(string, ...string) *exec.Cmd
2018-09-19 10:31:54 +02:00
test func([]*StashEntry)
}
2018-08-27 22:41:46 +02:00
scenarios := []scenario{
{
2018-09-05 22:16:26 +02:00
"No stash entries found",
2018-08-27 22:41:46 +02:00
func(string, ...string) *exec.Cmd {
return exec.Command("echo")
},
2018-09-19 10:31:54 +02:00
func(entries []*StashEntry) {
2018-08-27 22:41:46 +02:00
assert.Len(t, entries, 0)
},
},
{
2018-09-05 22:16:26 +02:00
"Several stash entries found",
2018-08-27 22:41:46 +02:00
func(string, ...string) *exec.Cmd {
return exec.Command("echo", "WIP on add-pkg-commands-test: 55c6af2 increase parallel build\nWIP on master: bb86a3f update github template")
},
2018-09-19 10:31:54 +02:00
func(entries []*StashEntry) {
expected := []*StashEntry{
2018-08-27 22:41:46 +02:00
{
0,
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
},
{
1,
"WIP on master: bb86a3f update github template",
"WIP on master: bb86a3f update github template",
},
}
assert.Len(t, entries, 2)
assert.EqualValues(t, expected, entries)
},
},
}
for _, s := range scenarios {
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-09-05 22:16:26 +02:00
gitCmd.OSCommand.command = s.command
2018-08-27 22:41:46 +02:00
2018-09-05 22:16:26 +02:00
s.test(gitCmd.GetStashEntries())
})
2018-08-27 22:41:46 +02:00
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandGetStatusFiles is a function.
2018-09-06 23:05:35 +02:00
func TestGitCommandGetStatusFiles(t *testing.T) {
2018-08-27 23:20:44 +02:00
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
command func(string, ...string) *exec.Cmd
2018-09-19 10:31:54 +02:00
test func([]*File)
2018-08-27 23:20:44 +02:00
}
scenarios := []scenario{
{
2018-09-05 22:16:26 +02:00
"No files found",
2018-08-27 23:20:44 +02:00
func(cmd string, args ...string) *exec.Cmd {
return exec.Command("echo")
},
2018-09-19 10:31:54 +02:00
func(files []*File) {
2018-08-27 23:20:44 +02:00
assert.Len(t, files, 0)
},
},
{
2018-09-05 22:16:26 +02:00
"Several files found",
2018-08-27 23:20:44 +02:00
func(cmd string, args ...string) *exec.Cmd {
return exec.Command(
"echo",
"MM file1.txt\nA file3.txt\nAM file2.txt\n?? file4.txt\nUU file5.txt",
2018-08-27 23:20:44 +02:00
)
},
2018-09-19 10:31:54 +02:00
func(files []*File) {
assert.Len(t, files, 5)
2018-08-27 23:20:44 +02:00
2018-09-19 10:31:54 +02:00
expected := []*File{
2018-08-27 23:20:44 +02:00
{
Name: "file1.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: true,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
DisplayString: "MM file1.txt",
Type: "other",
ShortStatus: "MM",
2018-08-27 23:20:44 +02:00
},
{
Name: "file3.txt",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
DisplayString: "A file3.txt",
Type: "other",
ShortStatus: "A ",
2018-08-27 23:20:44 +02:00
},
{
Name: "file2.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
DisplayString: "AM file2.txt",
Type: "other",
ShortStatus: "AM",
2018-08-27 23:20:44 +02:00
},
{
Name: "file4.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
DisplayString: "?? file4.txt",
Type: "other",
ShortStatus: "??",
},
{
Name: "file5.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
Deleted: false,
HasMergeConflicts: true,
HasInlineMergeConflicts: true,
DisplayString: "UU file5.txt",
Type: "other",
ShortStatus: "UU",
2018-08-27 23:20:44 +02:00
},
}
assert.EqualValues(t, expected, files)
},
},
}
for _, s := range scenarios {
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-09-05 22:16:26 +02:00
gitCmd.OSCommand.command = s.command
2018-08-27 23:20:44 +02:00
2018-09-05 22:16:26 +02:00
s.test(gitCmd.GetStatusFiles())
})
2018-08-27 23:20:44 +02:00
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandStashDo is a function.
func TestGitCommandStashDo(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"stash", "drop", "stash@{1}"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.StashDo(1, "drop"))
}
2018-11-30 02:47:14 +02:00
// TestGitCommandStashSave is a function.
func TestGitCommandStashSave(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"stash", "save", "A stash message"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.StashSave("A stash message"))
}
2018-11-30 02:47:14 +02:00
// TestGitCommandCommitAmend is a function.
func TestGitCommandCommitAmend(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"commit", "--amend", "--allow-empty"}, args)
return exec.Command("echo")
}
_, err := gitCmd.PrepareCommitAmendSubProcess().CombinedOutput()
assert.NoError(t, err)
}
2018-11-30 02:47:14 +02:00
// TestGitCommandMergeStatusFiles is a function.
2018-08-28 20:55:14 +02:00
func TestGitCommandMergeStatusFiles(t *testing.T) {
type scenario struct {
2018-09-05 22:16:26 +02:00
testName string
2018-09-19 10:31:54 +02:00
oldFiles []*File
newFiles []*File
test func([]*File)
2018-08-28 20:55:14 +02:00
}
scenarios := []scenario{
{
2018-09-05 22:16:26 +02:00
"Old file and new file are the same",
2018-09-19 10:31:54 +02:00
[]*File{},
[]*File{
2018-08-28 20:55:14 +02:00
{
Name: "new_file.txt",
},
},
2018-09-19 10:31:54 +02:00
func(files []*File) {
expected := []*File{
2018-08-28 20:55:14 +02:00
{
Name: "new_file.txt",
},
}
assert.Len(t, files, 1)
assert.EqualValues(t, expected, files)
},
},
{
2018-09-05 22:16:26 +02:00
"Several files to merge, with some identical",
2018-09-19 10:31:54 +02:00
[]*File{
2018-08-28 20:55:14 +02:00
{
Name: "new_file1.txt",
},
{
Name: "new_file2.txt",
},
{
Name: "new_file3.txt",
},
},
2018-09-19 10:31:54 +02:00
[]*File{
2018-08-28 20:55:14 +02:00
{
Name: "new_file4.txt",
},
{
Name: "new_file5.txt",
},
{
Name: "new_file1.txt",
},
},
2018-09-19 10:31:54 +02:00
func(files []*File) {
expected := []*File{
2018-08-28 20:55:14 +02:00
{
Name: "new_file1.txt",
},
{
Name: "new_file4.txt",
},
{
Name: "new_file5.txt",
},
}
assert.Len(t, files, 3)
assert.EqualValues(t, expected, files)
},
},
}
for _, s := range scenarios {
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-08-28 20:55:14 +02:00
2018-09-05 22:16:26 +02:00
s.test(gitCmd.MergeStatusFiles(s.oldFiles, s.newFiles))
})
2018-08-28 20:55:14 +02:00
}
}
2018-12-07 09:52:31 +02:00
// TestGitCommandGetCommitDifferences is a function.
func TestGitCommandGetCommitDifferences(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(string, string)
}
scenarios := []scenario{
{
"Can't retrieve pushable count",
func(string, ...string) *exec.Cmd {
return exec.Command("test")
},
func(pushableCount string, pullableCount string) {
assert.EqualValues(t, "?", pushableCount)
assert.EqualValues(t, "?", pullableCount)
},
},
{
"Can't retrieve pullable count",
func(cmd string, args ...string) *exec.Cmd {
if args[1] == "HEAD..@{u}" {
return exec.Command("test")
}
return exec.Command("echo")
},
func(pushableCount string, pullableCount string) {
assert.EqualValues(t, "?", pushableCount)
assert.EqualValues(t, "?", pullableCount)
},
},
{
"Retrieve pullable and pushable count",
func(cmd string, args ...string) *exec.Cmd {
if args[1] == "HEAD..@{u}" {
return exec.Command("echo", "10")
}
return exec.Command("echo", "11")
},
func(pushableCount string, pullableCount string) {
assert.EqualValues(t, "11", pushableCount)
assert.EqualValues(t, "10", pullableCount)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = s.command
2018-12-07 09:52:31 +02:00
s.test(gitCmd.GetCommitDifferences("HEAD", "@{u}"))
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandRenameCommit is a function.
func TestGitCommandRenameCommit(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"commit", "--allow-empty", "--amend", "-m", "test"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.RenameCommit("test"))
}
2018-11-30 02:47:14 +02:00
// TestGitCommandResetToCommit is a function.
func TestGitCommandResetToCommit(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"reset", "--hard", "78976bc"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard"))
}
2018-11-30 02:47:14 +02:00
// TestGitCommandNewBranch is a function.
func TestGitCommandNewBranch(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"checkout", "-b", "test"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.NewBranch("test"))
}
2018-11-30 02:47:14 +02:00
// TestGitCommandDeleteBranch is a function.
func TestGitCommandDeleteBranch(t *testing.T) {
type scenario struct {
testName string
branch string
force bool
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"Delete a branch",
"test",
false,
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"branch", "-d", "test"}, args)
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
},
{
"Force delete a branch",
"test",
true,
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"branch", "-D", "test"}, args)
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = s.command
s.test(gitCmd.DeleteBranch(s.branch, s.force))
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandMerge is a function.
func TestGitCommandMerge(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"merge", "--no-edit", "test"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.Merge("test"))
}
2018-11-30 02:47:14 +02:00
// TestGitCommandUsingGpg is a function.
func TestGitCommandUsingGpg(t *testing.T) {
type scenario struct {
testName string
getLocalGitConfig func(string) (string, error)
getGlobalGitConfig func(string) (string, error)
test func(bool)
}
scenarios := []scenario{
{
"Option global and local config commit.gpgsign is not set",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.False(t, gpgEnabled)
},
},
{
"Option global config commit.gpgsign is not set, fallback on local config",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "true", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is true",
func(string) (string, error) {
return "True", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is on",
func(string) (string, error) {
return "ON", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is yes",
func(string) (string, error) {
return "YeS", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{
"Option commit.gpgsign is 1",
func(string) (string, error) {
return "1", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.getLocalGitConfig = s.getLocalGitConfig
s.test(gitCmd.usingGpg())
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandCommit is a function.
func TestGitCommandCommit(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error)
test func(*exec.Cmd, error)
flags string
}
scenarios := []scenario{
{
"Commit using gpg",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "bash", cmd)
assert.EqualValues(t, []string{"-c", `git commit -m 'test'`}, args)
return exec.Command("echo")
},
func(string) (string, error) {
return "true", nil
},
func(cmd *exec.Cmd, err error) {
assert.NotNil(t, cmd)
assert.Nil(t, err)
},
"",
},
{
"Commit without using gpg",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"commit", "-m", "test"}, args)
return exec.Command("echo")
},
func(string) (string, error) {
return "false", nil
},
func(cmd *exec.Cmd, err error) {
assert.Nil(t, cmd)
assert.Nil(t, err)
},
"",
},
{
"Commit with --no-verify flag",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"commit", "--no-verify", "-m", "test"}, args)
return exec.Command("echo")
},
func(string) (string, error) {
return "false", nil
},
func(cmd *exec.Cmd, err error) {
assert.Nil(t, cmd)
assert.Nil(t, err)
},
"--no-verify",
},
{
"Commit without using gpg with an error",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"commit", "-m", "test"}, args)
return exec.Command("test")
},
func(string) (string, error) {
return "false", nil
},
func(cmd *exec.Cmd, err error) {
assert.Nil(t, cmd)
assert.Error(t, err)
},
"",
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.OSCommand.command = s.command
s.test(gitCmd.Commit("test", s.flags))
})
}
}
// TestGitCommandAmendHead is a function.
func TestGitCommandAmendHead(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error)
test func(*exec.Cmd, error)
}
scenarios := []scenario{
{
"Amend commit using gpg",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "bash", cmd)
2019-11-05 10:03:36 +02:00
assert.EqualValues(t, []string{"-c", "git commit --amend --no-edit --allow-empty"}, args)
return exec.Command("echo")
},
func(string) (string, error) {
return "true", nil
},
func(cmd *exec.Cmd, err error) {
assert.NotNil(t, cmd)
assert.Nil(t, err)
},
},
{
"Amend commit without using gpg",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
2019-11-05 10:03:36 +02:00
assert.EqualValues(t, []string{"commit", "--amend", "--no-edit", "--allow-empty"}, args)
return exec.Command("echo")
},
func(string) (string, error) {
return "false", nil
},
func(cmd *exec.Cmd, err error) {
assert.Nil(t, cmd)
assert.Nil(t, err)
},
},
{
"Amend commit without using gpg with an error",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
2019-11-05 10:03:36 +02:00
assert.EqualValues(t, []string{"commit", "--amend", "--no-edit", "--allow-empty"}, args)
return exec.Command("test")
},
func(string) (string, error) {
return "false", nil
},
func(cmd *exec.Cmd, err error) {
assert.Nil(t, cmd)
assert.Error(t, err)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
gitCmd.OSCommand.command = s.command
s.test(gitCmd.AmendHead())
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandPush is a function.
func TestGitCommandPush(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
forcePush bool
test func(error)
}
scenarios := []scenario{
{
"Push with force disabled",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
2019-11-21 12:45:18 +02:00
assert.EqualValues(t, []string{"push", "--follow-tags"}, args)
return exec.Command("echo")
},
false,
func(err error) {
2019-02-16 01:18:38 +02:00
assert.NoError(t, err)
},
},
{
"Push with force enabled",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
2019-11-21 12:45:18 +02:00
assert.EqualValues(t, []string{"push", "--follow-tags", "--force-with-lease"}, args)
return exec.Command("echo")
},
true,
func(err error) {
2019-02-16 01:18:38 +02:00
assert.NoError(t, err)
},
},
{
"Push with an error occurring",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
2019-11-21 12:45:18 +02:00
assert.EqualValues(t, []string{"push", "--follow-tags"}, args)
return exec.Command("test")
},
false,
func(err error) {
2019-02-16 01:18:38 +02:00
assert.Error(t, err)
},
},
}
2018-10-29 09:23:56 +02:00
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = s.command
err := gitCmd.Push("test", s.forcePush, "", func(passOrUname string) string {
return "\n"
})
2018-10-29 09:23:56 +02:00
s.test(err)
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandCatFile is a function.
2018-09-13 21:44:26 +02:00
func TestGitCommandCatFile(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-09-13 21:44:26 +02:00
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "cat", cmd)
assert.EqualValues(t, []string{"test.txt"}, args)
return exec.Command("echo", "-n", "test")
}
o, err := gitCmd.CatFile("test.txt")
assert.NoError(t, err)
assert.Equal(t, "test", o)
}
2018-11-30 02:47:14 +02:00
// TestGitCommandStageFile is a function.
2018-09-13 21:44:26 +02:00
func TestGitCommandStageFile(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-09-13 21:44:26 +02:00
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"add", "test.txt"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.StageFile("test.txt"))
}
2018-11-30 02:47:14 +02:00
// TestGitCommandUnstageFile is a function.
2018-09-13 21:44:26 +02:00
func TestGitCommandUnstageFile(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(error)
tracked bool
}
scenarios := []scenario{
{
"Remove an untracked file from staging",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"rm", "--cached", "test.txt"}, args)
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
false,
},
{
"Remove a tracked file from staging",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"reset", "HEAD", "test.txt"}, args)
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
true,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-09-13 21:44:26 +02:00
gitCmd.OSCommand.command = s.command
s.test(gitCmd.UnStageFile("test.txt", s.tracked))
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandIsInMergeState is a function.
2018-09-13 21:44:26 +02:00
func TestGitCommandIsInMergeState(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(bool, error)
}
scenarios := []scenario{
{
"An error occurred when running status command",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args)
return exec.Command("test")
2018-09-13 21:44:26 +02:00
},
func(isInMergeState bool, err error) {
assert.Error(t, err)
assert.False(t, isInMergeState)
},
},
{
"Is not in merge state",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args)
return exec.Command("echo")
},
func(isInMergeState bool, err error) {
assert.False(t, isInMergeState)
assert.NoError(t, err)
},
},
{
"Command output contains conclude merge",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args)
return exec.Command("echo", "'conclude merge'")
},
func(isInMergeState bool, err error) {
assert.True(t, isInMergeState)
assert.NoError(t, err)
},
},
{
"Command output contains unmerged paths",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"status", "--untracked-files=all"}, args)
return exec.Command("echo", "'unmerged paths'")
},
func(isInMergeState bool, err error) {
assert.True(t, isInMergeState)
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-09-13 21:44:26 +02:00
gitCmd.OSCommand.command = s.command
s.test(gitCmd.IsInMergeState())
})
}
}
2019-03-18 11:44:33 +02:00
// TestGitCommandDiscardAllFileChanges is a function.
func TestGitCommandDiscardAllFileChanges(t *testing.T) {
type scenario struct {
testName string
command func() (func(string, ...string) *exec.Cmd, *[][]string)
test func(*[][]string, error)
2018-09-19 11:23:31 +02:00
file *File
removeFile func(string) error
}
scenarios := []scenario{
{
"An error occurred when resetting",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("test")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.Error(t, err)
assert.Len(t, *cmdsCalled, 1)
assert.EqualValues(t, *cmdsCalled, [][]string{
{"reset", "--", "test"},
})
},
2018-09-19 11:23:31 +02:00
&File{
Name: "test",
HasStagedChanges: true,
},
func(string) error {
return nil
},
},
{
"An error occurred when removing file",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("test")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.Error(t, err)
assert.EqualError(t, err, "an error occurred when removing file")
assert.Len(t, *cmdsCalled, 0)
},
2018-09-19 11:23:31 +02:00
&File{
Name: "test",
Tracked: false,
},
func(string) error {
return fmt.Errorf("an error occurred when removing file")
},
},
{
"An error occurred with checkout",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("test")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.Error(t, err)
assert.Len(t, *cmdsCalled, 1)
assert.EqualValues(t, *cmdsCalled, [][]string{
{"checkout", "--", "test"},
})
},
2018-09-19 11:23:31 +02:00
&File{
Name: "test",
Tracked: true,
HasStagedChanges: false,
},
func(string) error {
return nil
},
},
{
"Checkout only",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("echo")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.NoError(t, err)
assert.Len(t, *cmdsCalled, 1)
assert.EqualValues(t, *cmdsCalled, [][]string{
{"checkout", "--", "test"},
})
},
2018-09-19 11:23:31 +02:00
&File{
Name: "test",
Tracked: true,
HasStagedChanges: false,
},
func(string) error {
return nil
},
},
{
"Reset and checkout staged changes",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("echo")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.NoError(t, err)
assert.Len(t, *cmdsCalled, 2)
assert.EqualValues(t, *cmdsCalled, [][]string{
{"reset", "--", "test"},
{"checkout", "--", "test"},
})
},
&File{
Name: "test",
Tracked: true,
HasStagedChanges: true,
},
func(string) error {
return nil
},
},
{
"Reset and checkout merge conflicts",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("echo")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.NoError(t, err)
assert.Len(t, *cmdsCalled, 2)
assert.EqualValues(t, *cmdsCalled, [][]string{
{"reset", "--", "test"},
{"checkout", "--", "test"},
})
},
2018-09-19 11:23:31 +02:00
&File{
Name: "test",
Tracked: true,
HasMergeConflicts: true,
},
func(string) error {
return nil
},
},
{
"Reset and remove",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("echo")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.NoError(t, err)
assert.Len(t, *cmdsCalled, 1)
assert.EqualValues(t, *cmdsCalled, [][]string{
{"reset", "--", "test"},
})
},
2018-09-19 11:23:31 +02:00
&File{
Name: "test",
Tracked: false,
HasStagedChanges: true,
},
func(filename string) error {
assert.Equal(t, "test", filename)
return nil
},
},
{
"Remove only",
func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd {
cmdsCalled = append(cmdsCalled, args)
return exec.Command("echo")
}, &cmdsCalled
},
func(cmdsCalled *[][]string, err error) {
assert.NoError(t, err)
assert.Len(t, *cmdsCalled, 0)
},
2018-09-19 11:23:31 +02:00
&File{
Name: "test",
Tracked: false,
HasStagedChanges: false,
},
func(filename string) error {
assert.Equal(t, "test", filename)
return nil
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
var cmdsCalled *[][]string
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command, cmdsCalled = s.command()
gitCmd.removeFile = s.removeFile
2019-03-18 11:44:33 +02:00
s.test(cmdsCalled, gitCmd.DiscardAllFileChanges(s.file))
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandCheckout is a function.
func TestGitCommandCheckout(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(error)
force bool
}
scenarios := []scenario{
{
"Checkout",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"checkout", "test"}, args)
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
false,
},
{
"Checkout forced",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"checkout", "--force", "test"}, args)
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
true,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = s.command
s.test(gitCmd.Checkout("test", s.force))
})
}
}
2018-11-30 02:47:14 +02:00
// TestGitCommandGetBranchGraph is a function.
func TestGitCommandGetBranchGraph(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
assert.EqualValues(t, []string{"log", "--graph", "--color", "--abbrev-commit", "--decorate", "--date=relative", "--pretty=medium", "test"}, args)
return exec.Command("echo")
}
_, err := gitCmd.GetBranchGraph("test")
assert.NoError(t, err)
}
2018-11-30 02:47:14 +02:00
// TestGitCommandDiff is a function.
func TestGitCommandDiff(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
file *File
2018-12-05 10:33:46 +02:00
plain bool
cached bool
}
scenarios := []scenario{
{
"Default case",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"diff", "--color", "--", "test.txt"}, args)
return exec.Command("echo")
},
&File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
2018-12-05 10:33:46 +02:00
false,
false,
2018-12-05 10:33:46 +02:00
},
{
"cached",
2018-12-05 10:33:46 +02:00
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"diff", "--color", "--cached", "--", "test.txt"}, args)
2018-12-05 10:33:46 +02:00
return exec.Command("echo")
},
&File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
false,
2018-12-05 10:33:46 +02:00
true,
},
{
"plain",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"diff", "--", "test.txt"}, args)
return exec.Command("echo")
},
&File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
true,
2018-12-05 10:33:46 +02:00
false,
},
{
"File not tracked and file has no staged changes",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"diff", "--color", "--no-index", "/dev/null", "test.txt"}, args)
return exec.Command("echo")
},
&File{
Name: "test.txt",
HasStagedChanges: false,
Tracked: false,
},
2018-12-05 10:33:46 +02:00
false,
false,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = s.command
gitCmd.Diff(s.file, s.plain, s.cached)
2018-09-05 22:16:26 +02:00
})
}
}
2018-09-25 12:25:04 +02:00
2018-11-30 02:47:14 +02:00
// TestGitCommandCurrentBranchName is a function.
2018-09-25 12:31:19 +02:00
func TestGitCommandCurrentBranchName(t *testing.T) {
2018-10-05 01:11:19 +02:00
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(string, error)
}
scenarios := []scenario{
{
"says we are on the master branch if we are",
func(cmd string, args ...string) *exec.Cmd {
assert.Equal(t, "git", cmd)
return exec.Command("echo", "master")
},
func(output string, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "master", output)
},
},
{
2019-11-21 12:45:18 +02:00
"falls back to git `git branch --contains` if symbolic-ref fails",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
switch args[0] {
case "symbolic-ref":
assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
return exec.Command("test")
2019-11-21 12:45:18 +02:00
case "branch":
assert.EqualValues(t, []string{"branch", "--contains"}, args)
return exec.Command("echo", "* master")
}
return nil
},
func(output string, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "master", output)
},
},
2018-10-05 01:11:19 +02:00
{
"bubbles up error if there is one",
func(cmd string, args ...string) *exec.Cmd {
assert.Equal(t, "git", cmd)
return exec.Command("test")
},
func(output string, err error) {
assert.Error(t, err)
assert.EqualValues(t, "", output)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-10-05 01:11:19 +02:00
gitCmd.OSCommand.command = s.command
s.test(gitCmd.CurrentBranchName())
})
2018-09-25 12:31:19 +02:00
}
}
2018-12-05 10:33:46 +02:00
func TestGitCommandApplyPatch(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(error)
2018-12-05 10:33:46 +02:00
}
scenarios := []scenario{
{
"valid case",
func(cmd string, args ...string) *exec.Cmd {
assert.Equal(t, "git", cmd)
assert.EqualValues(t, []string{"apply", "--cached"}, args[0:2])
filename := args[2]
content, err := ioutil.ReadFile(filename)
assert.NoError(t, err)
assert.Equal(t, "test", string(content))
return exec.Command("echo", "done")
},
func(err error) {
2018-12-05 10:33:46 +02:00
assert.NoError(t, err)
},
},
{
"command returns error",
func(cmd string, args ...string) *exec.Cmd {
assert.Equal(t, "git", cmd)
assert.EqualValues(t, []string{"apply", "--cached"}, args[0:2])
filename := args[2]
// TODO: Ideally we want to mock out OSCommand here so that we're not
// double handling testing it's CreateTempFile functionality,
// but it is going to take a bit of work to make a proper mock for it
// so I'm leaving it for another PR
content, err := ioutil.ReadFile(filename)
assert.NoError(t, err)
assert.Equal(t, "test", string(content))
return exec.Command("test")
},
func(err error) {
2018-12-05 10:33:46 +02:00
assert.Error(t, err)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
2018-12-05 10:33:46 +02:00
gitCmd.OSCommand.command = s.command
s.test(gitCmd.ApplyPatch("test", "cached"))
2018-12-05 10:33:46 +02:00
})
}
}
// TestGitCommandRebaseBranch is a function.
func TestGitCommandRebaseBranch(t *testing.T) {
type scenario struct {
testName string
arg string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"successful rebase",
"master",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
2019-11-05 10:03:36 +02:00
Expect: "git rebase --interactive --autostash --keep-empty --rebase-merges master",
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
{
"unsuccessful rebase",
"master",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
2019-11-05 10:03:36 +02:00
Expect: "git rebase --interactive --autostash --keep-empty --rebase-merges master",
Replace: "test",
},
}),
func(err error) {
assert.Error(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
2019-03-12 10:56:04 +02:00
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.RebaseBranch(s.arg))
})
}
}
// TestGitCommandCheckoutFile is a function.
func TestGitCommandCheckoutFile(t *testing.T) {
type scenario struct {
testName string
commitSha string
fileName string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"typical case",
"11af912",
"test999.txt",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: "git checkout 11af912 test999.txt",
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
{
"returns error if there is one",
"11af912",
"test999.txt",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: "git checkout 11af912 test999.txt",
Replace: "test",
},
}),
func(err error) {
assert.Error(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.CheckoutFile(s.commitSha, s.fileName))
})
}
}
// TestGitCommandDiscardOldFileChanges is a function.
func TestGitCommandDiscardOldFileChanges(t *testing.T) {
type scenario struct {
testName string
getLocalGitConfig func(string) (string, error)
commits []*Commit
commitIndex int
fileName string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"returns error when index outside of range of commits",
func(string) (string, error) {
return "", nil
},
[]*Commit{},
0,
"test999.txt",
nil,
func(err error) {
assert.Error(t, err)
},
},
{
"returns error when using gpg",
func(string) (string, error) {
return "true", nil
},
2019-03-12 10:57:59 +02:00
[]*Commit{{Name: "commit", Sha: "123456"}},
2019-03-12 10:56:04 +02:00
0,
"test999.txt",
nil,
func(err error) {
assert.Error(t, err)
},
},
{
"checks out file if it already existed",
func(string) (string, error) {
return "", nil
},
[]*Commit{
2019-03-12 10:57:59 +02:00
{Name: "commit", Sha: "123456"},
{Name: "commit2", Sha: "abcdef"},
2019-03-12 10:56:04 +02:00
},
0,
"test999.txt",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
2019-11-05 10:03:36 +02:00
Expect: "git rebase --interactive --autostash --keep-empty --rebase-merges abcdef",
2019-03-12 10:56:04 +02:00
Replace: "echo",
},
{
Expect: "git cat-file -e HEAD^:test999.txt",
Replace: "echo",
},
{
Expect: "git checkout HEAD^ test999.txt",
Replace: "echo",
},
{
2019-11-05 10:03:36 +02:00
Expect: "git commit --amend --no-edit --allow-empty",
2019-03-12 10:56:04 +02:00
Replace: "echo",
},
{
Expect: "git rebase --continue",
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
// test for when the file was created within the commit requires a refactor to support proper mocks
// currently we'd need to mock out the os.Remove function and that's gonna introduce tech debt
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
gitCmd.getLocalGitConfig = s.getLocalGitConfig
s.test(gitCmd.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
})
}
}
2019-03-16 01:15:46 +02:00
// TestGitCommandShowCommitFile is a function.
func TestGitCommandShowCommitFile(t *testing.T) {
type scenario struct {
testName string
commitSha string
fileName string
command func(string, ...string) *exec.Cmd
test func(string, error)
}
scenarios := []scenario{
{
"valid case",
"123456",
"hello.txt",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
2019-11-05 10:03:36 +02:00
Expect: "git show --no-renames 123456 -- hello.txt",
2019-03-16 01:15:46 +02:00
Replace: "echo -n hello",
},
}),
func(str string, err error) {
assert.NoError(t, err)
assert.Equal(t, "hello", str)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.ShowCommitFile(s.commitSha, s.fileName, true))
2019-03-16 01:15:46 +02:00
})
}
}
// TestGitCommandGetCommitFiles is a function.
func TestGitCommandGetCommitFiles(t *testing.T) {
type scenario struct {
testName string
commitSha string
command func(string, ...string) *exec.Cmd
test func([]*CommitFile, error)
}
scenarios := []scenario{
{
"valid case",
"123456",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
2019-11-05 10:03:36 +02:00
Expect: "git show --pretty= --name-only --no-renames 123456",
2019-03-16 01:15:46 +02:00
Replace: "echo 'hello\nworld'",
},
}),
func(commitFiles []*CommitFile, err error) {
assert.NoError(t, err)
assert.Equal(t, []*CommitFile{
{Sha: "123456", Name: "hello", DisplayString: "hello"},
{Sha: "123456", Name: "world", DisplayString: "world"},
}, commitFiles)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.GetCommitFiles(s.commitSha, nil))
2019-03-16 01:15:46 +02:00
})
}
}
2019-03-18 11:44:33 +02:00
// TestGitCommandDiscardUnstagedFileChanges is a function.
func TestGitCommandDiscardUnstagedFileChanges(t *testing.T) {
2019-03-18 11:44:33 +02:00
type scenario struct {
testName string
file *File
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"valid case",
&File{Name: "test.txt"},
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git checkout -- "test.txt"`,
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.DiscardUnstagedFileChanges(s.file))
})
}
}
// TestGitCommandDiscardAnyUnstagedFileChanges is a function.
func TestGitCommandDiscardAnyUnstagedFileChanges(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"valid case",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git checkout -- .`,
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.DiscardAnyUnstagedFileChanges())
})
}
}
// TestGitCommandRemoveUntrackedFiles is a function.
func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
type scenario struct {
testName string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"valid case",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git clean -fd`,
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.RemoveUntrackedFiles())
})
}
}
// TestGitCommandResetHard is a function.
func TestGitCommandResetHard(t *testing.T) {
type scenario struct {
testName string
ref string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"valid case",
"HEAD",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git reset --hard HEAD`,
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.ResetHard(s.ref))
})
}
}
2019-04-07 03:35:34 +02:00
// TestGitCommandCreateFixupCommit is a function.
func TestGitCommandCreateFixupCommit(t *testing.T) {
type scenario struct {
testName string
sha string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"valid case",
"12345",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git commit --fixup=12345`,
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command
s.test(gitCmd.CreateFixupCommit(s.sha))
})
}
}
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 {
t.Run(s.testName, func(t *testing.T) {
s.test(findDotGitDir(s.stat, s.readFile))
})
}
}