2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2021-04-10 03:40:42 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"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 {
|
2022-01-08 06:46:35 +02:00
|
|
|
s := s
|
2021-04-10 03:40:42 +02:00
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
2023-04-17 12:37:33 +02:00
|
|
|
instance := buildSyncCommands(commonDeps{})
|
2023-07-09 13:09:52 +02:00
|
|
|
task := gocui.NewFakeTask()
|
|
|
|
s.test(instance.PushCmdObj(task, s.opts))
|
2021-04-10 03:40:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 20:30:47 +02:00
|
|
|
|
|
|
|
func TestSyncFetch(t *testing.T) {
|
|
|
|
type scenario struct {
|
2023-03-01 10:09:35 +02:00
|
|
|
testName string
|
|
|
|
fetchAllConfig bool
|
|
|
|
test func(oscommands.ICmdObj)
|
2023-05-30 20:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2023-03-01 10:09:35 +02:00
|
|
|
testName: "Fetch in foreground (all=false)",
|
|
|
|
fetchAllConfig: false,
|
2023-05-30 20:30:47 +02:00
|
|
|
test: func(cmdObj oscommands.ICmdObj) {
|
|
|
|
assert.True(t, cmdObj.ShouldLog())
|
|
|
|
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
|
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch"})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-03-01 10:09:35 +02:00
|
|
|
testName: "Fetch in foreground (all=true)",
|
|
|
|
fetchAllConfig: true,
|
|
|
|
test: func(cmdObj oscommands.ICmdObj) {
|
|
|
|
assert.True(t, cmdObj.ShouldLog())
|
|
|
|
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
|
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all"})
|
|
|
|
},
|
|
|
|
},
|
2023-07-09 13:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
s := s
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
instance := buildSyncCommands(commonDeps{})
|
|
|
|
instance.UserConfig.Git.FetchAll = s.fetchAllConfig
|
|
|
|
task := gocui.NewFakeTask()
|
|
|
|
s.test(instance.FetchCmdObj(task))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncFetchBackground(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
testName string
|
|
|
|
fetchAllConfig bool
|
|
|
|
test func(oscommands.ICmdObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
2023-03-01 10:09:35 +02:00
|
|
|
{
|
|
|
|
testName: "Fetch in background (all=false)",
|
|
|
|
fetchAllConfig: false,
|
2023-05-30 20:30:47 +02:00
|
|
|
test: func(cmdObj oscommands.ICmdObj) {
|
|
|
|
assert.False(t, cmdObj.ShouldLog())
|
|
|
|
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
|
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch"})
|
|
|
|
},
|
|
|
|
},
|
2023-03-01 10:09:35 +02:00
|
|
|
{
|
|
|
|
testName: "Fetch in background (all=true)",
|
|
|
|
fetchAllConfig: true,
|
|
|
|
test: func(cmdObj oscommands.ICmdObj) {
|
|
|
|
assert.False(t, cmdObj.ShouldLog())
|
|
|
|
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
|
|
|
|
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all"})
|
|
|
|
},
|
|
|
|
},
|
2023-05-30 20:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
s := s
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
instance := buildSyncCommands(commonDeps{})
|
2023-03-01 10:09:35 +02:00
|
|
|
instance.UserConfig.Git.FetchAll = s.fetchAllConfig
|
2023-07-09 13:09:52 +02:00
|
|
|
s.test(instance.FetchBackgroundCmdObj())
|
2023-05-30 20:30:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|