Debian Codegraph @

hugolib/filesystems/basefs_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 filesystems_test
15
16 import (
17 "context"
18 "errors"
19 "fmt"
20 "os"
21 "path/filepath"
22 "runtime"
23 "strings"
24 "testing"
25
26 "github.com/gohugoio/hugo/config"
27 "github.com/gohugoio/hugo/config/testconfig"
28 "github.com/gohugoio/hugo/hugolib"
29
30 "github.com/spf13/afero"
31
32 qt "github.com/frankban/quicktest"
33 "github.com/gohugoio/hugo/hugofs"
34 "github.com/gohugoio/hugo/hugolib/filesystems"
35 "github.com/gohugoio/hugo/hugolib/paths"
36 )
37
38 func TestNewBaseFs(t *testing.T) {
39 c := qt.New(t)
40 v := config.New()
41
42 themes := []string{"btheme", "atheme"}
43
44 workingDir := filepath.FromSlash("/my/work")
45 v.Set("workingDir", workingDir)
46 v.Set("contentDir", "content")
47 v.Set("themesDir", "themes")
48 v.Set("defaultContentLanguage", "en")
49 v.Set("theme", themes[:1])
50 v.Set("publishDir", "public")
51
52 afs := afero.NewMemMapFs()
53
54 // Write some data to the themes
55 for _, theme := range themes {
56 for _, dir := range []string{"i18n", "data", "archetypes", "layouts"} {
57 base := filepath.Join(workingDir, "themes", theme, dir)
58 filenameTheme := filepath.Join(base, fmt.Sprintf("theme-file-%s.txt", theme))
59 filenameOverlap := filepath.Join(base, "f3.txt")
60 afs.Mkdir(base, 0o755)
61 content := fmt.Appendf(nil, "content:%s:%s", theme, dir)
62 afero.WriteFile(afs, filenameTheme, content, 0o755)
63 afero.WriteFile(afs, filenameOverlap, content, 0o755)
64 }
65 // Write some files to the root of the theme
66 base := filepath.Join(workingDir, "themes", theme)
67 afero.WriteFile(afs, filepath.Join(base, fmt.Sprintf("theme-root-%s.txt", theme)), fmt.Appendf(nil, "content:%s", theme), 0o755)
68 afero.WriteFile(afs, filepath.Join(base, "file-theme-root.txt"), fmt.Appendf(nil, "content:%s", theme), 0o755)
69 }
70
71 afero.WriteFile(afs, filepath.Join(workingDir, "file-root.txt"), []byte("content-project"), 0o755)
72
73 afero.WriteFile(afs, filepath.Join(workingDir, "themes", "btheme", "config.toml"), []byte(`
74 theme = ["atheme"]
75 `), 0o755)
76
77 setConfigAndWriteSomeFilesTo(afs, v, "contentDir", "mycontent", 3)
78 setConfigAndWriteSomeFilesTo(afs, v, "i18nDir", "myi18n", 4)
79 setConfigAndWriteSomeFilesTo(afs, v, "layoutDir", "mylayouts", 5)
80 setConfigAndWriteSomeFilesTo(afs, v, "staticDir", "mystatic", 6)
81 setConfigAndWriteSomeFilesTo(afs, v, "dataDir", "mydata", 7)
82 setConfigAndWriteSomeFilesTo(afs, v, "archetypeDir", "myarchetypes", 8)
83 setConfigAndWriteSomeFilesTo(afs, v, "assetDir", "myassets", 9)
84 setConfigAndWriteSomeFilesTo(afs, v, "resourceDir", "myrsesource", 10)
85
86 conf := testconfig.GetTestConfig(afs, v)
87 fs := hugofs.NewFrom(afs, conf.BaseConfig())
88
89 p, err := paths.New(fs, conf)
90 c.Assert(err, qt.IsNil)
91
92 bfs, err := filesystems.NewBase(p, nil)
93 c.Assert(err, qt.IsNil)
94 c.Assert(bfs, qt.Not(qt.IsNil))
95
96 root, err := bfs.I18n.Fs.Open("")
97 c.Assert(err, qt.IsNil)
98 dirnames, err := root.Readdirnames(-1)
99 c.Assert(err, qt.IsNil)
100 c.Assert(dirnames, qt.DeepEquals, []string{"f1.txt", "f2.txt", "f3.txt", "f4.txt", "f3.txt", "theme-file-btheme.txt", "f3.txt", "theme-file-atheme.txt"})
101
102 root, err = bfs.Data.Fs.Open("")
103 c.Assert(err, qt.IsNil)
104 dirnames, err = root.Readdirnames(-1)
105 c.Assert(err, qt.IsNil)
106 c.Assert(dirnames, qt.DeepEquals, []string{"f1.txt", "f2.txt", "f3.txt", "f4.txt", "f5.txt", "f6.txt", "f7.txt", "f3.txt", "theme-file-btheme.txt", "f3.txt", "theme-file-atheme.txt"})
107
108 checkFileCount(bfs.Layouts.Fs, "", c, 7)
109
110 checkFileCount(bfs.Content.Fs, "", c, 3)
111 checkFileCount(bfs.I18n.Fs, "", c, 8) // 4 + 4 themes
112
113 checkFileCount(bfs.Static[""].Fs, "", c, 6)
114 checkFileCount(bfs.Data.Fs, "", c, 11) // 7 + 4 themes
115 checkFileCount(bfs.Archetypes.Fs, "", c, 10) // 8 + 2 themes
116 checkFileCount(bfs.Assets.Fs, "", c, 9)
117 checkFileCount(bfs.Work, "", c, 90)
118
119 c.Assert(bfs.IsStatic(filepath.Join(workingDir, "mystatic", "file1.txt")), qt.Equals, true)
120
121 contentFilename := filepath.Join(workingDir, "mycontent", "file1.txt")
122 c.Assert(bfs.IsContent(contentFilename), qt.Equals, true)
123 // Check Work fs vs theme
124 checkFileContent(bfs.Work, "file-root.txt", c, "content-project")
125 checkFileContent(bfs.Work, "theme-root-atheme.txt", c, "content:atheme")
126
127 // https://github.com/gohugoio/hugo/issues/5318
128 // Check both project and theme.
129 for _, fs := range []afero.Fs{bfs.Archetypes.Fs, bfs.Layouts.Fs} {
130 for _, filename := range []string{"/f1.txt", "/theme-file-atheme.txt"} {
131 filename = filepath.FromSlash(filename)
132 f, err := fs.Open(filename)
133 c.Assert(err, qt.IsNil)
134 f.Close()
135 }
136 }
137 }
138
139 func TestNewBaseFsEmpty(t *testing.T) {
140 c := qt.New(t)
141 afs := afero.NewMemMapFs()
142 conf := testconfig.GetTestConfig(afs, nil)
143 fs := hugofs.NewFrom(afs, conf.BaseConfig())
144 p, err := paths.New(fs, conf)
145 c.Assert(err, qt.IsNil)
146 bfs, err := filesystems.NewBase(p, nil)
147 c.Assert(err, qt.IsNil)
148 c.Assert(bfs, qt.Not(qt.IsNil))
149 c.Assert(bfs.Archetypes.Fs, qt.Not(qt.IsNil))
150 c.Assert(bfs.Layouts.Fs, qt.Not(qt.IsNil))
151 c.Assert(bfs.Data.Fs, qt.Not(qt.IsNil))
152 c.Assert(bfs.I18n.Fs, qt.Not(qt.IsNil))
153 c.Assert(bfs.Work, qt.Not(qt.IsNil))
154 c.Assert(bfs.Content.Fs, qt.Not(qt.IsNil))
155 c.Assert(bfs.Static, qt.Not(qt.IsNil))
156 }
157
158 func TestRealDirs(t *testing.T) {
159 c := qt.New(t)
160 v := config.New()
161 root, themesDir := t.TempDir(), t.TempDir()
162 v.Set("workingDir", root)
163 v.Set("themesDir", themesDir)
164 v.Set("assetDir", "myassets")
165 v.Set("theme", "mytheme")
166
167 afs := &hugofs.OpenFilesFs{Fs: hugofs.Os}
168
169 c.Assert(afs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf1"), 0o755), qt.IsNil)
170 c.Assert(afs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf2"), 0o755), qt.IsNil)
171 c.Assert(afs.MkdirAll(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf2"), 0o755), qt.IsNil)
172 c.Assert(afs.MkdirAll(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf3"), 0o755), qt.IsNil)
173 c.Assert(afs.MkdirAll(filepath.Join(root, "resources"), 0o755), qt.IsNil)
174 c.Assert(afs.MkdirAll(filepath.Join(themesDir, "mytheme", "resources"), 0o755), qt.IsNil)
175
176 c.Assert(afs.MkdirAll(filepath.Join(root, "myassets", "js", "f2"), 0o755), qt.IsNil)
177
178 afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "scss", "sf1", "a1.scss")), []byte("content"), 0o755)
179 afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "scss", "sf2", "a3.scss")), []byte("content"), 0o755)
180 afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "scss", "a2.scss")), []byte("content"), 0o755)
181 afero.WriteFile(afs, filepath.Join(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf2", "a3.scss")), []byte("content"), 0o755)
182 afero.WriteFile(afs, filepath.Join(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf3", "a4.scss")), []byte("content"), 0o755)
183
184 afero.WriteFile(afs, filepath.Join(filepath.Join(themesDir, "mytheme", "resources", "t1.txt")), []byte("content"), 0o755)
185 afero.WriteFile(afs, filepath.Join(filepath.Join(root, "resources", "p1.txt")), []byte("content"), 0o755)
186 afero.WriteFile(afs, filepath.Join(filepath.Join(root, "resources", "p2.txt")), []byte("content"), 0o755)
187
188 afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "js", "f2", "a1.js")), []byte("content"), 0o755)
189 afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "js", "a2.js")), []byte("content"), 0o755)
190
191 conf := testconfig.GetTestConfig(afs, v)
192 fs := hugofs.NewFrom(afs, conf.BaseConfig())
193 p, err := paths.New(fs, conf)
194 c.Assert(err, qt.IsNil)
195 bfs, err := filesystems.NewBase(p, nil)
196 c.Assert(err, qt.IsNil)
197 c.Assert(bfs, qt.Not(qt.IsNil))
198
199 checkFileCount(bfs.Assets.Fs, "", c, 6)
200
201 realDirs := bfs.Assets.RealDirs("scss")
202 c.Assert(len(realDirs), qt.Equals, 2)
203 c.Assert(realDirs[0], qt.Equals, filepath.Join(root, "myassets/scss"))
204 c.Assert(realDirs[len(realDirs)-1], qt.Equals, filepath.Join(themesDir, "mytheme/assets/scss"))
205
206 realDirs = bfs.Assets.RealDirs("foo")
207 c.Assert(len(realDirs), qt.Equals, 0)
208
209 c.Assert(afs.OpenFiles(), qt.HasLen, 0)
210 }
211
212 func TestWatchFilenames(t *testing.T) {
213 t.Parallel()
214 files := `
215 -- hugo.toml --
216 theme = "t1"
217 [[module.mounts]]
218 source = 'content'
219 target = 'content'
220 [[module.mounts]]
221 source = 'content2'
222 target = 'content/c2'
223 [[module.mounts]]
224 source = 'content3'
225 target = 'content/watchdisabled'
226 disableWatch = true
227 [[module.mounts]]
228 source = 'content4'
229 target = 'content/excludedsome'
230 files = '! p1.md'
231 [[module.mounts]]
232 source = 'content5'
233 target = 'content/excludedall'
234 files = '! /**'
235 [[module.mounts]]
236 source = "hugo_stats.json"
237 target = "assets/watching/hugo_stats.json"
238 -- hugo_stats.json --
239 Some stats.
240 -- content/foo.md --
241 foo
242 -- content2/bar.md --
243 -- themes/t1/layouts/_default/single.html --
244 {{ .Content }}
245 -- themes/t1/static/f1.txt --
246 -- content3/p1.md --
247 -- content4/p1.md --
248 -- content4/p2.md --
249 -- content5/p3.md --
250 -- content5/p4.md --
251 `
252 b := hugolib.Test(t, files)
253 bfs := b.H.BaseFs
254 watchFilenames := toSlashes(bfs.WatchFilenames())
255
256 // content3 has disableWatch = true
257 // content5 has excludeFiles = '/**'
258 b.Assert(watchFilenames, qt.DeepEquals, []string{"/hugo_stats.json", "/content", "/content2", "/content4", "/themes/t1/layouts", "/themes/t1/layouts/_default", "/themes/t1/static"})
259 }
260
261 func toSlashes(in []string) []string {
262 out := make([]string, len(in))
263 for i, s := range in {
264 out[i] = filepath.ToSlash(s)
265 }
266 return out
267 }
268
269 func TestNoSymlinks(t *testing.T) {
270 if runtime.GOOS == "windows" {
271 t.Skip("skip on Windows")
272 }
273 files := `
274 -- hugo.toml --
275 theme = "t1"
276 -- content/a/foo.md --
277 foo
278 -- static/a/f1.txt --
279 F1 text
280 -- themes/t1/layouts/_default/single.html --
281 {{ .Content }}
282 -- themes/t1/static/a/f1.txt --
283 `
284 tmpDir := t.TempDir()
285
286 wd, _ := os.Getwd()
287
288 for _, component := range []string{"content", "static"} {
289 aDir := filepath.Join(tmpDir, component, "a")
290 bDir := filepath.Join(tmpDir, component, "b")
291 os.MkdirAll(aDir, 0o755)
292 os.MkdirAll(bDir, 0o755)
293 os.Chdir(bDir)
294 os.Symlink("../a", "c")
295 }
296
297 os.Chdir(wd)
298
299 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) {
300 c.WorkingDir = tmpDir
301 }))
302
303 bfs := b.H.BaseFs
304 watchFilenames := bfs.WatchFilenames()
305 b.Assert(watchFilenames, qt.HasLen, 10)
306 }
307
308 func TestStaticFs(t *testing.T) {
309 c := qt.New(t)
310 v := config.New()
311 workDir := "mywork"
312 v.Set("workingDir", workDir)
313 v.Set("themesDir", "themes")
314 v.Set("staticDir", "mystatic")
315 v.Set("theme", []string{"t1", "t2"})
316
317 afs := afero.NewMemMapFs()
318
319 themeStaticDir := filepath.Join(workDir, "themes", "t1", "static")
320 themeStaticDir2 := filepath.Join(workDir, "themes", "t2", "static")
321
322 afero.WriteFile(afs, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0o755)
323 afero.WriteFile(afs, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0o755)
324 afero.WriteFile(afs, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0o755)
325 afero.WriteFile(afs, filepath.Join(themeStaticDir2, "f2.txt"), []byte("Hugo Themes Rocks in t2!"), 0o755)
326
327 conf := testconfig.GetTestConfig(afs, v)
328 fs := hugofs.NewFrom(afs, conf.BaseConfig())
329 p, err := paths.New(fs, conf)
330
331 c.Assert(err, qt.IsNil)
332 bfs, err := filesystems.NewBase(p, nil)
333 c.Assert(err, qt.IsNil)
334
335 sfs := bfs.StaticFs("en")
336
337 checkFileContent(sfs, "f1.txt", c, "Hugo Rocks!")
338 checkFileContent(sfs, "f2.txt", c, "Hugo Themes Still Rocks!")
339 }
340
341 func TestStaticFsMultihost(t *testing.T) {
342 c := qt.New(t)
343 v := config.New()
344 workDir := "mywork"
345 v.Set("workingDir", workDir)
346 v.Set("themesDir", "themes")
347 v.Set("staticDir", "mystatic")
348 v.Set("theme", "t1")
349 v.Set("defaultContentLanguage", "en")
350
351 langConfig := map[string]any{
352 "no": map[string]any{
353 "staticDir": "static_no",
354 "baseURL": "https://example.org/no/",
355 },
356 "en": map[string]any{
357 "baseURL": "https://example.org/en/",
358 },
359 }
360
361 v.Set("languages", langConfig)
362
363 afs := afero.NewMemMapFs()
364
365 themeStaticDir := filepath.Join(workDir, "themes", "t1", "static")
366
367 afero.WriteFile(afs, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0o755)
368 afero.WriteFile(afs, filepath.Join(workDir, "static_no", "f1.txt"), []byte("Hugo Rocks in Norway!"), 0o755)
369
370 afero.WriteFile(afs, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0o755)
371 afero.WriteFile(afs, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0o755)
372
373 conf := testconfig.GetTestConfig(afs, v)
374 fs := hugofs.NewFrom(afs, conf.BaseConfig())
375
376 p, err := paths.New(fs, conf)
377 c.Assert(err, qt.IsNil)
378 bfs, err := filesystems.NewBase(p, nil)
379 c.Assert(err, qt.IsNil)
380 enFs := bfs.StaticFs("en")
381 checkFileContent(enFs, "f1.txt", c, "Hugo Rocks!")
382 checkFileContent(enFs, "f2.txt", c, "Hugo Themes Still Rocks!")
383
384 noFs := bfs.StaticFs("no")
385 checkFileContent(noFs, "f1.txt", c, "Hugo Rocks in Norway!")
386 checkFileContent(noFs, "f2.txt", c, "Hugo Themes Still Rocks!")
387 }
388
389 func TestMakePathRelative(t *testing.T) {
390 files := `
391 -- hugo.toml --
392 [[module.mounts]]
393 source = "bar.txt"
394 target = "assets/foo/baz.txt"
395 [[module.imports]]
396 path = "t1"
397 [[module.imports.mounts]]
398 source = "src"
399 target = "assets/foo/bar"
400 -- bar.txt --
401 Bar.
402 -- themes/t1/src/main.js --
403 Main.
404 `
405 b := hugolib.Test(t, files)
406
407 rel, found := b.H.BaseFs.Assets.MakePathRelative(filepath.FromSlash("/themes/t1/src/main.js"), true)
408 b.Assert(found, qt.Equals, true)
409 b.Assert(rel, qt.Equals, filepath.FromSlash("foo/bar/main.js"))
410
411 rel, found = b.H.BaseFs.Assets.MakePathRelative(filepath.FromSlash("/bar.txt"), true)
412 b.Assert(found, qt.Equals, true)
413 b.Assert(rel, qt.Equals, filepath.FromSlash("foo/baz.txt"))
414 }
415
416 func TestAbsProjectContentDir(t *testing.T) {
417 tempDir := t.TempDir()
418
419 files := `
420 -- hugo.toml --
421 [[module.mounts]]
422 source = "content"
423 target = "content"
424 -- content/foo.md --
425 ---
426 title: "Foo"
427 ---
428 `
429
430 b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) {
431 c.WorkingDir = tempDir
432 }))
433
434 abs1 := filepath.Join(tempDir, "content", "foo.md")
435 rel, abs2, err := b.H.BaseFs.AbsProjectContentDir("foo.md")
436 b.Assert(err, qt.IsNil)
437 b.Assert(abs2, qt.Equals, abs1)
438 b.Assert(rel, qt.Equals, filepath.FromSlash("foo.md"))
439 rel2, abs3, err := b.H.BaseFs.AbsProjectContentDir(abs1)
440 b.Assert(err, qt.IsNil)
441 b.Assert(abs3, qt.Equals, abs1)
442 b.Assert(rel2, qt.Equals, rel)
443 }
444
445 func TestContentReverseLookup(t *testing.T) {
446 files := `
447 -- README.md --
448 ---
449 title: README
450 ---
451 -- blog/b1.md --
452 ---
453 title: b1
454 ---
455 -- docs/d1.md --
456 ---
457 title: d1
458 ---
459 -- hugo.toml --
460 baseURL = "https://example.com/"
461 [module]
462 [[module.mounts]]
463 source = "layouts"
464 target = "layouts"
465 [[module.mounts]]
466 source = "README.md"
467 target = "content/_index.md"
468 [[module.mounts]]
469 source = "blog"
470 target = "content/posts"
471 [[module.mounts]]
472 source = "docs"
473 target = "content/mydocs"
474 -- layouts/home.html --
475 Home.
476
477 `
478 b := hugolib.Test(t, files)
479
480 b.AssertFileContent("public/index.html", "Home.")
481
482 stat := func(path string) hugofs.FileMetaInfo {
483 ps, err := b.H.BaseFs.Content.ReverseLookup(filepath.FromSlash(path), true)
484 b.Assert(err, qt.IsNil)
485 b.Assert(ps, qt.HasLen, 1)
486 first := ps[0]
487 fi, err := b.H.BaseFs.Content.Fs.Stat(filepath.FromSlash(first.Path))
488 b.Assert(err, qt.IsNil)
489 b.Assert(fi, qt.Not(qt.IsNil))
490 return fi.(hugofs.FileMetaInfo)
491 }
492
493 sfs := b.H.Fs.Source
494
495 _, err := sfs.Stat("blog/b1.md")
496 b.Assert(err, qt.Not(qt.IsNil))
497
498 _ = stat("blog/b1.md")
499 }
500
501 func TestReverseLookupShouldOnlyConsiderFilesInCurrentComponent(t *testing.T) {
502 files := `
503 -- hugo.toml --
504 baseURL = "https://example.com/"
505 [module]
506 [[module.mounts]]
507 source = "files/layouts"
508 target = "layouts"
509 [[module.mounts]]
510 source = "files/layouts/assets"
511 target = "assets"
512 -- files/layouts/l1.txt --
513 l1
514 -- files/layouts/assets/l2.txt --
515 l2
516 `
517 b := hugolib.Test(t, files)
518
519 assetsFs := b.H.Assets
520
521 for _, checkExists := range []bool{false, true} {
522 cps, err := assetsFs.ReverseLookup(filepath.FromSlash("files/layouts/assets/l2.txt"), checkExists)
523 b.Assert(err, qt.IsNil)
524 b.Assert(cps, qt.HasLen, 1)
525 cps, err = assetsFs.ReverseLookup(filepath.FromSlash("files/layouts/l2.txt"), checkExists)
526 b.Assert(err, qt.IsNil)
527 b.Assert(cps, qt.HasLen, 0)
528 }
529 }
530
531 func TestAssetsIssue12175(t *testing.T) {
532 files := `
533 -- hugo.toml --
534 baseURL = "https://example.com/"
535 [module]
536 [[module.mounts]]
537 source = "node_modules/@foo/core/assets"
538 target = "assets"
539 [[module.mounts]]
540 source = "assets"
541 target = "assets"
542 -- node_modules/@foo/core/assets/js/app.js --
543 JS.
544 -- node_modules/@foo/core/assets/scss/app.scss --
545 body { color: red; }
546 -- assets/scss/app.scss --
547 body { color: blue; }
548 -- layouts/home.html --
549 Home.
550 SCSS: {{ with resources.Get "scss/app.scss" }}{{ .RelPermalink }}|{{ .Content }}{{ end }}|
551 # Note that the pattern below will match 2 resources, which doesn't make much sense,
552 # but is how the current (and also < v0.123.0) merge logic works, and for most practical purposes, it doesn't matter.
553 SCSS Match: {{ with resources.Match "**.scss" }}{{ . | len }}|{{ range .}}{{ .RelPermalink }}|{{ end }}{{ end }}|
554
555 `
556
557 b := hugolib.Test(t, files)
558
559 b.AssertFileContent("public/index.html", `
560 SCSS: /scss/app.scss|body { color: blue; }|
561 SCSS Match: 2|
562 `)
563 }
564
565 func TestStaticComposite(t *testing.T) {
566 files := `
567 -- hugo.toml --
568 disableKinds = ["taxonomy", "term"]
569 [module]
570 [[module.mounts]]
571 source = "myfiles/f1.txt"
572 target = "static/files/f1.txt"
573 [[module.mounts]]
574 source = "f3.txt"
575 target = "static/f3.txt"
576 [[module.mounts]]
577 source = "static"
578 target = "static"
579 -- static/files/f2.txt --
580 f2
581 -- myfiles/f1.txt --
582 f1
583 -- f3.txt --
584 f3
585 -- layouts/home.html --
586 Home.
587
588 `
589 b := hugolib.Test(t, files)
590
591 b.AssertFs(b.H.BaseFs.StaticFs(""), `
592 . true
593 f3.txt false
594 files true
595 files/f1.txt false
596 files/f2.txt false
597 `)
598 }
599
600 func TestMountIssue12141(t *testing.T) {
601 files := `
602 -- hugo.toml --
603 disableKinds = ["taxonomy", "term"]
604 [module]
605 [[module.mounts]]
606 source = "myfiles"
607 target = "static"
608 [[module.mounts]]
609 source = "myfiles/f1.txt"
610 target = "static/f2.txt"
611 -- myfiles/f1.txt --
612 f1
613 `
614 b := hugolib.Test(t, files)
615 fs := b.H.BaseFs.StaticFs("")
616
617 b.AssertFs(fs, `
618 . true
619 f1.txt false
620 f2.txt false
621 `)
622 }
623
624 func checkFileCount(fs afero.Fs, dirname string, c *qt.C, expected int) {
625 c.Helper()
626 count, names, err := countFilesAndGetFilenames(fs, dirname)
627 namesComment := qt.Commentf("filenames: %v", names)
628 c.Assert(err, qt.IsNil, namesComment)
629 c.Assert(count, qt.Equals, expected, namesComment)
630 }
631
632 func checkFileContent(fs afero.Fs, filename string, c *qt.C, expected ...string) {
633 b, err := afero.ReadFile(fs, filename)
634 c.Assert(err, qt.IsNil)
635
636 content := string(b)
637
638 for _, e := range expected {
639 c.Assert(content, qt.Contains, e)
640 }
641 }
642
643 func countFilesAndGetFilenames(fs afero.Fs, dirname string) (int, []string, error) {
644 if fs == nil {
645 return 0, nil, errors.New("no fs")
646 }
647
648 counter := 0
649 var filenames []string
650
651 wf := func(ctx context.Context, path string, info hugofs.FileMetaInfo) error {
652 if !info.IsDir() {
653 counter++
654 }
655
656 if info.Name() != "." {
657 name := info.Name()
658 name = strings.Replace(name, filepath.FromSlash("/my/work"), "WORK_DIR", 1)
659 filenames = append(filenames, name)
660 }
661
662 return nil
663 }
664
665 w := hugofs.NewWalkway(hugofs.WalkwayConfig{Fs: fs, Root: dirname, WalkFn: wf})
666
667 if err := w.Walk(); err != nil {
668 return -1, nil, err
669 }
670
671 return counter, filenames, nil
672 }
673
674 func setConfigAndWriteSomeFilesTo(fs afero.Fs, v config.Provider, key, val string, num int) {
675 workingDir := v.GetString("workingDir")
676 v.Set(key, val)
677 fs.Mkdir(val, 0o755)
678 for i := range num {
679 filename := filepath.Join(workingDir, val, fmt.Sprintf("f%d.txt", i+1))
680 afero.WriteFile(fs, filename, fmt.Appendf(nil, "content:%s:%d", key, i+1), 0o755)
681 }
682 }