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