github.com/SDLMoe/hugo@v0.47.1/hugolib/site_url_test.go (about) 1 // Copyright 2016 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 "html/template" 22 23 "github.com/gohugoio/hugo/deps" 24 "github.com/stretchr/testify/require" 25 ) 26 27 const slugDoc1 = "---\ntitle: slug doc 1\nslug: slug-doc-1\naliases:\n - sd1/foo/\n - sd2\n - sd3/\n - sd4.html\n---\nslug doc 1 content\n" 28 29 const slugDoc2 = `--- 30 title: slug doc 2 31 slug: slug-doc-2 32 --- 33 slug doc 2 content 34 ` 35 36 var urlFakeSource = [][2]string{ 37 {filepath.FromSlash("content/blue/doc1.md"), slugDoc1}, 38 {filepath.FromSlash("content/blue/doc2.md"), slugDoc2}, 39 } 40 41 // Issue #1105 42 func TestShouldNotAddTrailingSlashToBaseURL(t *testing.T) { 43 t.Parallel() 44 for i, this := range []struct { 45 in string 46 expected string 47 }{ 48 {"http://base.com/", "http://base.com/"}, 49 {"http://base.com/sub/", "http://base.com/sub/"}, 50 {"http://base.com/sub", "http://base.com/sub"}, 51 {"http://base.com", "http://base.com"}} { 52 53 cfg, fs := newTestCfg() 54 cfg.Set("baseURL", this.in) 55 d := deps.DepsCfg{Cfg: cfg, Fs: fs} 56 s, err := NewSiteForCfg(d) 57 require.NoError(t, err) 58 require.NoError(t, s.initializeSiteInfo()) 59 60 if s.Info.BaseURL() != template.URL(this.expected) { 61 t.Errorf("[%d] got %s expected %s", i, s.Info.BaseURL(), this.expected) 62 } 63 } 64 } 65 66 func TestPageCount(t *testing.T) { 67 t.Parallel() 68 cfg, fs := newTestCfg() 69 cfg.Set("uglyURLs", false) 70 cfg.Set("paginate", 10) 71 72 writeSourcesToSource(t, "", fs, urlFakeSource...) 73 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 74 75 _, err := s.Fs.Destination.Open("public/blue") 76 if err != nil { 77 t.Errorf("No indexed rendered.") 78 } 79 80 for _, pth := range []string{ 81 "public/sd1/foo/index.html", 82 "public/sd2/index.html", 83 "public/sd3/index.html", 84 "public/sd4.html", 85 } { 86 if _, err := s.Fs.Destination.Open(filepath.FromSlash(pth)); err != nil { 87 t.Errorf("No alias rendered: %s", pth) 88 } 89 } 90 } 91 92 func TestUglyURLsPerSection(t *testing.T) { 93 t.Parallel() 94 95 assert := require.New(t) 96 97 const dt = `--- 98 title: Do not go gentle into that good night 99 --- 100 101 Wild men who caught and sang the sun in flight, 102 And learn, too late, they grieved it on its way, 103 Do not go gentle into that good night. 104 105 ` 106 107 cfg, fs := newTestCfg() 108 109 cfg.Set("uglyURLs", map[string]bool{ 110 "sect2": true, 111 }) 112 113 writeSource(t, fs, filepath.Join("content", "sect1", "p1.md"), dt) 114 writeSource(t, fs, filepath.Join("content", "sect2", "p2.md"), dt) 115 116 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 117 118 assert.Len(s.RegularPages, 2) 119 120 notUgly := s.getPage(KindPage, "sect1/p1.md") 121 assert.NotNil(notUgly) 122 assert.Equal("sect1", notUgly.Section()) 123 assert.Equal("/sect1/p1/", notUgly.RelPermalink()) 124 125 ugly := s.getPage(KindPage, "sect2/p2.md") 126 assert.NotNil(ugly) 127 assert.Equal("sect2", ugly.Section()) 128 assert.Equal("/sect2/p2.html", ugly.RelPermalink()) 129 } 130 131 func TestSectionWithURLInFrontMatter(t *testing.T) { 132 t.Parallel() 133 134 assert := require.New(t) 135 136 const st = `--- 137 title: Do not go gentle into that good night 138 url: %s 139 --- 140 141 Wild men who caught and sang the sun in flight, 142 And learn, too late, they grieved it on its way, 143 Do not go gentle into that good night. 144 145 ` 146 147 const pt = `--- 148 title: Wild men who caught and sang the sun in flight 149 --- 150 151 Wild men who caught and sang the sun in flight, 152 And learn, too late, they grieved it on its way, 153 Do not go gentle into that good night. 154 155 ` 156 157 cfg, fs := newTestCfg() 158 th := testHelper{cfg, fs, t} 159 160 cfg.Set("paginate", 1) 161 162 writeSource(t, fs, filepath.Join("content", "sect1", "_index.md"), fmt.Sprintf(st, "/ss1/")) 163 writeSource(t, fs, filepath.Join("content", "sect2", "_index.md"), fmt.Sprintf(st, "/ss2/")) 164 165 for i := 0; i < 5; i++ { 166 writeSource(t, fs, filepath.Join("content", "sect1", fmt.Sprintf("p%d.md", i+1)), pt) 167 writeSource(t, fs, filepath.Join("content", "sect2", fmt.Sprintf("p%d.md", i+1)), pt) 168 } 169 170 writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), "<html><body>{{.Content}}</body></html>") 171 writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"), 172 "<html><body>P{{.Paginator.PageNumber}}|URL: {{.Paginator.URL}}|{{ if .Paginator.HasNext }}Next: {{.Paginator.Next.URL }}{{ end }}</body></html>") 173 174 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 175 176 assert.Len(s.RegularPages, 10) 177 178 sect1 := s.getPage(KindSection, "sect1") 179 assert.NotNil(sect1) 180 assert.Equal("/ss1/", sect1.RelPermalink()) 181 th.assertFileContent(filepath.Join("public", "ss1", "index.html"), "P1|URL: /ss1/|Next: /ss1/page/2/") 182 th.assertFileContent(filepath.Join("public", "ss1", "page", "2", "index.html"), "P2|URL: /ss1/page/2/|Next: /ss1/page/3/") 183 184 }