Debian Codegraph @

pathrs-lite/internal/fd/fd_test.go

raw
1 // SPDX-License-Identifier: MPL-2.0
2
3 // Copyright (C) 2025 Aleksa Sarai <[email protected]>
4 // Copyright (C) 2025 SUSE LLC
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
9
10 package fd_test
11
12 import (
13 "os"
14 "testing"
15
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18
19 "github.com/cyphar/filepath-securejoin/pathrs-lite/internal/fd"
20 )
21
22 func TestNopCloser(t *testing.T) {
23 f, err := os.Open("/")
24 require.NoError(t, err)
25 require.NotNil(t, f, "open /")
26
27 actualName := f.Name()
28 actualFd := f.Fd()
29
30 f2 := fd.NopCloser(f)
31 require.NotNil(t, f, "wrap f2")
32
33 assert.NoError(t, f2.Close(), "close no-op") //nolint:testifylint // this is an isolated operation so we can continue despite an error
34 assert.NoError(t, f2.Close(), "close no-op again") //nolint:testifylint // this is an isolated operation so we can continue despite an error
35
36 assert.Equal(t, actualFd, f2.Fd(), "fd should still be valid (file not closed)")
37 assert.Equal(t, actualName, f2.Name(), "fd should still be valid (file not closed)")
38
39 require.NoError(t, f.Close(), "close underlying file")
40
41 assert.NotEqual(t, actualFd, f2.Fd(), "fd should not be valid (file closed)")
42 }