1
0
mirror of https://github.com/go-task/task.git synced 2024-12-12 10:45:49 +02:00
task/internal/output/output_test.go

135 lines
3.4 KiB
Go
Raw Normal View History

2018-04-23 02:40:03 +02:00
package output_test
import (
"bytes"
"errors"
2018-04-23 02:40:03 +02:00
"fmt"
"io"
2018-04-23 02:40:03 +02:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-task/task/v3/internal/orderedmap"
"github.com/go-task/task/v3/internal/output"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile"
2018-04-23 02:40:03 +02:00
)
func TestInterleaved(t *testing.T) {
var b bytes.Buffer
var o output.Output = output.Interleaved{}
2023-03-31 21:13:29 +02:00
w, _, _ := o.WrapWriter(&b, io.Discard, "", nil)
2018-04-23 02:40:03 +02:00
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{}
2023-03-31 21:13:29 +02:00
stdOut, stdErr, cleanup := o.WrapWriter(&b, io.Discard, "", nil)
2018-04-23 02:40:03 +02:00
fmt.Fprintln(stdOut, "out\nout")
2018-04-23 02:40:03 +02:00
assert.Equal(t, "", b.String())
fmt.Fprintln(stdErr, "err\nerr")
2018-04-23 02:40:03 +02:00
assert.Equal(t, "", b.String())
fmt.Fprintln(stdOut, "out")
assert.Equal(t, "", b.String())
fmt.Fprintln(stdErr, "err")
assert.Equal(t, "", b.String())
require.NoError(t, cleanup(nil))
assert.Equal(t, "out\nout\nerr\nerr\nout\nerr\n", b.String())
2018-04-23 02:40:03 +02:00
}
func TestGroupWithBeginEnd(t *testing.T) {
tmpl := templater.Templater{
Vars: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"VAR1": {Static: "example-value"},
}),
},
}
var o output.Output = output.Group{
Begin: "::group::{{ .VAR1 }}",
End: "::endgroup::",
}
t.Run("simple", func(t *testing.T) {
var b bytes.Buffer
2023-03-31 21:13:29 +02:00
w, _, cleanup := o.WrapWriter(&b, io.Discard, "", &tmpl)
fmt.Fprintln(w, "foo\nbar")
assert.Equal(t, "", b.String())
fmt.Fprintln(w, "baz")
assert.Equal(t, "", b.String())
require.NoError(t, cleanup(nil))
assert.Equal(t, "::group::example-value\nfoo\nbar\nbaz\n::endgroup::\n", b.String())
})
t.Run("no output", func(t *testing.T) {
var b bytes.Buffer
2023-03-31 21:13:29 +02:00
_, _, cleanup := o.WrapWriter(&b, io.Discard, "", &tmpl)
require.NoError(t, cleanup(nil))
assert.Equal(t, "", b.String())
})
}
func TestGroupErrorOnlySwallowsOutputOnNoError(t *testing.T) {
var b bytes.Buffer
var o output.Output = output.Group{
ErrorOnly: true,
}
2023-03-31 21:13:29 +02:00
stdOut, stdErr, cleanup := o.WrapWriter(&b, io.Discard, "", nil)
_, _ = fmt.Fprintln(stdOut, "std-out")
_, _ = fmt.Fprintln(stdErr, "std-err")
require.NoError(t, cleanup(nil))
assert.Empty(t, b.String())
}
2023-03-31 21:13:29 +02:00
func TestGroupErrorOnlyShowsOutputOnError(t *testing.T) {
var b bytes.Buffer
var o output.Output = output.Group{
ErrorOnly: true,
}
2023-03-31 21:13:29 +02:00
stdOut, stdErr, cleanup := o.WrapWriter(&b, io.Discard, "", nil)
_, _ = fmt.Fprintln(stdOut, "std-out")
_, _ = fmt.Fprintln(stdErr, "std-err")
require.NoError(t, cleanup(errors.New("any-error")))
assert.Equal(t, "std-out\nstd-err\n", b.String())
}
2018-04-23 02:40:03 +02:00
func TestPrefixed(t *testing.T) {
var b bytes.Buffer
var o output.Output = output.Prefixed{}
2023-03-31 21:13:29 +02:00
w, _, cleanup := o.WrapWriter(&b, io.Discard, "prefix", nil)
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())
require.NoError(t, cleanup(nil))
2018-04-23 02:40:03 +02:00
})
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())
}
require.NoError(t, cleanup(nil))
2018-04-23 02:40:03 +02:00
assert.Equal(t, "[prefix] Test!\n", b.String())
})
}