2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
package page_test
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
import (
|
2020-12-02 07:23:25 -05:00
|
|
|
"fmt"
|
2019-01-02 06:33:26 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/media"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/output"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPageTargetPath(t *testing.T) {
|
|
|
|
pathSpec := newTestPathSpec()
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
noExtNoDelimMediaType := media.WithDelimiterAndSuffixes(media.Builtin.TextType, "", "")
|
2019-01-02 06:33:26 -05:00
|
|
|
noExtNoDelimMediaType.Delimiter = ""
|
|
|
|
|
|
|
|
// Netlify style _redirects
|
|
|
|
noExtDelimFormat := output.Format{
|
|
|
|
Name: "NER",
|
|
|
|
MediaType: noExtNoDelimMediaType,
|
|
|
|
BaseName: "_redirects",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, langPrefixPath := range []string{"", "no"} {
|
|
|
|
for _, langPrefixLink := range []string{"", "no"} {
|
|
|
|
for _, uglyURLs := range []bool{false, true} {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-01-04 12:24:36 -05:00
|
|
|
d page.TargetPathDescriptor
|
|
|
|
expected page.TargetPaths
|
2019-01-02 06:33:26 -05:00
|
|
|
}{
|
2023-01-04 12:24:36 -05:00
|
|
|
{"JSON home", page.TargetPathDescriptor{Kind: page.KindHome, Type: output.JSONFormat}, page.TargetPaths{TargetFilename: "/index.json", SubResourceBaseTarget: "", Link: "/index.json"}},
|
|
|
|
{"AMP home", page.TargetPathDescriptor{Kind: page.KindHome, Type: output.AMPFormat}, page.TargetPaths{TargetFilename: "/amp/index.html", SubResourceBaseTarget: "/amp", Link: "/amp/"}},
|
|
|
|
{"HTML home", page.TargetPathDescriptor{Kind: page.KindHome, BaseName: "_index", Type: output.HTMLFormat}, page.TargetPaths{TargetFilename: "/index.html", SubResourceBaseTarget: "", Link: "/"}},
|
|
|
|
{"Netlify redirects", page.TargetPathDescriptor{Kind: page.KindHome, BaseName: "_index", Type: noExtDelimFormat}, page.TargetPaths{TargetFilename: "/_redirects", SubResourceBaseTarget: "", Link: "/_redirects"}},
|
|
|
|
{"HTML section list", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindSection,
|
2019-01-02 06:33:26 -05:00
|
|
|
Sections: []string{"sect1"},
|
|
|
|
BaseName: "_index",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/sect1/index.html", SubResourceBaseTarget: "/sect1", Link: "/sect1/"}},
|
|
|
|
{"HTML taxonomy term", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindTerm,
|
2019-01-02 06:33:26 -05:00
|
|
|
Sections: []string{"tags", "hugo"},
|
|
|
|
BaseName: "_index",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/tags/hugo/index.html", SubResourceBaseTarget: "/tags/hugo", Link: "/tags/hugo/"}},
|
|
|
|
{"HTML taxonomy", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindTaxonomy,
|
2019-01-02 06:33:26 -05:00
|
|
|
Sections: []string{"tags"},
|
|
|
|
BaseName: "_index",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/tags/index.html", SubResourceBaseTarget: "/tags", Link: "/tags/"}},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"HTML page", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/a/b",
|
|
|
|
BaseName: "mypage",
|
|
|
|
Sections: []string{"a"},
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/a/b/mypage/index.html", SubResourceBaseTarget: "/a/b/mypage", Link: "/a/b/mypage/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"HTML page with index as base", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/a/b",
|
|
|
|
BaseName: "index",
|
|
|
|
Sections: []string{"a"},
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/a/b/index.html", SubResourceBaseTarget: "/a/b", Link: "/a/b/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"HTML page with special chars", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/a/b",
|
|
|
|
BaseName: "My Page!",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/a/b/my-page/index.html", SubResourceBaseTarget: "/a/b/my-page", Link: "/a/b/my-page/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2023-01-04 12:24:36 -05:00
|
|
|
{"RSS home", page.TargetPathDescriptor{Kind: "rss", Type: output.RSSFormat}, page.TargetPaths{TargetFilename: "/index.xml", SubResourceBaseTarget: "", Link: "/index.xml"}},
|
|
|
|
{"RSS section list", page.TargetPathDescriptor{
|
2019-01-02 06:33:26 -05:00
|
|
|
Kind: "rss",
|
|
|
|
Sections: []string{"sect1"},
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.RSSFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/sect1/index.xml", SubResourceBaseTarget: "/sect1", Link: "/sect1/index.xml"}},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"AMP page", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/a/b/c",
|
|
|
|
BaseName: "myamp",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.AMPFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/amp/a/b/c/myamp/index.html", SubResourceBaseTarget: "/amp/a/b/c/myamp", Link: "/amp/a/b/c/myamp/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"AMP page with URL with suffix", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/sect/",
|
|
|
|
BaseName: "mypage",
|
|
|
|
URL: "/some/other/url.xhtml",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/some/other/url.xhtml", SubResourceBaseTarget: "/some/other", Link: "/some/other/url.xhtml"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"JSON page with URL without suffix", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/sect/",
|
|
|
|
BaseName: "mypage",
|
|
|
|
URL: "/some/other/path/",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.JSONFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"JSON page with URL without suffix and no trailing slash", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/sect/",
|
|
|
|
BaseName: "mypage",
|
|
|
|
URL: "/some/other/path",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.JSONFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"HTML page with URL without suffix and no trailing slash", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/sect/",
|
|
|
|
BaseName: "mypage",
|
|
|
|
URL: "/some/other/path",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/some/other/path/index.html", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2020-11-21 12:58:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"HTML page with URL containing double hyphen", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2020-11-21 12:58:26 -05:00
|
|
|
Dir: "/sect/",
|
|
|
|
BaseName: "mypage",
|
|
|
|
URL: "/some/other--url/",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/some/other--url/index.html", SubResourceBaseTarget: "/some/other--url", Link: "/some/other--url/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"HTML page with expanded permalink", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/a/b",
|
|
|
|
BaseName: "mypage",
|
|
|
|
ExpandedPermalink: "/2017/10/my-title/",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/2017/10/my-title/index.html", SubResourceBaseTarget: "/2017/10/my-title", Link: "/2017/10/my-title/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"Paginated HTML home", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindHome,
|
2019-01-02 06:33:26 -05:00
|
|
|
BaseName: "_index",
|
|
|
|
Type: output.HTMLFormat,
|
2020-12-02 07:23:25 -05:00
|
|
|
Addends: "page/3",
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/page/3/index.html", SubResourceBaseTarget: "/page/3", Link: "/page/3/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"Paginated Taxonomy terms list", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindTerm,
|
2019-01-02 06:33:26 -05:00
|
|
|
BaseName: "_index",
|
|
|
|
Sections: []string{"tags", "hugo"},
|
|
|
|
Type: output.HTMLFormat,
|
2020-12-02 07:23:25 -05:00
|
|
|
Addends: "page/3",
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/tags/hugo/page/3/index.html", SubResourceBaseTarget: "/tags/hugo/page/3", Link: "/tags/hugo/page/3/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
{
|
2023-01-04 12:24:36 -05:00
|
|
|
"Regular page with addend", page.TargetPathDescriptor{
|
|
|
|
Kind: page.KindPage,
|
2019-01-02 06:33:26 -05:00
|
|
|
Dir: "/a/b",
|
|
|
|
BaseName: "mypage",
|
|
|
|
Addends: "c/d/e",
|
2020-12-02 07:23:25 -05:00
|
|
|
Type: output.HTMLFormat,
|
2023-01-04 12:24:36 -05:00
|
|
|
}, page.TargetPaths{TargetFilename: "/a/b/mypage/c/d/e/index.html", SubResourceBaseTarget: "/a/b/mypage/c/d/e", Link: "/a/b/mypage/c/d/e/"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
test.d.PrefixFilePath = langPrefixPath
|
|
|
|
test.d.PrefixLink = langPrefixLink
|
|
|
|
test.d.Dir = filepath.FromSlash(test.d.Dir)
|
|
|
|
isUgly := uglyURLs && !test.d.Type.NoUgly
|
|
|
|
|
|
|
|
expected := test.expected
|
|
|
|
|
|
|
|
// TODO(bep) simplify
|
2023-01-04 12:24:36 -05:00
|
|
|
if test.d.Kind == page.KindPage && test.d.BaseName == test.d.Type.BaseName {
|
|
|
|
} else if test.d.Kind == page.KindHome && test.d.Type.Path != "" {
|
2021-03-11 03:18:01 -05:00
|
|
|
} else if test.d.Type.MediaType.FirstSuffix.Suffix != "" && (!strings.HasPrefix(expected.TargetFilename, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
|
2019-01-02 06:33:26 -05:00
|
|
|
expected.TargetFilename = strings.Replace(expected.TargetFilename,
|
2021-03-11 03:18:01 -05:00
|
|
|
"/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.FirstSuffix.Suffix,
|
|
|
|
"."+test.d.Type.MediaType.FirstSuffix.Suffix, 1)
|
|
|
|
expected.Link = strings.TrimSuffix(expected.Link, "/") + "." + test.d.Type.MediaType.FirstSuffix.Suffix
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.d.PrefixFilePath != "" && !strings.HasPrefix(test.d.URL, "/"+test.d.PrefixFilePath) {
|
|
|
|
expected.TargetFilename = "/" + test.d.PrefixFilePath + expected.TargetFilename
|
|
|
|
expected.SubResourceBaseTarget = "/" + test.d.PrefixFilePath + expected.SubResourceBaseTarget
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.d.PrefixLink != "" && !strings.HasPrefix(test.d.URL, "/"+test.d.PrefixLink) {
|
|
|
|
expected.Link = "/" + test.d.PrefixLink + expected.Link
|
|
|
|
}
|
|
|
|
|
|
|
|
expected.TargetFilename = filepath.FromSlash(expected.TargetFilename)
|
|
|
|
expected.SubResourceBaseTarget = filepath.FromSlash(expected.SubResourceBaseTarget)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
pagePath := page.CreateTargetPaths(test.d)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
if !eqTargetPaths(pagePath, expected) {
|
|
|
|
t.Fatalf("[%d] [%s] targetPath expected\n%#v, got:\n%#v", i, test.name, expected, pagePath)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPageTargetPathPrefix(t *testing.T) {
|
|
|
|
pathSpec := newTestPathSpec()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-01-04 12:24:36 -05:00
|
|
|
d page.TargetPathDescriptor
|
|
|
|
expected page.TargetPaths
|
2019-01-02 06:33:26 -05:00
|
|
|
}{
|
2020-12-02 07:23:25 -05:00
|
|
|
{
|
|
|
|
"URL set, prefix both, no force",
|
2023-01-04 12:24:36 -05:00
|
|
|
page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"},
|
|
|
|
page.TargetPaths{TargetFilename: "/mydir/my.json", SubResourceBaseTarget: "/mydir", SubResourceBaseLink: "/mydir", Link: "/mydir/my.json"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"URL set, prefix both, force",
|
2023-01-04 12:24:36 -05:00
|
|
|
page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"},
|
|
|
|
page.TargetPaths{TargetFilename: "/pf/mydir/my.json", SubResourceBaseTarget: "/pf/mydir", SubResourceBaseLink: "/pl/mydir", Link: "/pl/mydir/my.json"},
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf(test.name),
|
|
|
|
func(t *testing.T) {
|
|
|
|
test.d.PathSpec = pathSpec
|
|
|
|
expected := test.expected
|
|
|
|
expected.TargetFilename = filepath.FromSlash(expected.TargetFilename)
|
|
|
|
expected.SubResourceBaseTarget = filepath.FromSlash(expected.SubResourceBaseTarget)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
pagePath := page.CreateTargetPaths(test.d)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
if pagePath != expected {
|
|
|
|
t.Fatalf("[%d] [%s] targetPath expected\n%#v, got:\n%#v", i, test.name, expected, pagePath)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func eqTargetPaths(p1, p2 page.TargetPaths) bool {
|
2019-01-02 06:33:26 -05:00
|
|
|
if p1.Link != p2.Link {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if p1.SubResourceBaseTarget != p2.SubResourceBaseTarget {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if p1.TargetFilename != p2.TargetFilename {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|