Source file src/os/path_test.go

     1  // Copyright 2009 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  	"internal/testenv"
     9  	. "os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"syscall"
    13  	"testing"
    14  )
    15  
    16  var isReadonlyError = func(error) bool { return false }
    17  
    18  func TestMkdirAll(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	tmpDir := TempDir()
    22  	path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
    23  	err := MkdirAll(path, 0777)
    24  	if err != nil {
    25  		t.Fatalf("MkdirAll %q: %s", path, err)
    26  	}
    27  	defer RemoveAll(tmpDir + "/_TestMkdirAll_")
    28  
    29  	// Already exists, should succeed.
    30  	err = MkdirAll(path, 0777)
    31  	if err != nil {
    32  		t.Fatalf("MkdirAll %q (second time): %s", path, err)
    33  	}
    34  
    35  	// Make file.
    36  	fpath := path + "/file"
    37  	f, err := Create(fpath)
    38  	if err != nil {
    39  		t.Fatalf("create %q: %s", fpath, err)
    40  	}
    41  	defer f.Close()
    42  
    43  	// Can't make directory named after file.
    44  	err = MkdirAll(fpath, 0777)
    45  	if err == nil {
    46  		t.Fatalf("MkdirAll %q: no error", fpath)
    47  	}
    48  	perr, ok := err.(*PathError)
    49  	if !ok {
    50  		t.Fatalf("MkdirAll %q returned %T, not *PathError", fpath, err)
    51  	}
    52  	if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
    53  		t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
    54  	}
    55  
    56  	// Can't make subdirectory of file.
    57  	ffpath := fpath + "/subdir"
    58  	err = MkdirAll(ffpath, 0777)
    59  	if err == nil {
    60  		t.Fatalf("MkdirAll %q: no error", ffpath)
    61  	}
    62  	perr, ok = err.(*PathError)
    63  	if !ok {
    64  		t.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath, err)
    65  	}
    66  	if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
    67  		t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
    68  	}
    69  
    70  	if runtime.GOOS == "windows" {
    71  		path := tmpDir + `\_TestMkdirAll_\dir\.\dir2\`
    72  		err := MkdirAll(path, 0777)
    73  		if err != nil {
    74  			t.Fatalf("MkdirAll %q: %s", path, err)
    75  		}
    76  	}
    77  }
    78  
    79  func TestMkdirAllWithSymlink(t *testing.T) {
    80  	testenv.MustHaveSymlink(t)
    81  	t.Parallel()
    82  
    83  	tmpDir := t.TempDir()
    84  	dir := tmpDir + "/dir"
    85  	if err := Mkdir(dir, 0755); err != nil {
    86  		t.Fatalf("Mkdir %s: %s", dir, err)
    87  	}
    88  
    89  	link := tmpDir + "/link"
    90  	if err := Symlink("dir", link); err != nil {
    91  		t.Fatalf("Symlink %s: %s", link, err)
    92  	}
    93  
    94  	path := link + "/foo"
    95  	if err := MkdirAll(path, 0755); err != nil {
    96  		t.Errorf("MkdirAll %q: %s", path, err)
    97  	}
    98  }
    99  
   100  func TestMkdirAllAtSlash(t *testing.T) {
   101  	switch runtime.GOOS {
   102  	case "android", "ios", "plan9", "windows":
   103  		t.Skipf("skipping on %s", runtime.GOOS)
   104  	}
   105  	if testenv.Builder() == "" {
   106  		t.Skipf("skipping non-hermetic test outside of Go builders")
   107  	}
   108  
   109  	RemoveAll("/_go_os_test")
   110  	const dir = "/_go_os_test/dir"
   111  	err := MkdirAll(dir, 0777)
   112  	if err != nil {
   113  		pathErr, ok := err.(*PathError)
   114  		// common for users not to be able to write to /
   115  		if ok && (pathErr.Err == syscall.EACCES || isReadonlyError(pathErr.Err)) {
   116  			t.Skipf("could not create %v: %v", dir, err)
   117  		}
   118  		t.Fatalf(`MkdirAll "/_go_os_test/dir": %v, %s`, err, pathErr.Err)
   119  	}
   120  	RemoveAll("/_go_os_test")
   121  }
   122  

View as plain text