Debian Codegraph @

resources/page/page_matcher_test.go

raw
1 // Copyright 2020 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 page
15
16 import (
17 "path/filepath"
18 "testing"
19
20 "github.com/gohugoio/hugo/common/hmaps"
21 "github.com/gohugoio/hugo/common/loggers"
22 "github.com/gohugoio/hugo/hugolib/sitesmatrix"
23
24 qt "github.com/frankban/quicktest"
25 )
26
27 func TestPageMatcher(t *testing.T) {
28 c := qt.New(t)
29
30 opts := HugoInfoOptions{
31 Conf: testConfig{environment: "development"},
32 }
33 developmentTestSite := &testSite{h: NewHugoInfo(opts)}
34
35 opts = HugoInfoOptions{
36 Conf: testConfig{environment: "production"},
37 }
38 productionTestSite := &testSite{h: NewHugoInfo(opts)}
39
40 dec := cascadeConfigDecoder{}
41
42 p1, p2, p3 := &testPage{path: "/p1", kind: "section", lang: "en", site: developmentTestSite},
43 &testPage{path: "p2", kind: "page", lang: "no", site: productionTestSite},
44 &testPage{path: "p3", kind: "page", lang: "en", site: developmentTestSite}
45
46 c.Run("Matches", func(c *qt.C) {
47 matches := func(m PageMatcher, p Page) bool {
48 c.Assert(m.compileGlobs(), qt.IsNil)
49 return m.Matches(p)
50 }
51
52 c.Assert(matches(PageMatcher{Kind: "section"}, p1), qt.Equals, true)
53 c.Assert(matches(PageMatcher{Kind: "section"}, p2), qt.Equals, false)
54
55 c.Assert(matches(PageMatcher{Kind: "page"}, p1), qt.Equals, false)
56 c.Assert(matches(PageMatcher{Kind: "page"}, p2), qt.Equals, true)
57 c.Assert(matches(PageMatcher{Kind: "page"}, p3), qt.Equals, true)
58
59 c.Assert(matches(PageMatcher{Kind: "page", Path: "/p2"}, p1), qt.Equals, false)
60 c.Assert(matches(PageMatcher{Kind: "page", Path: "/p2"}, p2), qt.Equals, true)
61 c.Assert(matches(PageMatcher{Kind: "page", Path: "/p2"}, p3), qt.Equals, false)
62
63 c.Assert(matches(PageMatcher{Path: "/p*"}, p1), qt.Equals, true)
64 c.Assert(matches(PageMatcher{Path: "/p*"}, p2), qt.Equals, true)
65 c.Assert(matches(PageMatcher{Path: "/p*"}, p3), qt.Equals, true)
66
67 c.Assert(matches(PageMatcher{Environment: "development"}, p1), qt.Equals, true)
68 c.Assert(matches(PageMatcher{Environment: "development"}, p2), qt.Equals, false)
69 c.Assert(matches(PageMatcher{Environment: "development"}, p3), qt.Equals, true)
70
71 c.Assert(matches(PageMatcher{Environment: "production"}, p1), qt.Equals, false)
72 c.Assert(matches(PageMatcher{Environment: "production"}, p2), qt.Equals, true)
73 c.Assert(matches(PageMatcher{Environment: "production"}, p3), qt.Equals, false)
74 })
75
76 c.Run("Decode", func(c *qt.C) {
77 var v PageMatcher
78 c.Assert(dec.decodePageMatcher(map[string]any{"kind": "foo"}, &v), qt.Not(qt.IsNil))
79 c.Assert(dec.decodePageMatcher(map[string]any{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil))
80 c.Assert(dec.decodePageMatcher(map[string]any{"kind": "taxonomy"}, &v), qt.IsNil)
81 c.Assert(dec.decodePageMatcher(map[string]any{"kind": "{taxonomy,foo}"}, &v), qt.IsNil)
82 c.Assert(dec.decodePageMatcher(map[string]any{"kind": "{taxonomy,term}"}, &v), qt.IsNil)
83 c.Assert(dec.decodePageMatcher(map[string]any{"kind": "*"}, &v), qt.IsNil)
84 c.Assert(dec.decodePageMatcher(map[string]any{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil)
85 c.Assert(v, qt.DeepEquals, PageMatcher{Kind: "home", Path: "/a/b/**"})
86 })
87
88 c.Run("mapToPageMatcherParamsConfig", func(c *qt.C) {
89 fn := func(m map[string]any) PageMatcherParamsConfig {
90 v, err := dec.mapToPageMatcherParamsConfig(m)
91 c.Assert(err, qt.IsNil)
92 return v
93 }
94 c.Assert(fn(map[string]any{"_target": map[string]any{"kind": "page"}, "foo": "bar"}), qt.DeepEquals, PageMatcherParamsConfig{
95 Params: hmaps.Params{},
96 Fields: hmaps.Params{
97 "foo": "bar",
98 },
99 Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
100 })
101
102 c.Assert(fn(map[string]any{"target": map[string]any{"kind": "page"}, "params": map[string]any{"foo": "bar"}}), qt.DeepEquals, PageMatcherParamsConfig{
103 Params: hmaps.Params{
104 "foo": "bar",
105 },
106 Fields: hmaps.Params{},
107 Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
108 })
109 })
110 }
111
112 func TestDecodeCascadeConfig(t *testing.T) {
113 c := qt.New(t)
114
115 in := []map[string]any{
116 {
117 "params": map[string]any{
118 "a": "av",
119 },
120 "target": map[string]any{
121 "kind": "page",
122 "Environment": "production",
123 },
124 },
125 {
126 "params": map[string]any{
127 "b": "bv",
128 },
129 "target": map[string]any{
130 "kind": "page",
131 },
132 },
133 }
134
135 got, err := DecodeCascadeConfig(in)
136 c.Assert(err, qt.IsNil)
137 c.Assert(got, qt.IsNotNil)
138 c.Assert(got.InitConfig(loggers.NewDefault(), nil, nil), qt.IsNil)
139 c.Assert(got.c[0].Config.Cascades, qt.HasLen, 2)
140 first := got.c[0].Config.Cascades[0]
141 c.Assert(first, qt.DeepEquals, PageMatcherParamsConfig{
142 Params: hmaps.Params{
143 "a": "av",
144 },
145 Fields: hmaps.Params{},
146 Target: PageMatcher{
147 Kind: "page",
148 Sites: sitesmatrix.Sites{},
149 Environment: "production",
150 },
151 })
152
153 c.Assert(got.c[0].SourceStructure, qt.DeepEquals, []PageMatcherParamsConfig{
154 {
155 Params: hmaps.Params{"a": string("av")},
156 Fields: hmaps.Params{},
157 Target: PageMatcher{Kind: "page", Environment: "production"},
158 },
159 {Params: hmaps.Params{"b": string("bv")}, Fields: hmaps.Params{}, Target: PageMatcher{Kind: "page"}},
160 })
161
162 got, err = DecodeCascadeConfig(nil)
163 c.Assert(err, qt.IsNil)
164 c.Assert(got.InitConfig(loggers.NewDefault(), nil, nil), qt.IsNil)
165 c.Assert(got.Len(), qt.Equals, 0)
166 }
167
168 func TestDecodeCascadeConfigWithSitesMatrix(t *testing.T) {
169 c := qt.New(t)
170
171 in := []map[string]any{
172 {
173 "params": map[string]any{
174 "a": "av",
175 },
176 "sites": map[string]any{
177 "matrix": map[string]any{
178 "roles": "pro",
179 },
180 },
181 "target": map[string]any{
182 "kind": "page",
183 "Environment": "production",
184 "sites": map[string]any{
185 "matrix": map[string]any{
186 "languages": []string{"en", "{no,sv}"},
187 "versions": "v1**",
188 },
189 },
190 },
191 },
192 }
193
194 dims := sitesmatrix.NewTestingDimensions([]string{"en", "no", "sv"}, []string{"v1", "v2"}, []string{"free", "pro"})
195
196 got, err := DecodeCascadeConfig(in)
197 c.Assert(err, qt.IsNil)
198 c.Assert(got, qt.IsNotNil)
199 c.Assert(got.InitConfig(loggers.NewDefault(), nil, dims), qt.IsNil)
200 v := got.c[0].Config.Cascades[0]
201 c.Assert(v.Target.Kind, qt.Equals, "page")
202 c.Assert(v.Target.Environment, qt.Equals, "production")
203
204 matrix := v.Target.SitesMatrixCompiled
205 c.Assert(matrix.HasVector(sitesmatrix.Vector{0, 0, 0}), qt.IsTrue) // en, v1, free
206 c.Assert(matrix.HasVector(sitesmatrix.Vector{0, 1, 0}), qt.IsFalse) // en, v2, free
207 }
208
209 type testConfig struct {
210 environment string
211 running bool
212 workingDir string
213 multihost bool
214 multilingual bool
215 }
216
217 func (c testConfig) Environment() string {
218 return c.environment
219 }
220
221 func (c testConfig) Running() bool {
222 return c.running
223 }
224
225 func (c testConfig) WorkingDir() string {
226 return c.workingDir
227 }
228
229 func (c testConfig) IsMultihost() bool {
230 return c.multihost
231 }
232
233 func (c testConfig) IsMultilingual() bool {
234 return c.multilingual
235 }
236
237 func TestIsGlobWithExtension(t *testing.T) {
238 c := qt.New(t)
239
240 c.Assert(isGlobWithExtension("index.md"), qt.Equals, true)
241 c.Assert(isGlobWithExtension("foo/index.html"), qt.Equals, true)
242 c.Assert(isGlobWithExtension("posts/page"), qt.Equals, false)
243 c.Assert(isGlobWithExtension("pa.th/foo"), qt.Equals, false)
244 c.Assert(isGlobWithExtension(""), qt.Equals, false)
245 c.Assert(isGlobWithExtension("*.md?"), qt.Equals, true)
246 c.Assert(isGlobWithExtension("*.md*"), qt.Equals, true)
247 c.Assert(isGlobWithExtension("posts/*"), qt.Equals, false)
248 c.Assert(isGlobWithExtension("*.md"), qt.Equals, true)
249 }