Skip to content

Commit 4287f8a

Browse files
Fix tests and add test scenarios for amend.
1 parent 190309e commit 4287f8a

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

pkg/commands/git.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ func (c *GitCommand) usingGpg() bool {
317317
func (c *GitCommand) Commit(message string, amend bool) (*exec.Cmd, error) {
318318
amendParam := ""
319319
if amend {
320-
amendParam = "--amend"
320+
amendParam = " --amend"
321321
}
322-
command := fmt.Sprintf("git commit %s -m %s", amendParam, c.OSCommand.Quote(message))
322+
command := fmt.Sprintf("git commit%s -m %s", amendParam, c.OSCommand.Quote(message))
323323
if c.usingGpg() {
324324
return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command), nil
325325
}

pkg/commands/git_test.go

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ func TestGitCommandUpstreamDifferentCount(t *testing.T) {
561561
{
562562
"Can't retrieve pullable count",
563563
func(cmd string, args ...string) *exec.Cmd {
564-
if args[1] == "head..@{u}" {
564+
if args[1] == "HEAD..@{u}" {
565565
return exec.Command("test")
566566
}
567567

@@ -575,7 +575,7 @@ func TestGitCommandUpstreamDifferentCount(t *testing.T) {
575575
{
576576
"Retrieve pullable and pushable count",
577577
func(cmd string, args ...string) *exec.Cmd {
578-
if args[1] == "head..@{u}" {
578+
if args[1] == "HEAD..@{u}" {
579579
return exec.Command("echo", "10")
580580
}
581581

@@ -889,7 +889,76 @@ func TestGitCommandCommit(t *testing.T) {
889889
gitCmd := newDummyGitCommand()
890890
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
891891
gitCmd.OSCommand.command = s.command
892-
s.test(gitCmd.Commit("test"))
892+
s.test(gitCmd.Commit("test", false))
893+
})
894+
}
895+
}
896+
897+
func TestGitCommandCommitAmendFromFiles(t *testing.T) {
898+
type scenario struct {
899+
testName string
900+
command func(string, ...string) *exec.Cmd
901+
getGlobalGitConfig func(string) (string, error)
902+
test func(*exec.Cmd, error)
903+
}
904+
905+
scenarios := []scenario{
906+
{
907+
"Amend commit using gpg",
908+
func(cmd string, args ...string) *exec.Cmd {
909+
assert.EqualValues(t, "bash", cmd)
910+
assert.EqualValues(t, []string{"-c", `git commit --amend -m 'test'`}, args)
911+
912+
return exec.Command("echo")
913+
},
914+
func(string) (string, error) {
915+
return "true", nil
916+
},
917+
func(cmd *exec.Cmd, err error) {
918+
assert.NotNil(t, cmd)
919+
assert.Nil(t, err)
920+
},
921+
},
922+
{
923+
"Amend commit without using gpg",
924+
func(cmd string, args ...string) *exec.Cmd {
925+
assert.EqualValues(t, "git", cmd)
926+
assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args)
927+
928+
return exec.Command("echo")
929+
},
930+
func(string) (string, error) {
931+
return "false", nil
932+
},
933+
func(cmd *exec.Cmd, err error) {
934+
assert.Nil(t, cmd)
935+
assert.Nil(t, err)
936+
},
937+
},
938+
{
939+
"Amend commit without using gpg with an error",
940+
func(cmd string, args ...string) *exec.Cmd {
941+
assert.EqualValues(t, "git", cmd)
942+
assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args)
943+
944+
return exec.Command("test")
945+
},
946+
func(string) (string, error) {
947+
return "false", nil
948+
},
949+
func(cmd *exec.Cmd, err error) {
950+
assert.Nil(t, cmd)
951+
assert.Error(t, err)
952+
},
953+
},
954+
}
955+
956+
for _, s := range scenarios {
957+
t.Run(s.testName, func(t *testing.T) {
958+
gitCmd := newDummyGitCommand()
959+
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
960+
gitCmd.OSCommand.command = s.command
961+
s.test(gitCmd.Commit("test", true))
893962
})
894963
}
895964
}
@@ -1507,7 +1576,7 @@ func TestGitCommandGetCommits(t *testing.T) {
15071576

15081577
switch args[0] {
15091578
case "rev-list":
1510-
assert.EqualValues(t, []string{"rev-list", "@{u}..head", "--abbrev-commit"}, args)
1579+
assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args)
15111580
return exec.Command("echo")
15121581
case "log":
15131582
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
@@ -1534,7 +1603,7 @@ func TestGitCommandGetCommits(t *testing.T) {
15341603

15351604
switch args[0] {
15361605
case "rev-list":
1537-
assert.EqualValues(t, []string{"rev-list", "@{u}..head", "--abbrev-commit"}, args)
1606+
assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args)
15381607
return exec.Command("echo", "8a2bb0e")
15391608
case "log":
15401609
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
@@ -1577,7 +1646,7 @@ func TestGitCommandGetCommits(t *testing.T) {
15771646

15781647
switch args[0] {
15791648
case "rev-list":
1580-
assert.EqualValues(t, []string{"rev-list", "@{u}..head", "--abbrev-commit"}, args)
1649+
assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args)
15811650
return exec.Command("echo", "8a2bb0e")
15821651
case "log":
15831652
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)

pkg/gui/files_panel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (gui *Gui) handleAmendCommitPress(g *gocui.Gui, filesView *gocui.View) erro
226226
lastCommitMsg := gui.State.Commits[0].Name
227227
_, err := gui.GitCommand.Commit(lastCommitMsg, true)
228228
if err != nil {
229-
gui.createErrorPanel(g, err.Error())
229+
return gui.createErrorPanel(g, err.Error())
230230
}
231231

232232
return gui.refreshSidePanels(g)

0 commit comments

Comments
 (0)