Retro68/gcc/libgo/go/os/path_test.go

129 lines
3.1 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os_test
import (
"internal/testenv"
2014-09-21 17:33:12 +00:00
"io/ioutil"
2012-03-27 23:13:14 +00:00
. "os"
"path/filepath"
"runtime"
"syscall"
"testing"
)
2017-04-10 11:32:00 +00:00
var isReadonlyError = func(error) bool { return false }
2012-03-27 23:13:14 +00:00
func TestMkdirAll(t *testing.T) {
tmpDir := TempDir()
path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
err := MkdirAll(path, 0777)
if err != nil {
t.Fatalf("MkdirAll %q: %s", path, err)
}
defer RemoveAll(tmpDir + "/_TestMkdirAll_")
// Already exists, should succeed.
err = MkdirAll(path, 0777)
if err != nil {
t.Fatalf("MkdirAll %q (second time): %s", path, err)
}
// Make file.
fpath := path + "/file"
f, err := Create(fpath)
if err != nil {
t.Fatalf("create %q: %s", fpath, err)
}
defer f.Close()
// Can't make directory named after file.
err = MkdirAll(fpath, 0777)
if err == nil {
t.Fatalf("MkdirAll %q: no error", fpath)
}
perr, ok := err.(*PathError)
if !ok {
t.Fatalf("MkdirAll %q returned %T, not *PathError", fpath, err)
}
if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
}
// Can't make subdirectory of file.
ffpath := fpath + "/subdir"
err = MkdirAll(ffpath, 0777)
if err == nil {
t.Fatalf("MkdirAll %q: no error", ffpath)
}
perr, ok = err.(*PathError)
if !ok {
t.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath, err)
}
if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
}
if runtime.GOOS == "windows" {
path := tmpDir + `\_TestMkdirAll_\dir\.\dir2\`
err := MkdirAll(path, 0777)
if err != nil {
t.Fatalf("MkdirAll %q: %s", path, err)
}
}
}
func TestMkdirAllWithSymlink(t *testing.T) {
testenv.MustHaveSymlink(t)
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
tmpDir, err := ioutil.TempDir("", "TestMkdirAllWithSymlink-")
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tmpDir)
2012-03-27 23:13:14 +00:00
dir := tmpDir + "/dir"
2014-09-21 17:33:12 +00:00
err = Mkdir(dir, 0755)
2012-03-27 23:13:14 +00:00
if err != nil {
t.Fatalf("Mkdir %s: %s", dir, err)
}
link := tmpDir + "/link"
err = Symlink("dir", link)
if err != nil {
t.Fatalf("Symlink %s: %s", link, err)
}
path := link + "/foo"
err = MkdirAll(path, 0755)
if err != nil {
t.Errorf("MkdirAll %q: %s", path, err)
}
}
func TestMkdirAllAtSlash(t *testing.T) {
2015-08-28 15:33:40 +00:00
switch runtime.GOOS {
case "android", "plan9", "windows":
t.Skipf("skipping on %s", runtime.GOOS)
2017-04-10 11:32:00 +00:00
case "darwin":
switch runtime.GOARCH {
case "arm", "arm64":
t.Skipf("skipping on darwin/%s, mkdir returns EPERM", runtime.GOARCH)
}
2012-03-27 23:13:14 +00:00
}
RemoveAll("/_go_os_test")
2017-04-10 11:32:00 +00:00
const dir = "/_go_os_test/dir"
err := MkdirAll(dir, 0777)
2012-03-27 23:13:14 +00:00
if err != nil {
pathErr, ok := err.(*PathError)
// common for users not to be able to write to /
2017-04-10 11:32:00 +00:00
if ok && (pathErr.Err == syscall.EACCES || isReadonlyError(pathErr.Err)) {
t.Skipf("could not create %v: %v", dir, err)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
t.Fatalf(`MkdirAll "/_go_os_test/dir": %v, %s`, err, pathErr.Err)
2012-03-27 23:13:14 +00:00
}
RemoveAll("/_go_os_test")
}