Debian Codegraph @

resources/resource/resources_integration_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 resource_test
15
16 import (
17 "testing"
18
19 "github.com/gohugoio/hugo/hugolib"
20 )
21
22 func TestResourcesMount(t *testing.T) {
23 files := `
24 -- hugo.toml --
25 -- assets/text/txt1.txt --
26 Text 1.
27 -- assets/text/txt2.txt --
28 Text 2.
29 -- assets/text/sub/txt3.txt --
30 Text 3.
31 -- assets/text/sub/txt4.txt --
32 Text 4.
33 -- content/mybundle/index.md --
34 ---
35 title: "My Bundle"
36 ---
37 -- content/mybundle/txt1.txt --
38 Text 1.
39 -- content/mybundle/sub/txt2.txt --
40 Text 1.
41 -- layouts/home.html --
42 {{ $mybundle := site.GetPage "mybundle" }}
43 {{ $subResources := resources.Match "/text/sub/*.*" }}
44 {{ $subResourcesMount := $subResources.Mount "/text/sub" "/newroot" }}
45 resources:text/txt1.txt:{{ with resources.Get "text/txt1.txt" }}{{ .Name }}{{ end }}|
46 resources:text/txt2.txt:{{ with resources.Get "text/txt2.txt" }}{{ .Name }}{{ end }}|
47 resources:text/sub/txt3.txt:{{ with resources.Get "text/sub/txt3.txt" }}{{ .Name }}{{ end }}|
48 subResources.range:{{ range $subResources }}{{ .Name }}|{{ end }}|
49 subResources:"text/sub/txt3.txt:{{ with $subResources.Get "text/sub/txt3.txt" }}{{ .Name }}{{ end }}|
50 subResourcesMount:/newroot/txt3.txt:{{ with $subResourcesMount.Get "/newroot/txt3.txt" }}{{ .Name }}{{ end }}|
51 page:txt1.txt:{{ with $mybundle.Resources.Get "txt1.txt" }}{{ .Name }}{{ end }}|
52 page:./txt1.txt:{{ with $mybundle.Resources.Get "./txt1.txt" }}{{ .Name }}{{ end }}|
53 page:sub/txt2.txt:{{ with $mybundle.Resources.Get "sub/txt2.txt" }}{{ .Name }}{{ end }}|
54 `
55 b := hugolib.Test(t, files)
56
57 b.AssertFileContent("public/index.html", `
58 resources:text/txt1.txt:/text/txt1.txt|
59 resources:text/txt2.txt:/text/txt2.txt|
60 resources:text/sub/txt3.txt:/text/sub/txt3.txt|
61 subResources:"text/sub/txt3.txt:/text/sub/txt3.txt|
62 subResourcesMount:/newroot/txt3.txt:/text/sub/txt3.txt|
63 page:txt1.txt:txt1.txt|
64 page:./txt1.txt:txt1.txt|
65 page:sub/txt2.txt:sub/txt2.txt|
66 `)
67 }
68
69 func TestResourcesMountOnRename(t *testing.T) {
70 files := `
71 -- hugo.toml --
72 disableKinds = ["taxonomy", "term", "home", "sitemap"]
73 -- content/mybundle/index.md --
74 ---
75 title: "My Bundle"
76 resources:
77 - name: /foo/bars.txt
78 src: foo/txt1.txt
79 - name: foo/bars2.txt
80 src: foo/txt2.txt
81 ---
82 -- content/mybundle/foo/txt1.txt --
83 Text 1.
84 -- content/mybundle/foo/txt2.txt --
85 Text 2.
86 -- layouts/single.html --
87 Single.
88 {{ $mybundle := site.GetPage "mybundle" }}
89 Resources:{{ range $mybundle.Resources }}Name: {{ .Name }}|{{ end }}$
90 {{ $subResourcesMount := $mybundle.Resources.Mount "/foo" "/newroot" }}
91 {{ $subResourcesMount2 := $mybundle.Resources.Mount "foo" "/newroot" }}
92 {{ $subResourcesMount3 := $mybundle.Resources.Mount "foo" "." }}
93 subResourcesMount:/newroot/bars.txt:{{ with $subResourcesMount.Get "/newroot/bars.txt" }}{{ .Name }}{{ end }}|
94 subResourcesMount:/newroot/bars2.txt:{{ with $subResourcesMount.Get "/newroot/bars2.txt" }}{{ .Name }}{{ end }}|
95 subResourcesMount2:/newroot/bars2.txt:{{ with $subResourcesMount2.Get "/newroot/bars2.txt" }}{{ .Name }}{{ end }}|
96 subResourcesMount3:bars2.txt:{{ with $subResourcesMount3.Get "bars2.txt" }}{{ .Name }}{{ end }}|
97 `
98 b := hugolib.Test(t, files)
99 b.AssertFileContent("public/mybundle/index.html",
100 "Resources:Name: foo/bars.txt|Name: foo/bars2.txt|$",
101 "subResourcesMount:/newroot/bars.txt:|\nsubResourcesMount:/newroot/bars2.txt:|",
102 "subResourcesMount2:/newroot/bars2.txt:foo/bars2.txt|",
103 "subResourcesMount3:bars2.txt:foo/bars2.txt|",
104 )
105 }