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
import (
"errors"
"fmt"
"internal/testenv"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"testing"
)
......@@ -1327,3 +1329,56 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
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
import (
"flag"
"fmt"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
......@@ -434,44 +435,28 @@ func TestUNC(t *testing.T) {
filepath.Glob(`\\?\c:\*`)
}
func TestWalkDirectoryJunction(t *testing.T) {
t.Skip("skipping broken test: see issue 10424")
func testWalkMklink(t *testing.T, linktype string) {
output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output()
if !strings.Contains(string(output), " /J ") {
t.Skip(`skipping test; mklink does not supports directory junctions`)
}
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)
if !strings.Contains(string(output), fmt.Sprintf(" /%s ", linktype)) {
t.Skipf(`skipping test; mklink does not supports /%s parameter`, linktype)
}
output, err = exec.Command("cmd", "/c", "mklink", "/J", "link", tmpdir).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 {
testWalkSymlink(t, func(target, link string) error {
output, err := exec.Command("cmd", "/c", "mklink", "/"+linktype, link, target).CombinedOutput()
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
}
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