gopass/pkg/termio/reader_test.go
Dominik Schulz 16c071a780
Enable golangci-lint on push and pr (#2158)
Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
2022-03-24 21:58:53 +01:00

65 lines
1.1 KiB
Go

package termio
import (
"context"
"io"
"strings"
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
)
func TestReadLines(t *testing.T) {
t.Parallel()
for _, tc := range [][]string{
{"foo", "bar"},
{"foo", "bar", "", "baz"},
{"foo", "µ"},
{"µ", "ĸ", "aŧ", "¶a"},
} {
stdin := strings.NewReader(strings.Join(tc, "\n"))
for _, s := range tc {
assert.Equal(t, s, mustReadLine(stdin))
}
}
}
func TestReadLineError(t *testing.T) {
t.Parallel()
ctx := context.Background()
stdin := strings.NewReader("fo")
lr := NewReader(ctx, iotest.TimeoutReader(stdin))
line, err := lr.ReadLine()
assert.Error(t, err)
assert.Equal(t, "f", line)
}
func TestRead(t *testing.T) {
t.Parallel()
ctx := context.Background()
stdin := strings.NewReader(`foobarbazzabzabzab`)
lr := NewReader(ctx, stdin)
b := make([]byte, 10)
n, err := lr.Read(b)
assert.NoError(t, err)
assert.Equal(t, 10, n)
assert.Equal(t, "foobarbazz", string(b))
}
func mustReadLine(r io.Reader) string {
ctx := context.Background()
line, err := NewReader(ctx, r).ReadLine()
if err != nil {
panic(err)
}
return line
}