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 @@
package os
func isExist(err error) bool {
if err == nil {
switch pe := err.(type) {
case nil:
return false
}
if pe, ok := err.(*PathError); ok {
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
}
return contains(err.Error(), " exists")
}
func isNotExist(err error) bool {
if err == nil {
switch pe := err.(type) {
case nil:
return false
}
if pe, ok := err.(*PathError); ok {
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
}
return contains(err.Error(), "does not exist")
}
func isPermission(err error) bool {
if err == nil {
switch pe := err.(type) {
case nil:
return false
}
if pe, ok := err.(*PathError); ok {
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
}
return contains(err.Error(), "permission denied")
......
......@@ -9,21 +9,36 @@ package os
import "syscall"
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
}
return err == syscall.EEXIST || err == ErrExist
}
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
}
return err == syscall.ENOENT || err == ErrNotExist
}
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
}
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
......
......@@ -79,3 +79,30 @@ func checkErrorPredicate(predName string, pred func(error) bool, err error) stri
}
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
import "syscall"
func isExist(err error) bool {
if pe, ok := err.(*PathError); ok {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
}
if pe, ok := err.(*LinkError); ok {
case *LinkError:
err = pe.Err
}
return err == syscall.ERROR_ALREADY_EXISTS ||
......@@ -18,7 +20,12 @@ func isExist(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
}
return err == syscall.ERROR_FILE_NOT_FOUND ||
......@@ -26,7 +33,12 @@ func isNotExist(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
}
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