a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package progress
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestNop_NoPanic(t *testing.T) {
|
|
Nop{}.Report("anything", 0, 0)
|
|
Nop{}.Report("stage", 1, 100)
|
|
}
|
|
|
|
func TestFromContext_NilCtx_ReturnsNop(t *testing.T) {
|
|
r := FromContext(context.TODO())
|
|
if r == nil {
|
|
t.Fatal("FromContext must never return nil")
|
|
}
|
|
r.Report("x", 0, 0) // must not panic
|
|
}
|
|
|
|
func TestFromContext_NoReporter_ReturnsNop(t *testing.T) {
|
|
r := FromContext(context.Background())
|
|
if _, ok := r.(Nop); !ok {
|
|
t.Errorf("expected Nop when no reporter attached, got %T", r)
|
|
}
|
|
}
|
|
|
|
func TestWithReporter_Roundtrip(t *testing.T) {
|
|
rec := &recorder{}
|
|
ctx := WithReporter(context.Background(), rec)
|
|
|
|
FromContext(ctx).Report("parse", 3, 10)
|
|
|
|
if len(rec.events) != 1 {
|
|
t.Fatalf("expected 1 event, got %d", len(rec.events))
|
|
}
|
|
ev := rec.events[0]
|
|
if ev.stage != "parse" || ev.current != 3 || ev.total != 10 {
|
|
t.Errorf("unexpected event: %+v", ev)
|
|
}
|
|
}
|
|
|
|
func TestWithReporter_NilReporter_NoOp(t *testing.T) {
|
|
ctx := WithReporter(context.Background(), nil)
|
|
r := FromContext(ctx)
|
|
if _, ok := r.(Nop); !ok {
|
|
t.Errorf("nil reporter must not leak through context, got %T", r)
|
|
}
|
|
}
|
|
|
|
type recEvent struct {
|
|
stage string
|
|
current, total int
|
|
}
|
|
|
|
type recorder struct {
|
|
mu sync.Mutex
|
|
events []recEvent
|
|
}
|
|
|
|
func (r *recorder) Report(stage string, current, total int) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.events = append(r.events, recEvent{stage, current, total})
|
|
}
|