mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-11 13:53:25 +02:00
Merge pull request #282 from antham/add-tests-part-4
Add tests to pkg/commands/git - Part 4
This commit is contained in:
commit
eb4b5cd43b
@ -327,49 +327,43 @@ func (c *GitCommand) Push(branchName string, force bool) error {
|
|||||||
// retaining the message of the higher commit
|
// retaining the message of the higher commit
|
||||||
func (c *GitCommand) SquashPreviousTwoCommits(message string) error {
|
func (c *GitCommand) SquashPreviousTwoCommits(message string) error {
|
||||||
// TODO: test this
|
// TODO: test this
|
||||||
err := c.OSCommand.RunCommand("git reset --soft HEAD^")
|
if err := c.OSCommand.RunCommand("git reset --soft HEAD^"); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// TODO: if password is required, we need to return a subprocess
|
// TODO: if password is required, we need to return a subprocess
|
||||||
return c.OSCommand.RunCommand("git commit --amend -m " + c.OSCommand.Quote(message))
|
return c.OSCommand.RunCommand(fmt.Sprintf("git commit --amend -m %s", c.OSCommand.Quote(message)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SquashFixupCommit squashes a 'FIXUP' commit into the commit beneath it,
|
// SquashFixupCommit squashes a 'FIXUP' commit into the commit beneath it,
|
||||||
// retaining the commit message of the lower commit
|
// retaining the commit message of the lower commit
|
||||||
func (c *GitCommand) SquashFixupCommit(branchName string, shaValue string) error {
|
func (c *GitCommand) SquashFixupCommit(branchName string, shaValue string) error {
|
||||||
var err error
|
|
||||||
commands := []string{
|
commands := []string{
|
||||||
"git checkout -q " + shaValue,
|
fmt.Sprintf("git checkout -q %s", shaValue),
|
||||||
"git reset --soft " + shaValue + "^",
|
fmt.Sprintf("git reset --soft %s^", shaValue),
|
||||||
"git commit --amend -C " + shaValue + "^",
|
fmt.Sprintf("git commit --amend -C %s^", shaValue),
|
||||||
"git rebase --onto HEAD " + shaValue + " " + branchName,
|
fmt.Sprintf("git rebase --onto HEAD %s %s", shaValue, branchName),
|
||||||
}
|
}
|
||||||
ret := ""
|
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
c.Log.Info(command)
|
c.Log.Info(command)
|
||||||
output, err := c.OSCommand.RunCommandWithOutput(command)
|
|
||||||
ret += output
|
if output, err := c.OSCommand.RunCommandWithOutput(command); err != nil {
|
||||||
if err != nil {
|
ret := output
|
||||||
c.Log.Info(ret)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
// We are already in an error state here so we're just going to append
|
// We are already in an error state here so we're just going to append
|
||||||
// the output of these commands
|
// the output of these commands
|
||||||
output, _ := c.OSCommand.RunCommandWithOutput("git branch -d " + shaValue)
|
output, _ := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git branch -d %s", shaValue))
|
||||||
ret += output
|
ret += output
|
||||||
output, _ = c.OSCommand.RunCommandWithOutput("git checkout " + branchName)
|
output, _ = c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git checkout %s", branchName))
|
||||||
ret += output
|
ret += output
|
||||||
}
|
|
||||||
if err != nil {
|
c.Log.Info(ret)
|
||||||
return errors.New(ret)
|
return errors.New(ret)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CatFile obtain the contents of a file
|
// CatFile obtains the content of a file
|
||||||
func (c *GitCommand) CatFile(fileName string) (string, error) {
|
func (c *GitCommand) CatFile(fileName string) (string, error) {
|
||||||
return c.OSCommand.RunCommandWithOutput("cat " + c.OSCommand.Quote(fileName))
|
return c.OSCommand.RunCommandWithOutput("cat " + c.OSCommand.Quote(fileName))
|
||||||
}
|
}
|
||||||
|
@ -953,6 +953,132 @@ func TestGitCommandPush(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGitCommandSquashPreviousTwoCommits(t *testing.T) {
|
||||||
|
type scenario struct {
|
||||||
|
testName string
|
||||||
|
command func(string, ...string) *exec.Cmd
|
||||||
|
test func(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
scenarios := []scenario{
|
||||||
|
{
|
||||||
|
"Git reset triggers an error",
|
||||||
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
assert.EqualValues(t, []string{"reset", "--soft", "HEAD^"}, args)
|
||||||
|
|
||||||
|
return exec.Command("exit", "1")
|
||||||
|
},
|
||||||
|
func(err error) {
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Git commit triggers an error",
|
||||||
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
if len(args) > 0 && args[0] == "reset" {
|
||||||
|
return exec.Command("echo")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args)
|
||||||
|
|
||||||
|
return exec.Command("exit", "1")
|
||||||
|
},
|
||||||
|
func(err error) {
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Stash succeeded",
|
||||||
|
func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
if len(args) > 0 && args[0] == "reset" {
|
||||||
|
return exec.Command("echo")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.EqualValues(t, "git", cmd)
|
||||||
|
assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args)
|
||||||
|
|
||||||
|
return exec.Command("echo")
|
||||||
|
},
|
||||||
|
func(err error) {
|
||||||
|
assert.Nil(t, err)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range scenarios {
|
||||||
|
t.Run(s.testName, func(t *testing.T) {
|
||||||
|
gitCmd := newDummyGitCommand()
|
||||||
|
gitCmd.OSCommand.command = s.command
|
||||||
|
s.test(gitCmd.SquashPreviousTwoCommits("test"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGitCommandSquashFixupCommit(t *testing.T) {
|
||||||
|
type scenario struct {
|
||||||
|
testName string
|
||||||
|
command func() (func(string, ...string) *exec.Cmd, *[][]string)
|
||||||
|
test func(*[][]string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
scenarios := []scenario{
|
||||||
|
{
|
||||||
|
"An error occurred with one of the sub git command",
|
||||||
|
func() (func(string, ...string) *exec.Cmd, *[][]string) {
|
||||||
|
cmdsCalled := [][]string{}
|
||||||
|
return func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
cmdsCalled = append(cmdsCalled, args)
|
||||||
|
if len(args) > 0 && args[0] == "checkout" {
|
||||||
|
return exec.Command("exit", "1")
|
||||||
|
}
|
||||||
|
|
||||||
|
return exec.Command("echo")
|
||||||
|
}, &cmdsCalled
|
||||||
|
},
|
||||||
|
func(cmdsCalled *[][]string, err error) {
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
assert.Len(t, *cmdsCalled, 3)
|
||||||
|
assert.EqualValues(t, *cmdsCalled, [][]string{
|
||||||
|
{"checkout", "-q", "6789abcd"},
|
||||||
|
{"branch", "-d", "6789abcd"},
|
||||||
|
{"checkout", "test"},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Squash fixup succeeded",
|
||||||
|
func() (func(string, ...string) *exec.Cmd, *[][]string) {
|
||||||
|
cmdsCalled := [][]string{}
|
||||||
|
return func(cmd string, args ...string) *exec.Cmd {
|
||||||
|
cmdsCalled = append(cmdsCalled, args)
|
||||||
|
return exec.Command("echo")
|
||||||
|
}, &cmdsCalled
|
||||||
|
},
|
||||||
|
func(cmdsCalled *[][]string, err error) {
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Len(t, *cmdsCalled, 4)
|
||||||
|
assert.EqualValues(t, *cmdsCalled, [][]string{
|
||||||
|
{"checkout", "-q", "6789abcd"},
|
||||||
|
{"reset", "--soft", "6789abcd^"},
|
||||||
|
{"commit", "--amend", "-C", "6789abcd^"},
|
||||||
|
{"rebase", "--onto", "HEAD", "6789abcd", "test"},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range scenarios {
|
||||||
|
t.Run(s.testName, func(t *testing.T) {
|
||||||
|
var cmdsCalled *[][]string
|
||||||
|
gitCmd := newDummyGitCommand()
|
||||||
|
gitCmd.OSCommand.command, cmdsCalled = s.command()
|
||||||
|
s.test(cmdsCalled, gitCmd.SquashFixupCommit("test", "6789abcd"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGitCommandDiff(t *testing.T) {
|
func TestGitCommandDiff(t *testing.T) {
|
||||||
gitCommand := newDummyGitCommand()
|
gitCommand := newDummyGitCommand()
|
||||||
assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))
|
assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user