From: Brad Fitzpatrick Date: Sun, 3 Apr 2016 00:44:39 +0000 (+0000) Subject: os: make IsExists also recognize syscall.ENOTEMPTY X-Git-Tag: go1.7beta1~936 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7c15b2ab4bfec46b07794c5406a438f993cdc56e;p=gostls13.git os: make IsExists also recognize syscall.ENOTEMPTY And adds missing tests. Fixes #14970 Change-Id: I0dba02603bc245f555498cb5dd3e0a9d87c52353 Reviewed-on: https://go-review.googlesource.com/21467 Reviewed-by: Ian Lance Taylor Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/os/error_test.go b/src/os/error_test.go index 5477e7ecbd..a47c1732cb 100644 --- a/src/os/error_test.go +++ b/src/os/error_test.go @@ -80,11 +80,13 @@ func checkErrorPredicate(predName string, pred func(error) bool, err error) stri return "" } -var isExistTests = []struct { +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}, @@ -109,10 +111,12 @@ func TestIsExist(t *testing.T) { } } -var isPermissionTests = []struct { +type isPermissionTest struct { err error want bool -}{ +} + +var isPermissionTests = []isPermissionTest{ {nil, false}, {&os.PathError{Err: os.ErrPermission}, true}, {&os.SyscallError{Err: os.ErrPermission}, true}, diff --git a/src/os/error_unix.go b/src/os/error_unix.go index c6002279da..3c78eb4dd2 100644 --- a/src/os/error_unix.go +++ b/src/os/error_unix.go @@ -19,7 +19,7 @@ func isExist(err error) bool { case *SyscallError: err = pe.Err } - return err == syscall.EEXIST || err == ErrExist + return err == syscall.EEXIST || err == syscall.ENOTEMPTY || err == ErrExist } func isNotExist(err error) bool { diff --git a/src/os/error_unix_test.go b/src/os/error_unix_test.go new file mode 100644 index 0000000000..76fe015b22 --- /dev/null +++ b/src/os/error_unix_test.go @@ -0,0 +1,39 @@ +// Copyright 2016 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. + +// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris + +package os_test + +import ( + "os" + "syscall" +) + +func init() { + isExistTests = append(isExistTests, + isExistTest{err: &os.PathError{Err: syscall.EEXIST}, is: true, isnot: false}, + isExistTest{err: &os.PathError{Err: syscall.ENOTEMPTY}, is: true, isnot: false}, + + isExistTest{err: &os.LinkError{Err: syscall.EEXIST}, is: true, isnot: false}, + isExistTest{err: &os.LinkError{Err: syscall.ENOTEMPTY}, is: true, isnot: false}, + + isExistTest{err: &os.SyscallError{Err: syscall.EEXIST}, is: true, isnot: false}, + isExistTest{err: &os.SyscallError{Err: syscall.ENOTEMPTY}, is: true, isnot: false}, + ) + isPermissionTests = append(isPermissionTests, + isPermissionTest{err: &os.PathError{Err: syscall.EACCES}, want: true}, + isPermissionTest{err: &os.PathError{Err: syscall.EPERM}, want: true}, + isPermissionTest{err: &os.PathError{Err: syscall.EEXIST}, want: false}, + + isPermissionTest{err: &os.LinkError{Err: syscall.EACCES}, want: true}, + isPermissionTest{err: &os.LinkError{Err: syscall.EPERM}, want: true}, + isPermissionTest{err: &os.LinkError{Err: syscall.EEXIST}, want: false}, + + isPermissionTest{err: &os.SyscallError{Err: syscall.EACCES}, want: true}, + isPermissionTest{err: &os.SyscallError{Err: syscall.EPERM}, want: true}, + isPermissionTest{err: &os.SyscallError{Err: syscall.EEXIST}, want: false}, + ) + +} diff --git a/src/os/error_windows_test.go b/src/os/error_windows_test.go new file mode 100644 index 0000000000..427dfdb962 --- /dev/null +++ b/src/os/error_windows_test.go @@ -0,0 +1,35 @@ +// Copyright 2016 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. + +// +build windows + +package os_test + +import ( + "os" + "syscall" +) + +func init() { + const _ERROR_BAD_NETPATH = syscall.Errno(53) + + isExistTests = append(isExistTests, + isExistTest{err: &os.PathError{Err: syscall.ERROR_FILE_NOT_FOUND}, is: false, isnot: true}, + isExistTest{err: &os.LinkError{Err: syscall.ERROR_FILE_NOT_FOUND}, is: false, isnot: true}, + isExistTest{err: &os.SyscallError{Err: syscall.ERROR_FILE_NOT_FOUND}, is: false, isnot: true}, + + isExistTest{err: &os.PathError{Err: _ERROR_BAD_NETPATH}, is: false, isnot: true}, + isExistTest{err: &os.LinkError{Err: _ERROR_BAD_NETPATH}, is: false, isnot: true}, + isExistTest{err: &os.SyscallError{Err: _ERROR_BAD_NETPATH}, is: false, isnot: true}, + + isExistTest{err: &os.PathError{Err: syscall.ERROR_PATH_NOT_FOUND}, is: false, isnot: true}, + isExistTest{err: &os.LinkError{Err: syscall.ERROR_PATH_NOT_FOUND}, is: false, isnot: true}, + isExistTest{err: &os.SyscallError{Err: syscall.ERROR_PATH_NOT_FOUND}, is: false, isnot: true}, + ) + isPermissionTests = append(isPermissionTests, + isPermissionTest{err: &os.PathError{Err: syscall.ERROR_ACCESS_DENIED}, want: true}, + isPermissionTest{err: &os.LinkError{Err: syscall.ERROR_ACCESS_DENIED}, want: true}, + isPermissionTest{err: &os.SyscallError{Err: syscall.ERROR_ACCESS_DENIED}, want: true}, + ) +}