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

179 lines
5.8 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 (
2021-12-31 01:24:53 +02:00
"regexp"
"strconv"
2021-04-10 03:40:42 +02:00
"testing"
2021-12-31 01:24:53 +02:00
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/app/daemon"
2022-01-02 01:34:33 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/models"
2021-12-31 01:24:53 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2022-03-19 03:26:30 +02:00
"github.com/samber/lo"
2021-04-10 03:40:42 +02:00
"github.com/stretchr/testify/assert"
)
2022-01-08 04:22:29 +02:00
func TestRebaseRebaseBranch(t *testing.T) {
2021-04-10 03:40:42 +02:00
type scenario struct {
testName string
arg string
gitVersion *GitVersion
runner *oscommands.FakeCmdObjRunner
test func(error)
2021-04-10 03:40:42 +02:00
}
scenarios := []scenario{
{
testName: "successful rebase",
arg: "master",
gitVersion: &GitVersion{2, 26, 0, ""},
2021-12-31 01:24:53 +02:00
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "master"}, "", nil),
2021-12-31 01:24:53 +02:00
test: func(err error) {
2021-04-10 03:40:42 +02:00
assert.NoError(t, err)
},
},
{
testName: "unsuccessful rebase",
arg: "master",
gitVersion: &GitVersion{2, 26, 0, ""},
2021-12-31 01:24:53 +02:00
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "master"}, "", errors.New("error")),
2021-12-31 01:24:53 +02:00
test: func(err error) {
2021-04-10 03:40:42 +02:00
assert.Error(t, err)
},
},
{
testName: "successful rebase (< 2.26.0)",
arg: "master",
gitVersion: &GitVersion{2, 25, 5, ""},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "master"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
testName: "successful rebase (< 2.22.0)",
arg: "master",
gitVersion: &GitVersion{2, 21, 9, ""},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "master"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
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 := buildRebaseCommands(commonDeps{runner: s.runner, gitVersion: s.gitVersion})
2022-01-08 04:22:29 +02:00
s.test(instance.RebaseBranch(s.arg))
2021-04-10 03:40:42 +02:00
})
}
}
2022-01-08 04:22:29 +02:00
// TestRebaseSkipEditorCommand confirms that SkipEditorCommand injects
2021-12-31 01:24:53 +02:00
// environment variables that suppress an interactive editor
2022-01-08 04:22:29 +02:00
func TestRebaseSkipEditorCommand(t *testing.T) {
cmdArgs := []string{"git", "blah"}
runner := oscommands.NewFakeRunner(t).ExpectFunc("matches editor env var", func(cmdObj oscommands.ICmdObj) bool {
assert.EqualValues(t, cmdArgs, cmdObj.Args())
2021-12-31 01:24:53 +02:00
envVars := cmdObj.GetEnvVars()
for _, regexStr := range []string{
`^VISUAL=.*$`,
`^EDITOR=.*$`,
`^GIT_EDITOR=.*$`,
`^GIT_SEQUENCE_EDITOR=.*$`,
"^" + daemon.DaemonKindEnvKey + "=" + strconv.Itoa(int(daemon.DaemonKindExitImmediately)) + "$",
2021-12-31 01:24:53 +02:00
} {
2022-01-08 06:46:35 +02:00
regexStr := regexStr
2022-03-19 03:26:30 +02:00
foundMatch := lo.ContainsBy(envVars, func(envVar string) bool {
2021-12-31 01:24:53 +02:00
return regexp.MustCompile(regexStr).MatchString(envVar)
})
if !foundMatch {
return false
2021-12-31 01:24:53 +02:00
}
}
return true
}, "", nil)
2022-01-08 04:22:29 +02:00
instance := buildRebaseCommands(commonDeps{runner: runner})
err := instance.runSkipEditorCommand(instance.cmd.New(cmdArgs))
2021-12-31 01:24:53 +02:00
assert.NoError(t, err)
runner.CheckForMissingCalls()
}
2022-01-02 01:34:33 +02:00
2022-01-08 04:22:29 +02:00
func TestRebaseDiscardOldFileChanges(t *testing.T) {
2022-01-02 01:34:33 +02:00
type scenario struct {
testName string
gitConfigMockResponses map[string]string
commits []*models.Commit
commitIndex int
fileName string
runner *oscommands.FakeCmdObjRunner
test func(error)
}
scenarios := []scenario{
{
testName: "returns error when index outside of range of commits",
gitConfigMockResponses: nil,
commits: []*models.Commit{},
commitIndex: 0,
fileName: "test999.txt",
runner: oscommands.NewFakeRunner(t),
test: func(err error) {
assert.Error(t, err)
},
},
{
testName: "returns error when using gpg",
gitConfigMockResponses: map[string]string{"commit.gpgsign": "true"},
commits: []*models.Commit{{Name: "commit", Sha: "123456"}},
commitIndex: 0,
fileName: "test999.txt",
runner: oscommands.NewFakeRunner(t),
test: func(err error) {
assert.Error(t, err)
},
},
{
testName: "checks out file if it already existed",
gitConfigMockResponses: nil,
commits: []*models.Commit{
{Name: "commit", Sha: "123456"},
{Name: "commit2", Sha: "abcdef"},
},
commitIndex: 0,
fileName: "test999.txt",
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "abcdef"}, "", nil).
ExpectGitArgs([]string{"cat-file", "-e", "HEAD^:test999.txt"}, "", nil).
ExpectGitArgs([]string{"checkout", "HEAD^", "--", "test999.txt"}, "", nil).
ExpectGitArgs([]string{"commit", "--amend", "--no-edit", "--allow-empty"}, "", nil).
ExpectGitArgs([]string{"rebase", "--continue"}, "", nil),
2022-01-02 01:34:33 +02:00
test: func(err error) {
assert.NoError(t, err)
},
},
// test for when the file was created within the commit requires a refactor to support proper mocks
// currently we'd need to mock out the os.Remove function and that's gonna introduce tech debt
}
for _, s := range scenarios {
2022-01-08 06:46:35 +02:00
s := s
2022-01-02 01:34:33 +02:00
t.Run(s.testName, func(t *testing.T) {
2022-01-08 04:22:29 +02:00
instance := buildRebaseCommands(commonDeps{
runner: s.runner,
gitVersion: &GitVersion{2, 26, 0, ""},
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
2022-01-08 04:22:29 +02:00
})
s.test(instance.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
2022-01-02 01:34:33 +02:00
s.runner.CheckForMissingCalls()
})
}
}