2021-04-10 03:40:42 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-12-31 01:04:32 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2021-04-10 03:40:42 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
func TestSyncPush(t *testing.T) {
|
2021-04-10 03:40:42 +02:00
|
|
|
type scenario struct {
|
2021-10-23 00:52:19 +02:00
|
|
|
testName string
|
|
|
|
opts PushOpts
|
2021-12-31 01:04:32 +02:00
|
|
|
test func(oscommands.ICmdObj, error)
|
2021-10-19 13:41:19 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 03:40:42 +02:00
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2021-12-31 01:04:32 +02:00
|
|
|
testName: "Push with force disabled",
|
|
|
|
opts: PushOpts{Force: false},
|
|
|
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
|
|
|
assert.Equal(t, cmdObj.ToString(), "git push")
|
2021-04-10 03:40:42 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:04:32 +02:00
|
|
|
testName: "Push with force enabled",
|
|
|
|
opts: PushOpts{Force: true},
|
|
|
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
|
|
|
assert.Equal(t, cmdObj.ToString(), "git push --force-with-lease")
|
2021-04-10 03:40:42 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:04:32 +02:00
|
|
|
testName: "Push with force disabled, upstream supplied",
|
|
|
|
opts: PushOpts{
|
|
|
|
Force: false,
|
|
|
|
UpstreamRemote: "origin",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
},
|
|
|
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
|
|
|
assert.Equal(t, cmdObj.ToString(), `git push "origin" "master"`)
|
2021-10-19 13:41:19 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:04:32 +02:00
|
|
|
testName: "Push with force disabled, setting upstream",
|
|
|
|
opts: PushOpts{
|
|
|
|
Force: false,
|
|
|
|
UpstreamRemote: "origin",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
SetUpstream: true,
|
|
|
|
},
|
|
|
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
|
|
|
assert.Equal(t, cmdObj.ToString(), `git push --set-upstream "origin" "master"`)
|
2021-10-19 13:41:19 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:04:32 +02:00
|
|
|
testName: "Push with force enabled, setting upstream",
|
|
|
|
opts: PushOpts{
|
|
|
|
Force: true,
|
|
|
|
UpstreamRemote: "origin",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
SetUpstream: true,
|
|
|
|
},
|
|
|
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
|
|
|
assert.Equal(t, cmdObj.ToString(), `git push --force-with-lease --set-upstream "origin" "master"`)
|
2021-10-19 13:41:19 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:04:32 +02:00
|
|
|
testName: "Push with remote branch but no origin",
|
|
|
|
opts: PushOpts{
|
|
|
|
Force: true,
|
|
|
|
UpstreamRemote: "",
|
|
|
|
UpstreamBranch: "master",
|
|
|
|
SetUpstream: true,
|
|
|
|
},
|
|
|
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
2021-10-19 13:41:19 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualValues(t, "Must specify a remote if specifying a branch", err.Error())
|
|
|
|
},
|
|
|
|
},
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
2022-01-08 04:22:29 +02:00
|
|
|
instance := buildSyncCommands(commonDeps{})
|
|
|
|
s.test(instance.PushCmdObj(s.opts))
|
2021-04-10 03:40:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|