-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress_test.go
146 lines (115 loc) · 4.83 KB
/
progress_test.go
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
package progress_test
import (
"bytes"
"regexp"
"runtime"
"testing"
"time"
"github.com/UpCloudLtd/progress"
"github.com/UpCloudLtd/progress/messages"
"github.com/stretchr/testify/assert"
)
func removeColorsOnWindows(expected string) string {
if runtime.GOOS == "windows" {
re := regexp.MustCompile("\x1b\\[[0-9]+m")
return re.ReplaceAllString(expected, "")
}
return expected
}
func TestProgress_Push_ErrorChannel(t *testing.T) {
t.Parallel()
taskLog := progress.NewProgress(nil)
taskLog.Start()
defer taskLog.Stop()
err := taskLog.Push(messages.Update{Message: "No status"})
assert.EqualError(t, err, `can not push message with invalid status ""`)
err = taskLog.Push(messages.Update{Message: "Valid update", Status: messages.MessageStatusStarted})
assert.NoError(t, err)
}
func TestProgress_Start_PanicsIfCalledTwice(t *testing.T) {
t.Parallel()
taskLog := progress.NewProgress(nil)
taskLog.Start()
defer taskLog.Stop()
assert.PanicsWithValue(t, "can not start progress log more than once", taskLog.Start)
}
func TestProgress_Push_ErrorsIfCalledBeforeStart(t *testing.T) {
t.Parallel()
taskLog := progress.NewProgress(nil)
err := taskLog.Push(messages.Update{})
assert.EqualError(t, err, "can not push updates into progress log that has not been started")
}
func TestProgress_Push_PanicsIfCalledAfterStop(t *testing.T) {
t.Parallel()
taskLog := progress.NewProgress(nil)
taskLog.Start()
taskLog.Stop()
assert.Panics(t, func() { _ = taskLog.Push(messages.Update{}) })
}
func TestProgress_Stop_PanicsIfCalledBeforeStart(t *testing.T) {
t.Parallel()
taskLog := progress.NewProgress(nil)
assert.PanicsWithValue(t, "can not stop progress log that has not been started", taskLog.Stop)
}
func TestProgress_Stop_PanicsIfCalledTwice(t *testing.T) {
t.Parallel()
taskLog := progress.NewProgress(nil)
taskLog.Start()
taskLog.Stop()
assert.Panics(t, taskLog.Stop)
}
func TestProgress_Output(t *testing.T) {
t.Parallel()
cfg := progress.GetDefaultOutputConfig()
buf := bytes.NewBuffer(nil)
cfg.Target = buf
taskLog := progress.NewProgress(cfg)
taskLog.Start()
err := taskLog.Push(messages.Update{Message: "Test update", Status: messages.MessageStatusStarted})
assert.NoError(t, err)
time.Sleep(time.Millisecond * 150) // Wait for the first render
err = taskLog.Push(messages.Update{Message: "Test update", Status: messages.MessageStatusSuccess})
assert.NoError(t, err)
taskLog.Stop()
output := buf.String()
expected := removeColorsOnWindows("\x1b[34m> \x1b[0mTest update \n\x1b[32m✓ \x1b[0mTest update \n")
assert.Equal(t, expected, output)
}
func TestProgress_NoProgressMessage(t *testing.T) {
t.Parallel()
cfg := progress.GetDefaultOutputConfig()
buf := bytes.NewBuffer(nil)
cfg.Target = buf
taskLog := progress.NewProgress(cfg)
taskLog.Start()
defer taskLog.Stop()
err := taskLog.Push(messages.Update{Message: "Test update", Status: messages.MessageStatusStarted})
assert.NoError(t, err)
err = taskLog.Push(messages.Update{Message: "Test update", ProgressMessage: "(50 %)"})
assert.NoError(t, err)
time.Sleep(time.Millisecond * 150) // Wait for the first render
output := buf.String()
expected := removeColorsOnWindows("\x1b[34m> \x1b[0mTest update \n")
assert.Equal(t, expected, output)
}
func TestProgress_ClosesInProgressMessagesOnStop(t *testing.T) {
t.Parallel()
cfg := progress.GetDefaultOutputConfig()
buf := bytes.NewBuffer(nil)
cfg.Target = buf
taskLog := progress.NewProgress(cfg)
taskLog.Start()
err := taskLog.Push(messages.Update{Message: "Test pending 1", Status: messages.MessageStatusPending})
assert.NoError(t, err)
time.Sleep(time.Microsecond * 25) // Ensure time difference on Windows
err = taskLog.Push(messages.Update{Message: "Test started", Status: messages.MessageStatusStarted})
assert.NoError(t, err)
time.Sleep(time.Microsecond * 25) // Ensure time difference on Windows
err = taskLog.Push(messages.Update{Message: "Test pending 2", Status: messages.MessageStatusPending})
assert.NoError(t, err)
time.Sleep(time.Millisecond * 150) // Wait for the first render
taskLog.Stop()
output := buf.String()
expected := removeColorsOnWindows("\x1b[34m> \x1b[0mTest started \n\x1b[35m- \x1b[0mTest pending 1 \n\x1b[35m- \x1b[0mTest pending 2 \n\x1b[37m? \x1b[0mTest started \n")
assert.Equal(t, expected, output)
}