]> Cypherpunks repositories - gostls13.git/commitdiff
path/filepath: make Walk example runnable in the playground
authorMostyn Bramley-Moore <mostyn@antipode.se>
Wed, 4 Jul 2018 21:32:59 +0000 (23:32 +0200)
committerIan Lance Taylor <iant@golang.org>
Tue, 17 Jul 2018 21:26:47 +0000 (21:26 +0000)
Relates to #9679

Change-Id: I68951f664d2a03812dae309c580c181869d8af21
Reviewed-on: https://go-review.googlesource.com/122237
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/path/filepath/example_unix_test.go
src/path/filepath/example_unix_walk_test.go [new file with mode: 0644]

index fe49c12d8d3cbf7d16101743b2e006e8fa17ae84..cd8233ceb6a60e2d3f0d55199d0dcb60d7912176 100644 (file)
@@ -8,7 +8,6 @@ package filepath_test
 
 import (
        "fmt"
-       "os"
        "path/filepath"
 )
 
@@ -80,25 +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", 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: %q\n", path)
-               return nil
-       })
-
-       if err != nil {
-               fmt.Printf("error walking the path %q: %v\n", dir, err)
-       }
-}
diff --git a/src/path/filepath/example_unix_walk_test.go b/src/path/filepath/example_unix_walk_test.go
new file mode 100644 (file)
index 0000000..fa8b8e3
--- /dev/null
@@ -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
+}