2013-12-11 19:31:02 -05:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
func abs(t *testing.T, p string) string {
|
2013-12-11 19:31:02 -05:00
|
|
|
o, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
2013-12-11 20:12:53 -05:00
|
|
|
t.Fatal(err)
|
2013-12-11 19:31:02 -05:00
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkNormal(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/d/c/data"
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/b/c/data"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkRandomString(t *testing.T) {
|
2013-12-11 20:12:53 -05:00
|
|
|
if _, err := FollowSymlinkInScope("toto", "testdata"); err == nil {
|
|
|
|
t.Fatal("Random string should fail but didn't")
|
2013-12-11 19:31:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkLastLink(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/d"
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/b"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkRelativeLink(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/e/c/data"
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/fs/b/c/data"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkRelativeLinkScope(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/f"
|
2013-12-11 20:12:53 -05:00
|
|
|
|
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/test"); expected != rewrite {
|
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
|
|
|
|
link = "testdata/fs/b/h"
|
2013-12-11 19:31:02 -05:00
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err = FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/root"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|