2021-04-10 03:40:42 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestGitCommandPush is a function.
|
|
|
|
func TestGitCommandPush(t *testing.T) {
|
|
|
|
type scenario struct {
|
2021-10-23 00:52:19 +02:00
|
|
|
testName string
|
|
|
|
command func(string, ...string) *exec.Cmd
|
|
|
|
opts PushOpts
|
|
|
|
test func(error)
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:41:19 +02:00
|
|
|
prompt := func(passOrUname string) string {
|
|
|
|
return "\n"
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:40:42 +02:00
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2021-10-23 00:52:19 +02:00
|
|
|
"Push with force disabled",
|
2021-04-10 03:40:42 +02:00
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
assert.EqualValues(t, "git", cmd)
|
2021-10-23 00:52:19 +02:00
|
|
|
assert.EqualValues(t, []string{"push"}, args)
|
2021-04-10 03:40:42 +02:00
|
|
|
|
|
|
|
return secureexec.Command("echo")
|
|
|
|
},
|
2021-10-19 13:41:19 +02:00
|
|
|
PushOpts{Force: false, PromptUserForCredential: prompt},
|
2021-04-10 03:40:42 +02:00
|
|
|
func(err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 00:52:19 +02:00
|
|
|
"Push with force enabled",
|
2021-04-10 03:40:42 +02:00
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
assert.EqualValues(t, "git", cmd)
|
2021-10-23 00:52:19 +02:00
|
|
|
assert.EqualValues(t, []string{"push", "--force-with-lease"}, args)
|
2021-04-10 03:40:42 +02:00
|
|
|
|
|
|
|
return secureexec.Command("echo")
|
|
|
|
},
|
2021-10-19 13:41:19 +02:00
|
|
|
PushOpts{Force: true, PromptUserForCredential: prompt},
|
2021-04-10 03:40:42 +02:00
|
|
|
func(err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 00:52:19 +02:00
|
|
|
"Push with an error occurring",
|
2021-04-10 03:40:42 +02:00
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
assert.EqualValues(t, "git", cmd)
|
|
|
|
assert.EqualValues(t, []string{"push"}, args)
|
|
|
|
return secureexec.Command("test")
|
|
|
|
},
|
2021-10-19 13:41:19 +02:00
|
|
|
PushOpts{Force: false, PromptUserForCredential: prompt},
|
2021-04-10 03:40:42 +02:00
|
|
|
func(err error) {
|
|
|
|
assert.Error(t, err)
|
|
|
|
},
|
|
|
|
},
|
2021-10-19 13:41:19 +02:00
|
|
|
{
|
2021-10-23 00:52:19 +02:00
|
|
|
"Push with force disabled, upstream supplied",
|
2021-10-19 13:41:19 +02:00
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
assert.EqualValues(t, "git", cmd)
|
|
|
|
assert.EqualValues(t, []string{"push", "origin", "master"}, args)
|
|
|
|
|
|
|
|
return secureexec.Command("echo")
|
|
|
|
},
|
|
|
|
PushOpts{
|
|
|
|
Force: false,
|
|
|
|
UpstreamRemote: "origin",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
PromptUserForCredential: prompt,
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 00:52:19 +02:00
|
|
|
"Push with force disabled, setting upstream",
|
2021-10-19 13:41:19 +02:00
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
assert.EqualValues(t, "git", cmd)
|
|
|
|
assert.EqualValues(t, []string{"push", "--set-upstream", "origin", "master"}, args)
|
|
|
|
|
|
|
|
return secureexec.Command("echo")
|
|
|
|
},
|
|
|
|
PushOpts{
|
|
|
|
Force: false,
|
|
|
|
UpstreamRemote: "origin",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
PromptUserForCredential: prompt,
|
|
|
|
SetUpstream: true,
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 00:52:19 +02:00
|
|
|
"Push with force enabled, setting upstream",
|
2021-10-19 13:41:19 +02:00
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
assert.EqualValues(t, "git", cmd)
|
|
|
|
assert.EqualValues(t, []string{"push", "--force-with-lease", "--set-upstream", "origin", "master"}, args)
|
|
|
|
|
|
|
|
return secureexec.Command("echo")
|
|
|
|
},
|
|
|
|
PushOpts{
|
|
|
|
Force: true,
|
|
|
|
UpstreamRemote: "origin",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
PromptUserForCredential: prompt,
|
|
|
|
SetUpstream: true,
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Push with remote branch but no origin",
|
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
PushOpts{
|
|
|
|
Force: true,
|
|
|
|
UpstreamRemote: "",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
PromptUserForCredential: prompt,
|
|
|
|
SetUpstream: true,
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualValues(t, "Must specify a remote if specifying a branch", err.Error())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 00:52:19 +02:00
|
|
|
"Push with force disabled, upstream supplied",
|
2021-10-19 13:41:19 +02:00
|
|
|
func(cmd string, args ...string) *exec.Cmd {
|
|
|
|
assert.EqualValues(t, "git", cmd)
|
|
|
|
assert.EqualValues(t, []string{"push", "origin", "master"}, args)
|
|
|
|
|
|
|
|
return secureexec.Command("echo")
|
|
|
|
},
|
|
|
|
PushOpts{
|
|
|
|
Force: false,
|
|
|
|
UpstreamRemote: "origin",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
PromptUserForCredential: prompt,
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
gitCmd := NewDummyGitCommand()
|
|
|
|
gitCmd.OSCommand.Command = s.command
|
2021-10-19 13:41:19 +02:00
|
|
|
err := gitCmd.Push(s.opts)
|
2021-04-10 03:40:42 +02:00
|
|
|
s.test(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|