2018-04-23 02:40:03 +02:00
|
|
|
package output_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-03-09 03:34:52 +02:00
|
|
|
"errors"
|
2018-04-23 02:40:03 +02:00
|
|
|
"fmt"
|
2019-04-21 21:55:47 +02:00
|
|
|
"io"
|
2018-04-23 02:40:03 +02:00
|
|
|
"testing"
|
|
|
|
|
2024-06-03 11:37:24 +02:00
|
|
|
"github.com/fatih/color"
|
2018-04-23 02:40:03 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-06 12:18:41 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-07 16:48:33 +02:00
|
|
|
|
2024-06-03 11:37:24 +02:00
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2024-01-04 02:31:24 +02:00
|
|
|
"github.com/go-task/task/v3/internal/omap"
|
2021-01-07 16:48:33 +02:00
|
|
|
"github.com/go-task/task/v3/internal/output"
|
2022-12-19 03:06:09 +02:00
|
|
|
"github.com/go-task/task/v3/internal/templater"
|
2023-12-29 22:32:03 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
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
|
|
|
|
2022-07-06 15:43:32 +02:00
|
|
|
fmt.Fprintln(stdOut, "out\nout")
|
2018-04-23 02:40:03 +02:00
|
|
|
assert.Equal(t, "", b.String())
|
2022-07-06 15:43:32 +02:00
|
|
|
fmt.Fprintln(stdErr, "err\nerr")
|
2018-04-23 02:40:03 +02:00
|
|
|
assert.Equal(t, "", b.String())
|
2022-07-06 15:43:32 +02:00
|
|
|
fmt.Fprintln(stdOut, "out")
|
|
|
|
assert.Equal(t, "", b.String())
|
|
|
|
fmt.Fprintln(stdErr, "err")
|
|
|
|
assert.Equal(t, "", b.String())
|
|
|
|
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, cleanup(nil))
|
2022-07-06 15:43:32 +02:00
|
|
|
assert.Equal(t, "out\nout\nerr\nerr\nout\nerr\n", b.String())
|
2018-04-23 02:40:03 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 02:11:47 +02:00
|
|
|
func TestGroupWithBeginEnd(t *testing.T) {
|
2024-03-10 19:11:07 +02:00
|
|
|
tmpl := templater.Cache{
|
2023-12-29 22:32:03 +02:00
|
|
|
Vars: &ast.Vars{
|
2024-01-04 02:31:24 +02:00
|
|
|
OrderedMap: omap.FromMap(map[string]ast.Var{
|
2023-11-28 20:18:28 +02:00
|
|
|
"VAR1": {Value: "example-value"},
|
2023-04-06 13:07:57 +02:00
|
|
|
}),
|
2022-01-14 02:11:47 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-01-14 02:11:47 +02:00
|
|
|
|
|
|
|
fmt.Fprintln(w, "foo\nbar")
|
|
|
|
assert.Equal(t, "", b.String())
|
|
|
|
fmt.Fprintln(w, "baz")
|
|
|
|
assert.Equal(t, "", b.String())
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, cleanup(nil))
|
2022-01-14 02:11:47 +02:00
|
|
|
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)
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, cleanup(nil))
|
2022-01-14 02:11:47 +02:00
|
|
|
assert.Equal(t, "", b.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-09 03:34:52 +02:00
|
|
|
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)
|
2023-03-09 03:34:52 +02:00
|
|
|
|
|
|
|
_, _ = fmt.Fprintln(stdOut, "std-out")
|
|
|
|
_, _ = fmt.Fprintln(stdErr, "std-err")
|
|
|
|
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, cleanup(nil))
|
2023-03-09 03:34:52 +02:00
|
|
|
assert.Empty(t, b.String())
|
|
|
|
}
|
2023-03-31 21:13:29 +02:00
|
|
|
|
2023-03-09 03:34:52 +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)
|
2023-03-09 03:34:52 +02:00
|
|
|
|
|
|
|
_, _ = fmt.Fprintln(stdOut, "std-out")
|
|
|
|
_, _ = fmt.Fprintln(stdErr, "std-err")
|
|
|
|
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, cleanup(errors.New("any-error")))
|
2023-03-09 03:34:52 +02:00
|
|
|
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
|
2024-06-03 11:37:24 +02:00
|
|
|
l := &logger.Logger{
|
|
|
|
Color: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
var o output.Output = output.NewPrefixed(l)
|
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())
|
2023-04-06 12:18:41 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, cleanup(nil))
|
2018-04-23 02:40:03 +02:00
|
|
|
assert.Equal(t, "[prefix] Test!\n", b.String())
|
|
|
|
})
|
|
|
|
}
|
2024-06-03 11:37:24 +02:00
|
|
|
|
|
|
|
func TestPrefixedWithColor(t *testing.T) {
|
|
|
|
color.NoColor = false
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
l := &logger.Logger{
|
|
|
|
Color: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
var o output.Output = output.NewPrefixed(l)
|
|
|
|
|
|
|
|
writers := make([]io.Writer, 16)
|
|
|
|
for i := range writers {
|
|
|
|
writers[i], _, _ = o.WrapWriter(&b, io.Discard, fmt.Sprintf("prefix-%d", i), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("colors should loop", func(t *testing.T) {
|
|
|
|
for i, w := range writers {
|
|
|
|
b.Reset()
|
|
|
|
|
|
|
|
color := output.PrefixColorSequence[i%len(output.PrefixColorSequence)]
|
|
|
|
|
|
|
|
var prefix bytes.Buffer
|
|
|
|
l.FOutf(&prefix, color, fmt.Sprintf("prefix-%d", i))
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "foo\nbar")
|
|
|
|
assert.Equal(t, fmt.Sprintf("[%s] foo\n[%s] bar\n", prefix.String(), prefix.String()), b.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|