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"
|
2023-04-16 07:43:54 +02:00
|
|
|
"strconv"
|
2021-04-10 03:40:42 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-12-31 01:24:53 +02:00
|
|
|
"github.com/go-errors/errors"
|
2022-05-07 07:23:08 +02:00
|
|
|
"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
|
2021-12-31 01:24:53 +02:00
|
|
|
runner *oscommands.FakeCmdObjRunner
|
2021-04-10 03:40:42 +02:00
|
|
|
test func(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2021-12-31 01:24:53 +02:00
|
|
|
testName: "successful rebase",
|
|
|
|
arg: "master",
|
|
|
|
runner: oscommands.NewFakeRunner(t).
|
2023-03-18 09:17:47 +02:00
|
|
|
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash 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)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:24:53 +02:00
|
|
|
testName: "unsuccessful rebase",
|
|
|
|
arg: "master",
|
|
|
|
runner: oscommands.NewFakeRunner(t).
|
2023-03-18 09:17:47 +02:00
|
|
|
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash 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)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-01-08 04:22:29 +02:00
|
|
|
instance := buildRebaseCommands(commonDeps{runner: s.runner})
|
|
|
|
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) {
|
2021-12-31 01:24:53 +02:00
|
|
|
commandStr := "git blah"
|
|
|
|
runner := oscommands.NewFakeRunner(t).ExpectFunc(func(cmdObj oscommands.ICmdObj) (string, error) {
|
|
|
|
assert.Equal(t, commandStr, cmdObj.ToString())
|
|
|
|
envVars := cmdObj.GetEnvVars()
|
|
|
|
for _, regexStr := range []string{
|
|
|
|
`^VISUAL=.*$`,
|
|
|
|
`^EDITOR=.*$`,
|
|
|
|
`^GIT_EDITOR=.*$`,
|
2023-01-01 05:33:58 +02:00
|
|
|
`^GIT_SEQUENCE_EDITOR=.*$`,
|
2023-04-16 07:43:54 +02:00
|
|
|
"^" + 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 {
|
|
|
|
t.Errorf("expected environment variable %s to be set", regexStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
})
|
2022-01-08 04:22:29 +02:00
|
|
|
instance := buildRebaseCommands(commonDeps{runner: runner})
|
|
|
|
err := instance.runSkipEditorCommand(instance.cmd.New(commandStr))
|
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).
|
2023-03-18 09:17:47 +02:00
|
|
|
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash abcdef`, "", nil).
|
2022-01-02 01:34:33 +02:00
|
|
|
Expect(`git cat-file -e HEAD^:"test999.txt"`, "", nil).
|
|
|
|
Expect(`git checkout HEAD^ -- "test999.txt"`, "", nil).
|
|
|
|
Expect(`git commit --amend --no-edit --allow-empty`, "", nil).
|
|
|
|
Expect(`git rebase --continue`, "", nil),
|
|
|
|
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,
|
|
|
|
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
|
|
|
|
})
|
|
|
|
|
|
|
|
s.test(instance.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
|
2022-01-02 01:34:33 +02:00
|
|
|
s.runner.CheckForMissingCalls()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|