...

Source file src/os/error_test.go

Documentation: os

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

View as plain text