Skip to content

Commit

Permalink
fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Nov 21, 2019
1 parent f905b27 commit 67a4462
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/branch_list_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/commit_list_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/commands/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
},
Expand All @@ -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")
},
Expand All @@ -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,
Expand Down Expand Up @@ -1639,17 +1639,17 @@ 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)

switch args[0] {
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
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/loading_remotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/commands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 67a4462

Please sign in to comment.