1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
package internal_test
import (
"errors"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/ginkgo/v2/internal"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("Writer", func() {
var writer *internal.Writer
var out *gbytes.Buffer
BeforeEach(func() {
out = gbytes.NewBuffer()
writer = internal.NewWriter(out)
})
Context("when configured to WriterModeStreamAndBuffer (the default setting)", func() {
It("streams directly to the passed in writer, indenting content as it writes it", func() {
writer.Write([]byte("foo"))
Ω(out).Should(gbytes.Say(" foo"))
})
It("does also stores the bytes", func() {
writer.Write([]byte("foo"))
Ω(out).Should(gbytes.Say("foo"))
Ω(string(writer.Bytes())).Should(Equal("foo"))
})
})
Describe("indenting output", func() {
It("handles all the vagaries of indenting a stream", func() {
writer.Write([]byte("foo"))
writer.Write([]byte(" bar\nbaz"))
writer.Write([]byte("zle\n"))
writer.Write([]byte("\nbloop"))
writer.Write([]byte("floop\n"))
writer.Write([]byte("flarp\r\n"))
writer.Write([]byte("flan"))
Ω(string(out.Contents())).Should(Equal(" foo bar\n bazzle\n\n bloopfloop\n flarp\r\n flan"))
})
})
Context("when configured to WriterModeBufferOnly", func() {
BeforeEach(func() {
writer.SetMode(internal.WriterModeBufferOnly)
})
It("should not write to the passed in writer", func() {
writer.Write([]byte("foo"))
Ω(out).ShouldNot(gbytes.Say("foo"))
})
Describe("Bytes()", func() {
BeforeEach(func() {
writer.Write([]byte("foo"))
})
It("returns all that's been written so far", func() {
Ω(writer.Bytes()).Should(Equal([]byte("foo")))
})
It("clears when told to truncate", func() {
writer.Truncate()
Ω(writer.Bytes()).Should(BeEmpty())
writer.Write([]byte("bar"))
Ω(writer.Bytes()).Should(Equal([]byte("bar")))
})
})
})
Describe("Teeing to additional writers", func() {
var tee1, tee2 *gbytes.Buffer
BeforeEach(func() {
tee1 = gbytes.NewBuffer()
tee2 = gbytes.NewBuffer()
})
Context("when told to tee to additional writers", func() {
BeforeEach(func() {
writer.TeeTo(tee1)
writer.TeeTo(tee2)
})
It("tees to all registered tee writers", func() {
writer.Print("hello")
Ω(string(tee1.Contents())).Should(Equal("hello"))
Ω(string(tee2.Contents())).Should(Equal("hello"))
})
Context("when told to clear tee writers", func() {
BeforeEach(func() {
writer.ClearTeeWriters()
})
It("stops teeing to said writers", func() {
writer.Print("goodbye")
Ω(tee1.Contents()).Should(BeEmpty())
Ω(tee2.Contents()).Should(BeEmpty())
})
})
})
})
Describe("Convenience print methods", func() {
It("can Print", func() {
writer.Print("foo", "baz", " ", "bizzle")
Ω(string(out.Contents())).Should(Equal(" foobaz bizzle"))
})
It("can Println", func() {
writer.Println("foo", "baz", " ", "bizzle")
Ω(string(out.Contents())).Should(Equal(" foo baz bizzle\n"))
})
It("can Printf", func() {
writer.Printf("%s%d - %s\n", "foo", 17, "bar")
Ω(string(out.Contents())).Should(Equal(" foo17 - bar\n"))
})
})
When("wrapped by logr", func() {
It("can print Info logs", func() {
log := internal.GinkgoLogrFunc(writer)
log.Info("message", "key", 5)
Ω(string(out.Contents())).Should(Equal(" \"level\"=0 \"msg\"=\"message\" \"key\"=5\n"))
})
It("can print Error logs", func() {
log := internal.GinkgoLogrFunc(writer)
log.Error(errors.New("cake"), "planned failure", "key", "banana")
Ω(string(out.Contents())).Should(Equal(" \"msg\"=\"planned failure\" \"error\"=\"cake\" \"key\"=\"banana\"\n"))
})
It("can print multiple logs", func() {
log := internal.GinkgoLogrFunc(writer)
log.Info("message", "key", 5)
log.Error(errors.New("cake"), "planned failure", "key", "banana")
Ω(string(out.Contents())).Should(Equal(" \"level\"=0 \"msg\"=\"message\" \"key\"=5\n \"msg\"=\"planned failure\" \"error\"=\"cake\" \"key\"=\"banana\"\n"))
})
It("can print the logr prefix", func() {
log := internal.GinkgoLogrFunc(writer)
log.WithName("berry").Info("message", "key", 5)
Ω(string(out.Contents())).Should(Equal(" berry \"level\"=0 \"msg\"=\"message\" \"key\"=5\n"))
})
})
})
|