mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-02 23:27:32 +02:00
refactor sync test
This commit is contained in:
parent
38bc48312e
commit
9c4a819683
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Push pushes to a branch
|
// Push pushes to a branch
|
||||||
@ -12,10 +13,9 @@ type PushOpts struct {
|
|||||||
UpstreamRemote string
|
UpstreamRemote string
|
||||||
UpstreamBranch string
|
UpstreamBranch string
|
||||||
SetUpstream bool
|
SetUpstream bool
|
||||||
PromptUserForCredential func(string) string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GitCommand) Push(opts PushOpts) error {
|
func (c *GitCommand) PushCmdObj(opts PushOpts) (oscommands.ICmdObj, error) {
|
||||||
cmdStr := "git push"
|
cmdStr := "git push"
|
||||||
|
|
||||||
if opts.Force {
|
if opts.Force {
|
||||||
@ -32,13 +32,22 @@ func (c *GitCommand) Push(opts PushOpts) error {
|
|||||||
|
|
||||||
if opts.UpstreamBranch != "" {
|
if opts.UpstreamBranch != "" {
|
||||||
if opts.UpstreamRemote == "" {
|
if opts.UpstreamRemote == "" {
|
||||||
return errors.New(c.Tr.MustSpecifyOriginError)
|
return nil, errors.New(c.Tr.MustSpecifyOriginError)
|
||||||
}
|
}
|
||||||
cmdStr += " " + c.OSCommand.Quote(opts.UpstreamBranch)
|
cmdStr += " " + c.OSCommand.Quote(opts.UpstreamBranch)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdObj := c.Cmd.New(cmdStr)
|
cmdObj := c.Cmd.New(cmdStr)
|
||||||
return c.DetectUnamePass(cmdObj, opts.PromptUserForCredential)
|
return cmdObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *GitCommand) Push(opts PushOpts, promptUserForCredential func(string) string) error {
|
||||||
|
cmdObj, err := c.PushCmdObj(opts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.DetectUnamePass(cmdObj, promptUserForCredential)
|
||||||
}
|
}
|
||||||
|
|
||||||
type FetchOptions struct {
|
type FetchOptions struct {
|
||||||
|
@ -1,164 +1,93 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestGitCommandPush is a function.
|
|
||||||
func TestGitCommandPush(t *testing.T) {
|
func TestGitCommandPush(t *testing.T) {
|
||||||
type scenario struct {
|
type scenario struct {
|
||||||
testName string
|
testName string
|
||||||
command func(string, ...string) *exec.Cmd
|
|
||||||
opts PushOpts
|
opts PushOpts
|
||||||
test func(error)
|
test func(oscommands.ICmdObj, error)
|
||||||
}
|
|
||||||
|
|
||||||
prompt := func(passOrUname string) string {
|
|
||||||
return "\n"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarios := []scenario{
|
scenarios := []scenario{
|
||||||
{
|
{
|
||||||
"Push with force disabled",
|
testName: "Push with force disabled",
|
||||||
func(cmd string, args ...string) *exec.Cmd {
|
opts: PushOpts{Force: false},
|
||||||
assert.EqualValues(t, "git", cmd)
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
assert.EqualValues(t, []string{"push"}, args)
|
assert.Equal(t, cmdObj.ToString(), "git push")
|
||||||
|
|
||||||
return secureexec.Command("echo")
|
|
||||||
},
|
|
||||||
PushOpts{Force: false, PromptUserForCredential: prompt},
|
|
||||||
func(err error) {
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Push with force enabled",
|
testName: "Push with force enabled",
|
||||||
func(cmd string, args ...string) *exec.Cmd {
|
opts: PushOpts{Force: true},
|
||||||
assert.EqualValues(t, "git", cmd)
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
assert.EqualValues(t, []string{"push", "--force-with-lease"}, args)
|
assert.Equal(t, cmdObj.ToString(), "git push --force-with-lease")
|
||||||
|
|
||||||
return secureexec.Command("echo")
|
|
||||||
},
|
|
||||||
PushOpts{Force: true, PromptUserForCredential: prompt},
|
|
||||||
func(err error) {
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Push with an error occurring",
|
testName: "Push with force disabled, upstream supplied",
|
||||||
func(cmd string, args ...string) *exec.Cmd {
|
opts: PushOpts{
|
||||||
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,
|
Force: false,
|
||||||
UpstreamRemote: "origin",
|
UpstreamRemote: "origin",
|
||||||
UpstreamBranch: "master",
|
UpstreamBranch: "master",
|
||||||
PromptUserForCredential: prompt,
|
|
||||||
},
|
},
|
||||||
func(err error) {
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
|
assert.Equal(t, cmdObj.ToString(), `git push "origin" "master"`)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Push with force disabled, setting upstream",
|
testName: "Push with force disabled, setting upstream",
|
||||||
func(cmd string, args ...string) *exec.Cmd {
|
opts: PushOpts{
|
||||||
assert.EqualValues(t, "git", cmd)
|
|
||||||
assert.EqualValues(t, []string{"push", "--set-upstream", "origin", "master"}, args)
|
|
||||||
|
|
||||||
return secureexec.Command("echo")
|
|
||||||
},
|
|
||||||
PushOpts{
|
|
||||||
Force: false,
|
Force: false,
|
||||||
UpstreamRemote: "origin",
|
UpstreamRemote: "origin",
|
||||||
UpstreamBranch: "master",
|
UpstreamBranch: "master",
|
||||||
PromptUserForCredential: prompt,
|
|
||||||
SetUpstream: true,
|
SetUpstream: true,
|
||||||
},
|
},
|
||||||
func(err error) {
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
|
assert.Equal(t, cmdObj.ToString(), `git push --set-upstream "origin" "master"`)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Push with force enabled, setting upstream",
|
testName: "Push with force enabled, setting upstream",
|
||||||
func(cmd string, args ...string) *exec.Cmd {
|
opts: PushOpts{
|
||||||
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,
|
Force: true,
|
||||||
UpstreamRemote: "origin",
|
UpstreamRemote: "origin",
|
||||||
UpstreamBranch: "master",
|
UpstreamBranch: "master",
|
||||||
PromptUserForCredential: prompt,
|
|
||||||
SetUpstream: true,
|
SetUpstream: true,
|
||||||
},
|
},
|
||||||
func(err error) {
|
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)
|
assert.NoError(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Push with remote branch but no origin",
|
testName: "Push with remote branch but no origin",
|
||||||
func(cmd string, args ...string) *exec.Cmd {
|
opts: PushOpts{
|
||||||
return nil
|
|
||||||
},
|
|
||||||
PushOpts{
|
|
||||||
Force: true,
|
Force: true,
|
||||||
UpstreamRemote: "",
|
UpstreamRemote: "",
|
||||||
UpstreamBranch: "master",
|
UpstreamBranch: "master",
|
||||||
PromptUserForCredential: prompt,
|
|
||||||
SetUpstream: true,
|
SetUpstream: true,
|
||||||
},
|
},
|
||||||
func(err error) {
|
test: func(cmdObj oscommands.ICmdObj, err error) {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.EqualValues(t, "Must specify a remote if specifying a branch", err.Error())
|
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 {
|
for _, s := range scenarios {
|
||||||
t.Run(s.testName, func(t *testing.T) {
|
t.Run(s.testName, func(t *testing.T) {
|
||||||
gitCmd := NewDummyGitCommand()
|
gitCmd := NewDummyGitCommandWithRunner(oscommands.NewFakeRunner(t))
|
||||||
gitCmd.OSCommand.Command = s.command
|
s.test(gitCmd.PushCmdObj(s.opts))
|
||||||
err := gitCmd.Push(s.opts)
|
|
||||||
s.test(err)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -740,8 +740,7 @@ func (gui *Gui) push(opts pushOpts) error {
|
|||||||
UpstreamRemote: opts.upstreamRemote,
|
UpstreamRemote: opts.upstreamRemote,
|
||||||
UpstreamBranch: opts.upstreamBranch,
|
UpstreamBranch: opts.upstreamBranch,
|
||||||
SetUpstream: opts.setUpstream,
|
SetUpstream: opts.setUpstream,
|
||||||
PromptUserForCredential: gui.promptUserForCredential,
|
}, gui.promptUserForCredential)
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil && !opts.force && strings.Contains(err.Error(), "Updates were rejected") {
|
if err != nil && !opts.force && strings.Contains(err.Error(), "Updates were rejected") {
|
||||||
forcePushDisabled := gui.UserConfig.Git.DisableForcePushing
|
forcePushDisabled := gui.UserConfig.Git.DisableForcePushing
|
||||||
|
Loading…
x
Reference in New Issue
Block a user