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
|
package components
import (
"fmt"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/jesseduffield/lazygit/pkg/config"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
)
type TestDriver struct {
gui integrationTypes.GuiDriver
keys config.KeybindingConfig
pushKeyDelay int
*assertionHelper
shell *Shell
}
func NewTestDriver(gui integrationTypes.GuiDriver, shell *Shell, keys config.KeybindingConfig, pushKeyDelay int) *TestDriver {
return &TestDriver{
gui: gui,
keys: keys,
pushKeyDelay: pushKeyDelay,
assertionHelper: &assertionHelper{gui: gui},
shell: shell,
}
}
// key is something like 'w' or '<space>'. It's best not to pass a direct value,
// but instead to go through the default user config to get a more meaningful key name
func (self *TestDriver) press(keyStr string) {
self.Wait(self.pushKeyDelay)
self.gui.PressKey(keyStr)
}
// Should only be used in specific cases where you're doing something weird!
// E.g. invoking a global keybinding from within a popup.
// You probably shouldn't use this function, and should instead go through a view like t.Views().Commit().Focus().Press(...)
func (self *TestDriver) GlobalPress(keyStr string) {
self.press(keyStr)
}
func (self *TestDriver) typeContent(content string) {
for _, char := range content {
self.press(string(char))
}
}
func (self *TestDriver) Actions() *Actions {
return &Actions{t: self}
}
// for when you want to allow lazygit to process something before continuing
func (self *TestDriver) Wait(milliseconds int) {
time.Sleep(time.Duration(milliseconds) * time.Millisecond)
}
func (self *TestDriver) LogUI(message string) {
self.gui.LogUI(message)
}
func (self *TestDriver) Log(message string) {
self.gui.LogUI(message)
}
// allows the user to run shell commands during the test to emulate background activity
func (self *TestDriver) Shell() *Shell {
return self.shell
}
// this will look for a list item in the current panel and if it finds it, it will
// enter the keypresses required to navigate to it.
// The test will fail if:
// - the user is not in a list item
// - no list item is found containing the given text
// - multiple list items are found containing the given text in the initial page of items
//
// NOTE: this currently assumes that ViewBufferLines returns all the lines that can be accessed.
// If this changes in future, we'll need to update this code to first attempt to find the item
// in the current page and failing that, jump to the top of the view and iterate through all of it,
// looking for the item.
func (self *TestDriver) navigateToListItem(matcher *matcher) {
currentContext := self.gui.CurrentContext()
view := currentContext.GetView()
var matchIndex int
self.assertWithRetries(func() (bool, string) {
matchIndex = -1
var matches []string
lines := view.ViewBufferLines()
// first we look for a duplicate on the current screen. We won't bother looking beyond that though.
for i, line := range lines {
ok, _ := matcher.test(line)
if ok {
matches = append(matches, line)
matchIndex = i
}
}
if len(matches) > 1 {
return false, fmt.Sprintf("Found %d matches for `%s`, expected only a single match. Matching lines:\n%s", len(matches), matcher.name(), strings.Join(matches, "\n"))
} else if len(matches) == 0 {
return false, fmt.Sprintf("Could not find item matching: %s. Lines:\n%s", matcher.name(), strings.Join(lines, "\n"))
} else {
return true, ""
}
})
selectedLineIdx := view.SelectedLineIdx()
if selectedLineIdx == matchIndex {
self.Views().current().SelectedLine(matcher)
return
}
if selectedLineIdx < matchIndex {
for i := selectedLineIdx; i < matchIndex; i++ {
self.Views().current().SelectNextItem()
}
self.Views().current().SelectedLine(matcher)
return
} else {
for i := selectedLineIdx; i > matchIndex; i-- {
self.Views().current().SelectPreviousItem()
}
self.Views().current().SelectedLine(matcher)
return
}
}
// for making assertions on lazygit views
func (self *TestDriver) Views() *Views {
return &Views{t: self}
}
// for interacting with popups
func (self *TestDriver) ExpectPopup() *Popup {
return &Popup{t: self}
}
func (self *TestDriver) ExpectToast(matcher *matcher) {
self.Views().AppStatus().Content(matcher)
}
func (self *TestDriver) ExpectClipboard(matcher *matcher) {
self.assertWithRetries(func() (bool, string) {
text, err := clipboard.ReadAll()
if err != nil {
return false, "Error occured when reading from clipboard: " + err.Error()
}
ok, _ := matcher.test(text)
return ok, fmt.Sprintf("Expected clipboard to match %s, but got %s", matcher.name(), text)
})
}
func (self *TestDriver) ExpectSearch() *SearchDriver {
self.inSearch()
return &SearchDriver{t: self}
}
func (self *TestDriver) inSearch() {
self.assertWithRetries(func() (bool, string) {
currentView := self.gui.CurrentContext().GetView()
return currentView.Name() == "search", "Expected search prompt to be focused"
})
}
// for making assertions through git itself
func (self *TestDriver) Git() *Git {
return &Git{assertionHelper: self.assertionHelper, shell: self.shell}
}
// for making assertions on the file system
func (self *TestDriver) FileSystem() *FileSystem {
return &FileSystem{assertionHelper: self.assertionHelper}
}
// for when you just want to fail the test yourself.
// This runs callbacks to ensure we render the error after closing the gui.
func (self *TestDriver) Fail(message string) {
self.assertionHelper.fail(message)
}
|