Tests use a fake terminal and an injected clock and /proc readers. - Rendering: layout, gradient endpoints, bar width limits, truncation, plain output without color, and the unknown-total and overshoot states. - Behavior: speed smoothing, accumulation across processes, the show delay, erase sequences, messages printed above the display, and both kinds of prompt.
294 lines
8.6 KiB
Go
294 lines
8.6 KiB
Go
package progress
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
var ansi = regexp.MustCompile(`\x1b\[[0-9;?]*[A-Za-z]`)
|
|
|
|
func plain(s string) string { return ansi.ReplaceAllString(s, "") }
|
|
|
|
func TestRenderLayout(t *testing.T) {
|
|
f := frame{op: "extract", label: "big.tar.zst", tick: 3, done: 512 << 20, total: 1 << 30,
|
|
speed: 64 << 20, columns: 80, color: true}
|
|
lines := render(f)
|
|
if len(lines) != 3 {
|
|
t.Fatalf("got %d lines", len(lines))
|
|
}
|
|
head, bar, info := plain(lines[0]), plain(lines[1]), plain(lines[2])
|
|
|
|
if head != " extract ⠸ big.tar.zst" {
|
|
t.Errorf("head = %q", head)
|
|
}
|
|
// 80 columns -> bar width 44 (zap: columns - 36, clamped to 20..60); half done.
|
|
if got := strings.Count(bar, barFull); got != 22 {
|
|
t.Errorf("filled = %d, want 22", got)
|
|
}
|
|
if got := strings.Count(bar, barEmpty); got != 22 {
|
|
t.Errorf("empty = %d, want 22", got)
|
|
}
|
|
if info != " data 512.0 MiB / 1.0 GiB · 64.0 MiB/s · eta 8s" {
|
|
t.Errorf("info = %q", info)
|
|
}
|
|
for _, l := range lines {
|
|
if n := utf8.RuneCountInString(plain(l)); n > f.columns {
|
|
t.Errorf("line wider than terminal (%d): %q", n, plain(l))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRenderGradientAndTrack(t *testing.T) {
|
|
f := frame{op: "create", label: "x", done: 1, total: 1, columns: 80, color: true}
|
|
bar := render(f)[1]
|
|
if !strings.Contains(bar, "\x1b[38;2;137;180;250m"+barFull) {
|
|
t.Error("bar does not start at #89b4fa")
|
|
}
|
|
if !strings.Contains(bar, "\x1b[38;2;203;166;247m"+barFull) {
|
|
t.Error("bar does not end at #cba6f7")
|
|
}
|
|
f.done = 0
|
|
if !strings.Contains(render(f)[1], "\x1b[38;2;49;50;68m"+barEmpty) {
|
|
t.Error("empty track is not #313244")
|
|
}
|
|
}
|
|
|
|
func TestRenderWithoutColor(t *testing.T) {
|
|
lines := render(frame{op: "create", label: "out.tar.zst", done: 10, total: 100, columns: 60})
|
|
for _, l := range lines {
|
|
if strings.Contains(l, "\x1b") {
|
|
t.Fatalf("escape codes without color: %q", l)
|
|
}
|
|
}
|
|
if !strings.HasPrefix(lines[0], " [create] ⠋ out.tar.zst") {
|
|
t.Errorf("head = %q", lines[0])
|
|
}
|
|
}
|
|
|
|
func TestRenderEdgeCases(t *testing.T) {
|
|
// Unknown total: empty bar, no ETA.
|
|
lines := render(frame{op: "create", done: 3 << 20, speed: 1 << 20, columns: 80})
|
|
if strings.Contains(lines[1], barFull) || strings.Contains(lines[2], "eta") {
|
|
t.Errorf("unknown total: %q / %q", lines[1], lines[2])
|
|
}
|
|
if !strings.Contains(lines[0], "scanning…") || !strings.Contains(lines[2], "data 3.0 MiB ·") {
|
|
t.Errorf("scanning frame: %q", lines)
|
|
}
|
|
// Overshoot (a backend that re-reads) is clamped, and once everything is
|
|
// read the backend is shown as finishing rather than with a zero ETA.
|
|
lines = render(frame{op: "extract", label: "a", done: 300, total: 100, speed: 5, columns: 80})
|
|
if strings.Contains(lines[1], barEmpty) || lines[2] != " data 100 B / 100 B · finishing…" {
|
|
t.Errorf("overshoot: %q", lines)
|
|
}
|
|
// Long labels are truncated so the line never wraps.
|
|
long := strings.Repeat("ü", 200)
|
|
head := render(frame{op: "extract", label: long, columns: 50})[0]
|
|
if n := utf8.RuneCountInString(head); n > 50 {
|
|
t.Errorf("head has %d runes on a 50-column terminal", n)
|
|
}
|
|
// Bar width bounds.
|
|
for cols, want := range map[int]int{40: 20, 56: 20, 80: 44, 96: 60, 300: 60} {
|
|
if got := barWidth(cols); got != want {
|
|
t.Errorf("barWidth(%d) = %d, want %d", cols, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFormatting(t *testing.T) {
|
|
for d, want := range map[time.Duration]string{
|
|
0: "<1s", 900 * time.Millisecond: "<1s", 42 * time.Second: "42s",
|
|
185 * time.Second: "3m 05s", 2*time.Hour + 7*time.Minute: "2h 07m",
|
|
} {
|
|
if got := formatDuration(d); got != want {
|
|
t.Errorf("formatDuration(%v) = %q, want %q", d, got, want)
|
|
}
|
|
}
|
|
for n, want := range map[int64]string{
|
|
0: "0 B", 1023: "1023 B", 1024: "1.0 KiB", 1536: "1.5 KiB",
|
|
1048575: "1023.9 KiB", 1048576: "1.0 MiB", 5 << 30: "5.0 GiB",
|
|
} {
|
|
if got := FormatBytes(n); got != want {
|
|
t.Errorf("FormatBytes(%d) = %q, want %q", n, got, want)
|
|
}
|
|
}
|
|
if _, ok := eta(0, 1<<40, 1); ok {
|
|
t.Error("ETA over a day should be hidden")
|
|
}
|
|
}
|
|
|
|
// fakeTerminal records what the monitor writes.
|
|
type fakeTerminal struct {
|
|
mu sync.Mutex
|
|
buf bytes.Buffer
|
|
}
|
|
|
|
func (f *fakeTerminal) Write(p []byte) (int, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.buf.Write(p)
|
|
}
|
|
|
|
func (f *fakeTerminal) String() string {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.buf.String()
|
|
}
|
|
|
|
func testMonitor(term *fakeTerminal, delay time.Duration) *Monitor {
|
|
m := newMonitor(term, func() int { return 80 }, "extract", "a.tar", 1000, false, delay)
|
|
m.bytesRead = func(int) (int64, bool) { return 500, true }
|
|
m.usesTTY = func(int) bool { return false }
|
|
return m
|
|
}
|
|
|
|
func TestMonitorDrawsSamplesAndErases(t *testing.T) {
|
|
term := &fakeTerminal{}
|
|
m := testMonitor(term, 0)
|
|
m.Watch(42)
|
|
now := time.Now()
|
|
m.tick(now)
|
|
m.bytesRead = func(int) (int64, bool) { return 600, true }
|
|
m.tick(now.Add(time.Second))
|
|
if m.frame.done != 600 || m.frame.speed != 100 {
|
|
t.Fatalf("done = %d, speed = %v", m.frame.done, m.frame.speed)
|
|
}
|
|
m.bytesRead = func(int) (int64, bool) { return 800, true }
|
|
m.tick(now.Add(2 * time.Second))
|
|
if want := 0.15*200 + 0.85*100; m.frame.speed != want {
|
|
t.Fatalf("smoothed speed = %v, want %v", m.frame.speed, want)
|
|
}
|
|
out := term.String()
|
|
if !strings.Contains(out, "data 800 B / 1000 B") {
|
|
t.Fatalf("display not drawn: %q", out)
|
|
}
|
|
m.Stop()
|
|
if !strings.HasSuffix(term.String(), "\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K") {
|
|
t.Fatalf("display not erased: %q", term.String())
|
|
}
|
|
}
|
|
|
|
func TestMonitorAccumulatesWatchedProcesses(t *testing.T) {
|
|
m := testMonitor(&fakeTerminal{}, time.Hour)
|
|
m.Watch(1)
|
|
m.tick(time.Now()) // pid 1 has read 500
|
|
m.Watch(2)
|
|
m.bytesRead = func(int) (int64, bool) { return 100, true }
|
|
m.tick(time.Now())
|
|
if m.frame.done != 600 {
|
|
t.Fatalf("done = %d, want 600", m.frame.done)
|
|
}
|
|
// An exited process (no /proc entry) keeps its last count.
|
|
m.bytesRead = func(int) (int64, bool) { return 0, false }
|
|
m.tick(time.Now())
|
|
if m.frame.done != 600 {
|
|
t.Fatalf("done after exit = %d, want 600", m.frame.done)
|
|
}
|
|
m.Stop()
|
|
}
|
|
|
|
func TestMonitorWaitsBeforeShowing(t *testing.T) {
|
|
term := &fakeTerminal{}
|
|
m := testMonitor(term, time.Hour)
|
|
m.tick(time.Now())
|
|
m.Stop()
|
|
if term.String() != "" {
|
|
t.Fatalf("quick operation drew something: %q", term.String())
|
|
}
|
|
}
|
|
|
|
func TestWriterKeepsMessagesAboveTheDisplay(t *testing.T) {
|
|
term := &fakeTerminal{}
|
|
m := testMonitor(term, 0)
|
|
m.Watch(1)
|
|
m.tick(time.Now())
|
|
w := Writer{m}
|
|
w.Write([]byte("bsdtar: some warn"))
|
|
if strings.Contains(term.String(), "warn") {
|
|
t.Fatal("partial line printed early")
|
|
}
|
|
w.Write([]byte("ing\n"))
|
|
out := term.String()
|
|
i := strings.Index(out, "bsdtar: some warning\n")
|
|
if i < 0 {
|
|
t.Fatalf("line not printed: %q", out)
|
|
}
|
|
// The display was erased before the line and redrawn after it.
|
|
if !strings.HasSuffix(out[:i], "\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K") {
|
|
t.Errorf("display not erased before the message: %q", out[:i])
|
|
}
|
|
if !strings.Contains(out[i:], "data 500 B / 1000 B") {
|
|
t.Errorf("display not redrawn after the message: %q", out[i:])
|
|
}
|
|
m.Stop()
|
|
}
|
|
|
|
func TestPromptsStopTheDisplay(t *testing.T) {
|
|
// A backend prompt without a newline is shown once it stays put.
|
|
term := &fakeTerminal{}
|
|
m := testMonitor(term, 0)
|
|
now := time.Now()
|
|
m.tick(now)
|
|
Writer{m}.Write([]byte("Enter password:"))
|
|
m.tick(time.Now().Add(promptAfter))
|
|
if !m.paused || !strings.HasSuffix(term.String(), "Enter password:") {
|
|
t.Fatalf("prompt not shown: %q", term.String())
|
|
}
|
|
m.Stop()
|
|
|
|
// A backend that opens the terminal itself pauses the display, which is
|
|
// then left alone rather than erased (the prompt follows it).
|
|
term = &fakeTerminal{}
|
|
m = testMonitor(term, 0)
|
|
m.Watch(7)
|
|
m.tick(now)
|
|
m.usesTTY = func(int) bool { return true }
|
|
m.tick(now.Add(interval))
|
|
before := term.String()
|
|
m.tick(now.Add(2 * interval))
|
|
m.Stop()
|
|
if !m.paused || term.String() != before {
|
|
t.Fatalf("display kept drawing or was erased after a prompt: %q", term.String()[len(before):])
|
|
}
|
|
}
|
|
|
|
func TestNilMonitor(t *testing.T) {
|
|
var m *Monitor
|
|
m.Watch(1)
|
|
m.Ready("x", 1)
|
|
m.Stop()
|
|
s := m.Streams(nil, os.Stderr, FirstStage)
|
|
if s.Stderr != nil || s.Stdout != os.Stderr || s.Started != nil {
|
|
t.Fatalf("nil monitor changed streams: %+v", s)
|
|
}
|
|
}
|
|
|
|
func TestReadRchar(t *testing.T) {
|
|
n, ok := readRchar(os.Getpid())
|
|
if !ok || n <= 0 {
|
|
t.Fatalf("readRchar(self) = %d, %v", n, ok)
|
|
}
|
|
if _, ok := readRchar(1 << 30); ok {
|
|
t.Fatal("readRchar of a missing process succeeded")
|
|
}
|
|
}
|
|
|
|
func TestOpensTerminal(t *testing.T) {
|
|
if opensTerminal(1 << 30) {
|
|
t.Fatal("missing process reported as using the terminal")
|
|
}
|
|
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
|
|
if err != nil {
|
|
t.Skip("no controlling terminal")
|
|
}
|
|
defer tty.Close()
|
|
if !opensTerminal(os.Getpid()) {
|
|
t.Fatal("open /dev/tty not detected")
|
|
}
|
|
}
|