github.com/neohugo/neohugo@v0.123.8/hugolib/site_url_test.go (about)

     1  // Copyright 2019 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  	"fmt"
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	qt "github.com/frankban/quicktest"
    22  	"github.com/neohugo/neohugo/deps"
    23  	"github.com/neohugo/neohugo/resources/kinds"
    24  )
    25  
    26  func TestUglyURLsPerSection(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	c := qt.New(t)
    30  
    31  	const dt = `---
    32  title: Do not go gentle into that good night
    33  ---
    34  
    35  Wild men who caught and sang the sun in flight,
    36  And learn, too late, they grieved it on its way,
    37  Do not go gentle into that good night.
    38  
    39  `
    40  
    41  	cfg, fs := newTestCfg()
    42  
    43  	cfg.Set("uglyURLs", map[string]bool{
    44  		"sect2": true,
    45  	})
    46  	configs, err := loadTestConfigFromProvider(cfg)
    47  	c.Assert(err, qt.IsNil)
    48  
    49  	writeSource(t, fs, filepath.Join("content", "sect1", "p1.md"), dt)
    50  	writeSource(t, fs, filepath.Join("content", "sect2", "p2.md"), dt)
    51  
    52  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
    53  
    54  	c.Assert(len(s.RegularPages()), qt.Equals, 2)
    55  
    56  	notUgly := s.getPageOldVersion(kinds.KindPage, "sect1/p1.md")
    57  	c.Assert(notUgly, qt.Not(qt.IsNil))
    58  	c.Assert(notUgly.Section(), qt.Equals, "sect1")
    59  	c.Assert(notUgly.RelPermalink(), qt.Equals, "/sect1/p1/")
    60  
    61  	ugly := s.getPageOldVersion(kinds.KindPage, "sect2/p2.md")
    62  	c.Assert(ugly, qt.Not(qt.IsNil))
    63  	c.Assert(ugly.Section(), qt.Equals, "sect2")
    64  	c.Assert(ugly.RelPermalink(), qt.Equals, "/sect2/p2.html")
    65  }
    66  
    67  func TestSectionWithURLInFrontMatter(t *testing.T) {
    68  	t.Parallel()
    69  
    70  	c := qt.New(t)
    71  
    72  	const st = `---
    73  title: Do not go gentle into that good night
    74  url: %s
    75  ---
    76  
    77  Wild men who caught and sang the sun in flight,
    78  And learn, too late, they grieved it on its way,
    79  Do not go gentle into that good night.
    80  
    81  `
    82  
    83  	const pt = `---
    84  title: Wild men who caught and sang the sun in flight
    85  ---
    86  
    87  Wild men who caught and sang the sun in flight,
    88  And learn, too late, they grieved it on its way,
    89  Do not go gentle into that good night.
    90  
    91  `
    92  
    93  	cfg, fs := newTestCfg()
    94  	cfg.Set("paginate", 1)
    95  	th, configs := newTestHelperFromProvider(cfg, fs, t)
    96  
    97  	writeSource(t, fs, filepath.Join("content", "sect1", "_index.md"), fmt.Sprintf(st, "/ss1/"))
    98  	writeSource(t, fs, filepath.Join("content", "sect2", "_index.md"), fmt.Sprintf(st, "/ss2/"))
    99  
   100  	for i := 0; i < 5; i++ {
   101  		writeSource(t, fs, filepath.Join("content", "sect1", fmt.Sprintf("p%d.md", i+1)), pt)
   102  		writeSource(t, fs, filepath.Join("content", "sect2", fmt.Sprintf("p%d.md", i+1)), pt)
   103  	}
   104  
   105  	writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), "<html><body>{{.Content}}</body></html>")
   106  	writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"),
   107  		"<html><body>P{{.Paginator.PageNumber}}|URL: {{.Paginator.URL}}|{{ if .Paginator.HasNext }}Next: {{.Paginator.Next.URL }}{{ end }}</body></html>")
   108  
   109  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
   110  
   111  	c.Assert(len(s.RegularPages()), qt.Equals, 10)
   112  
   113  	sect1 := s.getPageOldVersion(kinds.KindSection, "sect1")
   114  	c.Assert(sect1, qt.Not(qt.IsNil))
   115  	c.Assert(sect1.RelPermalink(), qt.Equals, "/ss1/")
   116  	th.assertFileContent(filepath.Join("public", "ss1", "index.html"), "P1|URL: /ss1/|Next: /ss1/page/2/")
   117  	th.assertFileContent(filepath.Join("public", "ss1", "page", "2", "index.html"), "P2|URL: /ss1/page/2/|Next: /ss1/page/3/")
   118  }
   119  
   120  func TestSectionsEntries(t *testing.T) {
   121  	files := `
   122  -- hugo.toml --
   123  -- content/withfile/_index.md --
   124  -- content/withoutfile/p1.md --
   125  -- layouts/_default/list.html --
   126  SectionsEntries: {{ .SectionsEntries }}
   127  
   128  
   129  `
   130  
   131  	b := Test(t, files)
   132  
   133  	b.AssertFileContent("public/withfile/index.html", "SectionsEntries: [withfile]")
   134  	b.AssertFileContent("public/withoutfile/index.html", "SectionsEntries: [withoutfile]")
   135  }