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

153 lines
3.8 KiB
Go
Raw Normal View History

2021-04-10 03:40:42 +02:00
package commands
import (
"os/exec"
"testing"
"github.com/jesseduffield/lazygit/pkg/secureexec"
"github.com/jesseduffield/lazygit/pkg/test"
"github.com/stretchr/testify/assert"
)
// TestGitCommandRenameCommit is a function.
func TestGitCommandRenameCommit(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, args)
return secureexec.Command("echo")
}
assert.NoError(t, gitCmd.RenameCommit("test"))
}
// TestGitCommandResetToCommit is a function.
func TestGitCommandResetToCommit(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"reset", "--hard", "78976bc"}, args)
return secureexec.Command("echo")
}
2021-12-07 12:59:36 +02:00
assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard", []string{}))
2021-04-10 03:40:42 +02:00
}
2021-12-07 12:59:36 +02:00
// TestGitCommandCommitObj is a function.
func TestGitCommandCommitObj(t *testing.T) {
2021-10-09 07:53:43 +02:00
gitCmd := NewDummyGitCommand()
2021-04-10 03:40:42 +02:00
type scenario struct {
testName string
message string
flags string
expected string
}
scenarios := []scenario{
{
testName: "Commit",
message: "test",
flags: "",
2021-10-09 07:53:43 +02:00
expected: "git commit -m " + gitCmd.OSCommand.Quote("test"),
2021-04-10 03:40:42 +02:00
},
{
testName: "Commit with --no-verify flag",
message: "test",
flags: "--no-verify",
2021-10-09 07:53:43 +02:00
expected: "git commit --no-verify -m " + gitCmd.OSCommand.Quote("test"),
2021-04-10 03:40:42 +02:00
},
{
testName: "Commit with multiline message",
message: "line1\nline2",
flags: "",
2021-10-09 07:53:43 +02:00
expected: "git commit -m " + gitCmd.OSCommand.Quote("line1") + " -m " + gitCmd.OSCommand.Quote("line2"),
2021-04-10 03:40:42 +02:00
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
2021-12-07 12:59:36 +02:00
cmdStr := gitCmd.CommitCmdObj(s.message, s.flags).ToString()
2021-04-10 03:40:42 +02:00
assert.Equal(t, s.expected, cmdStr)
})
}
}
// TestGitCommandCreateFixupCommit is a function.
func TestGitCommandCreateFixupCommit(t *testing.T) {
type scenario struct {
testName string
sha string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"valid case",
"12345",
test.CreateMockCommand(t, []*test.CommandSwapper{
{
Expect: `git commit --fixup=12345`,
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err)
},
},
}
gitCmd := NewDummyGitCommand()
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.Command = s.command
s.test(gitCmd.CreateFixupCommit(s.sha))
})
}
}
2021-09-03 22:37:46 +02:00
2021-12-07 12:59:36 +02:00
// TestGitCommandShowCmdObj is a function.
func TestGitCommandShowCmdObj(t *testing.T) {
2021-09-03 22:37:46 +02:00
type scenario struct {
testName string
filterPath string
contextSize int
expected string
}
gitCmd := NewDummyGitCommand()
2021-09-03 22:37:46 +02:00
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,
expected: "git show --submodule --color=always --unified=3 --no-renames --stat -p 1234567890 -- " + gitCmd.OSCommand.Quote("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 {
t.Run(s.testName, func(t *testing.T) {
gitCmd.Config.GetUserConfig().Git.DiffContextSize = s.contextSize
2021-12-07 12:59:36 +02:00
cmdStr := gitCmd.ShowCmdObj("1234567890", s.filterPath).ToString()
2021-09-03 22:37:46 +02:00
assert.Equal(t, s.expected, cmdStr)
})
}
}