diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-12-31 10:04:32 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-04 09:07:15 +1100 |
commit | 9c4a8196834d40c9b054d957dcfb69851edcb61f (patch) | |
tree | b71abe0e59e4ee8c9aed19741e11be01dc976fdd /pkg/commands/sync_test.go | |
parent | 38bc48312e3c4d65235e9392a8a7d35c9bdab031 (diff) |
refactor sync test
Diffstat (limited to 'pkg/commands/sync_test.go')
-rw-r--r-- | pkg/commands/sync_test.go | 163 |
1 files changed, 46 insertions, 117 deletions
diff --git a/pkg/commands/sync_test.go b/pkg/commands/sync_test.go index 924bd56ef..24b7bf6c3 100644 --- a/pkg/commands/sync_test.go +++ b/pkg/commands/sync_test.go @@ -1,164 +1,93 @@ package commands import ( - "os/exec" "testing" - "github.com/jesseduffield/lazygit/pkg/secureexec" + "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/stretchr/testify/assert" ) -// TestGitCommandPush is a function. func TestGitCommandPush(t *testing.T) { type scenario struct { testName string - command func(string, ...string) *exec.Cmd opts PushOpts - test func(error) - } - - prompt := func(passOrUname string) string { - return "\n" + test func(oscommands.ICmdObj, error) } scenarios := []scenario{ { - "Push with force disabled", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"push"}, args) - - return secureexec.Command("echo") - }, - PushOpts{Force: false, PromptUserForCredential: prompt}, - func(err error) { + testName: "Push with force disabled", + opts: PushOpts{Force: false}, + test: func(cmdObj oscommands.ICmdObj, err error) { + assert.Equal(t, cmdObj.ToString(), "git push") assert.NoError(t, err) }, }, { - "Push with force enabled", - func(cmd string, args ...string) *exec.Cmd { - assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"push", "--force-with-lease"}, args) - - return secureexec.Command("echo") - }, - PushOpts{Force: true, PromptUserForCredential: prompt}, - func(err error) { + 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") assert.NoError(t, err) }, }, { - "Push with an error occurring", - 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}, - func(err error) { - assert.Error(t, err) - }, - }, - { - "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) { + 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"`) assert.NoError(t, err) }, }, { - "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) { + 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"`) assert.NoError(t, err) }, }, { - "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) { + 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"`) 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) { + 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()) }, }, - { - "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) - }, - }, } for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { - gitCmd := NewDummyGitCommand() - gitCmd.OSCommand.Command = s.command - err := gitCmd.Push(s.opts) - s.test(err) + gitCmd := NewDummyGitCommandWithRunner(oscommands.NewFakeRunner(t)) + s.test(gitCmd.PushCmdObj(s.opts)) }) } } |