aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/path/filepath/path_test.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2013-12-17 12:19:01 -0800
committerBrad Fitzpatrick <bradfitz@golang.org>2013-12-17 12:19:01 -0800
commit6a1a2170bcd1fbbe7210d90939a485dadf5075fb (patch)
treea92d89a7c45168cc10a056ec361833dca6e9d854 /src/pkg/path/filepath/path_test.go
parent762a9d934eab267418595df7a220eec50919b77d (diff)
downloadgo-6a1a2170bcd1fbbe7210d90939a485dadf5075fb.tar.xz
os, path/filepath: don't ignore Lstat errors in Readdir
os: don't ignore LStat errors in Readdir. If it's ENOENT, on the second pass, just treat it as missing. If it's another error, it's real. path/filepath: use ReaddirNames instead of Readdir in Walk, in order to obey the documented WalkFunc contract of returning each walked item's LStat error, if any. Fixes #6656 Fixes #6680 R=golang-dev, r CC=golang-dev https://golang.org/cl/43530043
Diffstat (limited to 'src/pkg/path/filepath/path_test.go')
-rw-r--r--src/pkg/path/filepath/path_test.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go
index d32b70d6e2..1adc8cb072 100644
--- a/src/pkg/path/filepath/path_test.go
+++ b/src/pkg/path/filepath/path_test.go
@@ -5,6 +5,7 @@
package filepath_test
import (
+ "errors"
"io/ioutil"
"os"
"path/filepath"
@@ -458,6 +459,63 @@ func TestWalk(t *testing.T) {
}
}
+func touch(t *testing.T, name string) {
+ f, err := os.Create(name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := f.Close(); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestWalkFileError(t *testing.T) {
+ td, err := ioutil.TempDir("", "walktest")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(td)
+
+ touch(t, filepath.Join(td, "foo"))
+ touch(t, filepath.Join(td, "bar"))
+ dir := filepath.Join(td, "dir")
+ if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ touch(t, filepath.Join(dir, "baz"))
+ touch(t, filepath.Join(dir, "stat-error"))
+ defer func() {
+ *filepath.LstatP = os.Lstat
+ }()
+ statErr := errors.New("some stat error")
+ *filepath.LstatP = func(path string) (os.FileInfo, error) {
+ if strings.HasSuffix(path, "stat-error") {
+ return nil, statErr
+ }
+ return os.Lstat(path)
+ }
+ got := map[string]error{}
+ err = filepath.Walk(td, func(path string, fi os.FileInfo, err error) error {
+ rel, _ := filepath.Rel(td, path)
+ got[filepath.ToSlash(rel)] = err
+ return nil
+ })
+ if err != nil {
+ t.Errorf("Walk error: %v", err)
+ }
+ want := map[string]error{
+ ".": nil,
+ "foo": nil,
+ "bar": nil,
+ "dir": nil,
+ "dir/baz": nil,
+ "dir/stat-error": statErr,
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("Walked %#v; want %#v", got, want)
+ }
+}
+
var basetests = []PathTest{
{"", "."},
{".", "."},