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("public/fr/section/mybundle/index.html",
158 "fr: .body{color:french}|/section/mybundle/styles.min.css|",
159 )
160
161 b.AssertFileContent("public/en/section/mybundle/index.html",
162 "en: .body{color:english}|/section/mybundle/styles.min.css|",
163 )
164
165 b.AssertFileContent("public/en/section/mybundle/styles.min.css", ".body{color:english}")
166 b.AssertFileContent("public/fr/section/mybundle/styles.min.css", ".body{color:french}")
167 }
168
169 func TestResourcePerLanguageIssue12163(t *testing.T) {
170 files := `
171 -- hugo.toml --
172 defaultContentLanguage = 'de'
173 disableKinds = ['rss','sitemap','taxonomy','term']
174
175 [languages.de]
176 baseURL = 'https://de.example.org/'
177 contentDir = 'content/de'
178 weight = 1
179
180 [languages.en]
181 baseURL = 'https://en.example.org/'
182 contentDir = 'content/en'
183 weight = 2
184 -- content/de/mybundle/index.md --
185 ---
186 title: mybundle-de
187 ---
188 -- content/de/mybundle/pixel.png --
189 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
190 -- content/en/mybundle/index.md --
191 ---
192 title: mybundle-en
193 ---
194 -- layouts/single.html --
195 {{ with .Resources.Get "pixel.png" }}
196 {{ with .Resize "2x2" }}
197 {{ .RelPermalink }}|
198 {{ end }}
199 {{ end }}
200 `
201
202 b := Test(t, files)
203
204 b.AssertFileExists("public/de/mybundle/index.html", true)
205 b.AssertFileExists("public/en/mybundle/index.html", true)
206
207 b.AssertFileExists("public/de/mybundle/pixel.png", true)
208 b.AssertFileExists("public/en/mybundle/pixel.png", true)
209
210 b.AssertFileExists("public/de/mybundle/pixel_hu_c8522c65d73a0421.png", true)
211 // failing test below
212 b.AssertFileExists("public/en/mybundle/pixel_hu_c8522c65d73a0421.png", true)
213 }
214
215 func TestMultihostResourceOneBaseURLWithSubPath(t *testing.T) {
216 files := `
217 -- hugo.toml --
218 defaultContentLanguage = "en"
219 [languages]
220 [languages.en]
221 baseURL = "https://example.com/docs"
222 weight = 1
223 contentDir = "content/en"
224 [languages.en.permalinks]
225 section = "/enpages/:slug/"
226 [languages.fr]
227 baseURL = "https://example.fr"
228 contentDir = "content/fr"
229 -- content/en/section/mybundle/index.md --
230 ---
231 title: "Mybundle en"
232 ---
233 -- content/fr/section/mybundle/index.md --
234 ---
235 title: "Mybundle fr"
236 ---
237 -- content/fr/section/mybundle/file1.txt --
238 File 1 fr.
239 -- content/en/section/mybundle/file1.txt --
240 File 1 en.
241 -- content/en/section/mybundle/file2.txt --
242 File 2 en.
243 -- layouts/single.html --
244 {{ $files := .Resources.Match "file*" }}
245 Files: {{ range $files }}{{ .Permalink }}|{{ end }}$
246
247 `
248
249 b := Test(t, files)
250
251 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|$")
252 b.AssertFileContent("public/fr/section/mybundle/index.html", "Files: https://example.fr/section/mybundle/file1.txt|https://example.fr/section/mybundle/file2.txt|$")
253
254 b.AssertFileContent("public/en/enpages/mybundle-en/file1.txt", "File 1 en.")
255 b.AssertFileContent("public/fr/section/mybundle/file1.txt", "File 1 fr.")
256 b.AssertFileContent("public/en/enpages/mybundle-en/file2.txt", "File 2 en.")
257 b.AssertFileContent("public/fr/section/mybundle/file2.txt", "File 2 en.")
258 }
259
260 func TestMultihostAllButOneLanguageDisabledIssue12288(t *testing.T) {
261 t.Parallel()
262
263 files := `
264 -- hugo.toml --
265 defaultContentLanguage = "en"
266 disableLanguages = ["fr"]
267 #baseURL = "https://example.com"
268 [languages]
269 [languages.en]
270 baseURL = "https://example.en"
271 weight = 1
272 [languages.fr]
273 baseURL = "https://example.fr"
274 weight = 2
275 -- assets/css/main.css --
276 body { color: red; }
277 -- layouts/home.html --
278 {{ $css := resources.Get "css/main.css" | minify }}
279 CSS: {{ $css.Permalink }}|{{ $css.RelPermalink }}|
280 `
281
282 b := Test(t, files)
283
284 b.AssertFileContent("public/css/main.min.css", "body{color:red}")
285 b.AssertFileContent("public/index.html", "CSS: https://example.en/css/main.min.css|/css/main.min.css|")
286 }