Split out the puthe path/filepath functions into common/paths

So they can be used from the config package without cyclic troubles.

Updates #8654
This commit is contained in:
Bjørn Erik Pedersen 2021-06-18 10:27:27 +02:00
parent 5af045ebab
commit 93aad3c543
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
21 changed files with 956 additions and 591 deletions

View file

@ -17,7 +17,6 @@ import (
"strings"
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/langs"
)
@ -93,9 +92,8 @@ func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
{"/" + lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
}
for _, test := range newTests {
tests = append(tests, test)
}
tests = append(tests, newTests...)
}
for _, test := range tests {
@ -121,24 +119,6 @@ func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
}
}
func TestIsAbsURL(t *testing.T) {
c := qt.New(t)
for _, this := range []struct {
a string
b bool
}{
{"http://gohugo.io", true},
{"https://gohugo.io", true},
{"//gohugo.io", true},
{"http//gohugo.io", false},
{"/content", false},
{"content", false},
} {
c.Assert(IsAbsURL(this.a) == this.b, qt.Equals, true)
}
}
func TestRelURL(t *testing.T) {
for _, defaultInSubDir := range []bool{true, false} {
for _, addLanguage := range []bool{true, false} {
@ -187,10 +167,7 @@ func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
{lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
{"/" + lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
}
for _, test := range newTests {
tests = append(tests, test)
}
tests = append(tests, newTests...)
}
for i, test := range tests {
@ -247,28 +224,6 @@ func TestSanitizeURL(t *testing.T) {
}
}
func TestMakePermalink(t *testing.T) {
type test struct {
host, link, output string
}
data := []test{
{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
{"http://abc.com", "post/bar", "http://abc.com/post/bar"},
{"http://abc.com", "bar", "http://abc.com/bar"},
{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
}
for i, d := range data {
output := MakePermalink(d.host, d.link).String()
if d.output != output {
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
}
}
}
func TestURLPrep(t *testing.T) {
type test struct {
ugly bool
@ -293,60 +248,3 @@ func TestURLPrep(t *testing.T) {
}
}
}
func TestAddContextRoot(t *testing.T) {
tests := []struct {
baseURL string
url string
expected string
}{
{"http://example.com/sub/", "/foo", "/sub/foo"},
{"http://example.com/sub/", "/foo/index.html", "/sub/foo/index.html"},
{"http://example.com/sub1/sub2", "/foo", "/sub1/sub2/foo"},
{"http://example.com", "/foo", "/foo"},
// cannot guess that the context root is already added int the example below
{"http://example.com/sub/", "/sub/foo", "/sub/sub/foo"},
{"http://example.com/тря", "/трям/", "/тря/трям/"},
{"http://example.com", "/", "/"},
{"http://example.com/bar", "//", "/bar/"},
}
for _, test := range tests {
output := AddContextRoot(test.baseURL, test.url)
if output != test.expected {
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
}
}
}
func TestPretty(t *testing.T) {
c := qt.New(t)
c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name.html"))
c.Assert("/section/sub/name/index.html", qt.Equals, PrettifyURLPath("/section/sub/name.html"))
c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/"))
c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/index.html"))
c.Assert("/index.html", qt.Equals, PrettifyURLPath("/index.html"))
c.Assert("/name/index.xml", qt.Equals, PrettifyURLPath("/name.xml"))
c.Assert("/", qt.Equals, PrettifyURLPath("/"))
c.Assert("/", qt.Equals, PrettifyURLPath(""))
c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name.html"))
c.Assert("/section/sub/name", qt.Equals, PrettifyURL("/section/sub/name.html"))
c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/"))
c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/index.html"))
c.Assert("/", qt.Equals, PrettifyURL("/index.html"))
c.Assert("/name/index.xml", qt.Equals, PrettifyURL("/name.xml"))
c.Assert("/", qt.Equals, PrettifyURL("/"))
c.Assert("/", qt.Equals, PrettifyURL(""))
}
func TestUgly(t *testing.T) {
c := qt.New(t)
c.Assert("/section/name.html", qt.Equals, Uglify("/section/name.html"))
c.Assert("/section/sub/name.html", qt.Equals, Uglify("/section/sub/name.html"))
c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/"))
c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/index.html"))
c.Assert("/index.html", qt.Equals, Uglify("/index.html"))
c.Assert("/name.xml", qt.Equals, Uglify("/name.xml"))
c.Assert("/", qt.Equals, Uglify("/"))
c.Assert("/", qt.Equals, Uglify(""))
}