Source file
src/os/error_test.go
1
2
3
4
5 package os_test
6
7 import (
8 "errors"
9 "fmt"
10 "io/fs"
11 "os"
12 "path/filepath"
13 "testing"
14 )
15
16 func TestErrIsExist(t *testing.T) {
17 t.Parallel()
18
19 f, err := os.CreateTemp("", "_Go_ErrIsExist")
20 if err != nil {
21 t.Fatalf("open ErrIsExist tempfile: %s", err)
22 return
23 }
24 defer os.Remove(f.Name())
25 defer f.Close()
26 f2, err := os.OpenFile(f.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
27 if err == nil {
28 f2.Close()
29 t.Fatal("Open should have failed")
30 }
31 if s := checkErrorPredicate("os.IsExist", os.IsExist, err, fs.ErrExist); s != "" {
32 t.Fatal(s)
33 }
34 }
35
36 func testErrNotExist(t *testing.T, name string) string {
37 originalWD, err := os.Getwd()
38 if err != nil {
39 t.Fatal(err)
40 }
41
42 f, err := os.Open(name)
43 if err == nil {
44 f.Close()
45 return "Open should have failed"
46 }
47 if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err, fs.ErrNotExist); s != "" {
48 return s
49 }
50
51 err = os.Chdir(name)
52 if err == nil {
53 if err := os.Chdir(originalWD); err != nil {
54 t.Fatalf("Chdir should have failed, failed to restore original working directory: %v", err)
55 }
56 return "Chdir should have failed, restored original working directory"
57 }
58 if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err, fs.ErrNotExist); s != "" {
59 return s
60 }
61 return ""
62 }
63
64 func TestErrIsNotExist(t *testing.T) {
65 tmpDir := t.TempDir()
66 name := filepath.Join(tmpDir, "NotExists")
67 if s := testErrNotExist(t, name); s != "" {
68 t.Fatal(s)
69 }
70
71 name = filepath.Join(name, "NotExists2")
72 if s := testErrNotExist(t, name); s != "" {
73 t.Fatal(s)
74 }
75 }
76
77 func checkErrorPredicate(predName string, pred func(error) bool, err, target error) string {
78 if !pred(err) {
79 return fmt.Sprintf("%s does not work as expected for %#v", predName, err)
80 }
81 if !errors.Is(err, target) {
82 return fmt.Sprintf("errors.Is(%#v, %#v) = false, want true", err, target)
83 }
84 return ""
85 }
86
87 type isExistTest struct {
88 err error
89 is bool
90 isnot bool
91 }
92
93 var isExistTests = []isExistTest{
94 {&fs.PathError{Err: fs.ErrInvalid}, false, false},
95 {&fs.PathError{Err: fs.ErrPermission}, false, false},
96 {&fs.PathError{Err: fs.ErrExist}, true, false},
97 {&fs.PathError{Err: fs.ErrNotExist}, false, true},
98 {&fs.PathError{Err: fs.ErrClosed}, false, false},
99 {&os.LinkError{Err: fs.ErrInvalid}, false, false},
100 {&os.LinkError{Err: fs.ErrPermission}, false, false},
101 {&os.LinkError{Err: fs.ErrExist}, true, false},
102 {&os.LinkError{Err: fs.ErrNotExist}, false, true},
103 {&os.LinkError{Err: fs.ErrClosed}, false, false},
104 {&os.SyscallError{Err: fs.ErrNotExist}, false, true},
105 {&os.SyscallError{Err: fs.ErrExist}, true, false},
106 {nil, false, false},
107 }
108
109 func TestIsExist(t *testing.T) {
110 for _, tt := range isExistTests {
111 if is := os.IsExist(tt.err); is != tt.is {
112 t.Errorf("os.IsExist(%T %v) = %v, want %v", tt.err, tt.err, is, tt.is)
113 }
114 if is := errors.Is(tt.err, fs.ErrExist); is != tt.is {
115 t.Errorf("errors.Is(%T %v, fs.ErrExist) = %v, want %v", tt.err, tt.err, is, tt.is)
116 }
117 if isnot := os.IsNotExist(tt.err); isnot != tt.isnot {
118 t.Errorf("os.IsNotExist(%T %v) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
119 }
120 if isnot := errors.Is(tt.err, fs.ErrNotExist); isnot != tt.isnot {
121 t.Errorf("errors.Is(%T %v, fs.ErrNotExist) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
122 }
123 }
124 }
125
126 type isPermissionTest struct {
127 err error
128 want bool
129 }
130
131 var isPermissionTests = []isPermissionTest{
132 {nil, false},
133 {&fs.PathError{Err: fs.ErrPermission}, true},
134 {&os.SyscallError{Err: fs.ErrPermission}, true},
135 }
136
137 func TestIsPermission(t *testing.T) {
138 for _, tt := range isPermissionTests {
139 if got := os.IsPermission(tt.err); got != tt.want {
140 t.Errorf("os.IsPermission(%#v) = %v; want %v", tt.err, got, tt.want)
141 }
142 if got := errors.Is(tt.err, fs.ErrPermission); got != tt.want {
143 t.Errorf("errors.Is(%#v, fs.ErrPermission) = %v; want %v", tt.err, got, tt.want)
144 }
145 }
146 }
147
148 func TestErrPathNUL(t *testing.T) {
149 t.Parallel()
150
151 f, err := os.CreateTemp("", "_Go_ErrPathNUL\x00")
152 if err == nil {
153 f.Close()
154 t.Fatal("TempFile should have failed")
155 }
156 f, err = os.CreateTemp("", "_Go_ErrPathNUL")
157 if err != nil {
158 t.Fatalf("open ErrPathNUL tempfile: %s", err)
159 }
160 defer os.Remove(f.Name())
161 defer f.Close()
162 f2, err := os.OpenFile(f.Name(), os.O_RDWR, 0600)
163 if err != nil {
164 t.Fatalf("open ErrPathNUL: %s", err)
165 }
166 f2.Close()
167 f2, err = os.OpenFile(f.Name()+"\x00", os.O_RDWR, 0600)
168 if err == nil {
169 f2.Close()
170 t.Fatal("Open should have failed")
171 }
172 }
173
174 func TestPathErrorUnwrap(t *testing.T) {
175 pe := &fs.PathError{Err: fs.ErrInvalid}
176 if !errors.Is(pe, fs.ErrInvalid) {
177 t.Error("errors.Is failed, wanted success")
178 }
179 }
180
181 type myErrorIs struct{ error }
182
183 func (e myErrorIs) Is(target error) bool { return target == e.error }
184
185 func TestErrorIsMethods(t *testing.T) {
186 if os.IsPermission(myErrorIs{fs.ErrPermission}) {
187 t.Error("os.IsPermission(err) = true when err.Is(fs.ErrPermission), wanted false")
188 }
189 }
190
View as plain text