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:24:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2022-01-08 04:22:29 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
2021-04-10 03:40:42 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
func TestCommitRewordCommit(t *testing.T) {
|
2023-01-21 13:38:14 +02:00
|
|
|
type scenario struct {
|
2023-07-22 06:05:42 +02:00
|
|
|
testName string
|
|
|
|
runner *oscommands.FakeCmdObjRunner
|
|
|
|
summary string
|
|
|
|
description string
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"Single line reword",
|
|
|
|
oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil),
|
|
|
|
"test",
|
2023-07-22 06:05:42 +02:00
|
|
|
"",
|
2023-01-21 13:38:14 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Multi line reword",
|
|
|
|
oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test", "-m", "line 2\nline 3"}, "", nil),
|
2023-07-22 06:05:42 +02:00
|
|
|
"test",
|
|
|
|
"line 2\nline 3",
|
2023-01-21 13:38:14 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, s := range scenarios {
|
|
|
|
s := s
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
instance := buildCommitCommands(commonDeps{runner: s.runner})
|
2021-04-10 03:40:42 +02:00
|
|
|
|
2023-07-22 06:05:42 +02:00
|
|
|
assert.NoError(t, instance.RewordLastCommit(s.summary, s.description))
|
2023-01-21 13:38:14 +02:00
|
|
|
s.runner.CheckForMissingCalls()
|
|
|
|
})
|
|
|
|
}
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
func TestCommitResetToCommit(t *testing.T) {
|
2021-12-31 01:24:53 +02:00
|
|
|
runner := oscommands.NewFakeRunner(t).
|
2022-01-03 06:15:26 +02:00
|
|
|
ExpectGitArgs([]string{"reset", "--hard", "78976bc"}, "", nil)
|
2021-04-10 03:40:42 +02:00
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
instance := buildCommitCommands(commonDeps{runner: runner})
|
|
|
|
|
|
|
|
assert.NoError(t, instance.ResetToCommit("78976bc", "hard", []string{}))
|
2021-12-31 01:24:53 +02:00
|
|
|
runner.CheckForMissingCalls()
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
|
|
|
|
2023-01-01 17:49:59 +02:00
|
|
|
func TestCommitCommitCmdObj(t *testing.T) {
|
2021-04-10 03:40:42 +02:00
|
|
|
type scenario struct {
|
2022-01-08 04:22:29 +02:00
|
|
|
testName string
|
2023-07-22 06:05:42 +02:00
|
|
|
summary string
|
|
|
|
description string
|
2022-01-08 04:22:29 +02:00
|
|
|
configSignoff bool
|
|
|
|
configSkipHookPrefix string
|
2023-05-21 09:00:29 +02:00
|
|
|
expectedArgs []string
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2022-01-08 04:22:29 +02:00
|
|
|
testName: "Commit",
|
2023-07-22 06:05:42 +02:00
|
|
|
summary: "test",
|
2022-01-08 04:22:29 +02:00
|
|
|
configSignoff: false,
|
|
|
|
configSkipHookPrefix: "",
|
2023-05-21 09:00:29 +02:00
|
|
|
expectedArgs: []string{"commit", "-m", "test"},
|
2022-01-08 04:22:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "Commit with --no-verify flag",
|
2023-07-22 06:05:42 +02:00
|
|
|
summary: "WIP: test",
|
2022-01-08 04:22:29 +02:00
|
|
|
configSignoff: false,
|
|
|
|
configSkipHookPrefix: "WIP",
|
2023-05-21 09:00:29 +02:00
|
|
|
expectedArgs: []string{"commit", "--no-verify", "-m", "WIP: test"},
|
2022-01-08 04:22:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "Commit with multiline message",
|
2023-07-22 06:05:42 +02:00
|
|
|
summary: "line1",
|
|
|
|
description: "line2",
|
2022-01-08 04:22:29 +02:00
|
|
|
configSignoff: false,
|
|
|
|
configSkipHookPrefix: "",
|
2023-05-21 09:00:29 +02:00
|
|
|
expectedArgs: []string{"commit", "-m", "line1", "-m", "line2"},
|
2021-04-10 03:40:42 +02:00
|
|
|
},
|
|
|
|
{
|
2022-01-08 04:22:29 +02:00
|
|
|
testName: "Commit with signoff",
|
2023-07-22 06:05:42 +02:00
|
|
|
summary: "test",
|
2022-01-08 04:22:29 +02:00
|
|
|
configSignoff: true,
|
|
|
|
configSkipHookPrefix: "",
|
2023-05-21 09:00:29 +02:00
|
|
|
expectedArgs: []string{"commit", "--signoff", "-m", "test"},
|
2021-04-10 03:40:42 +02:00
|
|
|
},
|
|
|
|
{
|
2022-01-08 04:22:29 +02:00
|
|
|
testName: "Commit with signoff and no-verify",
|
2023-07-22 06:05:42 +02:00
|
|
|
summary: "WIP: test",
|
2022-01-08 04:22:29 +02:00
|
|
|
configSignoff: true,
|
|
|
|
configSkipHookPrefix: "WIP",
|
2023-05-21 09:00:29 +02:00
|
|
|
expectedArgs: []string{"commit", "--no-verify", "--signoff", "-m", "WIP: test"},
|
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) {
|
2022-01-08 04:22:29 +02:00
|
|
|
userConfig := config.GetDefaultConfig()
|
|
|
|
userConfig.Git.Commit.SignOff = s.configSignoff
|
|
|
|
userConfig.Git.SkipHookPrefix = s.configSkipHookPrefix
|
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expectedArgs, "", nil)
|
|
|
|
instance := buildCommitCommands(commonDeps{userConfig: userConfig, runner: runner})
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2023-07-22 06:05:42 +02:00
|
|
|
assert.NoError(t, instance.CommitCmdObj(s.summary, s.description).Run())
|
2023-05-21 09:00:29 +02:00
|
|
|
runner.CheckForMissingCalls()
|
2021-04-10 03:40:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 17:49:59 +02:00
|
|
|
func TestCommitCommitEditorCmdObj(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
testName string
|
|
|
|
configSignoff bool
|
2023-05-21 09:00:29 +02:00
|
|
|
expected []string
|
2023-01-01 17:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
testName: "Commit using editor",
|
|
|
|
configSignoff: false,
|
2023-05-21 09:00:29 +02:00
|
|
|
expected: []string{"commit"},
|
2023-01-01 17:49:59 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "Commit with --signoff",
|
|
|
|
configSignoff: true,
|
2023-05-21 09:00:29 +02:00
|
|
|
expected: []string{"commit", "--signoff"},
|
2023-01-01 17:49:59 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
s := s
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
userConfig := config.GetDefaultConfig()
|
|
|
|
userConfig.Git.Commit.SignOff = s.configSignoff
|
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expected, "", nil)
|
|
|
|
instance := buildCommitCommands(commonDeps{userConfig: userConfig, runner: runner})
|
2023-01-01 17:49:59 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
assert.NoError(t, instance.CommitEditorCmdObj().Run())
|
|
|
|
runner.CheckForMissingCalls()
|
2023-01-01 17:49:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
func TestCommitCreateFixupCommit(t *testing.T) {
|
2021-04-10 03:40:42 +02:00
|
|
|
type scenario struct {
|
|
|
|
testName string
|
|
|
|
sha 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: "valid case",
|
|
|
|
sha: "12345",
|
|
|
|
runner: oscommands.NewFakeRunner(t).
|
2023-05-21 09:00:29 +02:00
|
|
|
ExpectGitArgs([]string{"commit", "--fixup=12345"}, "", 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)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := buildCommitCommands(commonDeps{runner: s.runner})
|
|
|
|
s.test(instance.CreateFixupCommit(s.sha))
|
2021-12-31 01:24:53 +02:00
|
|
|
s.runner.CheckForMissingCalls()
|
2021-04-10 03:40:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 22:37:46 +02:00
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
func TestCommitShowCmdObj(t *testing.T) {
|
2021-09-03 22:37:46 +02:00
|
|
|
type scenario struct {
|
2023-02-03 21:20:20 +02:00
|
|
|
testName string
|
|
|
|
filterPath string
|
|
|
|
contextSize int
|
|
|
|
ignoreWhitespace bool
|
2023-06-30 08:42:39 +02:00
|
|
|
extDiffCmd string
|
2023-05-21 09:00:29 +02:00
|
|
|
expected []string
|
2021-09-03 22:37:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2023-02-03 21:20:20 +02:00
|
|
|
testName: "Default case without filter path",
|
|
|
|
filterPath: "",
|
|
|
|
contextSize: 3,
|
|
|
|
ignoreWhitespace: false,
|
2023-06-30 08:42:39 +02:00
|
|
|
extDiffCmd: "",
|
|
|
|
expected: []string{"show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890"},
|
2021-09-03 22:37:46 +02:00
|
|
|
},
|
|
|
|
{
|
2023-02-03 21:20:20 +02:00
|
|
|
testName: "Default case with filter path",
|
|
|
|
filterPath: "file.txt",
|
|
|
|
contextSize: 3,
|
2023-04-12 13:22:16 +02:00
|
|
|
ignoreWhitespace: false,
|
2023-06-30 08:42:39 +02:00
|
|
|
extDiffCmd: "",
|
|
|
|
expected: []string{"show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--", "file.txt"},
|
2021-09-03 22:37:46 +02:00
|
|
|
},
|
|
|
|
{
|
2023-02-03 21:20:20 +02:00
|
|
|
testName: "Show diff with custom context size",
|
|
|
|
filterPath: "",
|
|
|
|
contextSize: 77,
|
|
|
|
ignoreWhitespace: false,
|
2023-06-30 08:42:39 +02:00
|
|
|
extDiffCmd: "",
|
|
|
|
expected: []string{"show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890"},
|
2021-09-03 22:37:46 +02:00
|
|
|
},
|
2023-04-12 13:22:16 +02:00
|
|
|
{
|
|
|
|
testName: "Show diff, ignoring whitespace",
|
|
|
|
filterPath: "",
|
|
|
|
contextSize: 77,
|
|
|
|
ignoreWhitespace: true,
|
2023-06-30 08:42:39 +02:00
|
|
|
extDiffCmd: "",
|
|
|
|
expected: []string{"show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890", "--ignore-all-space"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "Show diff with external diff command",
|
|
|
|
filterPath: "",
|
|
|
|
contextSize: 3,
|
|
|
|
ignoreWhitespace: false,
|
|
|
|
extDiffCmd: "difft --color=always",
|
|
|
|
expected: []string{"-c", "diff.external=difft --color=always", "show", "--ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890"},
|
2023-04-12 13:22:16 +02:00
|
|
|
},
|
2021-09-03 22:37:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
2022-01-08 06:46:35 +02:00
|
|
|
s := s
|
2021-09-03 22:37:46 +02:00
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
2022-01-08 04:22:29 +02:00
|
|
|
userConfig := config.GetDefaultConfig()
|
|
|
|
userConfig.Git.DiffContextSize = s.contextSize
|
2023-06-30 08:42:39 +02:00
|
|
|
userConfig.Git.Paging.ExternalDiffCommand = s.extDiffCmd
|
2023-08-28 12:39:52 +02:00
|
|
|
appState := &config.AppState{}
|
|
|
|
appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expected, "", nil)
|
2023-08-28 12:39:52 +02:00
|
|
|
instance := buildCommitCommands(commonDeps{userConfig: userConfig, appState: appState, runner: runner})
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2023-08-28 12:39:52 +02:00
|
|
|
assert.NoError(t, instance.ShowCmdObj("1234567890", s.filterPath).Run())
|
2023-05-21 09:00:29 +02:00
|
|
|
runner.CheckForMissingCalls()
|
2021-09-03 22:37:46 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 14:29:34 +02:00
|
|
|
|
|
|
|
func TestGetCommitMsg(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
testName string
|
|
|
|
input string
|
|
|
|
expectedOutput string
|
|
|
|
}
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
` commit deadbeef`,
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"no line breaks (single line)",
|
|
|
|
`commit deadbeef
|
|
|
|
use generics to DRY up context code`,
|
|
|
|
`use generics to DRY up context code`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with line breaks",
|
|
|
|
`commit deadbeef
|
|
|
|
Merge pull request #1750 from mark2185/fix-issue-template
|
|
|
|
|
|
|
|
'git-rev parse' should be 'git rev-parse'`,
|
|
|
|
`Merge pull request #1750 from mark2185/fix-issue-template
|
|
|
|
|
|
|
|
'git-rev parse' should be 'git rev-parse'`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
s := s
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
instance := buildCommitCommands(commonDeps{
|
2023-05-21 09:00:29 +02:00
|
|
|
runner: oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"rev-list", "--format=%B", "--max-count=1", "deadbeef"}, s.input, nil),
|
2022-03-21 14:29:34 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
output, err := instance.GetCommitMessage("deadbeef")
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, s.expectedOutput, output)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 13:38:14 +02:00
|
|
|
|
|
|
|
func TestGetCommitMessageFromHistory(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
testName string
|
|
|
|
runner *oscommands.FakeCmdObjRunner
|
|
|
|
test func(string, error)
|
|
|
|
}
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"Empty message",
|
2023-05-21 09:00:29 +02:00
|
|
|
oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"log", "-1", "--skip=2", "--pretty=%H"}, "", nil).ExpectGitArgs([]string{"rev-list", "--format=%B", "--max-count=1"}, "", nil),
|
2023-01-21 13:38:14 +02:00
|
|
|
func(output string, err error) {
|
|
|
|
assert.Error(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Default case to retrieve a commit in history",
|
2023-05-21 09:00:29 +02:00
|
|
|
oscommands.NewFakeRunner(t).ExpectGitArgs([]string{"log", "-1", "--skip=2", "--pretty=%H"}, "sha3 \n", nil).ExpectGitArgs([]string{"rev-list", "--format=%B", "--max-count=1", "sha3"}, `commit sha3
|
2023-01-21 13:38:14 +02:00
|
|
|
use generics to DRY up context code`, nil),
|
|
|
|
func(output string, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "use generics to DRY up context code", output)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
s := s
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
instance := buildCommitCommands(commonDeps{runner: s.runner})
|
|
|
|
|
|
|
|
output, err := instance.GetCommitMessageFromHistory(2)
|
|
|
|
|
|
|
|
s.test(output, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|