github.com/x04/go/src@v0.0.0-20200202162449-3d481ceb3525/os/error_test.go (about)

     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.
     4  
     5  package os_test
     6  
     7  import (
     8  	"github.com/x04/go/src/errors"
     9  	"github.com/x04/go/src/fmt"
    10  	"github.com/x04/go/src/io/ioutil"
    11  	"github.com/x04/go/src/os"
    12  	"github.com/x04/go/src/path/filepath"
    13  	"github.com/x04/go/src/testing"
    14  )
    15  
    16  func TestErrIsExist(t *testing.T) {
    17  	f, err := ioutil.TempFile("", "_Go_ErrIsExist")
    18  	if err != nil {
    19  		t.Fatalf("open ErrIsExist tempfile: %s", err)
    20  		return
    21  	}
    22  	defer os.Remove(f.Name())
    23  	defer f.Close()
    24  	f2, err := os.OpenFile(f.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
    25  	if err == nil {
    26  		f2.Close()
    27  		t.Fatal("Open should have failed")
    28  		return
    29  	}
    30  	if s := checkErrorPredicate("os.IsExist", os.IsExist, err, os.ErrExist); s != "" {
    31  		t.Fatal(s)
    32  		return
    33  	}
    34  }
    35  
    36  func testErrNotExist(name string) string {
    37  	f, err := os.Open(name)
    38  	if err == nil {
    39  		f.Close()
    40  		return "Open should have failed"
    41  	}
    42  	if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err, os.ErrNotExist); s != "" {
    43  		return s
    44  	}
    45  
    46  	err = os.Chdir(name)
    47  	if err == nil {
    48  		return "Chdir should have failed"
    49  	}
    50  	if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err, os.ErrNotExist); s != "" {
    51  		return s
    52  	}
    53  	return ""
    54  }
    55  
    56  func TestErrIsNotExist(t *testing.T) {
    57  	tmpDir, err := ioutil.TempDir("", "_Go_ErrIsNotExist")
    58  	if err != nil {
    59  		t.Fatalf("create ErrIsNotExist tempdir: %s", err)
    60  		return
    61  	}
    62  	defer os.RemoveAll(tmpDir)
    63  
    64  	name := filepath.Join(tmpDir, "NotExists")
    65  	if s := testErrNotExist(name); s != "" {
    66  		t.Fatal(s)
    67  		return
    68  	}
    69  
    70  	name = filepath.Join(name, "NotExists2")
    71  	if s := testErrNotExist(name); s != "" {
    72  		t.Fatal(s)
    73  		return
    74  	}
    75  }
    76  
    77  func checkErrorPredicate(predName string, pred func(error) bool, err, target error) string {
    78  	if !pred(err) {
    79  		return fmt.Sprintf("%s does not work as expected for %#v", predName, err)
    80  	}
    81  	if !errors.Is(err, target) {
    82  		return fmt.Sprintf("errors.Is(%#v, %#v) = false, want true", err, target)
    83  	}
    84  	return ""
    85  }
    86  
    87  type isExistTest struct {
    88  	err	error
    89  	is	bool
    90  	isnot	bool
    91  }
    92  
    93  var isExistTests = []isExistTest{
    94  	{&os.PathError{Err: os.ErrInvalid}, false, false},
    95  	{&os.PathError{Err: os.ErrPermission}, false, false},
    96  	{&os.PathError{Err: os.ErrExist}, true, false},
    97  	{&os.PathError{Err: os.ErrNotExist}, false, true},
    98  	{&os.PathError{Err: os.ErrClosed}, false, false},
    99  	{&os.LinkError{Err: os.ErrInvalid}, false, false},
   100  	{&os.LinkError{Err: os.ErrPermission}, false, false},
   101  	{&os.LinkError{Err: os.ErrExist}, true, false},
   102  	{&os.LinkError{Err: os.ErrNotExist}, false, true},
   103  	{&os.LinkError{Err: os.ErrClosed}, false, false},
   104  	{&os.SyscallError{Err: os.ErrNotExist}, false, true},
   105  	{&os.SyscallError{Err: os.ErrExist}, true, false},
   106  	{nil, false, false},
   107  }
   108  
   109  func TestIsExist(t *testing.T) {
   110  	for _, tt := range isExistTests {
   111  		if is := os.IsExist(tt.err); is != tt.is {
   112  			t.Errorf("os.IsExist(%T %v) = %v, want %v", tt.err, tt.err, is, tt.is)
   113  		}
   114  		if is := errors.Is(tt.err, os.ErrExist); is != tt.is {
   115  			t.Errorf("errors.Is(%T %v, os.ErrExist) = %v, want %v", tt.err, tt.err, is, tt.is)
   116  		}
   117  		if isnot := os.IsNotExist(tt.err); isnot != tt.isnot {
   118  			t.Errorf("os.IsNotExist(%T %v) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
   119  		}
   120  		if isnot := errors.Is(tt.err, os.ErrNotExist); isnot != tt.isnot {
   121  			t.Errorf("errors.Is(%T %v, os.ErrNotExist) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
   122  		}
   123  	}
   124  }
   125  
   126  type isPermissionTest struct {
   127  	err	error
   128  	want	bool
   129  }
   130  
   131  var isPermissionTests = []isPermissionTest{
   132  	{nil, false},
   133  	{&os.PathError{Err: os.ErrPermission}, true},
   134  	{&os.SyscallError{Err: os.ErrPermission}, true},
   135  }
   136  
   137  func TestIsPermission(t *testing.T) {
   138  	for _, tt := range isPermissionTests {
   139  		if got := os.IsPermission(tt.err); got != tt.want {
   140  			t.Errorf("os.IsPermission(%#v) = %v; want %v", tt.err, got, tt.want)
   141  		}
   142  		if got := errors.Is(tt.err, os.ErrPermission); got != tt.want {
   143  			t.Errorf("errors.Is(%#v, os.ErrPermission) = %v; want %v", tt.err, got, tt.want)
   144  		}
   145  	}
   146  }
   147  
   148  func TestErrPathNUL(t *testing.T) {
   149  	f, err := ioutil.TempFile("", "_Go_ErrPathNUL\x00")
   150  	if err == nil {
   151  		f.Close()
   152  		t.Fatal("TempFile should have failed")
   153  	}
   154  	f, err = ioutil.TempFile("", "_Go_ErrPathNUL")
   155  	if err != nil {
   156  		t.Fatalf("open ErrPathNUL tempfile: %s", err)
   157  	}
   158  	defer os.Remove(f.Name())
   159  	defer f.Close()
   160  	f2, err := os.OpenFile(f.Name(), os.O_RDWR, 0600)
   161  	if err != nil {
   162  		t.Fatalf("open ErrPathNUL: %s", err)
   163  	}
   164  	f2.Close()
   165  	f2, err = os.OpenFile(f.Name()+"\x00", os.O_RDWR, 0600)
   166  	if err == nil {
   167  		f2.Close()
   168  		t.Fatal("Open should have failed")
   169  	}
   170  }
   171  
   172  func TestPathErrorUnwrap(t *testing.T) {
   173  	pe := &os.PathError{Err: os.ErrInvalid}
   174  	if !errors.Is(pe, os.ErrInvalid) {
   175  		t.Error("errors.Is failed, wanted success")
   176  	}
   177  }
   178  
   179  type myErrorIs struct{ error }
   180  
   181  func (e myErrorIs) Is(target error) bool	{ return target == e.error }
   182  
   183  func TestErrorIsMethods(t *testing.T) {
   184  	if os.IsPermission(myErrorIs{os.ErrPermission}) {
   185  		t.Error("os.IsPermission(err) = true when err.Is(os.ErrPermission), wanted false")
   186  	}
   187  }