Debian Codegraph @

hugolib/hugo_sites_multihost_test.go

raw
1 package hugolib
2
3 import (
4 "testing"
5
6 qt "github.com/frankban/quicktest"
7 )
8
9 func TestMultihost(t *testing.T) {
10 t.Parallel()
11
12 files := `
13 -- hugo.toml --
14 defaultContentLanguage = "fr"
15 defaultContentLanguageInSubdir = false
16 staticDir = ["s1", "s2"]
17 enableRobotsTXT = true
18
19 [pagination]
20 pagerSize = 1
21
22 [permalinks]
23 other = "/somewhere/else/:contentbasename"
24
25 [taxonomies]
26 tag = "tags"
27
28 [languages]
29 [languages.en]
30 staticDir2 = ["staticen"]
31 baseURL = "https://example.com/docs"
32 weight = 10
33 title = "In English"
34 label = "English"
35 [languages.fr]
36 staticDir2 = ["staticfr"]
37 baseURL = "https://example.fr"
38 weight = 20
39 title = "Le Français"
40 label = "Français"
41 -- assets/css/main.css --
42 body { color: red; }
43 -- content/mysect/mybundle/index.md --
44 ---
45 tags: [a, b]
46 title: "My Bundle fr"
47 ---
48 My Bundle
49 -- content/mysect/mybundle/index.en.md --
50 ---
51 tags: [c, d]
52 title: "My Bundle en"
53 ---
54 My Bundle
55 -- content/mysect/mybundle/foo.txt --
56 Foo
57 -- layouts/list.html --
58 List|{{ .Title }}|{{ .Lang }}|{{ .Permalink}}|{{ .RelPermalink }}|
59 -- layouts/single.html --
60 Single|{{ .Title }}|{{ .Lang }}|{{ .Permalink}}|{{ .RelPermalink }}|
61 {{ $foo := .Resources.Get "foo.txt" | fingerprint }}
62 Foo: {{ $foo.Permalink }}|
63 {{ $css := resources.Get "css/main.css" | fingerprint }}
64 CSS: {{ $css.Permalink }}|{{ $css.RelPermalink }}|
65 -- layouts/robots.txt --
66 robots|{{ site.Language.Lang }}
67 -- layouts/404.html --
68 404|{{ site.Language.Lang }}
69
70
71
72 `
73
74 b := Test(t, files)
75
76 b.Assert(b.H.Conf.IsMultilingual(), qt.Equals, true)
77 b.Assert(b.H.Conf.IsMultihost(), qt.Equals, true)
78
79 // helpers.PrintFs(b.H.Fs.PublishDir, "", os.Stdout)
80
81 // Check regular pages.
82 b.AssertFileContent("public/en/mysect/mybundle/index.html", "Single|My Bundle en|en|https://example.com/docs/mysect/mybundle/|")
83 b.AssertFileContent("public/fr/mysect/mybundle/index.html", "Single|My Bundle fr|fr|https://example.fr/mysect/mybundle/|")
84
85 // Check robots.txt
86 b.AssertFileContent("public/en/robots.txt", "robots|en")
87 b.AssertFileContent("public/fr/robots.txt", "robots|fr")
88
89 // Check sitemap.xml
90 b.AssertFileContent("public/en/sitemap.xml", "https://example.com/docs/mysect/mybundle/")
91 b.AssertFileContent("public/fr/sitemap.xml", "https://example.fr/mysect/mybundle/")
92
93 // Check 404
94 b.AssertFileContent("public/en/404.html", "404|en")
95 b.AssertFileContent("public/fr/404.html", "404|fr")
96
97 // Check tags.
98 b.AssertFileContent("public/en/tags/d/index.html", "List|D|en|https://example.com/docs/tags/d/")
99 b.AssertFileContent("public/fr/tags/b/index.html", "List|B|fr|https://example.fr/tags/b/")
100 b.AssertFileExists("public/en/tags/b/index.html", false)
101 b.AssertFileExists("public/fr/tags/d/index.html", false)
102
103 // en/mysect/mybundle/foo.txt fingerprinted
104 b.AssertFileContent("public/en/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt", "Foo")
105 b.AssertFileContent("public/en/mysect/mybundle/index.html", "Foo: https://example.com/docs/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt|")
106 b.AssertFileContent("public/fr/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt", "Foo")
107 b.AssertFileContent("public/fr/mysect/mybundle/index.html", "Foo: https://example.fr/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt|")
108
109 // Assets CSS fingerprinted
110 // Note that before Hugo v0.149.0 we rendered the project starting with defaultContentLanguage,
111 // now with a more complex matrix, we have one sort order.
112 b.AssertFileContent("public/en/mysect/mybundle/index.html", "CSS: https://example.com/docs/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css|")
113 b.AssertFileContent("public/en/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css", "body { color: red; }")
114 b.AssertFileContent("public/fr/mysect/mybundle/index.html", "CSS: https://example.com/docs/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css|")
115 b.AssertFileContent("public/fr/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css", "body { color: red; }")
116 }
117
118 func TestMultihostResourcePerLanguageMultihostMinify(t *testing.T) {
119 t.Parallel()
120 files := `
121 -- hugo.toml --
122 disableKinds = ["taxonomy", "term"]
123 defaultContentLanguage = "en"
124 defaultContentLanguageInSubDir = true
125 [languages]
126 [languages.en]
127 baseURL = "https://example.en"
128 weight = 1
129 contentDir = "content/en"
130 [languages.fr]
131 baseURL = "https://example.fr"
132 weight = 2
133 contentDir = "content/fr"
134 -- content/en/section/mybundle/index.md --
135 ---
136 title: "Mybundle en"
137 ---
138 -- content/fr/section/mybundle/index.md --
139 ---
140 title: "Mybundle fr"
141 ---
142 -- content/en/section/mybundle/styles.css --
143 .body {
144 color: english;
145 }
146 -- content/fr/section/mybundle/styles.css --
147 .body {
148 color: french;
149 }
150 -- layouts/single.html --
151 {{ $data := .Resources.GetMatch "styles*" | minify }}
152 {{ .Lang }}: {{ $data.Content}}|{{ $data.RelPermalink }}|
153
154 `
155 b := Test(t, files)
156
157 b.AssertFileContent(
158 "public/fr/section/mybundle/index.html",
159 "fr: .body{color:french}|/section/mybundle/styles.min.css|",
160 )
161
162 b.AssertFileContent(
163 "public/en/section/mybundle/index.html",
164 "en: .body{color:english}|/section/mybundle/styles.min.css|",
165 )
166
167 b.AssertFileContent("public/en/section/mybundle/styles.min.css", ".body{color:english}")
168 b.AssertFileContent("public/fr/section/mybundle/styles.min.css", ".body{color:french}")
169 }
170
171 func TestResourcePerLanguageIssue12163(t *testing.T) {
172 files := `
173 -- hugo.toml --
174 defaultContentLanguage = 'de'
175 disableKinds = ['rss','sitemap','taxonomy','term']
176
177 [languages.de]
178 baseURL = 'https://de.example.org/'
179 contentDir = 'content/de'
180 weight = 1
181
182 [languages.en]
183 baseURL = 'https://en.example.org/'
184 contentDir = 'content/en'
185 weight = 2
186 -- content/de/mybundle/index.md --
187 ---
188 title: mybundle-de
189 ---
190 -- content/de/mybundle/pixel.png --
191 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
192 -- content/en/mybundle/index.md --
193 ---
194 title: mybundle-en
195 ---
196 -- layouts/single.html --
197 {{ with .Resources.Get "pixel.png" }}
198 {{ with .Resize "2x2" }}
199 {{ .RelPermalink }}|
200 {{ end }}
201 {{ end }}
202 `
203
204 b := Test(t, files)
205
206 b.AssertFileExists("public/de/mybundle/index.html", true)
207 b.AssertFileExists("public/en/mybundle/index.html", true)
208
209 b.AssertFileExists("public/de/mybundle/pixel.png", true)
210 b.AssertFileExists("public/en/mybundle/pixel.png", true)
211
212 b.AssertFileExists("public/de/mybundle/pixel_hu_a25c5b589ae00856.png", true)
213 // failing test below
214 b.AssertFileExists("public/en/mybundle/pixel_hu_a25c5b589ae00856.png", true)
215 }
216
217 func TestMultihostResourceOneBaseURLWithSubPath(t *testing.T) {
218 files := `
219 -- hugo.toml --
220 defaultContentLanguage = "en"
221 [languages]
222 [languages.en]
223 baseURL = "https://example.com/docs"
224 weight = 1
225 contentDir = "content/en"
226 [languages.en.permalinks]
227 section = "/enpages/:slug/"
228 [languages.fr]
229 baseURL = "https://example.fr"
230 contentDir = "content/fr"
231 -- content/en/section/mybundle/index.md --
232 ---
233 title: "Mybundle en"
234 ---
235 -- content/fr/section/mybundle/index.md --
236 ---
237 title: "Mybundle fr"
238 ---
239 -- content/fr/section/mybundle/file1.txt --
240 File 1 fr.
241 -- content/en/section/mybundle/file1.txt --
242 File 1 en.
243 -- content/en/section/mybundle/file2.txt --
244 File 2 en.
245 -- layouts/single.html --
246 {{ $files := .Resources.Match "file*" }}
247 Files: {{ range $files }}{{ .Permalink }}|{{ end }}$
248
249 `
250
251 b := Test(t, files)
252
253 b.AssertFileContent("public/en/enpages/mybundle-en/index.html", "Files: https://example.com/docs/enpages/mybundle-en/file1.txt|https://example.com/docs/enpages/mybundle-en/file2.txt|$")
254 b.AssertFileContent("public/fr/section/mybundle/index.html", "Files: https://example.fr/section/mybundle/file1.txt|https://example.fr/section/mybundle/file2.txt|$")
255
256 b.AssertFileContent("public/en/enpages/mybundle-en/file1.txt", "File 1 en.")
257 b.AssertFileContent("public/fr/section/mybundle/file1.txt", "File 1 fr.")
258 b.AssertFileContent("public/en/enpages/mybundle-en/file2.txt", "File 2 en.")
259 b.AssertFileContent("public/fr/section/mybundle/file2.txt", "File 2 en.")
260 }
261
262 func TestMultihostAllButOneLanguageDisabledIssue12288(t *testing.T) {
263 t.Parallel()
264
265 files := `
266 -- hugo.toml --
267 defaultContentLanguage = "en"
268 disableLanguages = ["fr"]
269 #baseURL = "https://example.com"
270 [languages]
271 [languages.en]
272 baseURL = "https://example.en"
273 weight = 1
274 [languages.fr]
275 baseURL = "https://example.fr"
276 weight = 2
277 -- assets/css/main.css --
278 body { color: red; }
279 -- layouts/home.html --
280 {{ $css := resources.Get "css/main.css" | minify }}
281 CSS: {{ $css.Permalink }}|{{ $css.RelPermalink }}|
282 `
283
284 b := Test(t, files)
285
286 b.AssertFileContent("public/css/main.min.css", "body{color:red}")
287 b.AssertFileContent("public/index.html", "CSS: https://example.en/css/main.min.css|/css/main.min.css|")
288 }