1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-27 12:32:37 +02:00

Merge pull request from antham/master

Add tests to pkg/commands/git - Part 1
This commit is contained in:
Jesse Duffield 2018-08-29 20:25:39 +10:00 committed by GitHub
commit 3f26ddc06f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 267 additions and 39 deletions
.circleci
pkg/commands

@ -9,7 +9,7 @@ jobs:
- checkout - checkout
- restore_cache: - restore_cache:
keys: keys:
- v1-pkg-cache - pkg-cache-{{ checksum "Gopkg.lock" }}
- run: - run:
name: Run gofmt -s name: Run gofmt -s
command: | command: |
@ -31,7 +31,7 @@ jobs:
command: | command: |
bash <(curl -s https://codecov.io/bash) bash <(curl -s https://codecov.io/bash)
- save_cache: - save_cache:
key: v1-pkg-cache key: pkg-cache-{{ checksum "Gopkg.lock" }}
paths: paths:
- "/go/pkg" - "/go/pkg"

@ -46,8 +46,8 @@ func (c *GitCommand) SetupGit() {
// GetStashEntries stash entryies // GetStashEntries stash entryies
func (c *GitCommand) GetStashEntries() []StashEntry { func (c *GitCommand) GetStashEntries() []StashEntry {
stashEntries := make([]StashEntry, 0)
rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'") rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'")
stashEntries := []StashEntry{}
for i, line := range utils.SplitLines(rawString) { for i, line := range utils.SplitLines(rawString) {
stashEntries = append(stashEntries, stashEntryFromLine(line, i)) stashEntries = append(stashEntries, stashEntryFromLine(line, i))
} }
@ -80,7 +80,7 @@ func includes(array []string, str string) bool {
func (c *GitCommand) GetStatusFiles() []File { func (c *GitCommand) GetStatusFiles() []File {
statusOutput, _ := c.GitStatus() statusOutput, _ := c.GitStatus()
statusStrings := utils.SplitLines(statusOutput) statusStrings := utils.SplitLines(statusOutput)
files := make([]File, 0) files := []File{}
for _, statusString := range statusStrings { for _, statusString := range statusStrings {
change := statusString[0:2] change := statusString[0:2]
@ -106,13 +106,13 @@ func (c *GitCommand) GetStatusFiles() []File {
// StashDo modify stash // StashDo modify stash
func (c *GitCommand) StashDo(index int, method string) error { func (c *GitCommand) StashDo(index int, method string) error {
return c.OSCommand.RunCommand("git stash " + method + " stash@{" + fmt.Sprint(index) + "}") return c.OSCommand.RunCommand(fmt.Sprintf("git stash %s stash@{%d}", method, index))
} }
// StashSave save stash // StashSave save stash
// TODO: before calling this, check if there is anything to save // TODO: before calling this, check if there is anything to save
func (c *GitCommand) StashSave(message string) error { func (c *GitCommand) StashSave(message string) error {
return c.OSCommand.RunCommand("git stash save " + c.OSCommand.Quote(message)) return c.OSCommand.RunCommand(fmt.Sprintf("git stash save %s", c.OSCommand.Quote(message)))
} }
// MergeStatusFiles merge status files // MergeStatusFiles merge status files
@ -121,28 +121,28 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File {
return newFiles return newFiles
} }
appendedIndexes := make([]int, 0) headResults := []File{}
tailResults := []File{}
// retain position of files we already could see for _, newFile := range newFiles {
result := make([]File, 0) var isHeadResult bool
for _, oldFile := range oldFiles {
for newIndex, newFile := range newFiles { for _, oldFile := range oldFiles {
if oldFile.Name == newFile.Name { if oldFile.Name == newFile.Name {
result = append(result, newFile) isHeadResult = true
appendedIndexes = append(appendedIndexes, newIndex)
break break
} }
} }
}
// append any new files to the end if isHeadResult {
for index, newFile := range newFiles { headResults = append(headResults, newFile)
if !includesInt(appendedIndexes, index) { continue
result = append(result, newFile)
} }
tailResults = append(tailResults, newFile)
} }
return result return append(headResults, tailResults...)
} }
func (c *GitCommand) verifyInGitRepo() { func (c *GitCommand) verifyInGitRepo() {
@ -447,17 +447,6 @@ func includesString(list []string, a string) bool {
return false return false
} }
// not sure how to genericise this because []interface{} doesn't accept e.g.
// []int arguments
func includesInt(list []int, a int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// GetCommits obtains the commits of the current branch // GetCommits obtains the commits of the current branch
func (c *GitCommand) GetCommits() []Commit { func (c *GitCommand) GetCommits() []Commit {
pushables := c.GetCommitsToPush() pushables := c.GetCommitsToPush()

@ -2,11 +2,12 @@ package commands
import ( import (
"io/ioutil" "io/ioutil"
"strings" "os/exec"
"testing" "testing"
"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"
) )
func newDummyLog() *logrus.Entry { func newDummyLog() *logrus.Entry {
@ -22,11 +23,251 @@ func newDummyGitCommand() *GitCommand {
} }
} }
func TestDiff(t *testing.T) { func TestGitCommandGetStashEntries(t *testing.T) {
gitCommand := newDummyGitCommand() type scenario struct {
if err := test.GenerateRepo("lots_of_diffs.sh"); err != nil { command func(string, ...string) *exec.Cmd
t.Error(err.Error()) test func([]StashEntry)
} }
scenarios := []scenario{
{
func(string, ...string) *exec.Cmd {
return exec.Command("echo")
},
func(entries []StashEntry) {
assert.Len(t, entries, 0)
},
},
{
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{
{
0,
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
"WIP on add-pkg-commands-test: 55c6af2 increase parallel build",
},
{
1,
"WIP on master: bb86a3f update github template",
"WIP on master: bb86a3f update github template",
},
}
assert.Len(t, entries, 2)
assert.EqualValues(t, expected, entries)
},
},
}
for _, s := range scenarios {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
s.test(gitCmd.GetStashEntries())
}
}
func TestGetStashEntryDiff(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"stash", "show", "-p", "--color", "stash@{1}"}, args)
return exec.Command("echo")
}
_, err := gitCmd.GetStashEntryDiff(1)
assert.NoError(t, err)
}
func TestGetStatusFiles(t *testing.T) {
type scenario struct {
command func(string, ...string) *exec.Cmd
test func([]File)
}
scenarios := []scenario{
{
func(cmd string, args ...string) *exec.Cmd {
return exec.Command("echo")
},
func(files []File) {
assert.Len(t, files, 0)
},
},
{
func(cmd string, args ...string) *exec.Cmd {
return exec.Command(
"echo",
"MM file1.txt\nA file3.txt\nAM file2.txt\n?? file4.txt",
)
},
func(files []File) {
assert.Len(t, files, 4)
expected := []File{
{
Name: "file1.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: true,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "MM file1.txt",
Type: "other",
},
{
Name: "file3.txt",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "A file3.txt",
Type: "other",
},
{
Name: "file2.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "AM file2.txt",
Type: "other",
},
{
Name: "file4.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Deleted: false,
HasMergeConflicts: false,
DisplayString: "?? file4.txt",
Type: "other",
},
}
assert.EqualValues(t, expected, files)
},
},
}
for _, s := range scenarios {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command
s.test(gitCmd.GetStatusFiles())
}
}
func TestGitCommandStashDo(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"stash", "drop", "stash@{1}"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.StashDo(1, "drop"))
}
func TestGitCommandStashSave(t *testing.T) {
gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"stash", "save", "A stash message"}, args)
return exec.Command("echo")
}
assert.NoError(t, gitCmd.StashSave("A stash message"))
}
func TestGitCommandMergeStatusFiles(t *testing.T) {
type scenario struct {
oldFiles []File
newFiles []File
test func([]File)
}
scenarios := []scenario{
{
[]File{},
[]File{
{
Name: "new_file.txt",
},
},
func(files []File) {
expected := []File{
{
Name: "new_file.txt",
},
}
assert.Len(t, files, 1)
assert.EqualValues(t, expected, files)
},
},
{
[]File{
{
Name: "new_file1.txt",
},
{
Name: "new_file2.txt",
},
{
Name: "new_file3.txt",
},
},
[]File{
{
Name: "new_file4.txt",
},
{
Name: "new_file5.txt",
},
{
Name: "new_file1.txt",
},
},
func(files []File) {
expected := []File{
{
Name: "new_file1.txt",
},
{
Name: "new_file4.txt",
},
{
Name: "new_file5.txt",
},
}
assert.Len(t, files, 3)
assert.EqualValues(t, expected, files)
},
},
}
for _, s := range scenarios {
gitCmd := newDummyGitCommand()
s.test(gitCmd.MergeStatusFiles(s.oldFiles, s.newFiles))
}
}
func TestGitCommandDiff(t *testing.T) {
gitCommand := newDummyGitCommand()
assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))
files := []File{ files := []File{
{ {
Name: "deleted_staged", Name: "deleted_staged",
@ -110,10 +351,8 @@ func TestDiff(t *testing.T) {
DisplayString: "?? master", DisplayString: "?? master",
}, },
} }
for _, file := range files { for _, file := range files {
content := gitCommand.Diff(file) assert.NotContains(t, gitCommand.Diff(file), "error")
if strings.Contains(content, "error") {
t.Error("Error: diff test failed. File: " + file.Name + ", " + content)
}
} }
} }