2019-11-04 10:47:25 +02:00
|
|
|
package commands
|
2019-03-02 04:08:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-29 12:23:58 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2019-03-02 04:08:09 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/i18n"
|
2020-12-21 00:37:48 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
2020-09-29 11:10:57 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2019-03-02 04:08:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewDummyCommitListBuilder creates a new dummy CommitListBuilder for testing
|
|
|
|
func NewDummyCommitListBuilder() *CommitListBuilder {
|
2020-09-29 12:23:58 +02:00
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
2019-03-02 04:08:09 +02:00
|
|
|
|
|
|
|
return &CommitListBuilder{
|
2020-09-29 11:10:57 +02:00
|
|
|
Log: utils.NewDummyLog(),
|
2020-08-27 12:50:30 +02:00
|
|
|
GitCommand: NewDummyGitCommandWithOSCommand(osCommand),
|
|
|
|
OSCommand: osCommand,
|
2020-10-04 02:00:48 +02:00
|
|
|
Tr: i18n.NewTranslationSet(utils.NewDummyLog()),
|
2019-03-02 04:08:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCommitListBuilderGetMergeBase is a function.
|
|
|
|
func TestCommitListBuilderGetMergeBase(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
testName string
|
|
|
|
command func(string, ...string) *exec.Cmd
|
|
|
|
test func(string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"swallows an error if the call to merge-base returns an error",
|
|
|
|
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)
|
2020-12-21 00:37:48 +02:00
|
|
|
return secureexec.Command("echo", "master")
|
2019-03-02 04:08:09 +02:00
|
|
|
case "merge-base":
|
|
|
|
assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args)
|
2020-12-21 00:37:48 +02:00
|
|
|
return secureexec.Command("test")
|
2019-03-02 04:08:09 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(output string, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, "", output)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"returns the commit when master",
|
|
|
|
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)
|
2020-12-21 00:37:48 +02:00
|
|
|
return secureexec.Command("echo", "master")
|
2019-03-02 04:08:09 +02:00
|
|
|
case "merge-base":
|
|
|
|
assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args)
|
2020-12-21 00:37:48 +02:00
|
|
|
return secureexec.Command("echo", "blah")
|
2019-03-02 04:08:09 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(output string, err error) {
|
|
|
|
assert.NoError(t, err)
|
2020-09-17 23:46:12 +02:00
|
|
|
assert.Equal(t, "blah", output)
|
2019-03-02 04:08:09 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"checks against develop when a feature branch",
|
|
|
|
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)
|
2020-12-21 00:37:48 +02:00
|
|
|
return secureexec.Command("echo", "feature/test")
|
2019-03-02 04:08:09 +02:00
|
|
|
case "merge-base":
|
|
|
|
assert.EqualValues(t, []string{"merge-base", "HEAD", "develop"}, args)
|
2020-12-21 00:37:48 +02:00
|
|
|
return secureexec.Command("echo", "blah")
|
2019-03-02 04:08:09 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(output string, err error) {
|
|
|
|
assert.NoError(t, err)
|
2020-09-17 23:46:12 +02:00
|
|
|
assert.Equal(t, "blah", output)
|
2019-03-02 04:08:09 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bubbles up error if there is one",
|
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
2020-12-21 00:37:48 +02:00
|
|
|
return secureexec.Command("test")
|
2019-03-02 04:08:09 +02:00
|
|
|
},
|
|
|
|
func(output string, err error) {
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "", output)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
c := NewDummyCommitListBuilder()
|
|
|
|
c.OSCommand.SetCommand(s.command)
|
2020-09-02 12:55:53 +02:00
|
|
|
s.test(c.getMergeBase("HEAD"))
|
2019-03-02 04:08:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|