forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_test.go
137 lines (110 loc) · 4.35 KB
/
writer_test.go
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
package scribe_test
import (
"bytes"
"testing"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testWriter(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("Writer", func() {
var (
buffer *bytes.Buffer
writer *scribe.Writer
)
it.Before(func() {
buffer = bytes.NewBuffer(nil)
writer = scribe.NewWriter(buffer)
})
context("Write", func() {
it("prints to the writer", func() {
_, err := writer.Write([]byte("some-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("some-text"))
})
context("when the writer has a color", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.BlueColor))
})
it("prints to the writer with the correct color codes", func() {
_, err := writer.Write([]byte("some-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\x1b[0;38;5;4msome-text\x1b[0m"))
})
})
context("when the writer has an indent", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithIndent(2))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("some-text\nother-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal(" some-text\n other-text"))
})
context("when sequential write inputs are not newline terminated", func() {
it("handles the indentation correctly", func() {
_, err := writer.Write([]byte("some-text"))
Expect(err).NotTo(HaveOccurred())
_, err = writer.Write([]byte(" followed by other-text\n"))
Expect(err).NotTo(HaveOccurred())
_, err = writer.Write([]byte("followed by\neven-more-text\n"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal(" some-text followed by other-text\n followed by\n even-more-text\n"))
})
})
})
context("when the writer has a prefix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithPrefix("[some-prefix] "))
})
it("prints to the writer with the given prefix", func() {
_, err := writer.Write([]byte("some-text\nother-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("[some-prefix] some-text\n[some-prefix] other-text"))
})
context("when sequential write inputs are not newline terminated", func() {
it("handles the indentation correctly", func() {
_, err := writer.Write([]byte("some-text"))
Expect(err).NotTo(HaveOccurred())
_, err = writer.Write([]byte(" followed by other-text\n"))
Expect(err).NotTo(HaveOccurred())
_, err = writer.Write([]byte("followed by\neven-more-text\n"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("[some-prefix] some-text followed by other-text\n[some-prefix] followed by\n[some-prefix] even-more-text\n"))
})
})
})
context("when the writer has a return prefix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2), scribe.WithPrefix("[some-prefix] "))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("\rsome-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\r\x1b[0;38;5;1m [some-prefix] some-text\x1b[0m"))
})
})
context("when the writer has a newline suffix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2), scribe.WithPrefix("[some-prefix] "))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("some-text\n"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\x1b[0;38;5;1m [some-prefix] some-text\x1b[0m\n"))
})
})
context("when the input has a percent symbol", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.MagentaColor))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("some-%"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\x1b[0;38;5;5msome-%\x1b[0m"))
})
})
})
})
}