mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-31 14:24:25 +03:00
use RunCommand
This commit is contained in:
@ -15,16 +15,8 @@ import (
|
|||||||
gogit "gopkg.in/src-d/go-git.v4"
|
gogit "gopkg.in/src-d/go-git.v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrGitRepositoryInvalid is emitted when we run a git command in a folder
|
func verifyInGitRepo(runCmd func(string) error) error {
|
||||||
// to check if we have a valid git repository and we get an error instead
|
return runCmd("git status")
|
||||||
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 navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error {
|
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{
|
fs := []func() error{
|
||||||
func() error {
|
func() error {
|
||||||
return verifyInGitRepo(osCommand.RunCommandWithOutput)
|
return verifyInGitRepo(osCommand.RunCommand)
|
||||||
},
|
},
|
||||||
func() error {
|
func() error {
|
||||||
return navigateToRepoRootDirectory(os.Stat, os.Chdir)
|
return navigateToRepoRootDirectory(os.Stat, os.Chdir)
|
||||||
|
@ -64,32 +64,32 @@ func newDummyGitCommand() *GitCommand {
|
|||||||
|
|
||||||
func TestVerifyInGitRepo(t *testing.T) {
|
func TestVerifyInGitRepo(t *testing.T) {
|
||||||
type scenario struct {
|
type scenario struct {
|
||||||
runCmdWithOutput func(string) (string, error)
|
runCmd func(string) error
|
||||||
test func(error)
|
test func(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarios := []scenario{
|
scenarios := []scenario{
|
||||||
{
|
{
|
||||||
func(string) (string, error) {
|
func(string) error {
|
||||||
return "", nil
|
return nil
|
||||||
},
|
},
|
||||||
func(err error) {
|
func(err error) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func(string) (string, error) {
|
func(string) error {
|
||||||
return "", ErrGitRepositoryInvalid
|
return fmt.Errorf("fatal: Not a git repository (or any of the parent directories): .git")
|
||||||
},
|
},
|
||||||
func(err error) {
|
func(err error) {
|
||||||
assert.Error(t, err)
|
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 {
|
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) {
|
func(gitCmd *GitCommand, err error) {
|
||||||
assert.Error(t, err)
|
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())
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user