2018-04-23 02:40:03 +02:00
|
|
|
package output_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-04-21 21:55:47 +02:00
|
|
|
"io"
|
2018-04-23 02:40:03 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/output"
|
2018-04-23 02:40:03 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInterleaved(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
var o output.Output = output.Interleaved{}
|
|
|
|
var w = o.WrapWriter(&b, "")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "foo\nbar")
|
|
|
|
assert.Equal(t, "foo\nbar\n", b.String())
|
|
|
|
fmt.Fprintln(w, "baz")
|
|
|
|
assert.Equal(t, "foo\nbar\nbaz\n", b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroup(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
var o output.Output = output.Group{}
|
2019-04-21 21:55:47 +02:00
|
|
|
var w = o.WrapWriter(&b, "").(io.WriteCloser)
|
2018-04-23 02:40:03 +02:00
|
|
|
|
|
|
|
fmt.Fprintln(w, "foo\nbar")
|
|
|
|
assert.Equal(t, "", b.String())
|
|
|
|
fmt.Fprintln(w, "baz")
|
|
|
|
assert.Equal(t, "", b.String())
|
|
|
|
assert.NoError(t, w.Close())
|
|
|
|
assert.Equal(t, "foo\nbar\nbaz\n", b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixed(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
var o output.Output = output.Prefixed{}
|
2019-04-21 21:55:47 +02:00
|
|
|
var w = o.WrapWriter(&b, "prefix").(io.WriteCloser)
|
2018-04-23 02:40:03 +02:00
|
|
|
|
|
|
|
t.Run("simple use cases", func(t *testing.T) {
|
|
|
|
b.Reset()
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "foo\nbar")
|
|
|
|
assert.Equal(t, "[prefix] foo\n[prefix] bar\n", b.String())
|
|
|
|
fmt.Fprintln(w, "baz")
|
|
|
|
assert.Equal(t, "[prefix] foo\n[prefix] bar\n[prefix] baz\n", b.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("multiple writes for a single line", func(t *testing.T) {
|
|
|
|
b.Reset()
|
|
|
|
|
|
|
|
for _, char := range []string{"T", "e", "s", "t", "!"} {
|
|
|
|
fmt.Fprint(w, char)
|
|
|
|
assert.Equal(t, "", b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, w.Close())
|
|
|
|
assert.Equal(t, "[prefix] Test!\n", b.String())
|
|
|
|
})
|
|
|
|
}
|