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

156 lines
4.3 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2021-04-10 03:40:42 +02:00
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-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.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) {
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) {
assert.Equal(t, cmdObj.Args(), []string{"git", "push", "origin", "master"})
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.Args(), []string{"git", "push", "--set-upstream", "origin", "master"})
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.Args(), []string{"git", "push", "--force-with-lease", "--set-upstream", "origin", "master"})
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) {
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) {
instance := buildSyncCommands(commonDeps{})
2022-01-08 04:22:29 +02:00
s.test(instance.PushCmdObj(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 {
testName string
opts FetchOptions
fetchAllConfig bool
test func(oscommands.ICmdObj)
2023-05-30 20:30:47 +02:00
}
scenarios := []scenario{
{
testName: "Fetch in foreground (all=false)",
opts: FetchOptions{Background: 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"})
},
},
{
testName: "Fetch in foreground (all=true)",
opts: FetchOptions{Background: false},
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"})
},
},
{
testName: "Fetch in background (all=false)",
opts: FetchOptions{Background: true},
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"})
},
},
{
testName: "Fetch in background (all=true)",
opts: FetchOptions{Background: 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{})
instance.UserConfig.Git.FetchAll = s.fetchAllConfig
2023-05-30 20:30:47 +02:00
s.test(instance.FetchCmdObj(s.opts))
})
}
}