diff --git a/packer/progressbar.go b/packer/progressbar.go index 3052d3ee0..8f1d3d9a5 100644 --- a/packer/progressbar.go +++ b/packer/progressbar.go @@ -35,16 +35,25 @@ type StackableProgressBar struct { items int32 total int64 - started bool + started bool + ConfigProgressbarFN func(*pb.ProgressBar) } var _ ProgressBar = new(StackableProgressBar) -func (spb *StackableProgressBar) start() { - spb.Bar.ProgressBar = pb.New(0) - spb.Bar.ProgressBar.SetUnits(pb.U_BYTES) +func defaultProgressbarConfigFn(bar *pb.ProgressBar) { + bar.SetUnits(pb.U_BYTES) +} - spb.Bar.ProgressBar.Start() +func (spb *StackableProgressBar) start() { + bar := pb.New(0) + if spb.ConfigProgressbarFN == nil { + spb.ConfigProgressbarFN = defaultProgressbarConfigFn + } + spb.ConfigProgressbarFN(bar) + + bar.Start() + spb.Bar.ProgressBar = bar spb.started = true } diff --git a/packer/progressbar_test.go b/packer/progressbar_test.go index 89cbe862a..db75ad06b 100644 --- a/packer/progressbar_test.go +++ b/packer/progressbar_test.go @@ -3,10 +3,21 @@ package packer import ( "sync" "testing" + "time" + + "github.com/cheggaaa/pb" ) +func speedyProgressBar(bar *pb.ProgressBar) { + bar.SetUnits(pb.U_BYTES) + bar.SetRefreshRate(1 * time.Millisecond) + bar.NotPrint = true +} + func TestStackableProgressBar_race(t *testing.T) { - bar := &StackableProgressBar{} + bar := &StackableProgressBar{ + ConfigProgressbarFN: speedyProgressBar, + } start42Fn := func() { bar.Start(42) } finishFn := func() { bar.Finish() } @@ -24,7 +35,7 @@ func TestStackableProgressBar_race(t *testing.T) { iterations int }{ {"all public", fields{nil, []func(){start42Fn, finishFn, add21, add21}, finishFn}, 300}, - {"all public", fields{start42Fn, []func(){add21, add21}, finishFn}, 300}, + {"add", fields{start42Fn, []func(){add21}, finishFn}, 300}, } for _, tt := range tests {