1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 func TestErrIsExist(t
*testing
.T
) {
16 f
, err
:= ioutil
.TempFile("", "_Go_ErrIsExist")
18 t
.Fatalf("open ErrIsExist tempfile: %s", err
)
21 defer os
.Remove(f
.Name())
23 f2
, err
:= os
.OpenFile(f
.Name(), os
.O_RDWR|os
.O_CREATE|os
.O_EXCL
, 0600)
26 t
.Fatal("Open should have failed")
29 if s
:= checkErrorPredicate("os.IsExist", os
.IsExist
, err
); s
!= "" {
35 func testErrNotExist(name
string) string {
36 f
, err
:= os
.Open(name
)
39 return "Open should have failed"
41 if s
:= checkErrorPredicate("os.IsNotExist", os
.IsNotExist
, err
); s
!= "" {
47 return "Chdir should have failed"
49 if s
:= checkErrorPredicate("os.IsNotExist", os
.IsNotExist
, err
); s
!= "" {
55 func TestErrIsNotExist(t
*testing
.T
) {
56 tmpDir
, err
:= ioutil
.TempDir("", "_Go_ErrIsNotExist")
58 t
.Fatalf("create ErrIsNotExist tempdir: %s", err
)
61 defer os
.RemoveAll(tmpDir
)
63 name
:= filepath
.Join(tmpDir
, "NotExists")
64 if s
:= testErrNotExist(name
); s
!= "" {
69 name
= filepath
.Join(name
, "NotExists2")
70 if s
:= testErrNotExist(name
); s
!= "" {
76 func checkErrorPredicate(predName
string, pred
func(error
) bool, err error
) string {
78 return fmt
.Sprintf("%s does not work as expected for %#v", predName
, err
)
83 var isExistTests
= []struct {
88 {&os
.PathError
{Err
: os
.ErrInvalid
}, false, false},
89 {&os
.PathError
{Err
: os
.ErrPermission
}, false, false},
90 {&os
.PathError
{Err
: os
.ErrExist
}, true, false},
91 {&os
.PathError
{Err
: os
.ErrNotExist
}, false, true},
92 {&os
.LinkError
{Err
: os
.ErrInvalid
}, false, false},
93 {&os
.LinkError
{Err
: os
.ErrPermission
}, false, false},
94 {&os
.LinkError
{Err
: os
.ErrExist
}, true, false},
95 {&os
.LinkError
{Err
: os
.ErrNotExist
}, false, true},
99 func TestIsExist(t
*testing
.T
) {
100 for _
, tt
:= range isExistTests
{
101 if is
:= os
.IsExist(tt
.err
); is
!= tt
.is
{
102 t
.Errorf("os.IsExist(%T %v) = %v, want %v", tt
.err
, tt
.err
, is
, tt
.is
)
104 if isnot
:= os
.IsNotExist(tt
.err
); isnot
!= tt
.isnot
{
105 t
.Errorf("os.IsNotExist(%T %v) = %v, want %v", tt
.err
, tt
.err
, isnot
, tt
.isnot
)
110 func TestErrPathNUL(t
*testing
.T
) {
111 f
, err
:= ioutil
.TempFile("", "_Go_ErrPathNUL\x00")
114 t
.Fatal("TempFile should have failed")
116 f
, err
= ioutil
.TempFile("", "_Go_ErrPathNUL")
118 t
.Fatalf("open ErrPathNUL tempfile: %s", err
)
120 defer os
.Remove(f
.Name())
122 f2
, err
:= os
.OpenFile(f
.Name(), os
.O_RDWR
, 0600)
124 t
.Fatalf("open ErrPathNUL: %s", err
)
127 f2
, err
= os
.OpenFile(f
.Name()+"\x00", os
.O_RDWR
, 0600)
130 t
.Fatal("Open should have failed")