mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
@@ -14,19 +14,17 @@
|
||||
package page
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/media"
|
||||
|
||||
"fmt"
|
||||
|
||||
"github.com/gohugoio/hugo/output"
|
||||
)
|
||||
|
||||
func TestPageTargetPath(t *testing.T) {
|
||||
|
||||
pathSpec := newTestPathSpec()
|
||||
|
||||
noExtNoDelimMediaType := media.TextType
|
||||
@@ -57,24 +55,29 @@ func TestPageTargetPath(t *testing.T) {
|
||||
Kind: KindSection,
|
||||
Sections: []string{"sect1"},
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/sect1/index.html", SubResourceBaseTarget: "/sect1", Link: "/sect1/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/sect1/index.html", SubResourceBaseTarget: "/sect1", Link: "/sect1/"}},
|
||||
{"HTML taxonomy term", TargetPathDescriptor{
|
||||
Kind: KindTerm,
|
||||
Sections: []string{"tags", "hugo"},
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/tags/hugo/index.html", SubResourceBaseTarget: "/tags/hugo", Link: "/tags/hugo/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/tags/hugo/index.html", SubResourceBaseTarget: "/tags/hugo", Link: "/tags/hugo/"}},
|
||||
{"HTML taxonomy", TargetPathDescriptor{
|
||||
Kind: KindTaxonomy,
|
||||
Sections: []string{"tags"},
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/tags/index.html", SubResourceBaseTarget: "/tags", Link: "/tags/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/tags/index.html", SubResourceBaseTarget: "/tags", Link: "/tags/"}},
|
||||
{
|
||||
"HTML page", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "mypage",
|
||||
Sections: []string{"a"},
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/a/b/mypage/index.html", SubResourceBaseTarget: "/a/b/mypage", Link: "/a/b/mypage/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/mypage/index.html", SubResourceBaseTarget: "/a/b/mypage", Link: "/a/b/mypage/"},
|
||||
},
|
||||
|
||||
{
|
||||
"HTML page with index as base", TargetPathDescriptor{
|
||||
@@ -82,93 +85,117 @@ func TestPageTargetPath(t *testing.T) {
|
||||
Dir: "/a/b",
|
||||
BaseName: "index",
|
||||
Sections: []string{"a"},
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/a/b/index.html", SubResourceBaseTarget: "/a/b", Link: "/a/b/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/index.html", SubResourceBaseTarget: "/a/b", Link: "/a/b/"},
|
||||
},
|
||||
|
||||
{
|
||||
"HTML page with special chars", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "My Page!",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/a/b/my-page/index.html", SubResourceBaseTarget: "/a/b/my-page", Link: "/a/b/my-page/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/my-page/index.html", SubResourceBaseTarget: "/a/b/my-page", Link: "/a/b/my-page/"},
|
||||
},
|
||||
{"RSS home", TargetPathDescriptor{Kind: "rss", Type: output.RSSFormat}, TargetPaths{TargetFilename: "/index.xml", SubResourceBaseTarget: "", Link: "/index.xml"}},
|
||||
{"RSS section list", TargetPathDescriptor{
|
||||
Kind: "rss",
|
||||
Sections: []string{"sect1"},
|
||||
Type: output.RSSFormat}, TargetPaths{TargetFilename: "/sect1/index.xml", SubResourceBaseTarget: "/sect1", Link: "/sect1/index.xml"}},
|
||||
Type: output.RSSFormat,
|
||||
}, TargetPaths{TargetFilename: "/sect1/index.xml", SubResourceBaseTarget: "/sect1", Link: "/sect1/index.xml"}},
|
||||
{
|
||||
"AMP page", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/a/b/c",
|
||||
BaseName: "myamp",
|
||||
Type: output.AMPFormat}, TargetPaths{TargetFilename: "/amp/a/b/c/myamp/index.html", SubResourceBaseTarget: "/amp/a/b/c/myamp", Link: "/amp/a/b/c/myamp/"}},
|
||||
Type: output.AMPFormat,
|
||||
}, TargetPaths{TargetFilename: "/amp/a/b/c/myamp/index.html", SubResourceBaseTarget: "/amp/a/b/c/myamp", Link: "/amp/a/b/c/myamp/"},
|
||||
},
|
||||
{
|
||||
"AMP page with URL with suffix", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/url.xhtml",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/some/other/url.xhtml", SubResourceBaseTarget: "/some/other", Link: "/some/other/url.xhtml"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/url.xhtml", SubResourceBaseTarget: "/some/other", Link: "/some/other/url.xhtml"},
|
||||
},
|
||||
{
|
||||
"JSON page with URL without suffix", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/path/",
|
||||
Type: output.JSONFormat}, TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"}},
|
||||
Type: output.JSONFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
||||
},
|
||||
{
|
||||
"JSON page with URL without suffix and no trailing slash", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/path",
|
||||
Type: output.JSONFormat}, TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"}},
|
||||
Type: output.JSONFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
||||
},
|
||||
{
|
||||
"HTML page with URL without suffix and no trailing slash", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/path",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/some/other/path/index.html", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/path/index.html", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/"},
|
||||
},
|
||||
{
|
||||
"HTML page with URL containing double hyphen", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other--url/",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/some/other--url/index.html", SubResourceBaseTarget: "/some/other--url", Link: "/some/other--url/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other--url/index.html", SubResourceBaseTarget: "/some/other--url", Link: "/some/other--url/"},
|
||||
},
|
||||
{
|
||||
"HTML page with expanded permalink", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "mypage",
|
||||
ExpandedPermalink: "/2017/10/my-title/",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/2017/10/my-title/index.html", SubResourceBaseTarget: "/2017/10/my-title", Link: "/2017/10/my-title/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/2017/10/my-title/index.html", SubResourceBaseTarget: "/2017/10/my-title", Link: "/2017/10/my-title/"},
|
||||
},
|
||||
{
|
||||
"Paginated HTML home", TargetPathDescriptor{
|
||||
Kind: KindHome,
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat,
|
||||
Addends: "page/3"}, TargetPaths{TargetFilename: "/page/3/index.html", SubResourceBaseTarget: "/page/3", Link: "/page/3/"}},
|
||||
Addends: "page/3",
|
||||
}, TargetPaths{TargetFilename: "/page/3/index.html", SubResourceBaseTarget: "/page/3", Link: "/page/3/"},
|
||||
},
|
||||
{
|
||||
"Paginated Taxonomy terms list", TargetPathDescriptor{
|
||||
Kind: KindTerm,
|
||||
BaseName: "_index",
|
||||
Sections: []string{"tags", "hugo"},
|
||||
Type: output.HTMLFormat,
|
||||
Addends: "page/3"}, TargetPaths{TargetFilename: "/tags/hugo/page/3/index.html", SubResourceBaseTarget: "/tags/hugo/page/3", Link: "/tags/hugo/page/3/"}},
|
||||
Addends: "page/3",
|
||||
}, TargetPaths{TargetFilename: "/tags/hugo/page/3/index.html", SubResourceBaseTarget: "/tags/hugo/page/3", Link: "/tags/hugo/page/3/"},
|
||||
},
|
||||
{
|
||||
"Regular page with addend", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "mypage",
|
||||
Addends: "c/d/e",
|
||||
Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/a/b/mypage/c/d/e/index.html", SubResourceBaseTarget: "/a/b/mypage/c/d/e", Link: "/a/b/mypage/c/d/e/"}},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/mypage/c/d/e/index.html", SubResourceBaseTarget: "/a/b/mypage/c/d/e", Link: "/a/b/mypage/c/d/e/"},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
t.Run(fmt.Sprintf("langPrefixPath=%s,langPrefixLink=%s,uglyURLs=%t,name=%s", langPrefixPath, langPrefixLink, uglyURLs, test.name),
|
||||
func(t *testing.T) {
|
||||
|
||||
test.d.ForcePrefix = true
|
||||
test.d.PathSpec = pathSpec
|
||||
test.d.UglyURLs = uglyURLs
|
||||
@@ -206,12 +233,10 @@ func TestPageTargetPath(t *testing.T) {
|
||||
|
||||
if !eqTargetPaths(pagePath, expected) {
|
||||
t.Fatalf("[%d] [%s] targetPath expected\n%#v, got:\n%#v", i, test.name, expected, pagePath)
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,10 +248,16 @@ func TestPageTargetPathPrefix(t *testing.T) {
|
||||
d TargetPathDescriptor
|
||||
expected TargetPaths
|
||||
}{
|
||||
{"URL set, prefix both, no force", TargetPathDescriptor{Kind: KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
TargetPaths{TargetFilename: "/mydir/my.json", SubResourceBaseTarget: "/mydir", SubResourceBaseLink: "/mydir", Link: "/mydir/my.json"}},
|
||||
{"URL set, prefix both, force", TargetPathDescriptor{Kind: KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
TargetPaths{TargetFilename: "/pf/mydir/my.json", SubResourceBaseTarget: "/pf/mydir", SubResourceBaseLink: "/pl/mydir", Link: "/pl/mydir/my.json"}},
|
||||
{
|
||||
"URL set, prefix both, no force",
|
||||
TargetPathDescriptor{Kind: KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
TargetPaths{TargetFilename: "/mydir/my.json", SubResourceBaseTarget: "/mydir", SubResourceBaseLink: "/mydir", Link: "/mydir/my.json"},
|
||||
},
|
||||
{
|
||||
"URL set, prefix both, force",
|
||||
TargetPathDescriptor{Kind: KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
TargetPaths{TargetFilename: "/pf/mydir/my.json", SubResourceBaseTarget: "/pf/mydir", SubResourceBaseLink: "/pl/mydir", Link: "/pl/mydir/my.json"},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
@@ -244,11 +275,9 @@ func TestPageTargetPathPrefix(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func eqTargetPaths(p1, p2 TargetPaths) bool {
|
||||
|
||||
if p1.Link != p2.Link {
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user