-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfd_test.go
More file actions
70 lines (53 loc) · 1.93 KB
/
Copy pathfd_test.go
File metadata and controls
70 lines (53 loc) · 1.93 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
// 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"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestFdOf_NilFile verifies that a nil *[os.File] returns InvalidFd.
func TestFdOf_NilFile(t *testing.T) {
t.Parallel()
assert.Equalf(t, InvalidFd, fdOf((*os.File)(nil)), "nil *os.File must return InvalidFd")
}
// TestFdOf_Buffer verifies that a *[bytes.Buffer] returns InvalidFd.
func TestFdOf_Buffer(t *testing.T) {
t.Parallel()
assert.Equalf(t, InvalidFd, fdOf(&bytes.Buffer{}), "*bytes.Buffer must return InvalidFd")
}
// TestFdOf_FdIface verifies that a type implementing Fd() is forwarded.
func TestFdOf_FdIface(t *testing.T) {
t.Parallel()
const want = uintptr(42)
w := &stubFdWriter{fd: want}
assert.Equalf(t, want, fdOf(w), "Fd() implementation must be forwarded")
}
// TestFdOf_RealFile verifies that a real *[os.File] returns a valid FD.
func TestFdOf_RealFile(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp(t.TempDir(), "termio-fd-test-*")
require.NoErrorf(t, err, "create temp file")
t.Cleanup(func() { f.Close() }) //nolint:errcheck
got := fdOf(f)
assert.NotEqualf(t, InvalidFd, got, "real *os.File must not return InvalidFd")
}
// stubFdWriter is a minimal fileWriter used only in tests.
type stubFdWriter struct {
bytes.Buffer
fd uintptr
}
func (s *stubFdWriter) Fd() uintptr { return s.fd }