github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/experimental/sys/error_test.go (about)

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