-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter_test.go
More file actions
139 lines (102 loc) · 3.75 KB
/
Copy pathwriter_test.go
File metadata and controls
139 lines (102 loc) · 3.75 KB
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
// Copyright 2026 The Gopherly Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package termio
import (
"bytes"
"errors"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var errWriteFailed = errors.New("write failed")
// failWriter always returns errWriteFailed on the first write.
type failWriter struct {
bytes.Buffer
}
func (f *failWriter) Write(_ []byte) (int, error) { return 0, errWriteFailed }
// TestWriter_WriteForwardsBytes verifies bytes reach the underlying buffer.
func TestWriter_WriteForwardsBytes(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
w := newWriter(buf, nil)
_, err := io.WriteString(w, "hello")
require.NoErrorf(t, err, "write must not fail on healthy buffer")
assert.Equalf(t, "hello", buf.String(), "bytes must reach underlying buffer")
}
// TestWriter_StickyErrorLatchesOnFirstFailure verifies the error is latched.
func TestWriter_StickyErrorLatchesOnFirstFailure(t *testing.T) {
t.Parallel()
w := newWriter(&failWriter{}, nil)
_, err := io.WriteString(w, "trigger")
require.ErrorIsf(t, err, errWriteFailed, "first write must return underlying error")
assert.ErrorIsf(t, w.Err(), errWriteFailed, "Err() must return latched error")
}
// TestWriter_StickyErrorBlocksSubsequentWrites verifies later writes are blocked.
func TestWriter_StickyErrorBlocksSubsequentWrites(t *testing.T) {
t.Parallel()
w := newWriter(&failWriter{}, nil)
io.WriteString(w, "trigger") //nolint:errcheck
_, err := io.WriteString(w, "second")
require.ErrorIsf(t, err, errWriteFailed, "subsequent write must return latched error")
}
// TestWriter_NoErrorInitially verifies Err() is nil before any failure.
func TestWriter_NoErrorInitially(t *testing.T) {
t.Parallel()
w := newWriter(&bytes.Buffer{}, nil)
assert.NoErrorf(t, w.Err(), "Err() must be nil before any failed write")
}
// TestWriter_FdBuffer verifies that a plain buffer yields InvalidFd.
func TestWriter_FdBuffer(t *testing.T) {
t.Parallel()
w := newWriter(&bytes.Buffer{}, nil)
assert.Equalf(t, InvalidFd, w.Fd(), "*bytes.Buffer must yield InvalidFd")
}
// TestWriter_FdPreservedThroughPolicy verifies Fd is captured before wrapping.
func TestWriter_FdPreservedThroughPolicy(t *testing.T) {
t.Parallel()
const want = uintptr(42)
raw := &stubFdWriter{fd: want}
w := newWriter(raw, noopPolicy{})
assert.Equalf(t, want, w.Fd(), "Fd must be captured from raw before policy wraps it")
}
// TestWriter_PolicyAdapts verifies a ColorPolicy transforms written bytes.
func TestWriter_PolicyAdapts(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
prefix := "PREFIX:"
p := &prefixPolicy{prefix: prefix}
w := newWriter(buf, p)
_, err := io.WriteString(w, "hello")
require.NoErrorf(t, err, "policy write must not fail")
assert.Equalf(t, prefix+"hello", buf.String(), "policy must transform the bytes")
}
// prefixPolicy is a test-only ColorPolicy that prepends a fixed string.
type prefixPolicy struct {
prefix string
}
func (p *prefixPolicy) Apply(w io.Writer) io.Writer {
return &prefixWriter{w: w, prefix: p.prefix}
}
type prefixWriter struct {
w io.Writer
prefix string
}
func (pw *prefixWriter) Write(b []byte) (int, error) {
_, err := io.WriteString(pw.w, pw.prefix)
if err != nil {
return 0, err
}
return pw.w.Write(b)
}