github.com/fighterlyt/hugo@v0.47.1/hugolib/page_paths_test.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"path/filepath"
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/media"
    22  
    23  	"fmt"
    24  
    25  	"github.com/gohugoio/hugo/output"
    26  )
    27  
    28  func TestPageTargetPath(t *testing.T) {
    29  
    30  	pathSpec := newTestDefaultPathSpec(t)
    31  
    32  	noExtNoDelimMediaType := media.TextType
    33  	noExtNoDelimMediaType.Suffixes = []string{}
    34  	noExtNoDelimMediaType.Delimiter = ""
    35  
    36  	// Netlify style _redirects
    37  	noExtDelimFormat := output.Format{
    38  		Name:      "NER",
    39  		MediaType: noExtNoDelimMediaType,
    40  		BaseName:  "_redirects",
    41  	}
    42  
    43  	for _, multiHost := range []bool{false, true} {
    44  		for _, langPrefix := range []string{"", "no"} {
    45  			for _, uglyURLs := range []bool{false, true} {
    46  				t.Run(fmt.Sprintf("multihost=%t,langPrefix=%q,uglyURLs=%t", multiHost, langPrefix, uglyURLs),
    47  					func(t *testing.T) {
    48  
    49  						tests := []struct {
    50  							name     string
    51  							d        targetPathDescriptor
    52  							expected string
    53  						}{
    54  							{"JSON home", targetPathDescriptor{Kind: KindHome, Type: output.JSONFormat}, "/index.json"},
    55  							{"AMP home", targetPathDescriptor{Kind: KindHome, Type: output.AMPFormat}, "/amp/index.html"},
    56  							{"HTML home", targetPathDescriptor{Kind: KindHome, BaseName: "_index", Type: output.HTMLFormat}, "/index.html"},
    57  							{"Netlify redirects", targetPathDescriptor{Kind: KindHome, BaseName: "_index", Type: noExtDelimFormat}, "/_redirects"},
    58  							{"HTML section list", targetPathDescriptor{
    59  								Kind:     KindSection,
    60  								Sections: []string{"sect1"},
    61  								BaseName: "_index",
    62  								Type:     output.HTMLFormat}, "/sect1/index.html"},
    63  							{"HTML taxonomy list", targetPathDescriptor{
    64  								Kind:     KindTaxonomy,
    65  								Sections: []string{"tags", "hugo"},
    66  								BaseName: "_index",
    67  								Type:     output.HTMLFormat}, "/tags/hugo/index.html"},
    68  							{"HTML taxonomy term", targetPathDescriptor{
    69  								Kind:     KindTaxonomy,
    70  								Sections: []string{"tags"},
    71  								BaseName: "_index",
    72  								Type:     output.HTMLFormat}, "/tags/index.html"},
    73  							{
    74  								"HTML page", targetPathDescriptor{
    75  									Kind:     KindPage,
    76  									Dir:      "/a/b",
    77  									BaseName: "mypage",
    78  									Sections: []string{"a"},
    79  									Type:     output.HTMLFormat}, "/a/b/mypage/index.html"},
    80  
    81  							{
    82  								"HTML page with index as base", targetPathDescriptor{
    83  									Kind:     KindPage,
    84  									Dir:      "/a/b",
    85  									BaseName: "index",
    86  									Sections: []string{"a"},
    87  									Type:     output.HTMLFormat}, "/a/b/index.html"},
    88  
    89  							{
    90  								"HTML page with special chars", targetPathDescriptor{
    91  									Kind:     KindPage,
    92  									Dir:      "/a/b",
    93  									BaseName: "My Page!",
    94  									Type:     output.HTMLFormat}, "/a/b/My-Page/index.html"},
    95  							{"RSS home", targetPathDescriptor{Kind: kindRSS, Type: output.RSSFormat}, "/index.xml"},
    96  							{"RSS section list", targetPathDescriptor{
    97  								Kind:     kindRSS,
    98  								Sections: []string{"sect1"},
    99  								Type:     output.RSSFormat}, "/sect1/index.xml"},
   100  							{
   101  								"AMP page", targetPathDescriptor{
   102  									Kind:     KindPage,
   103  									Dir:      "/a/b/c",
   104  									BaseName: "myamp",
   105  									Type:     output.AMPFormat}, "/amp/a/b/c/myamp/index.html"},
   106  							{
   107  								"AMP page with URL with suffix", targetPathDescriptor{
   108  									Kind:     KindPage,
   109  									Dir:      "/sect/",
   110  									BaseName: "mypage",
   111  									URL:      "/some/other/url.xhtml",
   112  									Type:     output.HTMLFormat}, "/some/other/url.xhtml"},
   113  							{
   114  								"JSON page with URL without suffix", targetPathDescriptor{
   115  									Kind:     KindPage,
   116  									Dir:      "/sect/",
   117  									BaseName: "mypage",
   118  									URL:      "/some/other/path/",
   119  									Type:     output.JSONFormat}, "/some/other/path/index.json"},
   120  							{
   121  								"JSON page with URL without suffix and no trailing slash", targetPathDescriptor{
   122  									Kind:     KindPage,
   123  									Dir:      "/sect/",
   124  									BaseName: "mypage",
   125  									URL:      "/some/other/path",
   126  									Type:     output.JSONFormat}, "/some/other/path/index.json"},
   127  							{
   128  								"HTML page with expanded permalink", targetPathDescriptor{
   129  									Kind:              KindPage,
   130  									Dir:               "/a/b",
   131  									BaseName:          "mypage",
   132  									ExpandedPermalink: "/2017/10/my-title",
   133  									Type:              output.HTMLFormat}, "/2017/10/my-title/index.html"},
   134  							{
   135  								"Paginated HTML home", targetPathDescriptor{
   136  									Kind:     KindHome,
   137  									BaseName: "_index",
   138  									Type:     output.HTMLFormat,
   139  									Addends:  "page/3"}, "/page/3/index.html"},
   140  							{
   141  								"Paginated Taxonomy list", targetPathDescriptor{
   142  									Kind:     KindTaxonomy,
   143  									BaseName: "_index",
   144  									Sections: []string{"tags", "hugo"},
   145  									Type:     output.HTMLFormat,
   146  									Addends:  "page/3"}, "/tags/hugo/page/3/index.html"},
   147  							{
   148  								"Regular page with addend", targetPathDescriptor{
   149  									Kind:     KindPage,
   150  									Dir:      "/a/b",
   151  									BaseName: "mypage",
   152  									Addends:  "c/d/e",
   153  									Type:     output.HTMLFormat}, "/a/b/mypage/c/d/e/index.html"},
   154  						}
   155  
   156  						for i, test := range tests {
   157  							test.d.PathSpec = pathSpec
   158  							test.d.UglyURLs = uglyURLs
   159  							test.d.LangPrefix = langPrefix
   160  							test.d.IsMultihost = multiHost
   161  							test.d.Dir = filepath.FromSlash(test.d.Dir)
   162  							isUgly := uglyURLs && !test.d.Type.NoUgly
   163  
   164  							expected := test.expected
   165  
   166  							// TODO(bep) simplify
   167  							if test.d.Kind == KindPage && test.d.BaseName == test.d.Type.BaseName {
   168  
   169  							} else if test.d.Kind == KindHome && test.d.Type.Path != "" {
   170  							} else if (!strings.HasPrefix(expected, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
   171  								expected = strings.Replace(expected,
   172  									"/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.Suffix(),
   173  									"."+test.d.Type.MediaType.Suffix(), -1)
   174  							}
   175  
   176  							if test.d.LangPrefix != "" && !(test.d.Kind == KindPage && test.d.URL != "") {
   177  								expected = "/" + test.d.LangPrefix + expected
   178  							} else if multiHost && test.d.LangPrefix != "" && test.d.URL != "" {
   179  								expected = "/" + test.d.LangPrefix + expected
   180  							}
   181  
   182  							expected = filepath.FromSlash(expected)
   183  
   184  							pagePath := createTargetPath(test.d)
   185  
   186  							if pagePath != expected {
   187  								t.Fatalf("[%d] [%s] targetPath expected %q, got: %q", i, test.name, expected, pagePath)
   188  							}
   189  						}
   190  					})
   191  			}
   192  		}
   193  	}
   194  }