summaryrefslogtreecommitdiffstats
path: root/pkg/commands/loaders/reflog_commits_test.go
blob: bb93caba5126fe945db39f202e55cc09983bf8d1 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package loaders

import (
	"errors"
	"strings"
	"testing"

	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
	"github.com/jesseduffield/lazygit/pkg/utils"
	"github.com/sanity-io/litter"
	"github.com/stretchr/testify/assert"
)

var reflogOutput = strings.Replace(`c3c4b66b64c97ffeecde|1643150483|checkout: moving from A to B
c3c4b66b64c97ffeecde|1643150483|checkout: moving from B to A
c3c4b66b64c97ffeecde|1643150483|checkout: moving from A to B
c3c4b66b64c97ffeecde|1643150483|checkout: moving from master to A
f4ddf2f0d4be4ccc7efa|1643149435|checkout: moving from A to master
`, "|", "\x00", -1)

func TestGetReflogCommits(t *testing.T) {
	type scenario struct {
		testName                string
		runner                  *oscommands.FakeCmdObjRunner
		lastReflogCommit        *models.Commit
		filterPath              string
		expectedCommits         []*models.Commit
		expectedOnlyObtainedNew bool
		expectedError           error
	}

	scenarios := []scenario{
		{
			testName: "no reflog entries",
			runner: oscommands.NewFakeRunner(t).
				Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, "", nil),

			lastReflogCommit:        nil,
			expectedCommits:         []*models.Commit{},
			expectedOnlyObtainedNew: false,
			expectedError:           nil,
		},
		{
			testName: "some reflog entries",
			runner: oscommands.NewFakeRunner(t).
				Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, reflogOutput, nil),

			lastReflogCommit: nil,
			expectedCommits: []*models.Commit{
				{
					Sha:           "c3c4b66b64c97ffeecde",
					Name:          "checkout: moving from A to B",
					Status:        "reflog",
					UnixTimestamp: 1643150483,
				},
				{
					Sha:           "c3c4b66b64c97ffeecde",
					Name:          "checkout: moving from B to A",
					Status:        "reflog",
					UnixTimestamp: 1643150483,
				},
				{
					Sha:           "c3c4b66b64c97ffeecde",
					Name:          "checkout: moving from A to B",
					Status:        "reflog",
					UnixTimestamp: 1643150483,
				},
				{
					Sha:           "c3c4b66b64c97ffeecde",
					Name:          "checkout: moving from master to A",
					Status:        "reflog",
					UnixTimestamp: 1643150483,
				},
				{
					Sha:           "f4ddf2f0d4be4ccc7efa",
					Name:          "checkout: moving from A to master",
					Status:        "reflog",
					UnixTimestamp: 1643149435,
				},
			},
			expectedOnlyObtainedNew: false,
			expectedError:           nil,
		},
		{
			testName: "some reflog entries where last commit is given",
			runner: oscommands.NewFakeRunner(t).
				Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, reflogOutput, nil),

			lastReflogCommit: &models.Commit{
				Sha:           "c3c4b66b64c97ffeecde",
				Name:          "checkout: moving from B to A",
				Status:        "reflog",
				UnixTimestamp: 1643150483,
			},
			expectedCommits: []*models.Commit{
				{
					Sha:           "c3c4b66b64c97ffeecde",
					Name:          "checkout: moving from A to B",
					Status:        "reflog",
					UnixTimestamp: 1643150483,
				},
			},
			expectedOnlyObtainedNew: true,
			expectedError:           nil,
		},
		{
			testName: "when passing filterPath",
			runner: oscommands.NewFakeRunner(t).
				Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs" --follow -- "path"`, reflogOutput, nil),

			lastReflogCommit: &models.Commit{
				Sha:           "c3c4b66b64c97ffeecde",
				Name:          "checkout: moving from B to A",
				Status:        "reflog",
				UnixTimestamp: 1643150483,
			},
			filterPath: "path",
			expectedCommits: []*models.Commit{
				{
					Sha:           "c3c4b66b64c97ffeecde",
					Name:          "checkout: moving from A to B",
					Status:        "reflog",
					UnixTimestamp: 1643150483,
				},
			},
			expectedOnlyObtainedNew: true,
			expectedError:           nil,
		},
		{
			testName: "when command returns error",
			runner: oscommands.NewFakeRunner(t).
				Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, "", errors.New("haha")),

			lastReflogCommit:        nil,
			filterPath:              "",
			expectedCommits:         nil,
			expectedOnlyObtainedNew: false,
			expectedError:           errors.New("haha"),
		},
	}

	for _, scenario := range scenarios {
		scenario := scenario
		t.Run(scenario.testName, func(t *testing.T) {
			builder := &ReflogCommitLoader{
				Common: utils.NewDummyCommon(),
				cmd:    oscommands.NewDummyCmdObjBuilder(scenario.runner),
			}

			commits, onlyObtainednew, err := builder.GetReflogCommits(scenario.lastReflogCommit, scenario.filterPath)
			assert.Equal(t, scenario.expectedOnlyObtainedNew, onlyObtainednew)
			assert.Equal(t, scenario.expectedError, err)
			t.Logf("actual commits: \n%s", litter.Sdump(commits))
			assert.Equal(t, scenario.expectedCommits, commits)

			scenario.runner.CheckForMissingCalls()
		})
	}
}