-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtask_exec_tui_test.go
More file actions
390 lines (326 loc) · 11.6 KB
/
Copy pathtask_exec_tui_test.go
File metadata and controls
390 lines (326 loc) · 11.6 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package ui
import (
"runtime"
"strconv"
"testing"
"time"
"github.com/google/go-cmp/cmp"
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
"github.com/sourcegraph/sourcegraph/lib/output"
"github.com/sourcegraph/src-cli/internal/batches/executor"
"github.com/sourcegraph/src-cli/internal/batches/graphql"
)
var progressPrinterDiff = []byte(`diff --git README.md README.md
new file mode 100644
index 0000000..3363c39
--- /dev/null
+++ README.md
@@ -0,0 +1,3 @@
+# README
+
+This is the readme
diff --git a/b/c/c.txt a/b/c/c.txt
deleted file mode 100644
index 5da75cf..0000000
--- a/b/c/c.txt
+++ /dev/null
@@ -1 +0,0 @@
-this is c
diff --git x/x.txt x/x.txt
index 627c2ae..88f1836 100644
--- x/x.txt
+++ x/x.txt
@@ -1 +1 @@
-this is x
+this is x (or is it?)
`)
func TestTaskExecTUI_Integration(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Something emits different escape codes on windows.")
}
now := time.Now()
clock := func() time.Time { return now.UTC().Truncate(time.Millisecond) }
advanceClock := func(d time.Duration) { now = now.Add(d) }
buf := &ttyBuf{}
true_ := true
out := output.NewOutput(buf, output.OutputOpts{
ForceTTY: &true_,
ForceColor: true,
ForceHeight: 25,
ForceWidth: 80,
Verbose: true,
})
tasks := []*executor.Task{
{Repository: &graphql.Repository{Name: "github.com/sourcegraph/sourcegraph"}},
{Repository: &graphql.Repository{Name: "github.com/sourcegraph/src-cli"}},
{Repository: &graphql.Repository{Name: "github.com/sourcegraph/automation-testing"}},
{Repository: &graphql.Repository{Name: "github.com/sourcegraph/tiny-go-repo"}},
}
printer := newTaskExecTUI(out, true, 3)
printer.forceNoSpinner = true
printer.clock = clock
// Setup internal state
printer.Start(tasks)
// Start the first 3 tasks
printer.TaskStarted(tasks[0])
printer.TaskStarted(tasks[1])
printer.TaskStarted(tasks[2])
expectOutput(t, buf, []string{
"⠋ Executing... (0/4, 0 errored) 0%",
"│ ",
"├── github.com/sourcegraph/sourcegraph ... 0s",
"├── github.com/sourcegraph/src-cli ... 0s",
"└── github.com/sourcegraph/automati... ... 0s",
"",
})
// Update the currently executing of the first 3
printer.TaskCurrentlyExecuting(tasks[0], "echo Hello World > README.md")
printer.TaskCurrentlyExecuting(tasks[1], "Downloading archive")
printer.TaskCurrentlyExecuting(tasks[2], "echo Hello World > README.md")
expectOutput(t, buf, []string{
"⠋ Executing... (0/4, 0 errored) 0%",
"│ ",
"├── github.com/sourcegraph/sourcegraph echo Hello World > README.md 0s",
"├── github.com/sourcegraph/src-cli Downloading archive 0s",
"└── github.com/sourcegraph/automati... echo Hello World > README.md 0s",
"",
})
// Update it again
printer.TaskCurrentlyExecuting(tasks[0], "gofmt")
printer.TaskCurrentlyExecuting(tasks[1], "echo Hello World > README.md")
printer.TaskCurrentlyExecuting(tasks[2], "echo Hello World > README.md")
expectOutput(t, buf, []string{
"⠋ Executing... (0/4, 0 errored) 0%",
"│ ",
"├── github.com/sourcegraph/sourcegraph gofmt 0s",
"├── github.com/sourcegraph/src-cli echo Hello World > README.md 0s",
"└── github.com/sourcegraph/automati... echo Hello World > README.md 0s",
"",
})
// Now mark the last task as finished-execution
advanceClock(10 * time.Second)
printer.TaskFinished(tasks[2], nil)
expectOutput(t, buf, []string{
"⠋ Executing... (1/4, 0 errored) ██████████▎ 25%",
"│ ",
"├── github.com/sourcegraph/sourcegraph gofmt 0s",
"├── github.com/sourcegraph/src-cli echo Hello World > README.md 0s",
"└── github.com/sourcegraph/automati... Done! 0s",
"",
})
// Now mark the first task as finished-execution
advanceClock(5 * time.Second)
printer.TaskFinished(tasks[0], nil)
expectOutput(t, buf, []string{
"⠋ Executing... (2/4, 0 errored) ████████████████████▌ 50%",
"│ ",
"├── github.com/sourcegraph/sourcegraph Done! 0s",
"├── github.com/sourcegraph/src-cli echo Hello World > README.md 0s",
"└── github.com/sourcegraph/automati... Done! 0s",
"",
})
// Mark the last task as finished-building-specs
printer.TaskChangesetSpecsBuilt(tasks[2], []*batcheslib.ChangesetSpec{
{
BaseRepository: "graphql-id",
BaseRef: "refs/heads/main",
BaseRev: "d34db33f",
HeadRepository: "graphql-id",
HeadRef: "refs/heads/my-batch-change",
Title: "This is my batch change",
Body: "This is my batch change",
Commits: []batcheslib.GitCommitDescription{
{
Version: 2,
Message: "This is my batch change",
Diff: progressPrinterDiff,
},
},
Published: batcheslib.PublishedValue{Val: false},
},
})
expectOutput(t, buf, []string{
"github.com/sourcegraph/automation-testing",
"\tREADME.md | 3 +++",
"\ta/b/c/c.txt | 1 -",
"\tx/x.txt | 2 +-",
" 3 files changed, 4 insertions, 2 deletions",
" Execution took 10s",
"",
"⠋ Executing... (2/4, 0 errored) ████████████████████▌ 50%",
"│ ",
"├── github.com/sourcegraph/sourcegraph Done! 0s",
"├── github.com/sourcegraph/src-cli echo Hello World > README.md 0s",
"└── github.com/sourcegraph/automati... Done! 0s",
"",
})
// Now we start the 4th task
printer.TaskStarted(tasks[3])
printer.TaskCurrentlyExecuting(tasks[3], "rm -rf ~/.horse-ascii-art")
expectOutput(t, buf, []string{
"github.com/sourcegraph/automation-testing",
"\tREADME.md | 3 +++",
"\ta/b/c/c.txt | 1 -",
"\tx/x.txt | 2 +-",
" 3 files changed, 4 insertions, 2 deletions",
" Execution took 10s",
"",
"⠋ Executing... (2/4, 0 errored) ████████████████████▌ 50%",
"│ ",
"├── github.com/sourcegraph/tiny-go-... rm -rf ~/.horse-ascii-art 0s",
"├── github.com/sourcegraph/src-cli echo Hello World > README.md 0s",
"└── github.com/sourcegraph/automati... Done! 0s",
"",
})
}
func TestProgressUpdateAfterComplete(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Something emits different escape codes on windows.")
}
buf := &ttyBuf{}
now := time.Now()
clock := func() time.Time { return now.UTC().Truncate(time.Millisecond) }
true_ := true
out := output.NewOutput(buf, output.OutputOpts{
ForceTTY: &true_,
ForceColor: true,
ForceHeight: 25,
ForceWidth: 80,
Verbose: true,
})
tasks := []*executor.Task{
{Repository: &graphql.Repository{Name: "github.com/sourcegraph/sourcegraph"}},
{Repository: &graphql.Repository{Name: "github.com/sourcegraph/src-cli"}},
}
printer := newTaskExecTUI(out, true, 2)
printer.forceNoSpinner = true
printer.clock = clock
// Setup internal state.
printer.Start(tasks)
// Start the tasks.
printer.TaskStarted(tasks[0])
printer.TaskStarted(tasks[1])
// Update the tasks into a useful state.
printer.TaskCurrentlyExecuting(tasks[0], "echo Hello World > README.md")
printer.TaskCurrentlyExecuting(tasks[1], "Downloading archive")
expectOutput(t, buf, []string{
"⠋ Executing... (0/2, 0 errored) 0%",
"│ ",
"├── github.com/sourcegraph/sourcegraph echo Hello World > README.md 0s",
"└── github.com/sourcegraph/src-cli Downloading archive 0s",
"",
})
// Now mark the progress as complete.
printer.progress.Complete()
// Now send another update. This would panic before the relevant fix was
// merged in #666.
printer.TaskCurrentlyExecuting(tasks[0], "exit 42")
// The actual output is slightly less important at this point, but let's
// check it anyway.
expectOutput(t, buf, []string{
"✅ Executing... (0/2, 0 errored) ████████████████████████████████████████ 100%",
"│ ",
"├── github.com/sourcegraph/sourcegraph exit 42 0s",
"└── github.com/sourcegraph/src-cli Downloading archive 0s",
"",
})
}
type ttyBuf struct {
lines [][]byte
line int
column int
}
func (t *ttyBuf) Write(b []byte) (int, error) {
var cur int
for cur < len(b) {
switch b[cur] {
case '\n':
t.line++
t.column = 0
if len(t.lines) == t.line {
t.lines = append(t.lines, []byte{})
}
case '\x1b':
// Check if we're looking at a VT100 escape code.
if len(b) <= cur || b[cur+1] != '[' {
t.writeToCurrentLine(b[cur])
cur++
continue
}
// First of all: forgive me.
//
// Now. Looks like we ran into a VT100 escape code.
// They follow this structure:
//
// \x1b [ <digit> <command>
//
// So we jump over the \x1b[ and try to parse the digit.
cur = cur + 2 // cur == '\x1b', cur + 1 == '['
digitStart := cur
for isDigit(b[cur]) {
cur++
}
rawDigit := string(b[digitStart:cur])
digit, err := strconv.ParseInt(rawDigit, 0, 64)
if err != nil {
return 0, err
}
command := b[cur]
// Debug helper:
// fmt.Printf("command=%q, digit=%d (t.line=%d, t.column=%d)\n", command, digit, t.line, t.column)
switch command {
case 'K':
// reset current line
if len(t.lines) > t.line {
t.lines[t.line] = []byte{}
t.column = 0
}
case 'A':
// move line up by <digit>
t.line = t.line - int(digit)
case 'D':
// *d*elete cursor by <digit> amount
t.column = max(t.column-int(digit), 0)
case 'm':
// noop
case ';':
// color, skip over until end of color command
for b[cur] != 'm' {
cur++
}
}
default:
t.writeToCurrentLine(b[cur])
}
cur++
}
return len(b), nil
}
func (t *ttyBuf) writeToCurrentLine(b byte) {
if len(t.lines) == t.line {
t.lines = append(t.lines, []byte{})
}
if len(t.lines[t.line]) <= t.column {
t.lines[t.line] = append(t.lines[t.line], b)
} else {
t.lines[t.line][t.column] = b
}
t.column++
}
func (t *ttyBuf) Lines() []string {
var lines []string
for _, l := range t.lines {
lines = append(lines, string(l))
}
return lines
}
func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
func expectOutput(t *testing.T, buf *ttyBuf, want []string) {
t.Helper()
have := buf.Lines()
if !cmp.Equal(want, have) {
t.Fatalf("wrong output:\n%s", cmp.Diff(want, have))
}
}