1
0

output_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package output_test
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "testing"
  8. "github.com/fatih/color"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "github.com/go-task/task/v3/internal/logger"
  12. "github.com/go-task/task/v3/internal/omap"
  13. "github.com/go-task/task/v3/internal/output"
  14. "github.com/go-task/task/v3/internal/templater"
  15. "github.com/go-task/task/v3/taskfile/ast"
  16. )
  17. func TestInterleaved(t *testing.T) {
  18. var b bytes.Buffer
  19. var o output.Output = output.Interleaved{}
  20. w, _, _ := o.WrapWriter(&b, io.Discard, "", nil)
  21. fmt.Fprintln(w, "foo\nbar")
  22. assert.Equal(t, "foo\nbar\n", b.String())
  23. fmt.Fprintln(w, "baz")
  24. assert.Equal(t, "foo\nbar\nbaz\n", b.String())
  25. }
  26. func TestGroup(t *testing.T) {
  27. var b bytes.Buffer
  28. var o output.Output = output.Group{}
  29. stdOut, stdErr, cleanup := o.WrapWriter(&b, io.Discard, "", nil)
  30. fmt.Fprintln(stdOut, "out\nout")
  31. assert.Equal(t, "", b.String())
  32. fmt.Fprintln(stdErr, "err\nerr")
  33. assert.Equal(t, "", b.String())
  34. fmt.Fprintln(stdOut, "out")
  35. assert.Equal(t, "", b.String())
  36. fmt.Fprintln(stdErr, "err")
  37. assert.Equal(t, "", b.String())
  38. require.NoError(t, cleanup(nil))
  39. assert.Equal(t, "out\nout\nerr\nerr\nout\nerr\n", b.String())
  40. }
  41. func TestGroupWithBeginEnd(t *testing.T) {
  42. tmpl := templater.Cache{
  43. Vars: &ast.Vars{
  44. OrderedMap: omap.FromMap(map[string]ast.Var{
  45. "VAR1": {Value: "example-value"},
  46. }),
  47. },
  48. }
  49. var o output.Output = output.Group{
  50. Begin: "::group::{{ .VAR1 }}",
  51. End: "::endgroup::",
  52. }
  53. t.Run("simple", func(t *testing.T) {
  54. var b bytes.Buffer
  55. w, _, cleanup := o.WrapWriter(&b, io.Discard, "", &tmpl)
  56. fmt.Fprintln(w, "foo\nbar")
  57. assert.Equal(t, "", b.String())
  58. fmt.Fprintln(w, "baz")
  59. assert.Equal(t, "", b.String())
  60. require.NoError(t, cleanup(nil))
  61. assert.Equal(t, "::group::example-value\nfoo\nbar\nbaz\n::endgroup::\n", b.String())
  62. })
  63. t.Run("no output", func(t *testing.T) {
  64. var b bytes.Buffer
  65. _, _, cleanup := o.WrapWriter(&b, io.Discard, "", &tmpl)
  66. require.NoError(t, cleanup(nil))
  67. assert.Equal(t, "", b.String())
  68. })
  69. }
  70. func TestGroupErrorOnlySwallowsOutputOnNoError(t *testing.T) {
  71. var b bytes.Buffer
  72. var o output.Output = output.Group{
  73. ErrorOnly: true,
  74. }
  75. stdOut, stdErr, cleanup := o.WrapWriter(&b, io.Discard, "", nil)
  76. _, _ = fmt.Fprintln(stdOut, "std-out")
  77. _, _ = fmt.Fprintln(stdErr, "std-err")
  78. require.NoError(t, cleanup(nil))
  79. assert.Empty(t, b.String())
  80. }
  81. func TestGroupErrorOnlyShowsOutputOnError(t *testing.T) {
  82. var b bytes.Buffer
  83. var o output.Output = output.Group{
  84. ErrorOnly: true,
  85. }
  86. stdOut, stdErr, cleanup := o.WrapWriter(&b, io.Discard, "", nil)
  87. _, _ = fmt.Fprintln(stdOut, "std-out")
  88. _, _ = fmt.Fprintln(stdErr, "std-err")
  89. require.NoError(t, cleanup(errors.New("any-error")))
  90. assert.Equal(t, "std-out\nstd-err\n", b.String())
  91. }
  92. func TestPrefixed(t *testing.T) {
  93. var b bytes.Buffer
  94. l := &logger.Logger{
  95. Color: false,
  96. }
  97. var o output.Output = output.NewPrefixed(l)
  98. w, _, cleanup := o.WrapWriter(&b, io.Discard, "prefix", nil)
  99. t.Run("simple use cases", func(t *testing.T) {
  100. b.Reset()
  101. fmt.Fprintln(w, "foo\nbar")
  102. assert.Equal(t, "[prefix] foo\n[prefix] bar\n", b.String())
  103. fmt.Fprintln(w, "baz")
  104. assert.Equal(t, "[prefix] foo\n[prefix] bar\n[prefix] baz\n", b.String())
  105. require.NoError(t, cleanup(nil))
  106. })
  107. t.Run("multiple writes for a single line", func(t *testing.T) {
  108. b.Reset()
  109. for _, char := range []string{"T", "e", "s", "t", "!"} {
  110. fmt.Fprint(w, char)
  111. assert.Equal(t, "", b.String())
  112. }
  113. require.NoError(t, cleanup(nil))
  114. assert.Equal(t, "[prefix] Test!\n", b.String())
  115. })
  116. }
  117. func TestPrefixedWithColor(t *testing.T) {
  118. color.NoColor = false
  119. var b bytes.Buffer
  120. l := &logger.Logger{
  121. Color: true,
  122. }
  123. var o output.Output = output.NewPrefixed(l)
  124. writers := make([]io.Writer, 16)
  125. for i := range writers {
  126. writers[i], _, _ = o.WrapWriter(&b, io.Discard, fmt.Sprintf("prefix-%d", i), nil)
  127. }
  128. t.Run("colors should loop", func(t *testing.T) {
  129. for i, w := range writers {
  130. b.Reset()
  131. color := output.PrefixColorSequence[i%len(output.PrefixColorSequence)]
  132. var prefix bytes.Buffer
  133. l.FOutf(&prefix, color, fmt.Sprintf("prefix-%d", i))
  134. fmt.Fprintln(w, "foo\nbar")
  135. assert.Equal(t, fmt.Sprintf("[%s] foo\n[%s] bar\n", prefix.String(), prefix.String()), b.String())
  136. }
  137. })
  138. }