github.com/tetratelabs/wazero@v1.2.1/internal/platform/error_test.go (about) 1 package platform 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "io/fs" 8 "os" 9 "syscall" 10 "testing" 11 12 "github.com/tetratelabs/wazero/internal/testing/require" 13 ) 14 15 func TestUnwrapOSError(t *testing.T) { 16 tests := []struct { 17 name string 18 input error 19 expected syscall.Errno 20 }{ 21 { 22 name: "io.EOF is not an error", 23 input: io.EOF, 24 expected: 0, 25 }, 26 { 27 name: "LinkError ErrInvalid", 28 input: &os.LinkError{Err: fs.ErrInvalid}, 29 expected: syscall.EINVAL, 30 }, 31 { 32 name: "PathError ErrInvalid", 33 input: &os.PathError{Err: fs.ErrInvalid}, 34 expected: syscall.EINVAL, 35 }, 36 { 37 name: "SyscallError ErrInvalid", 38 input: &os.SyscallError{Err: fs.ErrInvalid}, 39 expected: syscall.EINVAL, 40 }, 41 { 42 name: "PathError ErrPermission", 43 input: &os.PathError{Err: os.ErrPermission}, 44 expected: syscall.EPERM, 45 }, 46 { 47 name: "PathError ErrExist", 48 input: &os.PathError{Err: os.ErrExist}, 49 expected: syscall.EEXIST, 50 }, 51 { 52 name: "PathError syscall.ErrnotExist", 53 input: &os.PathError{Err: os.ErrNotExist}, 54 expected: syscall.ENOENT, 55 }, 56 { 57 name: "PathError ErrClosed", 58 input: &os.PathError{Err: os.ErrClosed}, 59 expected: syscall.EBADF, 60 }, 61 { 62 name: "PathError unknown == syscall.EIO", 63 input: &os.PathError{Err: errors.New("ice cream")}, 64 expected: syscall.EIO, 65 }, 66 { 67 name: "unknown == syscall.EIO", 68 input: errors.New("ice cream"), 69 expected: syscall.EIO, 70 }, 71 { 72 name: "very wrapped unknown == syscall.EIO", 73 input: fmt.Errorf("%w", fmt.Errorf("%w", fmt.Errorf("%w", errors.New("ice cream")))), 74 expected: syscall.EIO, 75 }, 76 } 77 78 for _, tt := range tests { 79 tc := tt 80 t.Run(tc.name, func(t *testing.T) { 81 errno := UnwrapOSError(tc.input) 82 require.EqualErrno(t, tc.expected, errno) 83 }) 84 } 85 86 t.Run("nil -> zero", func(t *testing.T) { 87 require.Zero(t, UnwrapOSError(nil)) 88 }) 89 }