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

164 lines
4.3 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 (
"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) {
2021-12-31 01:24:53 +02:00
runner := oscommands.NewFakeRunner(t).
2022-01-03 06:15:26 +02:00
ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil)
2022-01-08 04:22:29 +02:00
instance := buildCommitCommands(commonDeps{runner: runner})
2021-04-10 03:40:42 +02:00
2022-01-08 04:22:29 +02:00
assert.NoError(t, instance.RewordLastCommit("test"))
2021-12-31 01:24:53 +02:00
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
}
2022-01-08 04:22:29 +02:00
func TestCommitCommitObj(t *testing.T) {
2021-04-10 03:40:42 +02:00
type scenario struct {
2022-01-08 04:22:29 +02:00
testName string
message string
configSignoff bool
configSkipHookPrefix string
expected string
2021-04-10 03:40:42 +02:00
}
scenarios := []scenario{
{
2022-01-08 04:22:29 +02:00
testName: "Commit",
message: "test",
configSignoff: false,
configSkipHookPrefix: "",
expected: `git commit -m "test"`,
},
{
testName: "Commit with --no-verify flag",
message: "WIP: test",
configSignoff: false,
configSkipHookPrefix: "WIP",
expected: `git commit --no-verify -m "WIP: test"`,
},
{
testName: "Commit with multiline message",
message: "line1\nline2",
configSignoff: false,
configSkipHookPrefix: "",
expected: `git 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",
message: "test",
configSignoff: true,
configSkipHookPrefix: "",
expected: `git 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",
message: "WIP: test",
configSignoff: true,
configSkipHookPrefix: "WIP",
expected: `git 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
instance := buildCommitCommands(commonDeps{userConfig: userConfig})
cmdStr := instance.CommitCmdObj(s.message).ToString()
2021-04-10 03:40:42 +02:00
assert.Equal(t, s.expected, cmdStr)
})
}
}
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).
Expect(`git commit --fixup=12345`, "", nil),
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 {
testName string
filterPath string
contextSize int
expected string
}
scenarios := []scenario{
{
testName: "Default case without filter path",
filterPath: "",
contextSize: 3,
expected: "git show --submodule --color=always --unified=3 --no-renames --stat -p 1234567890 ",
},
{
testName: "Default case with filter path",
filterPath: "file.txt",
contextSize: 3,
2022-01-03 06:15:26 +02:00
expected: `git show --submodule --color=always --unified=3 --no-renames --stat -p 1234567890 -- "file.txt"`,
2021-09-03 22:37:46 +02:00
},
{
testName: "Show diff with custom context size",
filterPath: "",
contextSize: 77,
expected: "git show --submodule --color=always --unified=77 --no-renames --stat -p 1234567890 ",
},
}
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
instance := buildCommitCommands(commonDeps{userConfig: userConfig})
cmdStr := instance.ShowCmdObj("1234567890", s.filterPath).ToString()
2021-09-03 22:37:46 +02:00
assert.Equal(t, s.expected, cmdStr)
})
}
}