From 9cc04472fb83096960a6bfcef90fff84b8dc0e91 Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Mon, 6 Jul 2020 13:26:34 +0200 Subject: [PATCH 1/3] progress: Stop event on error Ensures that we set an `endTime` when an error event is published. Signed-off-by: Christopher Crone --- progress/tty.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/progress/tty.go b/progress/tty.go index daef04b51..c79e9cf3b 100644 --- a/progress/tty.go +++ b/progress/tty.go @@ -68,8 +68,11 @@ func (w *ttyWriter) Event(e Event) { } if _, ok := w.events[e.ID]; ok { event := w.events[e.ID] - if event.Status != Done && e.Status == Done { - event.stop() + switch e.Status { + case Done, Error: + if event.Status != e.Status { + event.stop() + } } event.Status = e.Status event.Text = e.Text From fac4c60077158f39bc7dc201016de78d33143ff6 Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Mon, 6 Jul 2020 13:52:36 +0200 Subject: [PATCH 2/3] progress: Add test for error event Signed-off-by: Christopher Crone --- progress/tty_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/progress/tty_test.go b/progress/tty_test.go index 0a10eb45c..eac286dbf 100644 --- a/progress/tty_test.go +++ b/progress/tty_test.go @@ -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))) +} From 50d932195d49ce4eb2f03f191ea99a849f695c8c Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Mon, 6 Jul 2020 14:03:48 +0200 Subject: [PATCH 3/3] progress: Rename variable to clarify logic Signed-off-by: Christopher Crone --- progress/tty.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/progress/tty.go b/progress/tty.go index c79e9cf3b..caac150d3 100644 --- a/progress/tty.go +++ b/progress/tty.go @@ -67,17 +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] + last := w.events[e.ID] switch e.Status { case Done, Error: - if event.Status != e.Status { - event.stop() + 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()