github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/uio/progress_test.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package uio 6 7 import ( 8 "bytes" 9 "io" 10 "testing" 11 ) 12 13 func TestProgressReadCloser(t *testing.T) { 14 input := io.NopCloser(bytes.NewBufferString("01234567890123456789")) 15 stdout := &bytes.Buffer{} 16 prc := ProgressReadCloser{ 17 RC: input, 18 Symbol: "#", 19 Interval: 4, 20 W: stdout, 21 } 22 23 // Read one byte at a time. 24 output := make([]byte, 1) 25 prc.Read(output) 26 prc.Read(output) 27 prc.Read(output) 28 if len(stdout.Bytes()) != 0 { 29 t.Errorf("found %q, but expected no bytes to be written", stdout) 30 } 31 prc.Read(output) 32 if stdout.String() != "#" { 33 t.Errorf("found %q, expected %q to be written", stdout.String(), "#") 34 } 35 36 // Read 9 bytes all at once. 37 output = make([]byte, 9) 38 prc.Read(output) 39 if stdout.String() != "###" { 40 t.Errorf("found %q, expected %q to be written", stdout.String(), "###") 41 } 42 if string(output) != "456789012" { 43 t.Errorf("found %q, expected %q to be written", string(output), "456789012") 44 } 45 46 // Read until EOF 47 output, err := io.ReadAll(&prc) 48 if err != nil { 49 t.Errorf("got %v, expected nil error", err) 50 } 51 if stdout.String() != "#####\n" { 52 t.Errorf("found %q, expected %q to be written", stdout.String(), "#####\n") 53 } 54 if string(output) != "3456789" { 55 t.Errorf("found %q, expected %q to be written", string(output), "3456789") 56 } 57 58 err = prc.Close() 59 if err != nil { 60 t.Errorf("got %v, expected nil error", err) 61 } 62 }