sysfs: adds FS.Stat and companions in platform (#1140)

This centralizes filestat logic by making our own `Stat_t` similar to
`syscall.Stat_t`. This exposes utilities in the platform package and
adds a new function `FS.Stat` which avoids having to use `fs.File` to
get the same info. Doing so at the FS abstraction allows us to optimize
how it is implemented internally using portable means (e.g.
`os.StatFile`) or OS-specific means where necessary, e.g. in windows.

This also ensures `platform.OpenFile` returns syscall.Errno and
centralizes error checking with a new `require.EqualErrno` test.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2023-02-21 10:13:37 +08:00
committed by GitHub
parent 2587989f0a
commit 4ca0858e57
48 changed files with 967 additions and 679 deletions

View File

@@ -13,6 +13,7 @@ import (
"reflect"
"runtime"
"strings"
"syscall"
"unicode"
"unicode/utf8"
)
@@ -126,6 +127,19 @@ func Error(t TestingT, err error, formatWithArgs ...interface{}) {
}
}
// EqualErrno should be used for functions that return syscall.Errno or nil.
func EqualErrno(t TestingT, expected syscall.Errno, err error, formatWithArgs ...interface{}) {
if err == nil {
fail(t, "expected a syscall.Errno, but was nil", "", formatWithArgs...)
return
}
if se, ok := err.(syscall.Errno); !ok {
fail(t, fmt.Sprintf("expected %v to be a syscall.Errno", err), "", formatWithArgs...)
} else if se != expected {
fail(t, fmt.Sprintf("expected Errno %#[1]v(%[1]s), but was %#[2]v(%[2]s)", expected, err), "", formatWithArgs...)
}
}
// ErrorIs fails if the err is nil or errors.Is fails against the expected.
//
// - formatWithArgs are optional. When the first is a string that contains '%', it is treated like fmt.Sprintf.

View File

@@ -0,0 +1,66 @@
package require
import (
"io"
"runtime"
"syscall"
"testing"
)
func TestEqualErrno(t *testing.T) {
// This specifically chooses ENOENT and EIO as outside windows, they tend
// to have the same errno literal and text message.
if runtime.GOOS == "windows" {
t.Skipf("error literals are different on windows")
}
tests := []struct {
name string
require func(TestingT)
expectedLog string
}{
{
name: "EqualErrno passes on equal",
require: func(t TestingT) {
EqualErrno(t, syscall.ENOENT, syscall.ENOENT)
},
},
{
name: "EqualErrno fails on nil",
require: func(t TestingT) {
EqualErrno(t, syscall.ENOENT, nil)
},
expectedLog: "expected a syscall.Errno, but was nil",
},
{
name: "EqualErrno fails on not Errno",
require: func(t TestingT) {
EqualErrno(t, syscall.ENOENT, io.EOF)
},
expectedLog: `expected EOF to be a syscall.Errno`,
},
{
name: "EqualErrno fails on not equal",
require: func(t TestingT) {
EqualErrno(t, syscall.ENOENT, syscall.EIO)
},
expectedLog: `expected Errno 0x2(no such file or directory), but was 0x5(input/output error)`,
},
{
name: "EqualErrno fails on not equal with format",
require: func(t TestingT) {
EqualErrno(t, syscall.ENOENT, syscall.EIO, "pay me %d", 5)
},
expectedLog: `expected Errno 0x2(no such file or directory), but was 0x5(input/output error): pay me 5`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
m := &mockT{t: t}
tc.require(m)
m.require(tc.expectedLog)
})
}
}