Commit acc1f472 authored by Alex Brainman's avatar Alex Brainman

path/filepath: add test to walk symlink

For #17540.

Change-Id: Ie01f39797526934fa553f4279cbde6c7cbf14154
Reviewed-on: https://go-review.googlesource.com/36854Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 4c162208
...@@ -6,12 +6,14 @@ package filepath_test ...@@ -6,12 +6,14 @@ package filepath_test
import ( import (
"errors" "errors"
"fmt"
"internal/testenv" "internal/testenv"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"sort"
"strings" "strings"
"testing" "testing"
) )
...@@ -1327,3 +1329,56 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486 ...@@ -1327,3 +1329,56 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
t.Fatalf("%q not seen", ken) t.Fatalf("%q not seen", ken)
} }
} }
func testWalkSymlink(t *testing.T, mklink func(target, link string) error) {
tmpdir, err := ioutil.TempDir("", "testWalkSymlink")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
err = os.Chdir(tmpdir)
if err != nil {
t.Fatal(err)
}
err = mklink(tmpdir, "link")
if err != nil {
t.Fatal(err)
}
var visited []string
err = filepath.Walk(tmpdir, func(path string, info os.FileInfo, err error) error {
if err != nil {
t.Fatal(err)
}
rel, err := filepath.Rel(tmpdir, path)
if err != nil {
t.Fatal(err)
}
visited = append(visited, rel)
return nil
})
if err != nil {
t.Fatal(err)
}
sort.Strings(visited)
want := []string{".", "link"}
if fmt.Sprintf("%q", visited) != fmt.Sprintf("%q", want) {
t.Errorf("unexpected paths visited %q, want %q", visited, want)
}
}
func TestWalkSymlink(t *testing.T) {
testenv.MustHaveSymlink(t)
if runtime.GOOS == "windows" {
t.Skip("skipping broken test: see issue 17540")
}
testWalkSymlink(t, os.Symlink)
}
...@@ -7,6 +7,7 @@ package filepath_test ...@@ -7,6 +7,7 @@ package filepath_test
import ( import (
"flag" "flag"
"fmt" "fmt"
"internal/testenv"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
...@@ -434,44 +435,28 @@ func TestUNC(t *testing.T) { ...@@ -434,44 +435,28 @@ func TestUNC(t *testing.T) {
filepath.Glob(`\\?\c:\*`) filepath.Glob(`\\?\c:\*`)
} }
func TestWalkDirectoryJunction(t *testing.T) { func testWalkMklink(t *testing.T, linktype string) {
t.Skip("skipping broken test: see issue 10424")
output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output() output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output()
if !strings.Contains(string(output), " /J ") { if !strings.Contains(string(output), fmt.Sprintf(" /%s ", linktype)) {
t.Skip(`skipping test; mklink does not supports directory junctions`) t.Skipf(`skipping test; mklink does not supports /%s parameter`, linktype)
}
tmpdir, err := ioutil.TempDir("", "TestWalkDirectoryJunction")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
err = os.Chdir(tmpdir)
if err != nil {
t.Fatal(err)
} }
testWalkSymlink(t, func(target, link string) error {
output, err = exec.Command("cmd", "/c", "mklink", "/J", "link", tmpdir).CombinedOutput() output, err := exec.Command("cmd", "/c", "mklink", "/"+linktype, link, target).CombinedOutput()
if err != nil {
t.Errorf(`"mklink link %v" command failed: %v\n%v`, tmpdir, err, string(output))
}
walkfunc := func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
t.Log(err) return fmt.Errorf(`"mklink /%s %v %v" command failed: %v\n%v`, linktype, link, target, err, string(output))
} }
return nil return nil
} })
err = filepath.Walk(tmpdir, walkfunc) }
if err != nil {
t.Fatal(err) func TestWalkDirectoryJunction(t *testing.T) {
} testenv.MustHaveSymlink(t)
t.Skip("skipping broken test: see issue 10424")
testWalkMklink(t, "J")
}
func TestWalkDirectorySymlink(t *testing.T) {
testenv.MustHaveSymlink(t)
t.Skip("skipping broken test: see issue 17540")
testWalkMklink(t, "D")
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment