From 624d63d2fa90ad62998a62d8d17189cf0406864b Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 29 Aug 2018 21:47:48 +0200 Subject: [PATCH 1/8] pkg/git : remove panic in SetupGit method --- main.go | 8 +++++++- pkg/commands/git.go | 49 +++++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 4303af02f..6193f43d3 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,12 @@ func main() { app.Log.Error(err.Error()) panic(err) } - app.GitCommand.SetupGit() + + if err := app.GitCommand.SetupGit(); err != nil { + app.Log.Error(err.Error()) + fmt.Println(err) + os.Exit(1) + } + app.Gui.RunWithSubprocesses() } diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 704a45c73..68571769e 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -15,6 +15,10 @@ import ( gogit "gopkg.in/src-d/go-git.v4" ) +// ErrGitRepositoryInvalid is emitted when we run a git command in a folder +// to check if we have a valid git repository and we get an error instead +var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") + // GitCommand is our main git interface type GitCommand struct { Log *logrus.Entry @@ -35,13 +39,20 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) } // SetupGit sets git repo up -func (c *GitCommand) SetupGit() { - c.verifyInGitRepo() - c.navigateToRepoRootDirectory() - if err := c.setupWorktree(); err != nil { - c.Log.Error(err) - panic(err) +func (c *GitCommand) SetupGit() error { + fs := []func() error{ + c.verifyInGitRepo, + c.navigateToRepoRootDirectory, + c.setupWorktree, } + + for _, f := range fs { + if err := f(); err != nil { + return err + } + } + + return nil } // GetStashEntries stash entryies @@ -145,11 +156,12 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File { return append(headResults, tailResults...) } -func (c *GitCommand) verifyInGitRepo() { - if output, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { - fmt.Println(output) - os.Exit(1) +func (c *GitCommand) verifyInGitRepo() error { + if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { + return ErrGitRepositoryInvalid } + + return nil } // GetBranchName branch name @@ -157,12 +169,19 @@ func (c *GitCommand) GetBranchName() (string, error) { return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") } -func (c *GitCommand) navigateToRepoRootDirectory() { - _, err := os.Stat(".git") - for os.IsNotExist(err) { +func (c *GitCommand) navigateToRepoRootDirectory() error { + for { + f, err := os.Stat(".git") + + if err == nil && f.IsDir() { + return nil + } + c.Log.Debug("going up a directory to find the root") - os.Chdir("..") - _, err = os.Stat(".git") + + if err = os.Chdir(".."); err != nil { + return err + } } } From c1984528c8b8ee2d6d9ffdc50f31c1f81c7b97a3 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 29 Aug 2018 22:55:57 +0200 Subject: [PATCH 2/8] pkg/git : add tests for SetupGit --- pkg/commands/git.go | 117 ++++++++++++++++++++++----------------- pkg/commands/git_test.go | 70 +++++++++++++++++++++++ 2 files changed, 135 insertions(+), 52 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 68571769e..101a45515 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -19,21 +19,39 @@ import ( // to check if we have a valid git repository and we get an error instead var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") +func openGitRepositoryAndWorktree() (*gogit.Repository, *gogit.Worktree, error) { + r, err := gogit.PlainOpen(".") + + if err != nil { + return nil, nil, err + } + + w, err := r.Worktree() + + if err != nil { + return nil, nil, err + } + + return r, w, nil +} + // GitCommand is our main git interface type GitCommand struct { - Log *logrus.Entry - OSCommand *OSCommand - Worktree *gogit.Worktree - Repo *gogit.Repository - Tr *i18n.Localizer + Log *logrus.Entry + OSCommand *OSCommand + Worktree *gogit.Worktree + Repo *gogit.Repository + Tr *i18n.Localizer + openGitRepositoryAndWorktree func() (*gogit.Repository, *gogit.Worktree, error) } // NewGitCommand it runs git commands func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) { gitCommand := &GitCommand{ - Log: log, - OSCommand: osCommand, - Tr: tr, + Log: log, + OSCommand: osCommand, + Tr: tr, + openGitRepositoryAndWorktree: openGitRepositoryAndWorktree, } return gitCommand, nil } @@ -43,7 +61,7 @@ func (c *GitCommand) SetupGit() error { fs := []func() error{ c.verifyInGitRepo, c.navigateToRepoRootDirectory, - c.setupWorktree, + c.setupRepositoryAndWorktree, } for _, f := range fs { @@ -55,6 +73,44 @@ func (c *GitCommand) SetupGit() error { return nil } +func (c *GitCommand) verifyInGitRepo() error { + if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { + return ErrGitRepositoryInvalid + } + + return nil +} + +func (c *GitCommand) navigateToRepoRootDirectory() error { + for { + f, err := os.Stat(".git") + + if err == nil && f.IsDir() { + return nil + } + + c.Log.Debug("going up a directory to find the root") + + if err = os.Chdir(".."); err != nil { + return err + } + } +} + +func (c *GitCommand) setupRepositoryAndWorktree() (err error) { + c.Repo, c.Worktree, err = c.openGitRepositoryAndWorktree() + + if err == nil { + return + } + + if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { + return errors.New(c.Tr.SLocalize("GitconfigParseErr")) + } + + return +} + // GetStashEntries stash entryies func (c *GitCommand) GetStashEntries() []StashEntry { rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'") @@ -156,54 +212,11 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File { return append(headResults, tailResults...) } -func (c *GitCommand) verifyInGitRepo() error { - if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { - return ErrGitRepositoryInvalid - } - - return nil -} - // GetBranchName branch name func (c *GitCommand) GetBranchName() (string, error) { return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") } -func (c *GitCommand) navigateToRepoRootDirectory() error { - for { - f, err := os.Stat(".git") - - if err == nil && f.IsDir() { - return nil - } - - c.Log.Debug("going up a directory to find the root") - - if err = os.Chdir(".."); err != nil { - return err - } - } -} - -func (c *GitCommand) setupWorktree() error { - r, err := gogit.PlainOpen(".") - if err != nil { - if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { - errorMessage := c.Tr.SLocalize("GitconfigParseErr") - return errors.New(errorMessage) - } - return err - } - c.Repo = r - - w, err := r.Worktree() - if err != nil { - return err - } - c.Worktree = w - return nil -} - // ResetHard does the equivalent of `git reset --hard HEAD` func (c *GitCommand) ResetHard() error { return c.Worktree.Reset(&gogit.ResetOptions{Mode: gogit.HardReset}) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index fb3bafe6c..062e8ee3d 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1,13 +1,16 @@ package commands import ( + "fmt" "io/ioutil" "os/exec" "testing" + "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/test" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + gogit "gopkg.in/src-d/go-git.v4" ) func newDummyLog() *logrus.Entry { @@ -20,6 +23,73 @@ func newDummyGitCommand() *GitCommand { return &GitCommand{ Log: newDummyLog(), OSCommand: newDummyOSCommand(), + Tr: i18n.NewLocalizer(newDummyLog()), + } +} + +func TestGitCommandSetupGit(t *testing.T) { + type scenario struct { + command func(string, ...string) *exec.Cmd + openGitRepositoryAndWorktree func() (*gogit.Repository, *gogit.Worktree, error) + test func(error) + } + + scenarios := []scenario{ + { + func(string, ...string) *exec.Cmd { + return exec.Command("exit", "1") + }, + func() (*gogit.Repository, *gogit.Worktree, error) { + return nil, nil, nil + }, + func(err error) { + assert.Error(t, err) + assert.Equal(t, ErrGitRepositoryInvalid, err) + }, + }, + { + func(string, ...string) *exec.Cmd { + return exec.Command("echo") + }, + func() (*gogit.Repository, *gogit.Worktree, error) { + return nil, nil, fmt.Errorf(`unquoted '\' must be followed by new line`) + }, + func(err error) { + assert.Error(t, err) + assert.Contains(t, err.Error(), "gitconfig") + }, + }, + { + func(string, ...string) *exec.Cmd { + return exec.Command("echo") + }, + func() (*gogit.Repository, *gogit.Worktree, error) { + return nil, nil, fmt.Errorf("Error from inside gogit") + }, + func(err error) { + assert.Error(t, err) + assert.EqualError(t, err, "Error from inside gogit") + }, + }, + { + func(string, ...string) *exec.Cmd { + return exec.Command("echo") + }, + func() (*gogit.Repository, *gogit.Worktree, error) { + return &gogit.Repository{}, &gogit.Worktree{}, nil + }, + func(err error) { + assert.NoError(t, err) + }, + }, + } + + for _, s := range scenarios { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + gitCmd.openGitRepositoryAndWorktree = s.openGitRepositoryAndWorktree + + s.test(gitCmd.SetupGit()) } } From 9f7775df263a83c5e0e845955a7abcd989f62d2a Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Thu, 30 Aug 2018 00:54:54 +0200 Subject: [PATCH 3/8] pkg/git : remove unused Map function --- pkg/commands/git.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 101a45515..48d51e900 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -466,15 +466,6 @@ func (c *GitCommand) GetBranchGraph(branchName string) (string, error) { return c.OSCommand.RunCommandWithOutput("git log --graph --color --abbrev-commit --decorate --date=relative --pretty=medium -100 " + branchName) } -// Map (from https://gobyexample.com/collection-functions) -func Map(vs []string, f func(string) string) []string { - vsm := make([]string, len(vs)) - for i, v := range vs { - vsm[i] = f(v) - } - return vsm -} - func includesString(list []string, a string) bool { for _, b := range list { if b == a { From 43ad9a81c282022203e45ae3088b93763320bccc Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sun, 2 Sep 2018 17:15:27 +0200 Subject: [PATCH 4/8] merge setup in function that create a new git command --- main.go | 6 -- pkg/commands/git.go | 127 ++++++++++++++-------------- pkg/commands/git_test.go | 177 ++++++++++++++++++++++++++++++++------- 3 files changed, 211 insertions(+), 99 deletions(-) diff --git a/main.go b/main.go index 6193f43d3..25e55d364 100644 --- a/main.go +++ b/main.go @@ -49,11 +49,5 @@ func main() { panic(err) } - if err := app.GitCommand.SetupGit(); err != nil { - app.Log.Error(err.Error()) - fmt.Println(err) - os.Exit(1) - } - app.Gui.RunWithSubprocesses() } diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 48d51e900..6ba5f8a84 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -19,98 +19,95 @@ import ( // to check if we have a valid git repository and we get an error instead var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") -func openGitRepositoryAndWorktree() (*gogit.Repository, *gogit.Worktree, error) { - r, err := gogit.PlainOpen(".") - - if err != nil { - return nil, nil, err - } - - w, err := r.Worktree() - - if err != nil { - return nil, nil, err - } - - return r, w, nil -} - -// GitCommand is our main git interface -type GitCommand struct { - Log *logrus.Entry - OSCommand *OSCommand - Worktree *gogit.Worktree - Repo *gogit.Repository - Tr *i18n.Localizer - openGitRepositoryAndWorktree func() (*gogit.Repository, *gogit.Worktree, error) -} - -// NewGitCommand it runs git commands -func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) { - gitCommand := &GitCommand{ - Log: log, - OSCommand: osCommand, - Tr: tr, - openGitRepositoryAndWorktree: openGitRepositoryAndWorktree, - } - return gitCommand, nil -} - -// SetupGit sets git repo up -func (c *GitCommand) SetupGit() error { - fs := []func() error{ - c.verifyInGitRepo, - c.navigateToRepoRootDirectory, - c.setupRepositoryAndWorktree, - } - - for _, f := range fs { - if err := f(); err != nil { - return err - } - } - - return nil -} - -func (c *GitCommand) verifyInGitRepo() error { - if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { +func verifyInGitRepo(runCmdWithOutput func(string) (string, error)) error { + if _, err := runCmdWithOutput("git status"); err != nil { return ErrGitRepositoryInvalid } return nil } -func (c *GitCommand) navigateToRepoRootDirectory() error { +func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error { for { - f, err := os.Stat(".git") + f, err := stat(".git") if err == nil && f.IsDir() { return nil } - c.Log.Debug("going up a directory to find the root") + if !os.IsNotExist(err) { + return err + } - if err = os.Chdir(".."); err != nil { + if err = chdir(".."); err != nil { return err } } } -func (c *GitCommand) setupRepositoryAndWorktree() (err error) { - c.Repo, c.Worktree, err = c.openGitRepositoryAndWorktree() +func setupRepositoryAndWorktree(openGitRepository func(string) (*gogit.Repository, error), sLocalize func(string) string) (repository *gogit.Repository, worktree *gogit.Worktree, err error) { + repository, err = openGitRepository(".") + + if err != nil { + if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { + return nil, nil, errors.New(sLocalize("GitconfigParseErr")) + } - if err == nil { return } - if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { - return errors.New(c.Tr.SLocalize("GitconfigParseErr")) + worktree, err = repository.Worktree() + + if err != nil { + return } return } +// GitCommand is our main git interface +type GitCommand struct { + Log *logrus.Entry + OSCommand *OSCommand + Worktree *gogit.Worktree + Repo *gogit.Repository + Tr *i18n.Localizer +} + +// NewGitCommand it runs git commands +func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) { + var worktree *gogit.Worktree + var repo *gogit.Repository + + fs := []func() error{ + func() error { + return verifyInGitRepo(osCommand.RunCommandWithOutput) + }, + func() error { + return navigateToRepoRootDirectory(os.Stat, os.Chdir) + }, + func() error { + var err error + repo, worktree, err = setupRepositoryAndWorktree(gogit.PlainOpen, tr.SLocalize) + return err + }, + } + + for _, f := range fs { + if err := f(); err != nil { + return nil, err + } + } + + return &GitCommand{ + Log: log, + OSCommand: osCommand, + Tr: tr, + Worktree: worktree, + Repo: repo, + }, nil +} + // GetStashEntries stash entryies func (c *GitCommand) GetStashEntries() []StashEntry { rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'") diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 062e8ee3d..ebf4fd43d 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -3,8 +3,10 @@ package commands import ( "fmt" "io/ioutil" + "os" "os/exec" "testing" + "time" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/test" @@ -13,6 +15,39 @@ import ( 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{} +} + +func (f fileInfoMock) Name() string { + return f.name +} + +func (f fileInfoMock) Size() int64 { + return f.size +} + +func (f fileInfoMock) Mode() os.FileMode { + return f.fileMode +} + +func (f fileInfoMock) ModTime() time.Time { + return f.fileModTime +} + +func (f fileInfoMock) IsDir() bool { + return f.isDir +} + +func (f fileInfoMock) Sys() interface{} { + return f.sys +} + func newDummyLog() *logrus.Entry { log := logrus.New() log.Out = ioutil.Discard @@ -27,69 +62,155 @@ func newDummyGitCommand() *GitCommand { } } -func TestGitCommandSetupGit(t *testing.T) { +func TestVerifyInGitRepo(t *testing.T) { type scenario struct { - command func(string, ...string) *exec.Cmd - openGitRepositoryAndWorktree func() (*gogit.Repository, *gogit.Worktree, error) - test func(error) + runCmdWithOutput func(string) (string, error) + test func(error) } scenarios := []scenario{ { - func(string, ...string) *exec.Cmd { - return exec.Command("exit", "1") + func(string) (string, error) { + return "", nil }, - func() (*gogit.Repository, *gogit.Worktree, error) { - return nil, nil, nil + func(err error) { + assert.NoError(t, err) + }, + }, + { + func(string) (string, error) { + return "", ErrGitRepositoryInvalid }, func(err error) { assert.Error(t, err) assert.Equal(t, ErrGitRepositoryInvalid, err) }, }, + } + + for _, s := range scenarios { + s.test(verifyInGitRepo(s.runCmdWithOutput)) + } +} + +func TestNavigateToRepoRootDirectory(t *testing.T) { + type scenario struct { + stat func(string) (os.FileInfo, error) + chdir func(string) error + test func(error) + } + + scenarios := []scenario{ { - func(string, ...string) *exec.Cmd { - return exec.Command("echo") + func(string) (os.FileInfo, error) { + return fileInfoMock{isDir: true}, nil }, - func() (*gogit.Repository, *gogit.Worktree, error) { - return nil, nil, fmt.Errorf(`unquoted '\' must be followed by new line`) + func(string) error { + return nil }, func(err error) { - assert.Error(t, err) - assert.Contains(t, err.Error(), "gitconfig") + assert.NoError(t, err) }, }, { - func(string, ...string) *exec.Cmd { - return exec.Command("echo") + func(string) (os.FileInfo, error) { + return nil, fmt.Errorf("An error occurred") }, - func() (*gogit.Repository, *gogit.Worktree, error) { - return nil, nil, fmt.Errorf("Error from inside gogit") + func(string) error { + return nil }, func(err error) { + assert.Error(t, err) + assert.EqualError(t, err, "An error occurred") + }, + }, + { + 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") + }, + }, + { + 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") + }, + }, + } + + for _, s := range scenarios { + s.test(navigateToRepoRootDirectory(s.stat, s.chdir)) + } +} + +func TestSetupRepositoryAndWorktree(t *testing.T) { + type scenario struct { + openGitRepository func(string) (*gogit.Repository, error) + sLocalize func(string) string + test func(*gogit.Repository, *gogit.Worktree, error) + } + + scenarios := []scenario{ + { + 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") + }, + }, + { + 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") }, }, { - func(string, ...string) *exec.Cmd { - return exec.Command("echo") + func(string) (*gogit.Repository, error) { + return &gogit.Repository{}, nil }, - func() (*gogit.Repository, *gogit.Worktree, error) { - return &gogit.Repository{}, &gogit.Worktree{}, nil + func(string) string { return "" }, + func(r *gogit.Repository, w *gogit.Worktree, err error) { + assert.Error(t, err) + assert.Equal(t, gogit.ErrIsBareRepository, err) }, - func(err error) { + }, + { + 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) }, }, } for _, s := range scenarios { - gitCmd := newDummyGitCommand() - gitCmd.OSCommand.command = s.command - gitCmd.openGitRepositoryAndWorktree = s.openGitRepositoryAndWorktree - - s.test(gitCmd.SetupGit()) + s.test(setupRepositoryAndWorktree(s.openGitRepository, s.sLocalize)) } } From 06846ef3ae9aed88ff6249cc2fd5bdbe0a55bdc3 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sun, 2 Sep 2018 17:18:33 +0200 Subject: [PATCH 5/8] rename NewApp to Setup --- main.go | 2 +- pkg/app/app.go | 4 ++-- pkg/commands/git_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 25e55d364..890b69a7f 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func main() { panic(err) } - app, err := app.NewApp(appConfig) + app, err := app.Setup(appConfig) if err != nil { app.Log.Error(err.Error()) panic(err) diff --git a/pkg/app/app.go b/pkg/app/app.go index fa2415fc3..b03ec5b42 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -65,8 +65,8 @@ func newLogger(config config.AppConfigurer) *logrus.Entry { }) } -// NewApp retruns a new applications -func NewApp(config config.AppConfigurer) (*App, error) { +// Setup bootstrap a new application +func Setup(config config.AppConfigurer) (*App, error) { app := &App{ closers: []io.Closer{}, Config: config, diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index ebf4fd43d..64d3828ea 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -214,6 +214,48 @@ func TestSetupRepositoryAndWorktree(t *testing.T) { } } +func TestNewGitCommand(t *testing.T) { + actual, err := os.Getwd() + assert.NoError(t, err) + + defer func() { + assert.NoError(t, os.Chdir(actual)) + }() + + type scenario struct { + setup func() + test func(*GitCommand, error) + } + + scenarios := []scenario{ + { + func() { + assert.NoError(t, os.Chdir("/tmp")) + }, + func(gitCmd *GitCommand, err error) { + assert.Error(t, err) + assert.Equal(t, ErrGitRepositoryInvalid, err) + }, + }, + { + 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 { + s.setup() + s.test(NewGitCommand(newDummyLog(), newDummyOSCommand(), i18n.NewLocalizer(newDummyLog()))) + } +} + func TestGitCommandGetStashEntries(t *testing.T) { type scenario struct { command func(string, ...string) *exec.Cmd From 8c675780639ffb1f214c7fcfaa34c09862ccd092 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sun, 2 Sep 2018 22:46:37 +0200 Subject: [PATCH 6/8] replace fmt with errors --- pkg/commands/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 6ba5f8a84..df78a2e3a 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -17,7 +17,7 @@ import ( // ErrGitRepositoryInvalid is emitted when we run a git command in a folder // to check if we have a valid git repository and we get an error instead -var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") +var ErrGitRepositoryInvalid = errors.New("can't find a valid git repository in current directory") func verifyInGitRepo(runCmdWithOutput func(string) (string, error)) error { if _, err := runCmdWithOutput("git status"); err != nil { From df3e7abd688d17982d477f67e05670cacdff4bb4 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 4 Sep 2018 06:16:19 +0200 Subject: [PATCH 7/8] use RunCommand --- pkg/commands/git.go | 14 +++----------- pkg/commands/git_test.go | 18 +++++++++--------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index df78a2e3a..61c566780 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -15,16 +15,8 @@ import ( gogit "gopkg.in/src-d/go-git.v4" ) -// ErrGitRepositoryInvalid is emitted when we run a git command in a folder -// to check if we have a valid git repository and we get an error instead -var ErrGitRepositoryInvalid = errors.New("can't find a valid git repository in current directory") - -func verifyInGitRepo(runCmdWithOutput func(string) (string, error)) error { - if _, err := runCmdWithOutput("git status"); err != nil { - return ErrGitRepositoryInvalid - } - - return nil +func verifyInGitRepo(runCmd func(string) error) error { + return runCmd("git status") } func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error { @@ -81,7 +73,7 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) fs := []func() error{ func() error { - return verifyInGitRepo(osCommand.RunCommandWithOutput) + return verifyInGitRepo(osCommand.RunCommand) }, func() error { return navigateToRepoRootDirectory(os.Stat, os.Chdir) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 64d3828ea..777eebaec 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -64,32 +64,32 @@ func newDummyGitCommand() *GitCommand { func TestVerifyInGitRepo(t *testing.T) { type scenario struct { - runCmdWithOutput func(string) (string, error) - test func(error) + runCmd func(string) error + test func(error) } scenarios := []scenario{ { - func(string) (string, error) { - return "", nil + func(string) error { + return nil }, func(err error) { assert.NoError(t, err) }, }, { - func(string) (string, error) { - return "", ErrGitRepositoryInvalid + func(string) error { + return fmt.Errorf("fatal: Not a git repository (or any of the parent directories): .git") }, func(err error) { assert.Error(t, err) - assert.Equal(t, ErrGitRepositoryInvalid, err) + assert.Regexp(t, "fatal: .ot a git repository \\(or any of the parent directories\\): \\.git", err.Error()) }, }, } for _, s := range scenarios { - s.test(verifyInGitRepo(s.runCmdWithOutput)) + s.test(verifyInGitRepo(s.runCmd)) } } @@ -234,7 +234,7 @@ func TestNewGitCommand(t *testing.T) { }, func(gitCmd *GitCommand, err error) { assert.Error(t, err) - assert.Equal(t, ErrGitRepositoryInvalid, err) + assert.Regexp(t, "fatal: .ot a git repository \\(or any of the parent directories\\): \\.git", err.Error()) }, }, { From 172cd7c6871d47fee9fd132c3c3df0afd71bc445 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 4 Sep 2018 06:29:48 +0200 Subject: [PATCH 8/8] fix tests locally --- pkg/commands/os_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go index 5d1644a38..01173fb15 100644 --- a/pkg/commands/os_test.go +++ b/pkg/commands/os_test.go @@ -46,7 +46,7 @@ func TestOSCommandRunCommandWithOutput(t *testing.T) { { "rmdir unexisting-folder", func(output string, err error) { - assert.Regexp(t, "rmdir: .* 'unexisting-folder': .*", err.Error()) + assert.Regexp(t, "rmdir.*unexisting-folder.*", err.Error()) }, }, } @@ -66,7 +66,7 @@ func TestOSCommandRunCommand(t *testing.T) { { "rmdir unexisting-folder", func(err error) { - assert.Regexp(t, "rmdir: .* 'unexisting-folder': .*", err.Error()) + assert.Regexp(t, "rmdir.*unexisting-folder.*", err.Error()) }, }, }