diff options
author | Ian Lance Taylor <iant@golang.org> | 2018-09-24 21:46:21 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2018-09-24 21:46:21 +0000 |
commit | dd931d9b48647e898dc80927c532ae93cc09e192 (patch) | |
tree | 71be2295cd79b8a182f6130611658db8628772d5 /libgo/go/path | |
parent | 779d8a5ad09b01428726ea5a0e6c87bd9ac3c0e4 (diff) | |
download | gcc-dd931d9b48647e898dc80927c532ae93cc09e192.zip gcc-dd931d9b48647e898dc80927c532ae93cc09e192.tar.gz gcc-dd931d9b48647e898dc80927c532ae93cc09e192.tar.bz2 |
libgo: update to Go 1.11
Reviewed-on: https://go-review.googlesource.com/136435
gotools/:
* Makefile.am (mostlyclean-local): Run chmod on check-go-dir to
make sure it is writable.
(check-go-tools): Likewise.
(check-vet): Copy internal/objabi to check-vet-dir.
* Makefile.in: Rebuild.
From-SVN: r264546
Diffstat (limited to 'libgo/go/path')
-rw-r--r-- | libgo/go/path/example_test.go | 16 | ||||
-rw-r--r-- | libgo/go/path/filepath/example_unix_test.go | 22 | ||||
-rw-r--r-- | libgo/go/path/filepath/example_unix_walk_test.go | 66 | ||||
-rw-r--r-- | libgo/go/path/filepath/match.go | 9 | ||||
-rw-r--r-- | libgo/go/path/filepath/match_test.go | 16 | ||||
-rw-r--r-- | libgo/go/path/filepath/path.go | 13 | ||||
-rw-r--r-- | libgo/go/path/filepath/path_test.go | 64 | ||||
-rw-r--r-- | libgo/go/path/filepath/path_unix.go | 2 | ||||
-rw-r--r-- | libgo/go/path/filepath/path_windows.go | 6 | ||||
-rw-r--r-- | libgo/go/path/match.go | 4 | ||||
-rw-r--r-- | libgo/go/path/match_test.go | 2 |
11 files changed, 175 insertions, 45 deletions
diff --git a/libgo/go/path/example_test.go b/libgo/go/path/example_test.go index 5cac36c9..77fbfa9 100644 --- a/libgo/go/path/example_test.go +++ b/libgo/go/path/example_test.go @@ -2,9 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package path +// +build gccgo_examples -/* Commented out until gccgo has example support. +package path import ( "fmt" @@ -93,6 +93,16 @@ func ExampleJoin() { // a } +func ExampleMatch() { + fmt.Println(path.Match("abc", "abc")) + fmt.Println(path.Match("a*", "abc")) + fmt.Println(path.Match("a*/b", "a/c/b")) + // Output: + // true <nil> + // true <nil> + // false <nil> +} + func ExampleSplit() { fmt.Println(path.Split("static/myfile.css")) fmt.Println(path.Split("myfile.css")) @@ -102,5 +112,3 @@ func ExampleSplit() { // myfile.css // } - -*/ diff --git a/libgo/go/path/filepath/example_unix_test.go b/libgo/go/path/filepath/example_unix_test.go index 40bc547..cd8233c 100644 --- a/libgo/go/path/filepath/example_unix_test.go +++ b/libgo/go/path/filepath/example_unix_test.go @@ -8,7 +8,6 @@ package filepath_test import ( "fmt" - "os" "path/filepath" ) @@ -80,24 +79,3 @@ func ExampleJoin() { // a/b/c // a/b/c } -func ExampleWalk() { - dir := "dir/to/walk" - subDirToSkip := "skip" // dir/to/walk/skip - - err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err) - return err - } - if info.IsDir() && info.Name() == subDirToSkip { - fmt.Printf("skipping a dir without errors: %+v \n", info.Name()) - return filepath.SkipDir - } - fmt.Printf("visited file: %q\n", path) - return nil - }) - - if err != nil { - fmt.Printf("error walking the path %q: %v\n", dir, err) - } -} diff --git a/libgo/go/path/filepath/example_unix_walk_test.go b/libgo/go/path/filepath/example_unix_walk_test.go new file mode 100644 index 0000000..fa8b8e3 --- /dev/null +++ b/libgo/go/path/filepath/example_unix_walk_test.go @@ -0,0 +1,66 @@ +// Copyright 2018 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. + +// +build !windows,!plan9 + +package filepath_test + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" +) + +func prepareTestDirTree(tree string) (string, error) { + tmpDir, err := ioutil.TempDir("", "") + if err != nil { + return "", fmt.Errorf("error creating temp directory: %v\n", err) + } + + err = os.MkdirAll(filepath.Join(tmpDir, tree), 0755) + if err != nil { + os.RemoveAll(tmpDir) + return "", err + } + + return tmpDir, nil +} + +func ExampleWalk() { + tmpDir, err := prepareTestDirTree("dir/to/walk/skip") + if err != nil { + fmt.Printf("unable to create test dir tree: %v\n", err) + return + } + defer os.RemoveAll(tmpDir) + os.Chdir(tmpDir) + + subDirToSkip := "skip" + + fmt.Println("On Unix:") + err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error { + if err != nil { + fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err) + return err + } + if info.IsDir() && info.Name() == subDirToSkip { + fmt.Printf("skipping a dir without errors: %+v \n", info.Name()) + return filepath.SkipDir + } + fmt.Printf("visited file or dir: %q\n", path) + return nil + }) + if err != nil { + fmt.Printf("error walking the path %q: %v\n", tmpDir, err) + return + } + // Output: + // On Unix: + // visited file or dir: "." + // visited file or dir: "dir" + // visited file or dir: "dir/to" + // visited file or dir: "dir/to/walk" + // skipping a dir without errors: skip +} diff --git a/libgo/go/path/filepath/match.go b/libgo/go/path/filepath/match.go index 5168e03..46badb5 100644 --- a/libgo/go/path/filepath/match.go +++ b/libgo/go/path/filepath/match.go @@ -13,7 +13,7 @@ import ( "unicode/utf8" ) -// ErrBadPattern indicates a globbing pattern was malformed. +// ErrBadPattern indicates a pattern was malformed. var ErrBadPattern = errors.New("syntax error in pattern") // Match reports whether name matches the shell file name pattern. @@ -339,6 +339,9 @@ func glob(dir, pattern string, matches []string) (m []string, e error) { // hasMeta reports whether path contains any of the magic characters // recognized by Match. func hasMeta(path string) bool { - // TODO(niemeyer): Should other magic characters be added here? - return strings.ContainsAny(path, "*?[") + magicChars := `*?[` + if runtime.GOOS != "windows" { + magicChars = `*?[\` + } + return strings.ContainsAny(path, magicChars) } diff --git a/libgo/go/path/filepath/match_test.go b/libgo/go/path/filepath/match_test.go index 12d922f..b73d6d2 100644 --- a/libgo/go/path/filepath/match_test.go +++ b/libgo/go/path/filepath/match_test.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "os" . "path/filepath" + "reflect" "runtime" "sort" "strings" @@ -372,3 +373,18 @@ func TestWindowsGlob(t *testing.T) { } } } + +func TestNonWindowsGlobEscape(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skipf("skipping non-windows specific test") + } + pattern := `\match.go` + want := []string{"match.go"} + matches, err := Glob(pattern) + if err != nil { + t.Fatalf("Glob error for %q: %s", pattern, err) + } + if !reflect.DeepEqual(matches, want) { + t.Fatalf("Glob(%#q) = %v want %v", pattern, matches, want) + } +} diff --git a/libgo/go/path/filepath/path.go b/libgo/go/path/filepath/path.go index 87f8faf..1508137 100644 --- a/libgo/go/path/filepath/path.go +++ b/libgo/go/path/filepath/path.go @@ -341,12 +341,13 @@ var SkipDir = errors.New("skip this directory") // // If there was a problem walking to the file or directory named by path, the // incoming error will describe the problem and the function can decide how -// to handle that error (and Walk will not descend into that directory). If -// an error is returned, processing stops. The sole exception is when the function -// returns the special value SkipDir. If the function returns SkipDir when invoked -// on a directory, Walk skips the directory's contents entirely. -// If the function returns SkipDir when invoked on a non-directory file, -// Walk skips the remaining files in the containing directory. +// to handle that error (and Walk will not descend into that directory). In the +// case of an error, the info argument will be nil. If an error is returned, +// processing stops. The sole exception is when the function returns the special +// value SkipDir. If the function returns SkipDir when invoked on a directory, +// Walk skips the directory's contents entirely. If the function returns SkipDir +// when invoked on a non-directory file, Walk skips the remaining files in the +// containing directory. type WalkFunc func(path string, info os.FileInfo, err error) error var lstat = os.Lstat // for testing diff --git a/libgo/go/path/filepath/path_test.go b/libgo/go/path/filepath/path_test.go index ccffdba..5983a94 100644 --- a/libgo/go/path/filepath/path_test.go +++ b/libgo/go/path/filepath/path_test.go @@ -436,6 +436,22 @@ func TestWalk(t *testing.T) { defer restore() } } + + tmpDir, err := ioutil.TempDir("", "TestWalk") + if err != nil { + t.Fatal("creating temp dir:", err) + } + defer os.RemoveAll(tmpDir) + + origDir, err := os.Getwd() + if err != nil { + t.Fatal("finding working dir:", err) + } + if err = os.Chdir(tmpDir); err != nil { + t.Fatal("entering temp dir:", err) + } + defer os.Chdir(origDir) + makeTree(t) errors := make([]error, 0, 10) clear := true @@ -443,7 +459,7 @@ func TestWalk(t *testing.T) { return mark(info, err, &errors, clear) } // Expect no errors. - err := filepath.Walk(tree.name, markFn) + err = filepath.Walk(tree.name, markFn) if err != nil { t.Fatalf("no error expected, found: %s", err) } @@ -502,11 +518,6 @@ func TestWalk(t *testing.T) { os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0770) os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0770) } - - // cleanup - if err := os.RemoveAll(tree.name); err != nil { - t.Errorf("removeTree: %v", err) - } } func touch(t *testing.T, name string) { @@ -1061,6 +1072,47 @@ func TestAbs(t *testing.T) { } } +// Empty path needs to be special-cased on Windows. See golang.org/issue/24441. +// We test it separately from all other absTests because the empty string is not +// a valid path, so it can't be used with os.Stat. +func TestAbsEmptyString(t *testing.T) { + root, err := ioutil.TempDir("", "TestAbsEmptyString") + if err != nil { + t.Fatal("TempDir failed: ", err) + } + defer os.RemoveAll(root) + + wd, err := os.Getwd() + if err != nil { + t.Fatal("getwd failed: ", err) + } + err = os.Chdir(root) + if err != nil { + t.Fatal("chdir failed: ", err) + } + defer os.Chdir(wd) + + info, err := os.Stat(root) + if err != nil { + t.Fatalf("%s: %s", root, err) + } + + abspath, err := filepath.Abs("") + if err != nil { + t.Fatalf(`Abs("") error: %v`, err) + } + absinfo, err := os.Stat(abspath) + if err != nil || !os.SameFile(absinfo, info) { + t.Errorf(`Abs("")=%q, not the same file`, abspath) + } + if !filepath.IsAbs(abspath) { + t.Errorf(`Abs("")=%q, not an absolute path`, abspath) + } + if filepath.IsAbs(abspath) && abspath != filepath.Clean(abspath) { + t.Errorf(`Abs("")=%q, isn't clean`, abspath) + } +} + type RelTests struct { root, path, want string } diff --git a/libgo/go/path/filepath/path_unix.go b/libgo/go/path/filepath/path_unix.go index 2d407a8..c10b328 100644 --- a/libgo/go/path/filepath/path_unix.go +++ b/libgo/go/path/filepath/path_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build aix darwin dragonfly freebsd linux nacl netbsd openbsd solaris +// +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris package filepath diff --git a/libgo/go/path/filepath/path_windows.go b/libgo/go/path/filepath/path_windows.go index 0354255..409e8d6 100644 --- a/libgo/go/path/filepath/path_windows.go +++ b/libgo/go/path/filepath/path_windows.go @@ -107,6 +107,12 @@ func splitList(path string) []string { } func abs(path string) (string, error) { + if path == "" { + // syscall.FullPath returns an error on empty path, because it's not a valid path. + // To implement Abs behavior of returning working directory on empty string input, + // special-case empty path by changing it to "." path. See golang.org/issue/24441. + path = "." + } fullPath, err := syscall.FullPath(path) if err != nil { return "", err diff --git a/libgo/go/path/match.go b/libgo/go/path/match.go index 8d9aa51..d39d244 100644 --- a/libgo/go/path/match.go +++ b/libgo/go/path/match.go @@ -10,10 +10,10 @@ import ( "unicode/utf8" ) -// ErrBadPattern indicates a globbing pattern was malformed. +// ErrBadPattern indicates a pattern was malformed. var ErrBadPattern = errors.New("syntax error in pattern") -// Match reports whether name matches the shell file name pattern. +// Match reports whether name matches the shell pattern. // The pattern syntax is: // // pattern: diff --git a/libgo/go/path/match_test.go b/libgo/go/path/match_test.go index 6b0676f..127180e 100644 --- a/libgo/go/path/match_test.go +++ b/libgo/go/path/match_test.go @@ -73,7 +73,7 @@ func TestMatch(t *testing.T) { for _, tt := range matchTests { ok, err := Match(tt.pattern, tt.s) if ok != tt.match || err != tt.err { - t.Errorf("Match(%#q, %#q) = %v, %v want %v, nil", tt.pattern, tt.s, ok, err, tt.match) + t.Errorf("Match(%#q, %#q) = %v, %v want %v, %v", tt.pattern, tt.s, ok, err, tt.match, tt.err) } } } |