Debian Codegraph @

common/paths/pathparser_test.go

raw
1 // Copyright 2024 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 paths
15
16 import (
17 "path/filepath"
18 "reflect"
19 "testing"
20
21 "github.com/gohugoio/hugo/hugofs/files"
22 "github.com/gohugoio/hugo/hugolib/sitesmatrix"
23 "github.com/gohugoio/hugo/resources/kinds"
24
25 qt "github.com/frankban/quicktest"
26 )
27
28 func newTestParser() *PathParser {
29 dims := sitesmatrix.NewTestingDimensions([]string{"en", "no", "fr"}, []string{"v1", "v2", "v3", "v1.0.0"}, []string{"admin", "editor", "viewer", "guest"})
30
31 return &PathParser{
32 LanguageIndex: map[string]int{
33 "no": 0,
34 "en": 1,
35 "fr": 2,
36 },
37 IsContentExt: func(ext string) bool {
38 return ext == "md"
39 },
40 IsOutputFormat: func(name, ext string) bool {
41 switch name {
42 case "html", "amp", "csv", "rss":
43 return true
44 }
45 return false
46 },
47 ConfiguredDimensions: dims,
48 }
49 }
50
51 func TestParse(t *testing.T) {
52 c := qt.New(t)
53
54 tests := []struct {
55 name string
56 path string
57 assert func(c *qt.C, p *Path)
58 }{
59 {
60 "Basic text file",
61 "/a/b.txt",
62 func(c *qt.C, p *Path) {
63 c.Assert(p.Name(), qt.Equals, "b.txt")
64 c.Assert(p.Base(), qt.Equals, "/a/b.txt")
65 c.Assert(p.Container(), qt.Equals, "a")
66 c.Assert(p.Dir(), qt.Equals, "/a")
67 c.Assert(p.Ext(), qt.Equals, "txt")
68 c.Assert(p.IsContent(), qt.IsFalse)
69 },
70 },
71 {
72 "Basic text file, upper case",
73 "/A/B.txt",
74 func(c *qt.C, p *Path) {
75 c.Assert(p.Name(), qt.Equals, "b.txt")
76 c.Assert(p.NameNoExt(), qt.Equals, "b")
77 c.Assert(p.NameNoIdentifier(), qt.Equals, "b")
78 c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "b")
79 c.Assert(p.Base(), qt.Equals, "/a/b.txt")
80 c.Assert(p.Ext(), qt.Equals, "txt")
81 },
82 },
83 {
84 "Basic text file, 1 space in dir",
85 "/a b/c.txt",
86 func(c *qt.C, p *Path) {
87 c.Assert(p.Base(), qt.Equals, "/a-b/c.txt")
88 },
89 },
90 {
91 "Basic text file, 2 spaces in dir",
92 "/a b/c.txt",
93 func(c *qt.C, p *Path) {
94 c.Assert(p.Base(), qt.Equals, "/a--b/c.txt")
95 },
96 },
97 {
98 "Basic text file, 1 space in filename",
99 "/a/b c.txt",
100 func(c *qt.C, p *Path) {
101 c.Assert(p.Base(), qt.Equals, "/a/b-c.txt")
102 },
103 },
104 {
105 "Basic text file, 2 spaces in filename",
106 "/a/b c.txt",
107 func(c *qt.C, p *Path) {
108 c.Assert(p.Base(), qt.Equals, "/a/b--c.txt")
109 },
110 },
111 {
112 "Basic text file, mixed case and spaces, unnormalized",
113 "/a/Foo BAR.txt",
114 func(c *qt.C, p *Path) {
115 pp := p.Unnormalized()
116 c.Assert(pp, qt.IsNotNil)
117 c.Assert(pp.BaseNameNoIdentifier(), qt.Equals, "Foo BAR")
118 },
119 },
120 {
121 "Basic Markdown file",
122 "/a/b/c.md",
123 func(c *qt.C, p *Path) {
124 c.Assert(p.Ext(), qt.Equals, "md")
125 c.Assert(p.Type(), qt.Equals, TypeContentSingle)
126 c.Assert(p.IsContent(), qt.IsTrue)
127 c.Assert(p.IsLeafBundle(), qt.IsFalse)
128 c.Assert(p.Name(), qt.Equals, "c.md")
129 c.Assert(p.Base(), qt.Equals, "/a/b/c")
130 c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo/b/c")
131 c.Assert(p.Section(), qt.Equals, "a")
132 c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "c")
133 c.Assert(p.Path(), qt.Equals, "/a/b/c.md")
134 c.Assert(p.Dir(), qt.Equals, "/a/b")
135 c.Assert(p.Container(), qt.Equals, "b")
136 c.Assert(p.ContainerDir(), qt.Equals, "/a/b")
137 },
138 },
139 {
140 "Content resource",
141 "/a/b.md",
142 func(c *qt.C, p *Path) {
143 c.Assert(p.Name(), qt.Equals, "b.md")
144 c.Assert(p.Base(), qt.Equals, "/a/b")
145 c.Assert(p.Section(), qt.Equals, "a")
146 c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "b")
147
148 // Reclassify it as a content resource.
149 ModifyPathBundleTypeResource(p)
150 c.Assert(p.Type(), qt.Equals, TypeContentResource)
151 c.Assert(p.IsContent(), qt.IsTrue)
152 c.Assert(p.Name(), qt.Equals, "b.md")
153 c.Assert(p.Base(), qt.Equals, "/a/b.md")
154 },
155 },
156 {
157 "No ext",
158 "/a/b",
159 func(c *qt.C, p *Path) {
160 c.Assert(p.Name(), qt.Equals, "b")
161 c.Assert(p.NameNoExt(), qt.Equals, "b")
162 c.Assert(p.Base(), qt.Equals, "/a/b")
163 c.Assert(p.Ext(), qt.Equals, "")
164 },
165 },
166 {
167 "No ext, trailing slash",
168 "/a/b/",
169 func(c *qt.C, p *Path) {
170 c.Assert(p.Name(), qt.Equals, "b")
171 c.Assert(p.Base(), qt.Equals, "/a/b")
172 c.Assert(p.Ext(), qt.Equals, "")
173 },
174 },
175 {
176 "Identifiers",
177 "/a/b.a.b.no.txt",
178 func(c *qt.C, p *Path) {
179 c.Assert(p.Name(), qt.Equals, "b.a.b.no.txt")
180 c.Assert(p.NameNoIdentifier(), qt.Equals, "b.a.b")
181 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "no"})
182 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{"b", "a", "b"})
183 c.Assert(p.Base(), qt.Equals, "/a/b.a.b.txt")
184 c.Assert(p.Path(), qt.Equals, "/a/b.a.b.no.txt")
185 c.Assert(p.PathNoLang(), qt.Equals, "/a/b.a.b.txt")
186 c.Assert(p.Ext(), qt.Equals, "txt")
187 c.Assert(p.PathNoIdentifier(), qt.Equals, "/a/b.a.b")
188 },
189 },
190 {
191 "Home branch cundle",
192 "/_index.md",
193 func(c *qt.C, p *Path) {
194 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md"})
195 c.Assert(p.IsBranchBundle(), qt.IsTrue)
196 c.Assert(p.IsBundle(), qt.IsTrue)
197 c.Assert(p.Base(), qt.Equals, "")
198 c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo")
199 c.Assert(p.Path(), qt.Equals, "/_index.md")
200 c.Assert(p.Container(), qt.Equals, "")
201 c.Assert(p.ContainerDir(), qt.Equals, "/")
202 },
203 },
204 {
205 "Index content file in root",
206 "/a/index.md",
207 func(c *qt.C, p *Path) {
208 c.Assert(p.Base(), qt.Equals, "/a")
209 c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo/a")
210 c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "a")
211 c.Assert(p.Container(), qt.Equals, "a")
212 c.Assert(p.Container(), qt.Equals, "a")
213 c.Assert(p.ContainerDir(), qt.Equals, "")
214 c.Assert(p.Dir(), qt.Equals, "/a")
215 c.Assert(p.Ext(), qt.Equals, "md")
216 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{"index"})
217 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md"})
218 c.Assert(p.IsBranchBundle(), qt.IsFalse)
219 c.Assert(p.IsBundle(), qt.IsTrue)
220 c.Assert(p.IsLeafBundle(), qt.IsTrue)
221 c.Assert(p.Lang(), qt.Equals, "")
222 c.Assert(p.NameNoExt(), qt.Equals, "index")
223 c.Assert(p.NameNoIdentifier(), qt.Equals, "index")
224 c.Assert(p.Section(), qt.Equals, "")
225 },
226 },
227 {
228 "Index content file with lang",
229 "/a/b/index.no.md",
230 func(c *qt.C, p *Path) {
231 c.Assert(p.Base(), qt.Equals, "/a/b")
232 c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "b")
233 c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo/b")
234 c.Assert(p.Container(), qt.Equals, "b")
235 c.Assert(p.ContainerDir(), qt.Equals, "/a")
236 c.Assert(p.Dir(), qt.Equals, "/a/b")
237 c.Assert(p.Ext(), qt.Equals, "md")
238 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md", "no"})
239 c.Assert(p.IsBranchBundle(), qt.IsFalse)
240 c.Assert(p.IsBundle(), qt.IsTrue)
241 c.Assert(p.IsLeafBundle(), qt.IsTrue)
242 c.Assert(p.Lang(), qt.Equals, "no")
243 c.Assert(p.NameNoExt(), qt.Equals, "index.no")
244 c.Assert(p.NameNoIdentifier(), qt.Equals, "index")
245 c.Assert(p.Path(), qt.Equals, "/a/b/index.no.md")
246 c.Assert(p.PathNoLang(), qt.Equals, "/a/b/index.md")
247 c.Assert(p.Section(), qt.Equals, "a")
248 },
249 },
250 {
251 "Index branch content file",
252 "/a/b/_index.no.md",
253 func(c *qt.C, p *Path) {
254 c.Assert(p.Base(), qt.Equals, "/a/b")
255 c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "b")
256 c.Assert(p.Container(), qt.Equals, "b")
257 c.Assert(p.ContainerDir(), qt.Equals, "/a")
258 c.Assert(p.Ext(), qt.Equals, "md")
259 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md", "no"})
260 c.Assert(p.IsBranchBundle(), qt.IsTrue)
261 c.Assert(p.IsBundle(), qt.IsTrue)
262 c.Assert(p.IsLeafBundle(), qt.IsFalse)
263 c.Assert(p.NameNoExt(), qt.Equals, "_index.no")
264 },
265 },
266 {
267 "Index root no slash",
268 "_index.md",
269 func(c *qt.C, p *Path) {
270 c.Assert(p.Base(), qt.Equals, "")
271 c.Assert(p.Ext(), qt.Equals, "md")
272 c.Assert(p.Name(), qt.Equals, "_index.md")
273 },
274 },
275 {
276 "Index root",
277 "/_index.md",
278 func(c *qt.C, p *Path) {
279 c.Assert(p.Base(), qt.Equals, "")
280 c.Assert(p.Ext(), qt.Equals, "md")
281 c.Assert(p.Name(), qt.Equals, "_index.md")
282 },
283 },
284 {
285 "Index first",
286 "/a/_index.md",
287 func(c *qt.C, p *Path) {
288 c.Assert(p.Section(), qt.Equals, "a")
289 },
290 },
291 {
292 "Index text file",
293 "/a/b/index.no.txt",
294 func(c *qt.C, p *Path) {
295 c.Assert(p.Base(), qt.Equals, "/a/b/index.txt")
296 c.Assert(p.Ext(), qt.Equals, "txt")
297 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "no"})
298 c.Assert(p.IsLeafBundle(), qt.IsFalse)
299 c.Assert(p.PathNoIdentifier(), qt.Equals, "/a/b/index")
300 },
301 },
302 {
303 "Empty",
304 "",
305 func(c *qt.C, p *Path) {
306 c.Assert(p.Base(), qt.Equals, "/")
307 c.Assert(p.Ext(), qt.Equals, "")
308 c.Assert(p.Name(), qt.Equals, "")
309 c.Assert(p.Path(), qt.Equals, "/")
310 },
311 },
312 {
313 "Slash",
314 "/",
315 func(c *qt.C, p *Path) {
316 c.Assert(p.Base(), qt.Equals, "/")
317 c.Assert(p.Ext(), qt.Equals, "")
318 c.Assert(p.Name(), qt.Equals, "")
319 },
320 },
321 {
322 "Trim Leading Slash bundle",
323 "foo/bar/index.no.md",
324 func(c *qt.C, p *Path) {
325 c.Assert(p.Path(), qt.Equals, "/foo/bar/index.no.md")
326 pp := p.TrimLeadingSlash()
327 c.Assert(pp.Path(), qt.Equals, "foo/bar/index.no.md")
328 c.Assert(pp.PathNoLang(), qt.Equals, "foo/bar/index.md")
329 c.Assert(pp.Base(), qt.Equals, "foo/bar")
330 c.Assert(pp.Dir(), qt.Equals, "foo/bar")
331 c.Assert(pp.ContainerDir(), qt.Equals, "foo")
332 c.Assert(pp.Container(), qt.Equals, "bar")
333 c.Assert(pp.BaseNameNoIdentifier(), qt.Equals, "bar")
334 },
335 },
336 {
337 "Trim Leading Slash file",
338 "foo/bar.txt",
339 func(c *qt.C, p *Path) {
340 c.Assert(p.Path(), qt.Equals, "/foo/bar.txt")
341 pp := p.TrimLeadingSlash()
342 c.Assert(pp.Path(), qt.Equals, "foo/bar.txt")
343 c.Assert(pp.PathNoLang(), qt.Equals, "foo/bar.txt")
344 c.Assert(pp.Base(), qt.Equals, "foo/bar.txt")
345 c.Assert(pp.Dir(), qt.Equals, "foo")
346 c.Assert(pp.ContainerDir(), qt.Equals, "foo")
347 c.Assert(pp.Container(), qt.Equals, "foo")
348 c.Assert(pp.BaseNameNoIdentifier(), qt.Equals, "bar")
349 },
350 },
351 {
352 "File separator",
353 filepath.FromSlash("/a/b/c.txt"),
354 func(c *qt.C, p *Path) {
355 c.Assert(p.Base(), qt.Equals, "/a/b/c.txt")
356 c.Assert(p.Ext(), qt.Equals, "txt")
357 c.Assert(p.Name(), qt.Equals, "c.txt")
358 c.Assert(p.Path(), qt.Equals, "/a/b/c.txt")
359 },
360 },
361 {
362 "Content data file gotmpl",
363 "/a/b/_content.gotmpl",
364 func(c *qt.C, p *Path) {
365 c.Assert(p.Path(), qt.Equals, "/a/b/_content.gotmpl")
366 c.Assert(p.Ext(), qt.Equals, "gotmpl")
367 c.Assert(p.IsContentData(), qt.IsTrue)
368 },
369 },
370 {
371 "Content data file yaml",
372 "/a/b/_content.yaml",
373 func(c *qt.C, p *Path) {
374 c.Assert(p.IsContentData(), qt.IsFalse)
375 },
376 },
377 {
378 "Custom identifier",
379 "/a/b/p1._myid_.no.md",
380 func(c *qt.C, p *Path) {
381 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
382 c.Assert(p.Lang(), qt.Equals, "no")
383 c.Assert(p.Ext(), qt.Equals, "md")
384 c.Assert(p.Custom(), qt.Equals, "myid")
385 },
386 },
387 {
388 "Prefix language",
389 "/a/b/p1._language_no_.md",
390 func(c *qt.C, p *Path) {
391 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
392 c.Assert(p.Lang(), qt.Equals, "no")
393 c.Assert(p.Ext(), qt.Equals, "md")
394 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md", "no"})
395 },
396 },
397 {
398 "Prefix version",
399 "/a/b/p1._version_v1_.md",
400 func(c *qt.C, p *Path) {
401 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
402 c.Assert(p.Versions(), qt.DeepEquals, []string{"v1"})
403 c.Assert(p.Ext(), qt.Equals, "md")
404 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md", "v1"})
405 },
406 },
407 {
408 "Prefix version with dots",
409 "/a/b/p1._version_v1.0.0_.md",
410 func(c *qt.C, p *Path) {
411 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
412 c.Assert(p.Versions(), qt.DeepEquals, []string{"v1.0.0"})
413 c.Assert(p.Ext(), qt.Equals, "md")
414 },
415 },
416 {
417 "Prefix role",
418 "/a/b/p1._role_admin_.md",
419 func(c *qt.C, p *Path) {
420 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
421 c.Assert(p.Roles(), qt.DeepEquals, []string{"admin"})
422 c.Assert(p.Ext(), qt.Equals, "md")
423 },
424 },
425 {
426 "Multiple prefixes",
427 "/a/b/p1._language_no_._role_admin_._version_v2_.md",
428 func(c *qt.C, p *Path) {
429 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
430 c.Assert(p.Lang(), qt.Equals, "no")
431 c.Assert(p.Roles(), qt.DeepEquals, []string{"admin"})
432 c.Assert(p.Versions(), qt.DeepEquals, []string{"v2"})
433 c.Assert(p.Ext(), qt.Equals, "md")
434 },
435 },
436 {
437 "Prefix version with dots and role",
438 "/a/b/p1._version_v1.0.0_._role_editor_.md",
439 func(c *qt.C, p *Path) {
440 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
441 c.Assert(p.Versions(), qt.DeepEquals, []string{"v1.0.0"})
442 c.Assert(p.Roles(), qt.DeepEquals, []string{"editor"})
443 c.Assert(p.Ext(), qt.Equals, "md")
444 },
445 },
446 {
447 "Prefix with dot-based lang",
448 "/a/b/p1._version_v1_.no.md",
449 func(c *qt.C, p *Path) {
450 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
451 c.Assert(p.Versions(), qt.DeepEquals, []string{"v1"})
452 c.Assert(p.Lang(), qt.Equals, "no")
453 c.Assert(p.Ext(), qt.Equals, "md")
454 },
455 },
456 {
457 "Prefix leaf bundle with version",
458 "/a/b/index._version_v1_.md",
459 func(c *qt.C, p *Path) {
460 c.Assert(p.IsLeafBundle(), qt.IsTrue)
461 c.Assert(p.Versions(), qt.DeepEquals, []string{"v1"})
462 c.Assert(p.Base(), qt.Equals, "/a/b")
463 c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "b")
464 },
465 },
466 {
467 "Prefix branch bundle with role",
468 "/a/b/_index._role_admin_.md",
469 func(c *qt.C, p *Path) {
470 c.Assert(p.IsBranchBundle(), qt.IsTrue)
471 c.Assert(p.Roles(), qt.DeepEquals, []string{"admin"})
472 c.Assert(p.Base(), qt.Equals, "/a/b")
473 },
474 },
475 {
476 "Prefix with mixed case, unnormalized",
477 "/a/b/My Page._Version_V1_.md",
478 func(c *qt.C, p *Path) {
479 c.Assert(p.Base(), qt.Equals, "/a/b/my-page")
480 c.Assert(p.Versions(), qt.DeepEquals, []string{"v1"})
481 pp := p.Unnormalized()
482 c.Assert(pp.BaseNameNoIdentifier(), qt.Equals, "My Page")
483 },
484 },
485 {
486 "Multiple languages",
487 "/a/b/p1._language_en_._language_fr_.md",
488 func(c *qt.C, p *Path) {
489 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
490 // Right-to-left parse order.
491 c.Assert(p.Lang(), qt.Equals, "fr")
492 c.Assert(p.Langs(), qt.DeepEquals, []string{"fr", "en"})
493 c.Assert(p.Ext(), qt.Equals, "md")
494 },
495 },
496 {
497 "Multiple roles",
498 "/a/b/p1._role_guest_._role_admin_.md",
499 func(c *qt.C, p *Path) {
500 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
501 c.Assert(p.Roles(), qt.DeepEquals, []string{"admin", "guest"})
502 c.Assert(p.Ext(), qt.Equals, "md")
503 },
504 },
505 {
506 "Multiple versions",
507 "/a/b/p1._version_v1_._version_v2_.md",
508 func(c *qt.C, p *Path) {
509 c.Assert(p.Base(), qt.Equals, "/a/b/p1")
510 c.Assert(p.Versions(), qt.DeepEquals, []string{"v2", "v1"})
511 },
512 },
513 {
514 "Unknown prefix not extracted",
515 "/a/b/p1._unknown_foo_.md",
516 func(c *qt.C, p *Path) {
517 // Unknown prefix should be left in path and treated as custom wrapper.
518 c.Assert(p.Custom(), qt.Equals, "unknown_foo")
519 c.Assert(p.Versions(), qt.IsNil)
520 c.Assert(p.Roles(), qt.IsNil)
521 },
522 },
523 }
524 parser := newTestParser()
525 for _, test := range tests {
526 c.Run(test.name, func(c *qt.C) {
527 if test.name != "Caret up identifier" {
528 // return
529 }
530 test.assert(c, parser.Parse(files.ComponentFolderContent, test.path))
531 })
532 }
533 }
534
535 func TestParseLayouts(t *testing.T) {
536 c := qt.New(t)
537
538 tests := []struct {
539 name string
540 path string
541 assert func(c *qt.C, p *Path)
542 }{
543 {
544 "Basic",
545 "/list.html",
546 func(c *qt.C, p *Path) {
547 c.Assert(p.Base(), qt.Equals, "/list.html")
548 c.Assert(p.OutputFormat(), qt.Equals, "html")
549 },
550 },
551 {
552 "Lang",
553 "/list.no.html",
554 func(c *qt.C, p *Path) {
555 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "no", "list"})
556 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{})
557 c.Assert(p.Base(), qt.Equals, "/list.html")
558 c.Assert(p.Lang(), qt.Equals, "no")
559 },
560 },
561 {
562 "Kind",
563 "/section.no.html",
564 func(c *qt.C, p *Path) {
565 c.Assert(p.Kind(), qt.Equals, kinds.KindSection)
566 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "no", "section"})
567 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{})
568 c.Assert(p.Base(), qt.Equals, "/section.html")
569 c.Assert(p.Lang(), qt.Equals, "no")
570 },
571 },
572 {
573 "Layout",
574 "/list.section.no.html",
575 func(c *qt.C, p *Path) {
576 c.Assert(p.Layout(), qt.Equals, "list")
577 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "no", "section", "list"})
578 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{})
579 c.Assert(p.Base(), qt.Equals, "/list.html")
580 c.Assert(p.Lang(), qt.Equals, "no")
581 },
582 },
583 {
584 "Layout multiple",
585 "/mylayout.list.section.no.html",
586 func(c *qt.C, p *Path) {
587 c.Assert(p.Layout(), qt.Equals, "mylayout")
588 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "no", "section", "mylayout"})
589 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{"list"})
590 c.Assert(p.Base(), qt.Equals, "/mylayout.html")
591 c.Assert(p.Lang(), qt.Equals, "no")
592 },
593 },
594 {
595 "Layout shortcode",
596 "/_shortcodes/myshort.list.no.html",
597 func(c *qt.C, p *Path) {
598 c.Assert(p.Layout(), qt.Equals, "list")
599 },
600 },
601 {
602 "Layout baseof",
603 "/baseof.list.no.html",
604 func(c *qt.C, p *Path) {
605 c.Assert(p.Layout(), qt.Equals, "list")
606 },
607 },
608 {
609 "Lang and output format",
610 "/list.no.amp.not.html",
611 func(c *qt.C, p *Path) {
612 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "list", "amp", "no"})
613 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{"not"})
614 c.Assert(p.OutputFormat(), qt.Equals, "amp")
615 c.Assert(p.Ext(), qt.Equals, "html")
616 c.Assert(p.Lang(), qt.Equals, "no")
617 c.Assert(p.Base(), qt.Equals, "/list.html")
618 },
619 },
620 {
621 "Term",
622 "/term.html",
623 func(c *qt.C, p *Path) {
624 c.Assert(p.Base(), qt.Equals, "/term.html")
625 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "term"})
626 c.Assert(p.PathNoIdentifier(), qt.Equals, "/term")
627 c.Assert(p.PathBeforeLangAndOutputFormatAndExt(), qt.Equals, "/term")
628 c.Assert(p.Lang(), qt.Equals, "")
629 c.Assert(p.Kind(), qt.Equals, "term")
630 c.Assert(p.OutputFormat(), qt.Equals, "html")
631 },
632 },
633 {
634 "Shortcode with layout",
635 "/_shortcodes/myshortcode.list.html",
636 func(c *qt.C, p *Path) {
637 c.Assert(p.Base(), qt.Equals, "/_shortcodes/myshortcode.html")
638 c.Assert(p.Type(), qt.Equals, TypeShortcode)
639 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "list"})
640 c.Assert(p.Layout(), qt.Equals, "list")
641 c.Assert(p.PathNoIdentifier(), qt.Equals, "/_shortcodes/myshortcode")
642 c.Assert(p.PathBeforeLangAndOutputFormatAndExt(), qt.Equals, "/_shortcodes/myshortcode.list")
643 c.Assert(p.Lang(), qt.Equals, "")
644 c.Assert(p.Kind(), qt.Equals, "")
645 c.Assert(p.OutputFormat(), qt.Equals, "html")
646 },
647 },
648 {
649 "Sub dir",
650 "/pages/home.html",
651 func(c *qt.C, p *Path) {
652 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "home"})
653 c.Assert(p.Lang(), qt.Equals, "")
654 c.Assert(p.Kind(), qt.Equals, "home")
655 c.Assert(p.OutputFormat(), qt.Equals, "html")
656 c.Assert(p.Dir(), qt.Equals, "/pages")
657 },
658 },
659 {
660 "Baseof",
661 "/pages/baseof.list.section.fr.amp.html",
662 func(c *qt.C, p *Path) {
663 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "amp", "fr", "section", "list", "baseof"})
664 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{})
665 c.Assert(p.Kind(), qt.Equals, kinds.KindSection)
666 c.Assert(p.Lang(), qt.Equals, "fr")
667 c.Assert(p.OutputFormat(), qt.Equals, "amp")
668 c.Assert(p.Dir(), qt.Equals, "/pages")
669 c.Assert(p.NameNoIdentifier(), qt.Equals, "baseof")
670 c.Assert(p.Type(), qt.Equals, TypeBaseof)
671 c.Assert(p.IdentifierBase(), qt.Equals, "/pages/baseof.list.section.fr.amp.html")
672 },
673 },
674 {
675 "Markup",
676 "/_markup/render-link.html",
677 func(c *qt.C, p *Path) {
678 c.Assert(p.Type(), qt.Equals, TypeMarkup)
679 },
680 },
681 {
682 "Markup nested",
683 "/foo/_markup/render-link.html",
684 func(c *qt.C, p *Path) {
685 c.Assert(p.Type(), qt.Equals, TypeMarkup)
686 },
687 },
688 {
689 "Shortcode",
690 "/_shortcodes/myshortcode.html",
691 func(c *qt.C, p *Path) {
692 c.Assert(p.Type(), qt.Equals, TypeShortcode)
693 },
694 },
695 {
696 "Shortcode nested",
697 "/foo/_shortcodes/myshortcode.html",
698 func(c *qt.C, p *Path) {
699 c.Assert(p.Type(), qt.Equals, TypeShortcode)
700 },
701 },
702 {
703 "Shortcode nested sub",
704 "/foo/_shortcodes/foo/myshortcode.html",
705 func(c *qt.C, p *Path) {
706 c.Assert(p.Type(), qt.Equals, TypeShortcode)
707 },
708 },
709 {
710 "Partials",
711 "/_partials/foo.bar",
712 func(c *qt.C, p *Path) {
713 c.Assert(p.Type(), qt.Equals, TypePartial)
714 },
715 },
716 {
717 "Shortcode lang in root",
718 "/_shortcodes/no.html",
719 func(c *qt.C, p *Path) {
720 c.Assert(p.Type(), qt.Equals, TypeShortcode)
721 c.Assert(p.Lang(), qt.Equals, "")
722 c.Assert(p.NameNoIdentifier(), qt.Equals, "no")
723 },
724 },
725 {
726 "Shortcode lang layout",
727 "/_shortcodes/myshortcode.no.html",
728 func(c *qt.C, p *Path) {
729 c.Assert(p.Type(), qt.Equals, TypeShortcode)
730 c.Assert(p.Lang(), qt.Equals, "no")
731 c.Assert(p.Layout(), qt.Equals, "")
732 c.Assert(p.NameNoIdentifier(), qt.Equals, "myshortcode")
733 },
734 },
735 {
736 "Not lang",
737 "/foo/index.xy.html",
738 func(c *qt.C, p *Path) {
739 c.Assert(p.Lang(), qt.Equals, "")
740 c.Assert(p.Layout(), qt.Equals, "index")
741 c.Assert(p.PathNoLang(), qt.Equals, "/foo/index.xy.html")
742 c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "index"})
743 c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{"xy"})
744 },
745 },
746 {
747 "Prefix language layout",
748 "/page._language_no_.html",
749 func(c *qt.C, p *Path) {
750 c.Assert(p.Lang(), qt.Equals, "no")
751 c.Assert(p.Ext(), qt.Equals, "html")
752 c.Assert(p.OutputFormat(), qt.Equals, "html")
753 c.Assert(p.Base(), qt.Equals, "/page.html")
754 },
755 },
756 {
757 "Prefix outputformat layout",
758 "/page._outputformat_amp_.html",
759 func(c *qt.C, p *Path) {
760 c.Assert(p.OutputFormat(), qt.Equals, "amp")
761 c.Assert(p.Ext(), qt.Equals, "html")
762 c.Assert(p.Base(), qt.Equals, "/page.html")
763 },
764 },
765 {
766 "Prefix kind layout",
767 "/page._kind_section_.html",
768 func(c *qt.C, p *Path) {
769 c.Assert(p.Kind(), qt.Equals, kinds.KindSection)
770 c.Assert(p.Ext(), qt.Equals, "html")
771 c.Assert(p.Base(), qt.Equals, "/page.html")
772 },
773 },
774 {
775 "Prefix layout layout",
776 "/page._layout_list_.html",
777 func(c *qt.C, p *Path) {
778 c.Assert(p.Layout(), qt.Equals, "list")
779 c.Assert(p.Ext(), qt.Equals, "html")
780 c.Assert(p.Base(), qt.Equals, "/page.html")
781 },
782 },
783 {
784 "All prefix identifiers in layout",
785 "/page._language_fr_._kind_section_._outputformat_amp_._layout_list_.html",
786 func(c *qt.C, p *Path) {
787 c.Assert(p.Lang(), qt.Equals, "fr")
788 c.Assert(p.Kind(), qt.Equals, kinds.KindSection)
789 c.Assert(p.OutputFormat(), qt.Equals, "amp")
790 c.Assert(p.Layout(), qt.Equals, "list")
791 c.Assert(p.Ext(), qt.Equals, "html")
792 c.Assert(p.Base(), qt.Equals, "/page.html")
793 },
794 },
795 {
796 "Prefix version in layout",
797 "/page._version_v2_.html",
798 func(c *qt.C, p *Path) {
799 c.Assert(p.Versions(), qt.DeepEquals, []string{"v2"})
800 c.Assert(p.Base(), qt.Equals, "/page.html")
801 },
802 },
803 {
804 "Prefix role in layout",
805 "/page._role_guest_.html",
806 func(c *qt.C, p *Path) {
807 c.Assert(p.Roles(), qt.DeepEquals, []string{"guest"})
808 c.Assert(p.Base(), qt.Equals, "/page.html")
809 },
810 },
811 }
812
813 parser := newTestParser()
814 for _, test := range tests {
815 c.Run(test.name, func(c *qt.C) {
816 test.assert(c, parser.Parse(files.ComponentFolderLayouts, test.path))
817 })
818 }
819 }
820
821 func TestHasExt(t *testing.T) {
822 c := qt.New(t)
823
824 c.Assert(HasExt("/a/b/c.txt"), qt.IsTrue)
825 c.Assert(HasExt("/a/b.c/d.txt"), qt.IsTrue)
826 c.Assert(HasExt("/a/b/c"), qt.IsFalse)
827 c.Assert(HasExt("/a/b.c/d"), qt.IsFalse)
828 }
829
830 func TestSitesMatrixFromPath(t *testing.T) {
831 c := qt.New(t)
832
833 parser := newTestParser()
834
835 p := parser.Parse(files.ComponentFolderContent, "/a/b/c.fr.md")
836 v := parser.SitesMatrixFromPath(p)
837 c.Assert(v.HasLanguage(2), qt.IsTrue)
838 c.Assert(v.LenVectors(), qt.Equals, 1)
839 c.Assert(v.VectorSample(), qt.Equals, sitesmatrix.Vector{2, 0, 0})
840
841 // With version prefix.
842 p = parser.Parse(files.ComponentFolderContent, "/a/b/c._version_v2_.fr.md")
843 v = parser.SitesMatrixFromPath(p)
844 c.Assert(v.HasLanguage(2), qt.IsTrue)
845 c.Assert(v.HasVersion(1), qt.IsTrue) // v2 is index 1
846 c.Assert(v.LenVectors(), qt.Equals, 1)
847 c.Assert(v.VectorSample(), qt.Equals, sitesmatrix.Vector{2, 1, 0})
848
849 // With version and role prefixes.
850 p = parser.Parse(files.ComponentFolderContent, "/a/b/c._version_v1_._role_editor_.fr.md")
851 v = parser.SitesMatrixFromPath(p)
852 c.Assert(v.HasLanguage(2), qt.IsTrue)
853 c.Assert(v.HasVersion(0), qt.IsTrue) // v1 is index 0
854 c.Assert(v.HasRole(1), qt.IsTrue) // editor is index 1
855 c.Assert(v.LenVectors(), qt.Equals, 1)
856 c.Assert(v.VectorSample(), qt.Equals, sitesmatrix.Vector{2, 0, 1})
857
858 // With multiple roles.
859 p = parser.Parse(files.ComponentFolderContent, "/a/b/c._role_guest_._role_admin_.fr.md")
860 v = parser.SitesMatrixFromPath(p)
861 c.Assert(v.HasLanguage(2), qt.IsTrue)
862 c.Assert(v.HasRole(0), qt.IsTrue) // admin is index 0
863 c.Assert(v.HasRole(3), qt.IsTrue) // guest is index 3
864 c.Assert(v.LenVectors(), qt.Equals, 2)
865
866 // With multiple languages via prefix.
867 p = parser.Parse(files.ComponentFolderContent, "/a/b/c._language_en_._language_fr_.md")
868 v = parser.SitesMatrixFromPath(p)
869 c.Assert(v.HasLanguage(0), qt.IsFalse) // no is index 0, not included
870 c.Assert(v.HasLanguage(1), qt.IsTrue) // en is index 1
871 c.Assert(v.HasLanguage(2), qt.IsTrue) // fr is index 2
872 c.Assert(v.LenVectors(), qt.Equals, 2)
873 }
874
875 func FuzzParsePath(f *testing.F) {
876 componentPaths := []struct {
877 component string
878 path string
879 }{
880 {files.ComponentFolderContent, "/a/b/c.fr.md"},
881 {files.ComponentFolderContent, "/a/b/c._version_v2_.fr.md"},
882 {files.ComponentFolderContent, "/a/b/c._version_v1_._role_editor_.fr.md"},
883 {files.ComponentFolderContent, "/a/b/c._role_guest_._role_admin_.fr.md"},
884 {files.ComponentFolderContent, "/a/b/c._language_en_._language_fr_.md"},
885 {files.ComponentFolderLayouts, "/list.no.html"},
886 {files.ComponentFolderLayouts, "/page._language_fr_._kind_section_._outputformat_amp_._layout_list_.html"},
887 }
888
889 for _, cp := range componentPaths {
890 f.Add(cp.component, cp.path)
891 }
892
893 parser := newTestParser()
894
895 f.Fuzz(func(t *testing.T, c, s string) {
896 p := parser.Parse(c, s)
897 if p == nil {
898 t.Fatalf("Parse returned nil for path: %q", s)
899 }
900 // Execute all the methods using reflection to ensure they don't panic.
901 v := reflect.ValueOf(p)
902 for i := 0; i < v.NumMethod(); i++ {
903 method := v.Type().Method(i)
904 if method.Type.NumIn() == 1 {
905 method.Func.Call([]reflect.Value{v})
906 }
907 }
908 })
909 }
910
911 func BenchmarkSitesMatrixFromPath(b *testing.B) {
912 parser := newTestParser()
913 p := parser.Parse(files.ComponentFolderContent, "/a/b/c.fr.md")
914 for b.Loop() {
915 parser.SitesMatrixFromPath(p)
916 }
917 }