Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy: Don't print "skipped: 0.0b = 0.00%" #2576

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions copy/progress_bars.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ func (c *copier) newProgressPool() *mpb.Progress {

// customPartialBlobDecorFunc implements mpb.DecorFunc for the partial blobs retrieval progress bar
func customPartialBlobDecorFunc(s decor.Statistics) string {
current := decor.SizeB1024(s.Current)
total := decor.SizeB1024(s.Total)
refill := decor.SizeB1024(s.Refill)
if s.Total == 0 {
pairFmt := "%.1f / %.1f (skipped: %.1f)"
return fmt.Sprintf(pairFmt, decor.SizeB1024(s.Current), decor.SizeB1024(s.Total), decor.SizeB1024(s.Refill))
return fmt.Sprintf("%.1f / %.1f (skipped: %.1f)", current, total, refill)
}
// If we didn't do a partial fetch then let's not output a distracting ("skipped: 0.0b = 0.00%")
if s.Refill == 0 {
return fmt.Sprintf("%.1f / %.1f", current, total)
}
pairFmt := "%.1f / %.1f (skipped: %.1f = %.2f%%)"
percentage := 100.0 * float64(s.Refill) / float64(s.Total)
return fmt.Sprintf(pairFmt, decor.SizeB1024(s.Current), decor.SizeB1024(s.Total), decor.SizeB1024(s.Refill), percentage)
return fmt.Sprintf("%.1f / %.1f (skipped: %.1f = %.2f%%)", current, total, refill, percentage)
}

// progressBar wraps a *mpb.Bar, allowing us to add extra state and methods.
Expand Down
24 changes: 24 additions & 0 deletions copy/progress_bars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package copy

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vbauerster/mpb/v8/decor"
)

func TestCustomPartialBlobDecorFunc(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This works just fine. In Go, it is idiomatic to have table-driven tests, compare e.g. TestUpdatedBlobInfoFromUpload.)

// A stub test
s := decor.Statistics{}
assert.Equal(t, "0.0b / 0.0b (skipped: 0.0b)", customPartialBlobDecorFunc(s))
// Partial pull in progress
s = decor.Statistics{}
s.Current = 1097653
s.Total = 8329917
s.Refill = 509722
assert.Equal(t, "1.0MiB / 7.9MiB (skipped: 497.8KiB = 6.12%)", customPartialBlobDecorFunc(s))
// Almost complete, but no reuse
s.Current = int64(float64(s.Total) * 0.95)
s.Refill = 0
assert.Equal(t, "7.5MiB / 7.9MiB", customPartialBlobDecorFunc(s))
}