All checks were successful
release / build (push) Successful in 3m58s
self-update prints each step (checking releases, verifying signature, downloading with MB and percent, verifying sha256, replacing binary). Fleet commands show a spinner after 400 ms while an API call is in flight. Output goes to stderr on a terminal only; never with --json, TERM=dumb or HUB_NO_PROGRESS=1. stdout and exit codes are unchanged.
144 lines
3.5 KiB
Go
144 lines
3.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package progress
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type syncBuf struct {
|
|
mu sync.Mutex
|
|
b bytes.Buffer
|
|
}
|
|
|
|
func (s *syncBuf) Write(p []byte) (int, error) { s.mu.Lock(); defer s.mu.Unlock(); return s.b.Write(p) }
|
|
func (s *syncBuf) String() string { s.mu.Lock(); defer s.mu.Unlock(); return s.b.String() }
|
|
|
|
func TestTermFormat(t *testing.T) {
|
|
var b bytes.Buffer
|
|
r := New(&b)
|
|
r.Step("downloading hub-1 (10.1 MB)")
|
|
r.Bytes(3565158, 10590617)
|
|
r.Done()
|
|
got := b.String()
|
|
for _, want := range []string{"\r\x1b[Kdownloading hub-1 (10.1 MB)", " 3.4/10.1 MB 33%", " ok\n"} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("output %q lacks %q", got, want)
|
|
}
|
|
}
|
|
b.Reset()
|
|
r.Step("verifying")
|
|
r.Fail()
|
|
if !strings.HasSuffix(b.String(), "verifying failed\n") {
|
|
t.Errorf("fail output %q", b.String())
|
|
}
|
|
b.Reset()
|
|
r.Done() // no open step: silent
|
|
if b.Len() != 0 {
|
|
t.Errorf("Done without a step wrote %q", b.String())
|
|
}
|
|
}
|
|
|
|
func TestStepFinishesPreviousStep(t *testing.T) {
|
|
var b bytes.Buffer
|
|
r := New(&b)
|
|
r.Step("one")
|
|
r.Step("two")
|
|
if !strings.Contains(b.String(), "one ok\n") {
|
|
t.Errorf("first step not finished: %q", b.String())
|
|
}
|
|
}
|
|
|
|
func TestFormatBytes(t *testing.T) {
|
|
if got := FormatBytes(5*1024*1024, 10*1024*1024); got != "5.0/10.0 MB 50%" {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
if got := FormatBytes(2*1024*1024, 0); got != "2.0 MB" {
|
|
t.Errorf("unknown total: %q", got)
|
|
}
|
|
}
|
|
|
|
type rec struct {
|
|
Nop
|
|
calls [][2]int64
|
|
}
|
|
|
|
func (r *rec) Bytes(d, t int64) { r.calls = append(r.calls, [2]int64{d, t}) }
|
|
|
|
func TestReaderCountsBytes(t *testing.T) {
|
|
rp := &rec{}
|
|
rd := NewReader(strings.NewReader(strings.Repeat("x", 1000)), 1000, rp)
|
|
n, err := io.Copy(io.Discard, rd)
|
|
if err != nil || n != 1000 || rd.Count() != 1000 {
|
|
t.Fatalf("copied %d, count %d, err %v", n, rd.Count(), err)
|
|
}
|
|
last := rp.calls[len(rp.calls)-1]
|
|
if last != [2]int64{1000, 1000} {
|
|
t.Errorf("last report %v", last)
|
|
}
|
|
}
|
|
|
|
func TestEnabledGates(t *testing.T) {
|
|
env := func(m map[string]string) func(string) (string, bool) {
|
|
return func(k string) (string, bool) { v, ok := m[k]; return v, ok }
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
tty bool
|
|
json bool
|
|
env map[string]string
|
|
want bool
|
|
}{
|
|
{"terminal", true, false, map[string]string{"TERM": "xterm"}, true},
|
|
{"not a tty", false, false, nil, false},
|
|
{"json", true, true, nil, false},
|
|
{"HUB_NO_PROGRESS", true, false, map[string]string{"HUB_NO_PROGRESS": "1"}, false},
|
|
{"TERM dumb", true, false, map[string]string{"TERM": "dumb"}, false},
|
|
}
|
|
for _, c := range cases {
|
|
if got := Enabled(c.tty, c.json, env(c.env)); got != c.want {
|
|
t.Errorf("%s: Enabled = %v, want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsTerminalFalseForBuffersAndFiles(t *testing.T) {
|
|
if IsTerminal(&bytes.Buffer{}) {
|
|
t.Error("buffer is not a terminal")
|
|
}
|
|
f, err := os.CreateTemp(t.TempDir(), "x")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
if IsTerminal(f) {
|
|
t.Error("regular file is not a terminal")
|
|
}
|
|
}
|
|
|
|
func TestSpinnerSilentForFastWork(t *testing.T) {
|
|
var b syncBuf
|
|
stop := Spin(&b, "fetching /v1/x ...", 200*time.Millisecond)
|
|
stop()
|
|
stop()
|
|
if b.String() != "" {
|
|
t.Errorf("fast work printed %q", b.String())
|
|
}
|
|
}
|
|
|
|
func TestSpinnerShowsAfterDelayAndClears(t *testing.T) {
|
|
var b syncBuf
|
|
stop := Spin(&b, "fetching /v1/x ...", 10*time.Millisecond)
|
|
time.Sleep(80 * time.Millisecond)
|
|
stop()
|
|
got := b.String()
|
|
if !strings.Contains(got, "fetching /v1/x ...") || !strings.HasSuffix(got, "\r\x1b[K") {
|
|
t.Errorf("spinner output %q", got)
|
|
}
|
|
}
|