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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user