diff --git a/progress/tracker.go b/progress/tracker.go index 371b5fd..40cc32d 100644 --- a/progress/tracker.go +++ b/progress/tracker.go @@ -34,11 +34,11 @@ func (t *Tracker) ETA() time.Duration { return t.ExpectedDuration - timeTaken } - pDone := t.PercentDone() + pDone := int64(t.PercentDone()) if pDone == 0 { return time.Duration(0) } - return time.Duration((int64(timeTaken) / int64(pDone)) * int64(100-pDone)) + return time.Duration((int64(timeTaken) / pDone) * (100 - pDone)) } // Increment updates the current value of the task being tracked. @@ -66,6 +66,9 @@ func (t *Tracker) MarkAsDone() { // PercentDone returns the currently completed percentage value. func (t *Tracker) PercentDone() float64 { + if t.Total == 0 { + return 0 + } return float64(t.value) * 100.0 / float64(t.Total) } diff --git a/progress/tracker_test.go b/progress/tracker_test.go index 731f97c..4722a10 100644 --- a/progress/tracker_test.go +++ b/progress/tracker_test.go @@ -11,7 +11,10 @@ func TestTracker_ETA(t *testing.T) { timeDelayUnit := time.Millisecond timeDelay := timeDelayUnit * 25 - tracker := Tracker{Total: 100} + tracker := Tracker{} + assert.Equal(t, time.Duration(0), tracker.ETA()) + + tracker.Total = 100 tracker.start() assert.Equal(t, time.Duration(0), tracker.ETA()) time.Sleep(timeDelay) @@ -65,7 +68,10 @@ func TestTracker_MarkAsDone(t *testing.T) { } func TestTracker_PercentDone(t *testing.T) { - tracker := Tracker{Total: 100} + tracker := Tracker{} + assert.Equal(t, 0.00, tracker.PercentDone()) + + tracker.Total = 100 assert.Equal(t, 0.00, tracker.PercentDone()) for idx := 1; idx <= 100; idx++ {