summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-09-19 18:40:41 +1000
committerJesse Duffield <jessedduffield@gmail.com>2018-09-19 18:40:41 +1000
commit768b9453f8cf6b6bc22191803e0e440c0827a43f (patch)
treee7f283a2e0a8f54c7a730d3b2cee6b004802fcf6 /pkg
parentca715c5b23fdc20ad9b3dd983814ab9225c5fdbc (diff)
parente95b2e5f0b2aef3afcc6fa4e8b1f24d74cd1fb2a (diff)
Merge branch 'hotfix/cursor-positioning' into feature/recent-repos
Diffstat (limited to 'pkg')
-rw-r--r--pkg/commands/branch.go6
-rw-r--r--pkg/commands/commit.go26
-rw-r--r--pkg/commands/file.go36
-rw-r--r--pkg/commands/git.go210
-rw-r--r--pkg/commands/git_structs.go27
-rw-r--r--pkg/commands/git_test.go831
-rw-r--r--pkg/commands/os.go17
-rw-r--r--pkg/commands/os_default_platform.go11
-rw-r--r--pkg/commands/os_test.go26
-rw-r--r--pkg/commands/os_windows.go9
-rw-r--r--pkg/commands/stash_entry.go13
-rw-r--r--pkg/git/branch_list_builder.go32
-rw-r--r--pkg/gui/branches_panel.go27
-rw-r--r--pkg/gui/commit_message_panel.go2
-rw-r--r--pkg/gui/commits_panel.go41
-rw-r--r--pkg/gui/files_panel.go71
-rw-r--r--pkg/gui/gui.go23
-rw-r--r--pkg/gui/keybindings.go444
-rw-r--r--pkg/gui/menu_panel.go71
-rw-r--r--pkg/gui/options_menu_panel.go51
-rw-r--r--pkg/gui/stash_panel.go21
-rw-r--r--pkg/gui/status_panel.go6
-rw-r--r--pkg/gui/view_helpers.go48
-rw-r--r--pkg/i18n/dutch.go80
-rw-r--r--pkg/i18n/english.go56
-rw-r--r--pkg/i18n/polish.go82
-rw-r--r--pkg/utils/utils.go105
-rw-r--r--pkg/utils/utils_test.go179
28 files changed, 2120 insertions, 431 deletions
diff --git a/pkg/commands/branch.go b/pkg/commands/branch.go
index 13c26e766..19553a26b 100644
--- a/pkg/commands/branch.go
+++ b/pkg/commands/branch.go
@@ -14,9 +14,9 @@ type Branch struct {
Recency string
}
-// GetDisplayString returns the dispaly string of branch
-func (b *Branch) GetDisplayString() string {
- return utils.WithPadding(b.Recency, 4) + utils.ColoredString(b.Name, b.GetColor())
+// GetDisplayStrings returns the dispaly string of branch
+func (b *Branch) GetDisplayStrings() []string {
+ return []string{b.Recency, utils.ColoredString(b.Name, b.GetColor())}
}
// GetColor branch color
diff --git a/pkg/commands/commit.go b/pkg/commands/commit.go
new file mode 100644
index 000000000..37c3e9525
--- /dev/null
+++ b/pkg/commands/commit.go
@@ -0,0 +1,26 @@
+package commands
+
+import (
+ "github.com/fatih/color"
+)
+
+// Commit : A git commit
+type Commit struct {
+ Sha string
+ Name string
+ Pushed bool
+ DisplayString string
+}
+
+func (c *Commit) GetDisplayStrings() []string {
+ red := color.New(color.FgRed)
+ yellow := color.New(color.FgYellow)
+ white := color.New(color.FgWhite)
+
+ shaColor := yellow
+ if c.Pushed {
+ shaColor = red
+ }
+
+ return []string{shaColor.Sprint(c.Sha), white.Sprint(c.Name)}
+}
diff --git a/pkg/commands/file.go b/pkg/commands/file.go
new file mode 100644
index 000000000..8fcd9aff9
--- /dev/null
+++ b/pkg/commands/file.go
@@ -0,0 +1,36 @@
+package commands
+
+import "github.com/fatih/color"
+
+// File : A file from git status
+// duplicating this for now
+type File struct {
+ Name string
+ HasStagedChanges bool
+ HasUnstagedChanges bool
+ Tracked bool
+ Deleted bool
+ HasMergeConflicts bool
+ DisplayString string
+ Type string // one of 'file', 'directory', and 'other'
+}
+
+// GetDisplayStrings returns the display string of a file
+func (f *File) GetDisplayStrings() []string {
+ // potentially inefficient to be instantiating these color
+ // objects with each render
+ red := color.New(color.FgRed)
+ green := color.New(color.FgGreen)
+ if !f.Tracked && !f.HasStagedChanges {
+ return []string{red.Sprint(f.DisplayString)}
+ }
+
+ output := green.Sprint(f.DisplayString[0:1])
+ output += red.Sprint(f.DisplayString[1:3])
+ if f.HasUnstagedChanges {
+ output += red.Sprint(f.Name)
+ } else {
+ output += green.Sprint(f.Name)
+ }
+ return []string{output}
+}
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index 61c566780..017bd815f 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -7,7 +7,6 @@ import (
"os/exec"
"strings"
- "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
@@ -59,11 +58,13 @@ func setupRepositoryAndWorktree(openGitRepository func(string) (*gogit.Repositor
// 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
+ getGlobalGitConfig func(string) (string, error)
+ getLocalGitConfig func(string) (string, error)
}
// NewGitCommand it runs git commands
@@ -92,26 +93,28 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer)
}
return &GitCommand{
- Log: log,
- OSCommand: osCommand,
- Tr: tr,
- Worktree: worktree,
- Repo: repo,
+ Log: log,
+ OSCommand: osCommand,
+ Tr: tr,
+ Worktree: worktree,
+ Repo: repo,
+ getGlobalGitConfig: gitconfig.Global,
+ getLocalGitConfig: gitconfig.Local,
}, nil
}
// GetStashEntries stash entryies
-func (c *GitCommand) GetStashEntries() []StashEntry {
+func (c *GitCommand) GetStashEntries() []*StashEntry {
rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'")
- stashEntries := []StashEntry{}
+ stashEntries := []*StashEntry{}
for i, line := range utils.SplitLines(rawString) {
stashEntries = append(stashEntries, stashEntryFromLine(line, i))
}
return stashEntries
}
-func stashEntryFromLine(line string, index int) StashEntry {
- return StashEntry{
+func stashEntryFromLine(line string, index int) *StashEntry {
+ return &StashEntry{
Name: line,
Index: index,
DisplayString: line,
@@ -123,33 +126,26 @@ func (c *GitCommand) GetStashEntryDiff(index int) (string, error) {
return c.OSCommand.RunCommandWithOutput("git stash show -p --color stash@{" + fmt.Sprint(index) + "}")
}
-func includes(array []string, str string) bool {
- for _, arrayStr := range array {
- if arrayStr == str {
- return true
- }
- }
- return false
-}
-
// GetStatusFiles git status files
-func (c *GitCommand) GetStatusFiles() []File {
+func (c *GitCommand) GetStatusFiles() []*File {
statusOutput, _ := c.GitStatus()
statusStrings := utils.SplitLines(statusOutput)
- files := []File{}
+ files := []*File{}
for _, statusString := range statusStrings {
change := statusString[0:2]
stagedChange := change[0:1]
unstagedChange := statusString[1:2]
filename := c.OSCommand.Unquote(statusString[3:])
- tracked := !includes([]string{"??", "A ", "AM"}, change)
- file := File{
+ _, untracked := map[string]bool{"??": true, "A ": true, "AM": true}[change]
+ _, hasNoStagedChanges := map[string]bool{" ": true, "U": true, "?": true}[stagedChange]
+
+ file := &File{
Name: filename,
DisplayString: statusString,
- HasStagedChanges: !includes([]string{" ", "U", "?"}, stagedChange),
+ HasStagedChanges: !hasNoStagedChanges,
HasUnstagedChanges: unstagedChange != " ",
- Tracked: tracked,
+ Tracked: !untracked,
Deleted: unstagedChange == "D" || stagedChange == "D",
HasMergeConflicts: change == "UU",
Type: c.OSCommand.FileType(filename),
@@ -172,33 +168,42 @@ func (c *GitCommand) StashSave(message string) error {
}
// MergeStatusFiles merge status files
-func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File {
+func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*File) []*File {
if len(oldFiles) == 0 {
return newFiles
}
- headResults := []File{}
- tailResults := []File{}
+ appendedIndexes := []int{}
- for _, newFile := range newFiles {
- var isHeadResult bool
-
- for _, oldFile := range oldFiles {
+ // retain position of files we already could see
+ result := []*File{}
+ for _, oldFile := range oldFiles {
+ for newIndex, newFile := range newFiles {
if oldFile.Name == newFile.Name {
- isHeadResult = true
+ result = append(result, newFile)
+ appendedIndexes = append(appendedIndexes, newIndex)
break
}
}
+ }
- if isHeadResult {
- headResults = append(headResults, newFile)
- continue
+ // append any new files to the end
+ for index, newFile := range newFiles {
+ if !includesInt(appendedIndexes, index) {
+ result = append(result, newFile)
}
-
- tailResults = append(tailResults, newFile)
}
- return append(headResults, tailResults...)
+ return result
+}
+
+func includesInt(list []int, a int) bool {
+ for _, b := range list {
+ if b == a {
+ return true
+ }
+ }
+ return false
}
// GetBranchName branch name
@@ -230,14 +235,14 @@ func (c *GitCommand) UpstreamDifferenceCount() (string, string) {
func (c *GitCommand) GetCommitsToPush() []string {
pushables, err := c.OSCommand.RunCommandWithOutput("git rev-list @{u}..head --abbrev-commit")
if err != nil {
- return make([]string, 0)
+ return []string{}
}
return utils.SplitLines(pushables)
}
// RenameCommit renames the topmost commit with the given name
func (c *GitCommand) RenameCommit(name string) error {
- return c.OSCommand.RunCommand("git commit --allow-empty --amend -m " + c.OSCommand.Quote(name))
+ return c.OSCommand.RunCommand(fmt.Sprintf("git commit --allow-empty --amend -m %s", c.OSCommand.Quote(name)))
}
// Fetch fetch git repo
@@ -247,23 +252,23 @@ func (c *GitCommand) Fetch() error {
// ResetToCommit reset to commit
func (c *GitCommand) ResetToCommit(sha string) error {
- return c.OSCommand.RunCommand("git reset " + sha)
+ return c.OSCommand.RunCommand(fmt.Sprintf("git reset %s", sha))
}
// NewBranch create new branch
func (c *GitCommand) NewBranch(name string) error {
- return c.OSCommand.RunCommand("git checkout -b " + name)
+ return c.OSCommand.RunCommand(fmt.Sprintf("git checkout -b %s", name))
}
// DeleteBranch delete branch
func (c *GitCommand) DeleteBranch(branch string, force bool) error {
- var command string
+ command := "git branch -d"
+
if force {
- command = "git branch -D "
- } else {
- command = "git branch -d "
+ command = "git branch -D"
}
- return c.OSCommand.RunCommand(command + branch)
+
+ return c.OSCommand.RunCommand(fmt.Sprintf("%s %s", command, branch))
}
// ListStash list stash
@@ -273,7 +278,7 @@ func (c *GitCommand) ListStash() (string, error) {
// Merge merge
func (c *GitCommand) Merge(branchName string) error {
- return c.OSCommand.RunCommand("git merge --no-edit " + branchName)
+ return c.OSCommand.RunCommand(fmt.Sprintf("git merge --no-edit %s", branchName))
}
// AbortMerge abort merge
@@ -281,96 +286,91 @@ func (c *GitCommand) AbortMerge() error {
return c.OSCommand.RunCommand("git merge --abort")
}
-// UsingGpg tells us whether the user has gpg enabled so that we can know
+// usingGpg tells us whether the user has gpg enabled so that we can know
// whether we need to run a subprocess to allow them to enter their password
-func (c *GitCommand) UsingGpg() bool {
- gpgsign, _ := gitconfig.Global("commit.gpgsign")
+func (c *GitCommand) usingGpg() bool {
+ gpgsign, _ := c.getLocalGitConfig("commit.gpgsign")
if gpgsign == "" {
- gpgsign, _ = gitconfig.Local("commit.gpgsign")
+ gpgsign, _ = c.getGlobalGitConfig("commit.gpgsign")
}
- if gpgsign == "" {
- return false
- }
- return true
+ value := strings.ToLower(gpgsign)
+
+ return value == "true" || value == "1" || value == "yes" || value == "on"
}
-// Commit commit to git
-func (c *GitCommand) Commit(g *gocui.Gui, message string) (*exec.Cmd, error) {
- command := "git commit -m " + c.OSCommand.Quote(message)
- if c.UsingGpg() {
+// Commit commits to git
+func (c *GitCommand) Commit(message string) (*exec.Cmd, error) {
+ command := fmt.Sprintf("git commit -m %s", c.OSCommand.Quote(message))
+ if c.usingGpg() {
return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command), nil
}
+
return nil, c.OSCommand.RunCommand(command)
}
-// Pull pull from repo
+// Pull pulls from repo
func (c *GitCommand) Pull() error {
return c.OSCommand.RunCommand("git pull --no-edit")
}
-// Push push to a branch
+// Push pushes to a branch
func (c *GitCommand) Push(branchName string, force bool) error {
forceFlag := ""
if force {
forceFlag = "--force-with-lease "
}
- return c.OSCommand.RunCommand("git push " + forceFlag + "-u origin " + branchName)
+
+ return c.OSCommand.RunCommand(fmt.Sprintf("git push %s -u origin %s", forceFlag, branchName))
}
// SquashPreviousTwoCommits squashes a commit down to the one below it
// retaining the message of the higher commit
func (c *GitCommand) SquashPreviousTwoCommits(message string) error {
// TODO: test this
- err := c.OSCommand.RunCommand("git reset --soft HEAD^")
- if err != nil {
+ if err := c.OSCommand.RunCommand("git reset --soft HEAD^"); err != nil {
return err
}
// TODO: if password is required, we need to return a subprocess
- return c.OSCommand.RunCommand("git commit --amend -m " + c.OSCommand.Quote(message))
+ return c.OSCommand.RunCommand(fmt.Sprintf("git commit --amend -m %s", c.OSCommand.Quote(message)))
}
// SquashFixupCommit squashes a 'FIXUP' commit into the commit beneath it,
// retaining the commit message of the lower commit
func (c *GitCommand) SquashFixupCommit(branchName string, shaValue string) error {
- var err error
commands := []string{
- "git checkout -q " + shaValue,
- "git reset --soft " + shaValue + "^",
- "git commit --amend -C " + shaValue + "^",
- "git rebase --onto HEAD " + shaValue + " " + branchName,
+ fmt.Sprintf("git checkout -q %s", shaValue),
+ fmt.Sprintf("git reset --soft %s^", shaValue),
+ fmt.Sprintf("git commit --amend -C %s^", shaValue),
+ fmt.Sprintf("git rebase --onto HEAD %s %s", shaValue, branchName),
}
- ret := ""
for _, command := range commands {
c.Log.Info(command)
- output, err := c.OSCommand.RunCommandWithOutput(command)
- ret += output
- if err != nil {
+
+ if output, err := c.OSCommand.RunCommandWithOutput(command); err != nil {
+ ret := output
+ // We are already in an error state here so we're just going to append
+ // the output of these commands
+ output, _ := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git branch -d %s", shaValue))
+ ret += output
+ output, _ = c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git checkout %s", branchName))
+ ret += output
+
c.Log.Info(ret)
- break
+ return errors.New(ret)
}
}
- if err != nil {
- // We are already in an error state here so we're just going to append
- // the output of these commands
- output, _ := c.OSCommand.RunCommandWithOutput("git branch -d " + shaValue)
- ret += output
- output, _ = c.OSCommand.RunCommandWithOutput("git checkout " + branchName)
- ret += output
- }
- if err != nil {
- return errors.New(ret)
- }
+
return nil
}
-// CatFile obtain the contents of a file
+// CatFile obtains the content of a file
func (c *GitCommand) CatFile(fileName string) (string, error) {
- return c.OSCommand.RunCommandWithOutput("cat " + c.OSCommand.Quote(fileName))
+ return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("cat %s", c.OSCommand.Quote(fileName)))
}
// StageFile stages a file
func (c *GitCommand) StageFile(fileName string) error {
- return c.OSCommand.RunCommand("git add " + c.OSCommand.Quote(fileName))
+ return c.OSCommand.RunCommand(fmt.Sprintf("git add %s", c.OSCommand.Quote(fileName)))
}
// StageAll stages all files
@@ -385,13 +385,11 @@ func (c *GitCommand) UnstageAll() error {
// UnStageFile unstages a file
func (c *GitCommand) UnStageFile(fileName string, tracked bool) error {
- var command string
+ command := "git rm --cached %s"
if tracked {
- command = "git reset HEAD "
- } else {
- command = "git rm --cached "
+ command = "git reset HEAD %s"
}
- return c.OSCommand.RunCommand(command + c.OSCommand.Quote(fileName))
+ return c.OSCommand.RunCommand(fmt.Sprintf(command, c.OSCommand.Quote(fileName)))
}
// GitStatus returns the plaintext short status of the repo
@@ -409,7 +407,7 @@ func (c *GitCommand) IsInMergeState() (bool, error) {
}
// RemoveFile directly
-func (c *GitCommand) RemoveFile(file File) error {
+func (c *GitCommand) RemoveFile(file *File) error {
// if the file isn't tracked, we assume you want to delete it
if file.HasStagedChanges {
if err := c.OSCommand.RunCommand("git reset -- " + file.Name); err != nil {
@@ -465,17 +463,17 @@ func includesString(list []string, a string) bool {
}
// GetCommits obtains the commits of the current branch
-func (c *GitCommand) GetCommits() []Commit {
+func (c *GitCommand) GetCommits() []*Commit {
pushables := c.GetCommitsToPush()
log := c.GetLog()
- commits := make([]Commit, 0)
+ commits := []*Commit{}
// now we can split it up and turn it into commits
lines := utils.SplitLines(log)
for _, line := range lines {
splitLine := strings.Split(line, " ")
sha := splitLine[0]
pushed := includesString(pushables, sha)
- commits = append(commits, Commit{
+ commits = append(commits, &Commit{
Sha: sha,
Name: strings.Join(splitLine[1:], " "),
Pushed: pushed,
@@ -513,7 +511,7 @@ func (c *GitCommand) Show(sha string) string {
}
// Diff returns the diff of a file
-func (c *GitCommand) Diff(file File) string {
+func (c *GitCommand) Diff(file *File) string {
cachedArg := ""
fileName := c.OSCommand.Quote(file.Name)
if file.HasStagedChanges && !file.HasUnstagedChanges {
diff --git a/pkg/commands/git_structs.go b/pkg/commands/git_structs.go
index 2b3b3f2db..1590ce32e 100644
--- a/pkg/commands/git_structs.go
+++ b/pkg/commands/git_structs.go
@@ -1,32 +1,5 @@
package commands
-// File : A staged/unstaged file
-type File struct {
- Name string
- HasStagedChanges bool
- HasUnstagedChanges bool
- Tracked bool
- Deleted bool
- HasMergeConflicts bool
- DisplayString string
- Type string // one of 'file', 'directory', and 'other'
-}
-
-// Commit : A git commit
-type Commit struct {
- Sha string
- Name string
- Pushed bool
- DisplayString string
-}
-
-// StashEntry : A git stash entry
-type StashEntry struct {
- Index int
- Name string
- DisplayString string
-}
-
// Conflict : A git conflict with a start middle and end corresponding to line
// numbers in the file where the conflict bars appear
type Conflict struct {
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index 777eebaec..d1c27cd30 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -56,20 +56,24 @@ func newDummyLog() *logrus.Entry {
func newDummyGitCommand() *GitCommand {
return &GitCommand{
- Log: newDummyLog(),
- OSCommand: newDummyOSCommand(),
- Tr: i18n.NewLocalizer(newDummyLog()),
+ Log: newDummyLog(),
+ OSCommand: newDummyOSCommand(),
+ Tr: i18n.NewLocalizer(newDummyLog()),
+ getGlobalGitConfig: func(string) (string, error) { return "", nil },
+ getLocalGitConfig: func(string) (string, error) { return "", nil },
}
}
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 +82,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 +94,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 +122,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 +135,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 +150,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 +166,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 +179,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 +190,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 +201,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 +211,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 +233,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 +250,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,32 +264,37 @@ 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")
},
- func(entries []StashEntry) {
+ func(entries []*StashEntry) {
assert.Len(t, entries, 0)
},
},
{
+ "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")
},
- func(entries []StashEntry) {
- expected := []StashEntry{
+ func(entries []*StashEntry) {
+ expected := []*StashEntry{
{
0,
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
@@ -296,14 +314,16 @@ func TestGitCommandGetStashEntries(t *testing.T) {
}