Debian Codegraph @

related/related_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 related_test
15
16 import (
17 "fmt"
18 "math/rand"
19 "strings"
20 "testing"
21
22 "github.com/gohugoio/hugo/hugolib"
23 )
24
25 func TestRelatedFragments(t *testing.T) {
26 t.Parallel()
27
28 files := `
29 -- hugo.toml --
30 baseURL = "http://example.com/"
31 disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
32 [related]
33 includeNewer = false
34 threshold = 80
35 toLower = false
36 [[related.indices]]
37 name = 'pagerefs'
38 type = 'fragments'
39 applyFilter = true
40 weight = 90
41 [[related.indices]]
42 name = 'keywords'
43 weight = 80
44 -- content/p1.md --
45 ---
46 title: p1
47 pagerefs: ['ref1']
48 ---
49 {{< see-also >}}
50
51 ## P1 title
52
53 -- content/p2.md --
54 ---
55 title: p2
56 ---
57
58 ## P2 title 1
59
60 ## P2 title 2
61
62 ## First title {#ref1}
63 {{< see-also "ref1" >}}
64 -- content/p3.md --
65 ---
66 title: p3
67 keywords: ['foo']
68 ---
69
70 ## P3 title 1
71
72 ## P3 title 2
73
74 ## Common p3, p4, p5
75 -- content/p4.md --
76 ---
77 title: p4
78 ---
79
80 ## Common p3, p4, p5
81
82 ## P4 title 1
83
84 -- content/p5.md --
85 ---
86 title: p5
87 keywords: ['foo']
88 ---
89
90 ## P5 title 1
91
92 ## Common p3, p4, p5
93
94 -- layouts/_shortcodes/see-also.html --
95 {{ $p1 := site.GetPage "p1" }}
96 {{ $p2 := site.GetPage "p2" }}
97 {{ $p3 := site.GetPage "p3" }}
98 P1 Fragments: {{ $p1.Fragments.Identifiers }}
99 P2 Fragments: {{ $p2.Fragments.Identifiers }}
100 Contains ref1: {{ $p2.Fragments.Identifiers.Contains "ref1" }}
101 Count ref1: {{ $p2.Fragments.Identifiers.Count "ref1" }}
102 {{ $opts := dict "document" .Page "fragments" $.Params }}
103 {{ $related1 := site.RegularPages.Related $opts }}
104 {{ $related2 := site.RegularPages.Related $p3 }}
105 Len Related 1: {{ len $related1 }}
106 Len Related 2: {{ len $related2 }}
107 Related 1: {{ template "list-related" $related1 }}
108 Related 2: {{ template "list-related" $related2 }}
109
110 {{ define "list-related" }}{{ range $i, $e := . }} {{ $i }}: {{ .Title }}: {{ with .HeadingsFiltered}}{{ range $i, $e := .}}h{{ $i }}: {{ .Title }}|{{ .ID }}|{{ end }}{{ end }}::END{{ end }}{{ end }}
111
112 -- layouts/single.html --
113 Content: {{ .Content }}
114
115
116 `
117
118 b := hugolib.Test(t, files)
119
120 expect := `
121 P1 Fragments: [p1-title]
122 P2 Fragments: [p2-title-1 p2-title-2 ref1]
123 Len Related 1: 1
124 Related 2: 2
125 `
126
127 for _, p := range []string{"p1", "p2"} {
128 b.AssertFileContent("public/"+p+"/index.html", expect)
129 }
130
131 b.AssertFileContent("public/p1/index.html",
132 "Related 1: 0: p2: h0: First title|ref1|::END",
133 "Related 2: 0: p5: h0: Common p3, p4, p5|common-p3-p4-p5|::END 1: p4: h0: Common p3, p4, p5|common-p3-p4-p5|::END",
134 )
135 }
136
137 func BenchmarkRelatedSite(b *testing.B) {
138 var files strings.Builder
139 files.WriteString(`
140 -- hugo.toml --
141 baseURL = "http://example.com/"
142 disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
143 [related]
144 includeNewer = false
145 threshold = 80
146 toLower = false
147 [[related.indices]]
148 name = 'keywords'
149 weight = 70
150 [[related.indices]]
151 name = 'pagerefs'
152 type = 'fragments'
153 weight = 30
154 -- layouts/single.html --
155 Len related: {{ site.RegularPages.Related . | len }}
156 `)
157
158 createContent := func(n int) string {
159 var base strings.Builder
160 base.WriteString(`---
161 title: "Page %d"
162 keywords: ['k%d']
163 ---
164 `)
165
166 for range 32 {
167 base.WriteString(fmt.Sprintf("\n## Title %d", rand.Intn(100)))
168 }
169
170 return fmt.Sprintf(base.String(), n, rand.Intn(32))
171 }
172
173 for i := 1; i < 100; i++ {
174 files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n"+createContent(i+1), i+1))
175 }
176
177 cfg := hugolib.IntegrationTestConfig{
178 T: b,
179 TxtarString: files.String(),
180 }
181
182 for b.Loop() {
183 b.StopTimer()
184 bb := hugolib.NewIntegrationTestBuilder(cfg)
185 b.StartTimer()
186 bb.Build()
187 }
188 }