Merge pull request #351 from chris-crone/error-progress-fix

Set progress event endTime on error
This commit is contained in:
Chris Crone 2020-07-06 17:55:51 +02:00 committed by GitHub
commit 43d8f2fa18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 7 deletions

View File

@ -67,14 +67,17 @@ func (w *ttyWriter) Event(e Event) {
w.eventIDs = append(w.eventIDs, e.ID)
}
if _, ok := w.events[e.ID]; ok {
event := w.events[e.ID]
if event.Status != Done && e.Status == Done {
event.stop()
last := w.events[e.ID]
switch e.Status {
case Done, Error:
if last.Status != e.Status {
last.stop()
}
}
event.Status = e.Status
event.Text = e.Text
event.StatusText = e.StatusText
w.events[e.ID] = event
last.Status = e.Status
last.Text = e.Text
last.StatusText = e.StatusText
w.events[e.ID] = last
} else {
e.startTime = time.Now()
e.spinner = newSpinner()

View File

@ -18,6 +18,7 @@ package progress
import (
"fmt"
"sync"
"testing"
"time"
@ -54,3 +55,32 @@ func TestLineText(t *testing.T) {
out = lineText(ev, 50, lineWidth, true)
assert.Equal(t, "\x1b[31m . id Text Status 0.0s\n\x1b[0m", out)
}
func TestErrorEvent(t *testing.T) {
w := &ttyWriter{
events: map[string]Event{},
mtx: &sync.RWMutex{},
}
e := Event{
ID: "id",
Text: "Text",
Status: Working,
StatusText: "Working",
startTime: time.Now(),
spinner: &spinner{
chars: []string{"."},
},
}
// Fire "Working" event and check end time isn't touched
w.Event(e)
event, ok := w.events[e.ID]
assert.True(t, ok)
assert.True(t, event.endTime.Equal(time.Time{}))
// Fire "Error" event and check end time is set
e.Status = Error
w.Event(e)
event, ok = w.events[e.ID]
assert.True(t, ok)
assert.True(t, event.endTime.After(time.Now().Add(-10*time.Second)))
}