File: progress_test.go

package info (click to toggle)
golang-github-u-root-uio 0.0~git20240224.d2acac8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 276 kB
  • sloc: sh: 25; makefile: 2
file content (62 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package uio

import (
	"bytes"
	"io"
	"testing"
)

func TestProgressReadCloser(t *testing.T) {
	input := io.NopCloser(bytes.NewBufferString("01234567890123456789"))
	stdout := &bytes.Buffer{}
	prc := ProgressReadCloser{
		RC:       input,
		Symbol:   "#",
		Interval: 4,
		W:        stdout,
	}

	// Read one byte at a time.
	output := make([]byte, 1)
	_, _ = prc.Read(output)
	_, _ = prc.Read(output)
	_, _ = prc.Read(output)
	if len(stdout.Bytes()) != 0 {
		t.Errorf("found %q, but expected no bytes to be written", stdout)
	}
	_, _ = prc.Read(output)
	if stdout.String() != "#" {
		t.Errorf("found %q, expected %q to be written", stdout.String(), "#")
	}

	// Read 9 bytes all at once.
	output = make([]byte, 9)
	_, _ = prc.Read(output)
	if stdout.String() != "###" {
		t.Errorf("found %q, expected %q to be written", stdout.String(), "###")
	}
	if string(output) != "456789012" {
		t.Errorf("found %q, expected %q to be written", string(output), "456789012")
	}

	// Read until EOF
	output, err := io.ReadAll(&prc)
	if err != nil {
		t.Errorf("got %v, expected nil error", err)
	}
	if stdout.String() != "#####\n" {
		t.Errorf("found %q, expected %q to be written", stdout.String(), "#####\n")
	}
	if string(output) != "3456789" {
		t.Errorf("found %q, expected %q to be written", string(output), "3456789")
	}

	err = prc.Close()
	if err != nil {
		t.Errorf("got %v, expected nil error", err)
	}
}