1
0
mirror of https://github.com/golang/go synced 2024-11-25 13:27:57 -07:00

os: test that IsExist and IsNotExist handle PathError and LinkError

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6442080
This commit is contained in:
Alex Brainman 2012-08-03 14:25:35 +10:00
parent c12a63f7d3
commit b9b29ce2ba
4 changed files with 77 additions and 17 deletions

View File

@ -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")

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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 {
} }
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 {
} }
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