mirror of
https://github.com/golang/go
synced 2024-09-15 22:20:06 +00:00
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 <iant@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
0656da910a
commit
7c15b2ab4b
|
@ -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},
|
||||
|
|
|
@ -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 {
|
||||
|
|
39
src/os/error_unix_test.go
Normal file
39
src/os/error_unix_test.go
Normal file
|
@ -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},
|
||||
)
|
||||
|
||||
}
|
35
src/os/error_windows_test.go
Normal file
35
src/os/error_windows_test.go
Normal file
|
@ -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},
|
||||
)
|
||||
}
|
Loading…
Reference in a new issue