From ca9ce226938c20f29cf528581fae070be7e7f98c Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 27 Aug 2018 22:40:15 +0200 Subject: [PATCH 01/16] use assert in tests, rename testing method --- pkg/commands/git_test.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 9c6a17b2c..6deeee073 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -2,11 +2,11 @@ package commands import ( "io/ioutil" - "strings" "testing" "github.com/jesseduffield/lazygit/pkg/test" "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" ) func newDummyLog() *logrus.Entry { @@ -22,11 +22,11 @@ func newDummyGitCommand() *GitCommand { } } -func TestDiff(t *testing.T) { - gitCommand := newDummyGitCommand() - if err := test.GenerateRepo("lots_of_diffs.sh"); err != nil { - t.Error(err.Error()) } +func TestGitCommandDiff(t *testing.T) { + gitCommand := newDummyGitCommand() + assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) + files := []File{ { Name: "deleted_staged", @@ -110,10 +110,8 @@ func TestDiff(t *testing.T) { DisplayString: "?? master", }, } + for _, file := range files { - content := gitCommand.Diff(file) - if strings.Contains(content, "error") { - t.Error("Error: diff test failed. File: " + file.Name + ", " + content) - } + assert.NotContains(t, gitCommand.Diff(file), "error") } } From 983d0bd5868d2cd7f1aa4a793247de3dace60de4 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 27 Aug 2018 22:41:23 +0200 Subject: [PATCH 02/16] replace make --- 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 b56650e97..25cde40a1 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -46,8 +46,8 @@ func (c *GitCommand) SetupGit() { // GetStashEntries stash entryies func (c *GitCommand) GetStashEntries() []StashEntry { - stashEntries := make([]StashEntry, 0) rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'") + stashEntries := []StashEntry{} for i, line := range utils.SplitLines(rawString) { stashEntries = append(stashEntries, stashEntryFromLine(line, i)) } From 8247fd69c9ede8d829c83d41cfc847ce282b658a Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 27 Aug 2018 22:41:46 +0200 Subject: [PATCH 03/16] add test for GetStashEntries --- pkg/commands/git_test.go | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 6deeee073..e709e747a 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -2,6 +2,7 @@ package commands import ( "io/ioutil" + "os/exec" "testing" "github.com/jesseduffield/lazygit/pkg/test" @@ -22,7 +23,53 @@ func newDummyGitCommand() *GitCommand { } } +func TestGitCommandGetStashEntries(t *testing.T) { + type scenario struct { + command func(string, ...string) *exec.Cmd + 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 TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) From 49b507d2ff71d0ba1797695ed55be8217a3e1e6a Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 27 Aug 2018 23:20:01 +0200 Subject: [PATCH 04/16] replace make --- 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 25cde40a1..d02d75b4f 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -80,7 +80,7 @@ func includes(array []string, str string) bool { func (c *GitCommand) GetStatusFiles() []File { statusOutput, _ := c.GitStatus() statusStrings := utils.SplitLines(statusOutput) - files := make([]File, 0) + files := []File{} for _, statusString := range statusStrings { change := statusString[0:2] From 13f90735522d55d83c724aa949dcef3061da290e Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 27 Aug 2018 23:20:27 +0200 Subject: [PATCH 05/16] add test for GetStashEntryDiff --- pkg/commands/git_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index e709e747a..d8df7b4b0 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -70,6 +70,20 @@ func TestGitCommandGetStashEntries(t *testing.T) { } } +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 TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) From 85012dbc8f8e767076e1ea906eb2498beb6d96bf Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Mon, 27 Aug 2018 23:20:44 +0200 Subject: [PATCH 06/16] add tests for GetStatusFiles --- pkg/commands/git_test.go | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index d8df7b4b0..f77c2a3b9 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -84,6 +84,83 @@ func TestGetStashEntryDiff(t *testing.T) { 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", + }, + { + Name: "file3.txt", + HasStagedChanges: true, + HasUnstagedChanges: false, + Tracked: false, + Deleted: false, + HasMergeConflicts: false, + DisplayString: "A file3.txt", + }, + { + Name: "file2.txt", + HasStagedChanges: true, + HasUnstagedChanges: true, + Tracked: false, + Deleted: false, + HasMergeConflicts: false, + DisplayString: "AM file2.txt", + }, + { + Name: "file4.txt", + HasStagedChanges: false, + HasUnstagedChanges: true, + Tracked: false, + Deleted: false, + HasMergeConflicts: false, + DisplayString: "?? file4.txt", + }, + } + + assert.EqualValues(t, expected, files) + }, + }, + } + + for _, s := range scenarios { + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command = s.command + + s.test(gitCmd.GetStatusFiles()) + } +} + func TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) From 99840d8fc4e4e877f80280272f29394a28699190 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 28 Aug 2018 20:18:34 +0200 Subject: [PATCH 07/16] add test for StashDo and refactor StashDo method --- pkg/commands/git.go | 2 +- pkg/commands/git_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index d02d75b4f..9b3c72fd9 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -106,7 +106,7 @@ func (c *GitCommand) GetStatusFiles() []File { // StashDo modify stash 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 diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index f77c2a3b9..554ffbd1a 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -161,6 +161,18 @@ func TestGetStatusFiles(t *testing.T) { } } +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 TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) From 45fa2571287ac45045b9d6127dc0a38538b4fa4f Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 28 Aug 2018 20:24:19 +0200 Subject: [PATCH 08/16] add test for StashSave and refactor StashSave method --- pkg/commands/git.go | 2 +- pkg/commands/git_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 9b3c72fd9..5a4d910d3 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -112,7 +112,7 @@ func (c *GitCommand) StashDo(index int, method string) error { // StashSave save stash // TODO: before calling this, check if there is anything to save 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 diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 554ffbd1a..920f527a8 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -173,6 +173,18 @@ func TestGitCommandStashDo(t *testing.T) { 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 TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) From d6b4d4b063e4def1fbdeea5a90e31cdb2fb1a00b Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 28 Aug 2018 20:55:14 +0200 Subject: [PATCH 09/16] add tests for MergesStatusFiles --- pkg/commands/git.go | 4 +-- pkg/commands/git_test.go | 75 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 5a4d910d3..0cedb2150 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -121,10 +121,10 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File { return newFiles } - appendedIndexes := make([]int, 0) + appendedIndexes := []int{} // retain position of files we already could see - result := make([]File, 0) + result := []File{} for _, oldFile := range oldFiles { for newIndex, newFile := range newFiles { if oldFile.Name == newFile.Name { diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 920f527a8..2380887e1 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -185,6 +185,81 @@ func TestGitCommandStashSave(t *testing.T) { 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")) From e3ed899b20c1e1cec25245bcb32c272ad02580fd Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 28 Aug 2018 21:09:56 +0200 Subject: [PATCH 10/16] refactor MergeStatusFiles --- pkg/commands/git.go | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 0cedb2150..619db05e7 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -121,28 +121,28 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File { return newFiles } - appendedIndexes := []int{} + headResults := []File{} + tailResults := []File{} - // retain position of files we already could see - result := []File{} - for _, oldFile := range oldFiles { - for newIndex, newFile := range newFiles { + for _, newFile := range newFiles { + var isHeadResult bool + + for _, oldFile := range oldFiles { if oldFile.Name == newFile.Name { - result = append(result, newFile) - appendedIndexes = append(appendedIndexes, newIndex) + isHeadResult = true break } } - } - // append any new files to the end - for index, newFile := range newFiles { - if !includesInt(appendedIndexes, index) { - result = append(result, newFile) + if isHeadResult { + headResults = append(headResults, newFile) + continue } + + tailResults = append(tailResults, newFile) } - return result + return append(headResults, tailResults...) } func (c *GitCommand) verifyInGitRepo() { @@ -447,17 +447,6 @@ func includesString(list []string, a string) bool { 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 func (c *GitCommand) GetCommits() []Commit { pushables := c.GetCommitsToPush() From 66e5dacf5ea8e1474ec7470ffa2c0f7865947a1c Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 28 Aug 2018 21:17:18 +0200 Subject: [PATCH 11/16] fix git tests --- pkg/commands/git_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 2380887e1..8117ed1e5 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -118,6 +118,7 @@ func TestGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, DisplayString: "MM file1.txt", + Type: "other", }, { Name: "file3.txt", @@ -127,6 +128,7 @@ func TestGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, DisplayString: "A file3.txt", + Type: "other", }, { Name: "file2.txt", @@ -136,6 +138,7 @@ func TestGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, DisplayString: "AM file2.txt", + Type: "other", }, { Name: "file4.txt", @@ -145,6 +148,7 @@ func TestGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, DisplayString: "?? file4.txt", + Type: "other", }, } From dac7c90483705a4839fb0c31ecad5c806fcc43d4 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 28 Aug 2018 21:28:55 +0200 Subject: [PATCH 12/16] add cache based on Gopkg.lock checksum --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d60eadaab..16bafdb58 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -9,7 +9,7 @@ jobs: - checkout - restore_cache: keys: - - v1-pkg-cache + - pkg-cache-{{ checksum "Gopkg.lock" }} - run: name: Run gofmt -s command: | @@ -31,7 +31,7 @@ jobs: command: | bash <(curl -s https://codecov.io/bash) - save_cache: - key: v1-pkg-cache + key: pkg-cache-{{ checksum "Gopkg.lock" }} paths: - "/go/pkg" From e889a40caf3a3b3b8ac3e7bc029310249a923d34 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 29 Aug 2018 13:31:27 +0200 Subject: [PATCH 13/16] fix golint issue --- pkg/utils/utils.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 62706559e..0b6ffeddd 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -83,6 +83,7 @@ func GetProjectRoot() string { return strings.Split(dir, "lazygit")[0] + "lazygit" } +// Loader dumps a string to be displayed as a loader func Loader() string { characters := "|/-\\" now := time.Now() From cda7b374e2b95d4f6b71e896e775bc0d688cc710 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 29 Aug 2018 13:32:34 +0200 Subject: [PATCH 14/16] fix linting issues --- pkg/config/app_config.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index c85b8e5eb..0acd9d820 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -190,6 +190,7 @@ func (c *AppConfig) SaveAppState() error { return ioutil.WriteFile(filepath, marshalledAppState, 0644) } +// LoadAppState loads recorded AppState from file func (c *AppConfig) LoadAppState() error { filepath, err := prepareConfigFile("state.yml") if err != nil { @@ -205,6 +206,7 @@ func (c *AppConfig) LoadAppState() error { return yaml.Unmarshal(appStateBytes, c.AppState) } + // GetDefaultConfig returns the application default configuration func GetDefaultConfig() []byte { return []byte( `gui: From 54326907c38565d970a6d15f685abbac80fcc9ea Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 29 Aug 2018 13:34:56 +0200 Subject: [PATCH 15/16] fix linting issues --- pkg/updates/updates.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/updates/updates.go b/pkg/updates/updates.go index ad5dd57b6..5f533e5a0 100644 --- a/pkg/updates/updates.go +++ b/pkg/updates/updates.go @@ -22,7 +22,7 @@ import ( "github.com/sirupsen/logrus" ) -// Update checks for updates and does updates +// Updater checks for updates and does updates type Updater struct { Log *logrus.Entry Config config.AppConfigurer @@ -30,7 +30,7 @@ type Updater struct { Tr *i18n.Localizer } -// Updater implements the check and update methods +// Updaterer implements the check and update methods type Updaterer interface { CheckForNewUpdate() Update() @@ -78,6 +78,7 @@ func (u *Updater) getLatestVersionNumber() (string, error) { return dat["tag_name"].(string), nil } +// RecordLastUpdateCheck records last time an update check was performed func (u *Updater) RecordLastUpdateCheck() error { u.Config.GetAppState().LastUpdateCheck = time.Now().Unix() return u.Config.SaveAppState() From 1b8836e92d7d59cf99d17d5df981337f459712df Mon Sep 17 00:00:00 2001 From: antham Date: Wed, 29 Aug 2018 13:45:52 +0200 Subject: [PATCH 16/16] fix fmt issue --- pkg/config/app_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 0acd9d820..899d27fbc 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -206,7 +206,7 @@ func (c *AppConfig) LoadAppState() error { return yaml.Unmarshal(appStateBytes, c.AppState) } - // GetDefaultConfig returns the application default configuration +// GetDefaultConfig returns the application default configuration func GetDefaultConfig() []byte { return []byte( `gui: