github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/os/path_test.go (about)

     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  	. "os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  func TestMkdirAll(t *testing.T) {
    15  	tmpDir := TempDir()
    16  	path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
    17  	err := MkdirAll(path, 0777)
    18  	if err != nil {
    19  		t.Fatalf("MkdirAll %q: %s", path, err)
    20  	}
    21  	// TODO: revert to upstream code which uses RemoveAll
    22  	defer Remove(tmpDir + "/_TestMkdirAll_/dir/dir2")
    23  	defer Remove(tmpDir + "/_TestMkdirAll_/dir")
    24  	defer Remove(tmpDir + "/_TestMkdirAll_")
    25  
    26  	// Already exists, should succeed.
    27  	err = MkdirAll(path, 0777)
    28  	if err != nil {
    29  		t.Fatalf("MkdirAll %q (second time): %s", path, err)
    30  	}
    31  
    32  	// Make file.
    33  	fpath := path + "/file"
    34  	f, err := Create(fpath)
    35  	if err != nil {
    36  		t.Fatalf("create %q: %s", fpath, err)
    37  	}
    38  	defer Remove(fpath)
    39  	defer f.Close()
    40  
    41  	// Can't make directory named after file.
    42  	err = MkdirAll(fpath, 0777)
    43  	if err == nil {
    44  		t.Fatalf("MkdirAll %q: no error", fpath)
    45  	}
    46  	perr, ok := err.(*PathError)
    47  	if !ok {
    48  		t.Fatalf("MkdirAll %q returned %T, not *PathError", fpath, err)
    49  	}
    50  	if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
    51  		t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
    52  	}
    53  
    54  	// Can't make subdirectory of file.
    55  	ffpath := fpath + "/subdir"
    56  	err = MkdirAll(ffpath, 0777)
    57  	if err == nil {
    58  		t.Fatalf("MkdirAll %q: no error", ffpath)
    59  	}
    60  	perr, ok = err.(*PathError)
    61  	if !ok {
    62  		t.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath, err)
    63  	}
    64  	if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
    65  		t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
    66  	}
    67  
    68  	if runtime.GOOS == "windows" {
    69  		path := tmpDir + `\_TestMkdirAll_\dir\.\dir2\`
    70  		err := MkdirAll(path, 0777)
    71  		if err != nil {
    72  			t.Fatalf("MkdirAll %q: %s", path, err)
    73  		}
    74  	}
    75  }