summaryrefslogtreecommitdiffstats
path: root/pkg/commands/commits_test.go
blob: 4ca36969ad5e606d1340798e225e73444885f105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package commands

import (
	"testing"

	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
	"github.com/stretchr/testify/assert"
)

func TestGitCommandRenameCommit(t *testing.T) {
	runner := oscommands.NewFakeRunner(t).
		ExpectArgs([]string{"git", "commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil)
	gitCmd := NewDummyGitCommandWithRunner(runner)

	assert.NoError(t, gitCmd.RenameCommit("test"))
	runner.CheckForMissingCalls()
}

func TestGitCommandResetToCommit(t *testing.T) {
	runner := oscommands.NewFakeRunner(t).
		ExpectArgs([]string{"git", "reset", "--hard", "78976bc"}, "", nil)
	gitCmd := NewDummyGitCommandWithRunner(runner)

	assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard", []string{}))
	runner.CheckForMissingCalls()
}

func TestGitCommandCommitObj(t *testing.T) {
	gitCmd := NewDummyGitCommand()

	type scenario struct {
		testName string
		message  string
		flags    string
		expected string
	}

	scenarios := []scenario{
		{
			testName: "Commit",
			message:  "test",
			flags:    "",
			expected: "git commit -m " + gitCmd.OSCommand.Quote("test"),
		},
		{
			testName: "Commit with --no-verify flag",
			message:  "test",
			flags:    "--no-verify",
			expected: "git commit --no-verify -m " + gitCmd.OSCommand.Quote("test"),
		},
		{
			testName: "Commit with multiline message",
			message:  "line1\nline2",
			flags:    "",
			expected: "git commit -m " + gitCmd.OSCommand.Quote("line1") + " -m " + gitCmd.OSCommand.Quote("line2"),
		},
	}

	for _, s := range scenarios {
		t.Run(s.testName, func(t *testing.T) {
			cmdStr := gitCmd.CommitCmdObj(s.message, s.flags).ToString()
			assert.Equal(t, s.expected, cmdStr)
		})
	}
}

func TestGitCommandCreateFixupCommit(t *testing.T) {
	type scenario struct {
		testName string
		sha      string
		runner   *oscommands.FakeCmdObjRunner
		test     func(error)
	}

	scenarios := []scenario{
		{
			testName: "valid case",
			sha:      "12345",
			runner: oscommands.NewFakeRunner(t).
				Expect(`git commit --fixup=12345`, "", nil),
			test: func(err error) {
				assert.NoError(t, err)
			},
		},
	}

	for _, s := range scenarios {
		t.Run(s.testName, func(t *testing.T) {
			gitCmd := NewDummyGitCommandWithRunner(s.runner)
			s.test(gitCmd.CreateFixupCommit(s.sha))
			s.runner.CheckForMissingCalls()
		})
	}
}

func TestGitCommandShowCmdObj(t *testing.T) {
	type scenario struct {
		testName    string
		filterPath  string
		contextSize int
		expected    string
	}

	gitCmd := NewDummyGitCommand()

	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"),
		},
		{
			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.UserConfig.Git.DiffContextSize = s.contextSize
			cmdStr := gitCmd.ShowCmdObj("1234567890", s.filterPath).ToString()
			assert.Equal(t, s.expected, cmdStr)
		})
	}
}