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