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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
package gui
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/creack/pty"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/stretchr/testify/assert"
)
// To run an integration test, e.g. for test 'commit', go:
// go test pkg/gui/gui_test.go -run /commit
//
// To record keypresses for an integration test, pass RECORD_EVENTS=true like so:
// RECORD_EVENTS=true go test pkg/gui/gui_test.go -run /commit
//
// To update a snapshot for an integration test, pass UPDATE_SNAPSHOT=true
// UPDATE_SNAPSHOT=true go test pkg/gui/gui_test.go -run /commit
//
// When RECORD_EVENTS is true, updates will be updated automatically
//
// integration tests are run in test/integration_test and the final test does
// not clean up that directory so you can cd into it to see for yourself what
// happened when a test failed.
//
// TODO: support passing an env var for playback speed, given it's currently pretty fast
type integrationTest struct {
name string
fixture string
}
func tests() []integrationTest {
return []integrationTest{
{
name: "commit",
fixture: "newFile",
},
{
name: "squash",
fixture: "manyCommits",
},
{
name: "patchBuilding",
fixture: "updatedFile",
},
{
name: "patchBuilding2",
fixture: "updatedFile",
},
{
name: "mergeConflicts",
fixture: "mergeConflicts",
},
{
name: "searching",
fixture: "newFile",
},
{
name: "searchingInStagingPanel",
fixture: "newFile2",
},
}
}
func generateSnapshot(t *testing.T) string {
osCommand := oscommands.NewDummyOSCommand()
cmd := `sh -c "git status; cat ./*; git log --pretty=%B -p"`
snapshot, err := osCommand.RunCommandWithOutput(cmd)
assert.NoError(t, err)
return snapshot
}
func findOrCreateDir(path string) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(path, 0777)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
}
func Test(t *testing.T) {
tests := tests()
gotoRootDirectory()
rootDir, err := os.Getwd()
if err != nil {
panic(err)
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
testPath := filepath.Join(rootDir, "test", "integration", test.name)
findOrCreateDir(testPath)
snapshotPath := filepath.Join(testPath, "snapshot.txt")
err := os.Chdir(rootDir)
assert.NoError(t, err)
prepareIntegrationTestDir()
err = createFixture(rootDir, test.fixture)
assert.NoError(t, err)
record := os.Getenv("RECORD_EVENTS") != ""
runLazygit(t, testPath, rootDir, record)
updateSnapshot := record || os.Getenv("UPDATE_SNAPSHOT") != ""
actual := generateSnapshot(t)
if updateSnapshot {
err := ioutil.WriteFile(snapshotPath, []byte(actual), 0600)
assert.NoError(t, err)
}
expectedBytes, err := ioutil.ReadFile(snapshotPath)
assert.NoError(t, err)
expected := string(expectedBytes)
assert.Equal(t, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual))
})
}
}
func createFixture(rootDir string, name string) error {
osCommand := oscommands.NewDummyOSCommand()
cmd := exec.Command("sh", filepath.Join(rootDir, "test", "fixtures", fmt.Sprintf("%s.sh", name)))
if err := osCommand.RunExecutable(cmd); err != nil {
return err
}
return nil
}
func gotoRootDirectory() {
for {
_, err := os.Stat(".git")
if err == nil {
return
}
if !os.IsNotExist(err) {
panic(err)
}
if err = os.Chdir(".."); err != nil {
panic(err)
}
}
}
func runLazygit(t *testing.T, testPath string, rootDir string, record bool) {
osCommand := oscommands.NewDummyOSCommand()
replayPath := filepath.Join(testPath, "recording.json")
cmdStr := fmt.Sprintf("go run %s", filepath.Join(rootDir, "main.go"))
templateConfigDir := filepath.Join(rootDir, "test", "default_test_config")
exists, err := osCommand.FileExists(filepath.Join(testPath, "config"))
assert.NoError(t, err)
if exists {
templateConfigDir = filepath.Join(testPath, "config")
}
configDir := filepath.Join(rootDir, "test", "integration_test_config")
err = os.RemoveAll(configDir)
assert.NoError(t, err)
err = oscommands.CopyDir(templateConfigDir, configDir)
assert.NoError(t, err)
cmdStr = fmt.Sprintf("%s --use-config-dir=%s", cmdStr, configDir)
cmd := osCommand.ExecutableFromString(cmdStr)
if record {
cmd.Env = append(
cmd.Env,
fmt.Sprintf("RECORD_EVENTS_TO=%s", replayPath),
)
} else {
cmd.Env = append(
cmd.Env,
fmt.Sprintf("REPLAY_EVENTS_FROM=%s", replayPath),
)
}
// if we're on CI we'll need to use a PTY. We can work that out by seeing if the 'TERM' env is defined.
if os.Getenv("TERM") == "" {
cmd.Env = append(cmd.Env, "TERM=xterm")
f, err := pty.StartWithSize(cmd, &pty.Winsize{Rows: 100, Cols: 100})
assert.NoError(t, err)
_, err = io.Copy(os.Stdout, f)
assert.NoError(t, err)
} else {
err := osCommand.RunExecutable(cmd)
assert.NoError(t, err)
}
}
func prepareIntegrationTestDir() {
path := filepath.Join("test", "integration_test")
// remove contents of integration test directory
dir, err := ioutil.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(path, 0777)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
for _, d := range dir {
os.RemoveAll(filepath.Join(path, d.Name()))
}
if err := os.Chdir(path); err != nil {
panic(err)
}
}
|