1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-28 09:08:41 +02:00

rewrite to use subtests

This commit is contained in:
Anthony HAMON 2018-09-05 22:16:26 +02:00
parent 5af03b6820
commit 1cc7e9c02a

View File

@ -64,12 +64,14 @@ func newDummyGitCommand() *GitCommand {
func TestVerifyInGitRepo(t *testing.T) {
type scenario struct {
runCmd func(string) error
test func(error)
testName string
runCmd func(string) error
test func(error)
}
scenarios := []scenario{
{
"Valid git repository",
func(string) error {
return nil
},
@ -78,6 +80,7 @@ func TestVerifyInGitRepo(t *testing.T) {
},
},
{
"Not a valid git repository",
func(string) error {
return fmt.Errorf("fatal: Not a git repository (or any of the parent directories): .git")
},
@ -89,19 +92,23 @@ func TestVerifyInGitRepo(t *testing.T) {
}
for _, s := range scenarios {
s.test(verifyInGitRepo(s.runCmd))
t.Run(s.testName, func(t *testing.T) {
s.test(verifyInGitRepo(s.runCmd))
})
}
}
func TestNavigateToRepoRootDirectory(t *testing.T) {
type scenario struct {
stat func(string) (os.FileInfo, error)
chdir func(string) error
test func(error)
testName string
stat func(string) (os.FileInfo, error)
chdir func(string) error
test func(error)
}
scenarios := []scenario{
{
"Navigate to git repository",
func(string) (os.FileInfo, error) {
return fileInfoMock{isDir: true}, nil
},
@ -113,6 +120,7 @@ func TestNavigateToRepoRootDirectory(t *testing.T) {
},
},
{
"An error occurred when getting path informations",
func(string) (os.FileInfo, error) {
return nil, fmt.Errorf("An error occurred")
},
@ -125,18 +133,7 @@ func TestNavigateToRepoRootDirectory(t *testing.T) {
},
},
{
func(string) (os.FileInfo, error) {
return nil, os.ErrNotExist
},
func(string) error {
return fmt.Errorf("An error occurred")
},
func(err error) {
assert.Error(t, err)
assert.EqualError(t, err, "An error occurred")
},
},
{
"An error occurred when trying to move one path backward",
func(string) (os.FileInfo, error) {
return nil, os.ErrNotExist
},
@ -151,12 +148,15 @@ func TestNavigateToRepoRootDirectory(t *testing.T) {
}
for _, s := range scenarios {
s.test(navigateToRepoRootDirectory(s.stat, s.chdir))
t.Run(s.testName, func(t *testing.T) {
s.test(navigateToRepoRootDirectory(s.stat, s.chdir))
})
}
}
func TestSetupRepositoryAndWorktree(t *testing.T) {
type scenario struct {
testName string
openGitRepository func(string) (*gogit.Repository, error)
sLocalize func(string) string
test func(*gogit.Repository, *gogit.Worktree, error)
@ -164,6 +164,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
scenarios := []scenario{
{
"A gitconfig parsing error occurred",
func(string) (*gogit.Repository, error) {
return nil, fmt.Errorf(`unquoted '\' must be followed by new line`)
},
@ -176,6 +177,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
},
},
{
"A gogit error occurred",
func(string) (*gogit.Repository, error) {
return nil, fmt.Errorf("Error from inside gogit")
},
@ -186,6 +188,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
},
},
{
"An error occurred cause git repository is a bare repository",
func(string) (*gogit.Repository, error) {
return &gogit.Repository{}, nil
},
@ -196,6 +199,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
},
},
{
"Setup done properly",
func(string) (*gogit.Repository, error) {
assert.NoError(t, os.RemoveAll("/tmp/lazygit-test"))
r, err := gogit.PlainInit("/tmp/lazygit-test", false)
@ -205,12 +209,16 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
func(string) string { return "" },
func(r *gogit.Repository, w *gogit.Worktree, err error) {
assert.NoError(t, err)
assert.NotNil(t, w)
assert.NotNil(t, r)
},
},
}
for _, s := range scenarios {
s.test(setupRepositoryAndWorktree(s.openGitRepository, s.sLocalize))
t.Run(s.testName, func(t *testing.T) {
s.test(setupRepositoryAndWorktree(s.openGitRepository, s.sLocalize))
})
}
}
@ -223,12 +231,14 @@ func TestNewGitCommand(t *testing.T) {
}()
type scenario struct {
setup func()
test func(*GitCommand, error)
testName string
setup func()
test func(*GitCommand, error)
}
scenarios := []scenario{
{
"An error occurred, folder doesn't contains a git repository",
func() {
assert.NoError(t, os.Chdir("/tmp"))
},
@ -238,6 +248,7 @@ func TestNewGitCommand(t *testing.T) {
},
},
{
"New GitCommand object created",
func() {
assert.NoError(t, os.RemoveAll("/tmp/lazygit-test"))
_, err := gogit.PlainInit("/tmp/lazygit-test", false)
@ -251,19 +262,23 @@ func TestNewGitCommand(t *testing.T) {
}
for _, s := range scenarios {
s.setup()
s.test(NewGitCommand(newDummyLog(), newDummyOSCommand(), i18n.NewLocalizer(newDummyLog())))
t.Run(s.testName, func(t *testing.T) {
s.setup()
s.test(NewGitCommand(newDummyLog(), newDummyOSCommand(), i18n.NewLocalizer(newDummyLog())))
})
}
}
func TestGitCommandGetStashEntries(t *testing.T) {
type scenario struct {
command func(string, ...string) *exec.Cmd
test func([]StashEntry)
testName string
command func(string, ...string) *exec.Cmd
test func([]StashEntry)
}
scenarios := []scenario{
{
"No stash entries found",
func(string, ...string) *exec.Cmd {
return exec.Command("echo")
},
@ -272,6 +287,7 @@ func TestGitCommandGetStashEntries(t *testing.T) {
},
},
{
"Several stash entries found",
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")
},
@ -296,10 +312,12 @@ func TestGitCommandGetStashEntries(t *testing.T) {
}
for _, s := range scenarios {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
t.Run(s.testName, func(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
s.test(gitCmd.GetStashEntries())
s.test(gitCmd.GetStashEntries())
})
}
}
@ -319,12 +337,14 @@ func TestGetStashEntryDiff(t *testing.T) {
func TestGetStatusFiles(t *testing.T) {
type scenario struct {
command func(string, ...string) *exec.Cmd
test func([]File)
testName string
command func(string, ...string) *exec.Cmd
test func([]File)
}
scenarios := []scenario{
{
"No files found",
func(cmd string, args ...string) *exec.Cmd {
return exec.Command("echo")
},
@ -333,6 +353,7 @@ func TestGetStatusFiles(t *testing.T) {
},
},
{
"Several files found",
func(cmd string, args ...string) *exec.Cmd {
return exec.Command(
"echo",
@ -391,10 +412,12 @@ func TestGetStatusFiles(t *testing.T) {
}
for _, s := range scenarios {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
t.Run(s.testName, func(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
s.test(gitCmd.GetStatusFiles())
s.test(gitCmd.GetStatusFiles())
})
}
}
@ -437,6 +460,7 @@ func TestGitCommandCommitAmend(t *testing.T) {
func TestGitCommandMergeStatusFiles(t *testing.T) {
type scenario struct {
testName string
oldFiles []File
newFiles []File
test func([]File)
@ -444,6 +468,7 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
scenarios := []scenario{
{
"Old file and new file are the same",
[]File{},
[]File{
{
@ -462,6 +487,7 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
},
},
{
"Several files to merge, with some identical",
[]File{
{
Name: "new_file1.txt",
@ -504,9 +530,11 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
}
for _, s := range scenarios {
gitCmd := newDummyGitCommand()
t.Run(s.testName, func(t *testing.T) {
gitCmd := newDummyGitCommand()
s.test(gitCmd.MergeStatusFiles(s.oldFiles, s.newFiles))
s.test(gitCmd.MergeStatusFiles(s.oldFiles, s.newFiles))
})
}
}
@ -599,6 +627,8 @@ func TestGitCommandDiff(t *testing.T) {
}
for _, file := range files {
assert.NotContains(t, gitCommand.Diff(file), "error")
t.Run(file.Name, func(t *testing.T) {
assert.NotContains(t, gitCommand.Diff(file), "error")
})
}
}