Repository navigation
Expand file tree
/
Copy pathprogress_test.go
More file actions
149 lines (118 loc) · 4.39 KB
/
Copy pathprogress_test.go
File metadata and controls
149 lines (118 loc) · 4.39 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
package main
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// clock is a fake time source that only moves when told to.
type clock struct{ now time.Time }
func (c *clock) tick(d time.Duration) { c.now = c.now.Add(d) }
func newClockedReporter(out *bytes.Buffer, terminal bool) (*progressReporter, *clock) {
c := &clock{now: time.Date(2026, time.September, 13, 9, 0, 0, 0, time.UTC)}
reporter := newProgressReporter(out, terminal, "commits fetched")
reporter.now = func() time.Time { return c.now }
return reporter, c
}
func TestProgressReporter_Terminal_RewritesInPlaceAFewTimesASecond(t *testing.T) {
// Given
var out bytes.Buffer
reporter, clock := newClockedReporter(&out, true)
// When: four commits complete in quick succession, then one after a pause
reporter.update(1, 200)
clock.tick(progressInterval / 2)
reporter.update(2, 200)
reporter.update(3, 200)
clock.tick(progressInterval / 2)
reporter.update(4, 200)
clock.tick(progressInterval)
reporter.update(5, 200)
// Then: the first and the ones after each interval are drawn, the ones in between are dropped
assert.Equal(t, "\r1 of 200 commits fetched (0%)...\r4 of 200 commits fetched (2%)...\r5 of 200 commits fetched (2%)...", out.String())
}
func TestProgressReporter_Terminal_AlwaysEndsTheLine(t *testing.T) {
// Given
var out bytes.Buffer
reporter, _ := newClockedReporter(&out, true)
// When: the last commit completes within the interval of the previous rewrite
reporter.update(1, 2)
reporter.update(2, 2)
// Then: the final count is drawn regardless, and the line is ended
assert.Equal(t, "\r1 of 2 commits fetched (50%)...\r2 of 2 commits fetched (100%)...\n", out.String())
}
func TestProgressReporter_Log_PrintsALineEveryTenth(t *testing.T) {
// Given
var out bytes.Buffer
reporter, _ := newClockedReporter(&out, false)
// When
for done := uint(1); done <= 25; done++ {
reporter.update(done, 25)
}
// Then: nothing at zero, a line at each tenth, and the last line is the total
assert.Equal(t, "3 of 25 commits fetched (12%)...\n"+
"5 of 25 commits fetched (20%)...\n"+
"8 of 25 commits fetched (32%)...\n"+
"10 of 25 commits fetched (40%)...\n"+
"13 of 25 commits fetched (52%)...\n"+
"15 of 25 commits fetched (60%)...\n"+
"18 of 25 commits fetched (72%)...\n"+
"20 of 25 commits fetched (80%)...\n"+
"23 of 25 commits fetched (92%)...\n"+
"25 of 25 commits fetched (100%)...\n", out.String())
}
func TestProgressReporter_Log_SkipsTenthsAtOnceWhenUpdatesAreSparse(t *testing.T) {
// Given
var out bytes.Buffer
reporter, _ := newClockedReporter(&out, false)
// When
reporter.update(7, 10)
reporter.update(8, 10)
// Then: one line for the jump, then one for the next tenth
assert.Equal(t, "7 of 10 commits fetched (70%)...\n8 of 10 commits fetched (80%)...\n", out.String())
}
func TestProgressReporter_IgnoresAnEmptyTotal(t *testing.T) {
// Given
var out bytes.Buffer
reporter, _ := newClockedReporter(&out, false)
// When
reporter.update(0, 0)
// Then
assert.Empty(t, out.String())
}
func TestStatusWriter_FinishesAnOpenCounterBeforeAMessage(t *testing.T) {
// Given: a counter drawn in place, twice
var out bytes.Buffer
status := &statusWriter{Writer: &out}
_, _ = status.Write([]byte("\r1 of 3"))
_, _ = status.Write([]byte("\r2 of 3"))
// When: a rate-limit notice arrives, and the counter goes on
_, _ = status.Write([]byte("waiting 5s...\n"))
_, _ = status.Write([]byte("\r3 of 3\n"))
// Then: redraws stay on their line, the notice gets a line of its own, and the counter resumes below it
assert.Equal(t, "\r1 of 3\r2 of 3\nwaiting 5s...\n\r3 of 3\n", out.String())
}
func TestStatusWriter_SeparatesAfterAnOpenCounter(t *testing.T) {
// Given: a collection that failed mid-counter
var out bytes.Buffer
status := &statusWriter{Writer: &out}
_, _ = status.Write([]byte("\r1 of 3"))
// When
status.separate()
// Then: the line is finished, then the blank line
assert.Equal(t, "\r1 of 3\n\n", out.String())
}
func TestStatusWriter_SeparatesOnlyAfterOutput(t *testing.T) {
// Given
var out bytes.Buffer
status := &statusWriter{Writer: &out}
// When: nothing has been written
status.separate()
// Then
assert.Empty(t, out.String())
// When: something has been written, and then separated twice
_, _ = status.Write([]byte("Listing...\n"))
status.separate()
status.separate()
// Then: one blank line follows it
assert.Equal(t, "Listing...\n\n", out.String())
}