From 85017d43a7738e8a7b6208314ed76a82c3fcbf32 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 21 Nov 2019 21:45:18 +1100 Subject: fix specs --- pkg/commands/branch_list_builder.go | 2 +- pkg/commands/commit_list_builder_test.go | 4 ++++ pkg/commands/git.go | 8 ++++---- pkg/commands/git_test.go | 20 ++++++++++---------- pkg/commands/loading_remotes.go | 2 +- pkg/commands/os.go | 4 +++- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/pkg/commands/branch_list_builder.go b/pkg/commands/branch_list_builder.go index c768bac35..d7a232055 100644 --- a/pkg/commands/branch_list_builder.go +++ b/pkg/commands/branch_list_builder.go @@ -47,7 +47,7 @@ func (b *BranchListBuilder) obtainCurrentBranch() *Branch { func (b *BranchListBuilder) obtainReflogBranches() []*Branch { branches := make([]*Branch, 0) - rawString, err := b.GitCommand.OSCommand.RunCommandWithOutput(`git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD`) + rawString, err := b.GitCommand.OSCommand.RunCommandWithOutput("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD") if err != nil { return branches } diff --git a/pkg/commands/commit_list_builder_test.go b/pkg/commands/commit_list_builder_test.go index cdd360ce8..a1f4a4da9 100644 --- a/pkg/commands/commit_list_builder_test.go +++ b/pkg/commands/commit_list_builder_test.go @@ -289,6 +289,10 @@ func TestCommitListBuilderGetCommits(t *testing.T) { assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args) // here's where we are returning the error return exec.Command("test") + case "branch": + assert.EqualValues(t, []string{"branch", "--contains"}, args) + // here too + return exec.Command("test") case "rev-parse": assert.EqualValues(t, []string{"rev-parse", "--short", "HEAD"}, args) // here too diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 33ef1d25f..fe0600065 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -157,7 +157,7 @@ func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filenam // GetStashEntries stash entries func (c *GitCommand) GetStashEntries() []*StashEntry { - 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) { stashEntries = append(stashEntries, stashEntryFromLine(line, i)) @@ -333,12 +333,12 @@ func (c *GitCommand) CurrentBranchName() (string, error) { branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") if err != nil || branchName == "HEAD\n" { output, err := c.OSCommand.RunCommandWithOutput("git branch --contains") - re := regexp.MustCompile(CurrentBranchNameRegex) - match := re.FindStringSubmatch(output) - branchName = match[1] if err != nil { return "", err } + re := regexp.MustCompile(CurrentBranchNameRegex) + match := re.FindStringSubmatch(output) + branchName = match[1] } return utils.TrimTrailingNewline(branchName), nil } diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 3d5e33998..4991714f1 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -58,14 +58,14 @@ func (f fileInfoMock) Sys() interface{} { func TestVerifyInGitRepo(t *testing.T) { type scenario struct { testName string - runCmd func(string) error + runCmd func(string, ...interface{}) error test func(error) } scenarios := []scenario{ { "Valid git repository", - func(string) error { + func(string, ...interface{}) error { return nil }, func(err error) { @@ -74,7 +74,7 @@ func TestVerifyInGitRepo(t *testing.T) { }, { "Not a valid git repository", - func(string) error { + func(string, ...interface{}) error { return fmt.Errorf("fatal: Not a git repository (or any of the parent directories): .git") }, func(err error) { @@ -990,7 +990,7 @@ func TestGitCommandPush(t *testing.T) { "Push with force disabled", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"push"}, args) + assert.EqualValues(t, []string{"push", "--follow-tags"}, args) return exec.Command("echo") }, @@ -1003,7 +1003,7 @@ func TestGitCommandPush(t *testing.T) { "Push with force enabled", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"push", "--force-with-lease"}, args) + assert.EqualValues(t, []string{"push", "--follow-tags", "--force-with-lease"}, args) return exec.Command("echo") }, @@ -1016,7 +1016,7 @@ func TestGitCommandPush(t *testing.T) { "Push with an error occurring", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"push"}, args) + assert.EqualValues(t, []string{"push", "--follow-tags"}, args) return exec.Command("test") }, false, @@ -1639,7 +1639,7 @@ func TestGitCommandCurrentBranchName(t *testing.T) { }, }, { - "falls back to git rev-parse if symbolic-ref fails", + "falls back to git `git branch --contains` if symbolic-ref fails", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) @@ -1647,9 +1647,9 @@ func TestGitCommandCurrentBranchName(t *testing.T) { case "symbolic-ref": assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args) return exec.Command("test") - case "rev-parse": - assert.EqualValues(t, []string{"rev-parse", "--short", "HEAD"}, args) - return exec.Command("echo", "master") + case "branch": + assert.EqualValues(t, []string{"branch", "--contains"}, args) + return exec.Command("echo", "* master") } return nil diff --git a/pkg/commands/loading_remotes.go b/pkg/commands/loading_remotes.go index d44d4c713..f4f1b4a01 100644 --- a/pkg/commands/loading_remotes.go +++ b/pkg/commands/loading_remotes.go @@ -9,7 +9,7 @@ import ( func (c *GitCommand) GetRemotes() ([]*Remote, error) { // get remote branches - remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput(`git for-each-ref --format='%(refname:strip=2)' refs/remotes`) + remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput("git for-each-ref --format='%%(refname:strip=2)' refs/remotes") if err != nil { return nil, err } diff --git a/pkg/commands/os.go b/pkg/commands/os.go index 8f49483c6..e421c4e6a 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -59,9 +59,11 @@ func (c *OSCommand) SetCommand(cmd func(string, ...string) *exec.Cmd) { } // RunCommandWithOutput wrapper around commands returning their output and error +// NOTE: because this takes a format string followed by format args, you'll need +// to escape any percentage signs via '%%'. func (c *OSCommand) RunCommandWithOutput(formatString string, formatArgs ...interface{}) (string, error) { command := formatString - if len(formatArgs) > 0 { + if formatArgs != nil { command = fmt.Sprintf(formatString, formatArgs...) } c.Log.WithField("command", command).Info("RunCommand") -- cgit v1.2.3