Commit b9b29ce2 authored by Alex Brainman's avatar Alex Brainman

os: test that IsExist and IsNotExist handle PathError and LinkError

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6442080
parent c12a63f7
...@@ -5,30 +5,36 @@ ...@@ -5,30 +5,36 @@
package os package os
func isExist(err error) bool { func isExist(err error) bool {
if err == nil { switch pe := err.(type) {
case nil:
return false return false
} case *PathError:
if pe, ok := err.(*PathError); ok { err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return contains(err.Error(), " exists") return contains(err.Error(), " exists")
} }
func isNotExist(err error) bool { func isNotExist(err error) bool {
if err == nil { switch pe := err.(type) {
case nil:
return false return false
} case *PathError:
if pe, ok := err.(*PathError); ok { err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return contains(err.Error(), "does not exist") return contains(err.Error(), "does not exist")
} }
func isPermission(err error) bool { func isPermission(err error) bool {
if err == nil { switch pe := err.(type) {
case nil:
return false return false
} case *PathError:
if pe, ok := err.(*PathError); ok { err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return contains(err.Error(), "permission denied") return contains(err.Error(), "permission denied")
......
...@@ -9,21 +9,36 @@ package os ...@@ -9,21 +9,36 @@ package os
import "syscall" import "syscall"
func isExist(err error) bool { func isExist(err error) bool {
if pe, ok := err.(*PathError); ok { switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return err == syscall.EEXIST || err == ErrExist return err == syscall.EEXIST || err == ErrExist
} }
func isNotExist(err error) bool { func isNotExist(err error) bool {
if pe, ok := err.(*PathError); ok { switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return err == syscall.ENOENT || err == ErrNotExist return err == syscall.ENOENT || err == ErrNotExist
} }
func isPermission(err error) bool { func isPermission(err error) bool {
if pe, ok := err.(*PathError); ok { switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
......
...@@ -79,3 +79,30 @@ func checkErrorPredicate(predName string, pred func(error) bool, err error) stri ...@@ -79,3 +79,30 @@ func checkErrorPredicate(predName string, pred func(error) bool, err error) stri
} }
return "" return ""
} }
var isExistTests = []struct {
err error
is bool
isnot bool
}{
{&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.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},
{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 isnot := os.IsNotExist(tt.err); isnot != tt.isnot {
t.Errorf("os.IsNotExist(%T %v) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
}
}
}
...@@ -7,10 +7,12 @@ package os ...@@ -7,10 +7,12 @@ package os
import "syscall" import "syscall"
func isExist(err error) bool { func isExist(err error) bool {
if pe, ok := err.(*PathError); ok { switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err err = pe.Err
} case *LinkError:
if pe, ok := err.(*LinkError); ok {
err = pe.Err err = pe.Err
} }
return err == syscall.ERROR_ALREADY_EXISTS || return err == syscall.ERROR_ALREADY_EXISTS ||
...@@ -18,7 +20,12 @@ func isExist(err error) bool { ...@@ -18,7 +20,12 @@ func isExist(err error) bool {
} }
func isNotExist(err error) bool { func isNotExist(err error) bool {
if pe, ok := err.(*PathError); ok { switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return err == syscall.ERROR_FILE_NOT_FOUND || return err == syscall.ERROR_FILE_NOT_FOUND ||
...@@ -26,7 +33,12 @@ func isNotExist(err error) bool { ...@@ -26,7 +33,12 @@ func isNotExist(err error) bool {
} }
func isPermission(err error) bool { func isPermission(err error) bool {
if pe, ok := err.(*PathError); ok { switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err err = pe.Err
} }
return err == syscall.ERROR_ACCESS_DENIED || err == ErrPermission return err == syscall.ERROR_ACCESS_DENIED || err == ErrPermission
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment