1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03: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) { func TestVerifyInGitRepo(t *testing.T) {
type scenario struct { type scenario struct {
runCmd func(string) error testName string
test func(error) runCmd func(string) error
test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"Valid git repository",
func(string) error { func(string) error {
return nil return nil
}, },
@ -78,6 +80,7 @@ func TestVerifyInGitRepo(t *testing.T) {
}, },
}, },
{ {
"Not a valid git repository",
func(string) error { func(string) error {
return fmt.Errorf("fatal: Not a git repository (or any of the parent directories): .git") 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 { 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) { func TestNavigateToRepoRootDirectory(t *testing.T) {
type scenario struct { type scenario struct {
stat func(string) (os.FileInfo, error) testName string
chdir func(string) error stat func(string) (os.FileInfo, error)
test func(error) chdir func(string) error
test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"Navigate to git repository",
func(string) (os.FileInfo, error) { func(string) (os.FileInfo, error) {
return fileInfoMock{isDir: true}, nil 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) { func(string) (os.FileInfo, error) {
return nil, fmt.Errorf("An error occurred") return nil, fmt.Errorf("An error occurred")
}, },
@ -125,18 +133,7 @@ func TestNavigateToRepoRootDirectory(t *testing.T) {
}, },
}, },
{ {
func(string) (os.FileInfo, error) { "An error occurred when trying to move one path backward",
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")
},
},
{
func(string) (os.FileInfo, error) { func(string) (os.FileInfo, error) {
return nil, os.ErrNotExist return nil, os.ErrNotExist
}, },
@ -151,12 +148,15 @@ func TestNavigateToRepoRootDirectory(t *testing.T) {
} }
for _, s := range scenarios { 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) { func TestSetupRepositoryAndWorktree(t *testing.T) {
type scenario struct { type scenario struct {
testName string
openGitRepository func(string) (*gogit.Repository, error) openGitRepository func(string) (*gogit.Repository, error)
sLocalize func(string) string sLocalize func(string) string
test func(*gogit.Repository, *gogit.Worktree, error) test func(*gogit.Repository, *gogit.Worktree, error)
@ -164,6 +164,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
scenarios := []scenario{ scenarios := []scenario{
{ {
"A gitconfig parsing error occurred",
func(string) (*gogit.Repository, error) { func(string) (*gogit.Repository, error) {
return nil, fmt.Errorf(`unquoted '\' must be followed by new line`) 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) { func(string) (*gogit.Repository, error) {
return nil, fmt.Errorf("Error from inside gogit") 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) { func(string) (*gogit.Repository, error) {
return &gogit.Repository{}, nil return &gogit.Repository{}, nil
}, },
@ -196,6 +199,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
}, },
}, },
{ {
"Setup done properly",
func(string) (*gogit.Repository, error) { func(string) (*gogit.Repository, error) {
assert.NoError(t, os.RemoveAll("/tmp/lazygit-test")) assert.NoError(t, os.RemoveAll("/tmp/lazygit-test"))
r, err := gogit.PlainInit("/tmp/lazygit-test", false) r, err := gogit.PlainInit("/tmp/lazygit-test", false)
@ -205,12 +209,16 @@ func TestSetupRepositoryAndWorktree(t *testing.T) {
func(string) string { return "" }, func(string) string { return "" },
func(r *gogit.Repository, w *gogit.Worktree, err error) { func(r *gogit.Repository, w *gogit.Worktree, err error) {
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, w)
assert.NotNil(t, r)
}, },
}, },
} }
for _, s := range scenarios { 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 { type scenario struct {
setup func() testName string
test func(*GitCommand, error) setup func()
test func(*GitCommand, error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"An error occurred, folder doesn't contains a git repository",
func() { func() {
assert.NoError(t, os.Chdir("/tmp")) assert.NoError(t, os.Chdir("/tmp"))
}, },
@ -238,6 +248,7 @@ func TestNewGitCommand(t *testing.T) {
}, },
}, },
{ {
"New GitCommand object created",
func() { func() {
assert.NoError(t, os.RemoveAll("/tmp/lazygit-test")) assert.NoError(t, os.RemoveAll("/tmp/lazygit-test"))
_, err := gogit.PlainInit("/tmp/lazygit-test", false) _, err := gogit.PlainInit("/tmp/lazygit-test", false)
@ -251,19 +262,23 @@ func TestNewGitCommand(t *testing.T) {
} }
for _, s := range scenarios { for _, s := range scenarios {
s.setup() t.Run(s.testName, func(t *testing.T) {
s.test(NewGitCommand(newDummyLog(), newDummyOSCommand(), i18n.NewLocalizer(newDummyLog()))) s.setup()
s.test(NewGitCommand(newDummyLog(), newDummyOSCommand(), i18n.NewLocalizer(newDummyLog())))
})
} }
} }
func TestGitCommandGetStashEntries(t *testing.T) { func TestGitCommandGetStashEntries(t *testing.T) {
type scenario struct { type scenario struct {
command func(string, ...string) *exec.Cmd testName string
test func([]StashEntry) command func(string, ...string) *exec.Cmd
test func([]StashEntry)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"No stash entries found",
func(string, ...string) *exec.Cmd { func(string, ...string) *exec.Cmd {
return exec.Command("echo") return exec.Command("echo")
}, },
@ -272,6 +287,7 @@ func TestGitCommandGetStashEntries(t *testing.T) {
}, },
}, },
{ {
"Several stash entries found",
func(string, ...string) *exec.Cmd { 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") 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 { for _, s := range scenarios {
gitCmd := newDummyGitCommand() t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command 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) { func TestGetStatusFiles(t *testing.T) {
type scenario struct { type scenario struct {
command func(string, ...string) *exec.Cmd testName string
test func([]File) command func(string, ...string) *exec.Cmd
test func([]File)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"No files found",
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
return exec.Command("echo") return exec.Command("echo")
}, },
@ -333,6 +353,7 @@ func TestGetStatusFiles(t *testing.T) {
}, },
}, },
{ {
"Several files found",
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
return exec.Command( return exec.Command(
"echo", "echo",
@ -391,10 +412,12 @@ func TestGetStatusFiles(t *testing.T) {
} }
for _, s := range scenarios { for _, s := range scenarios {
gitCmd := newDummyGitCommand() t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.command = s.command 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) { func TestGitCommandMergeStatusFiles(t *testing.T) {
type scenario struct { type scenario struct {
testName string
oldFiles []File oldFiles []File
newFiles []File newFiles []File
test func([]File) test func([]File)
@ -444,6 +468,7 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
scenarios := []scenario{ scenarios := []scenario{
{ {
"Old file and new file are the same",
[]File{}, []File{},
[]File{ []File{
{ {
@ -462,6 +487,7 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
}, },
}, },
{ {
"Several files to merge, with some identical",
[]File{ []File{
{ {
Name: "new_file1.txt", Name: "new_file1.txt",
@ -504,9 +530,11 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
} }
for _, s := range scenarios { 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 { 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")
})
} }
} }