mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
pkg/git : add tests for SetupGit
This commit is contained in:
@ -19,21 +19,39 @@ import (
|
|||||||
// to check if we have a valid git repository and we get an error instead
|
// 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 = 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
|
// GitCommand is our main git interface
|
||||||
type GitCommand struct {
|
type GitCommand struct {
|
||||||
Log *logrus.Entry
|
Log *logrus.Entry
|
||||||
OSCommand *OSCommand
|
OSCommand *OSCommand
|
||||||
Worktree *gogit.Worktree
|
Worktree *gogit.Worktree
|
||||||
Repo *gogit.Repository
|
Repo *gogit.Repository
|
||||||
Tr *i18n.Localizer
|
Tr *i18n.Localizer
|
||||||
|
openGitRepositoryAndWorktree func() (*gogit.Repository, *gogit.Worktree, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGitCommand it runs git commands
|
// NewGitCommand it runs git commands
|
||||||
func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) {
|
func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) {
|
||||||
gitCommand := &GitCommand{
|
gitCommand := &GitCommand{
|
||||||
Log: log,
|
Log: log,
|
||||||
OSCommand: osCommand,
|
OSCommand: osCommand,
|
||||||
Tr: tr,
|
Tr: tr,
|
||||||
|
openGitRepositoryAndWorktree: openGitRepositoryAndWorktree,
|
||||||
}
|
}
|
||||||
return gitCommand, nil
|
return gitCommand, nil
|
||||||
}
|
}
|
||||||
@ -43,7 +61,7 @@ func (c *GitCommand) SetupGit() error {
|
|||||||
fs := []func() error{
|
fs := []func() error{
|
||||||
c.verifyInGitRepo,
|
c.verifyInGitRepo,
|
||||||
c.navigateToRepoRootDirectory,
|
c.navigateToRepoRootDirectory,
|
||||||
c.setupWorktree,
|
c.setupRepositoryAndWorktree,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
@ -55,6 +73,44 @@ func (c *GitCommand) SetupGit() error {
|
|||||||
return nil
|
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
|
// GetStashEntries stash entryies
|
||||||
func (c *GitCommand) GetStashEntries() []StashEntry {
|
func (c *GitCommand) GetStashEntries() []StashEntry {
|
||||||
rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'")
|
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...)
|
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
|
// GetBranchName branch name
|
||||||
func (c *GitCommand) GetBranchName() (string, error) {
|
func (c *GitCommand) GetBranchName() (string, error) {
|
||||||
return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
|
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`
|
// ResetHard does the equivalent of `git reset --hard HEAD`
|
||||||
func (c *GitCommand) ResetHard() error {
|
func (c *GitCommand) ResetHard() error {
|
||||||
return c.Worktree.Reset(&gogit.ResetOptions{Mode: gogit.HardReset})
|
return c.Worktree.Reset(&gogit.ResetOptions{Mode: gogit.HardReset})
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/i18n"
|
||||||
"github.com/jesseduffield/lazygit/pkg/test"
|
"github.com/jesseduffield/lazygit/pkg/test"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
gogit "gopkg.in/src-d/go-git.v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newDummyLog() *logrus.Entry {
|
func newDummyLog() *logrus.Entry {
|
||||||
@ -20,6 +23,73 @@ func newDummyGitCommand() *GitCommand {
|
|||||||
return &GitCommand{
|
return &GitCommand{
|
||||||
Log: newDummyLog(),
|
Log: newDummyLog(),
|
||||||
OSCommand: newDummyOSCommand(),
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user