1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/sync_test.go

165 lines
4.1 KiB
Go
Raw Normal View History

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
}
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")
},
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")
},
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")
},
PushOpts{Force: false, PromptUserForCredential: prompt},
2021-04-10 03:40:42 +02:00
func(err error) {
assert.Error(t, err)
},
},
{
2021-10-23 00:52:19 +02:00
"Push with force disabled, upstream supplied",
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",
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",
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",
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
err := gitCmd.Push(s.opts)
2021-04-10 03:40:42 +02:00
s.test(err)
})
}
}