Debian Codegraph @

resources/page/pagemeta/page_frontmatter_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 pagemeta_test
15
16 import (
17 "io"
18 "strings"
19 "testing"
20 "time"
21
22 "github.com/gohugoio/hugo/common/loggers"
23 "github.com/gohugoio/hugo/config"
24 "github.com/gohugoio/hugo/config/testconfig"
25 "github.com/gohugoio/hugo/media"
26
27 "github.com/gohugoio/hugo/resources/page/pagemeta"
28
29 qt "github.com/frankban/quicktest"
30 )
31
32 func newTestFd() *pagemeta.FrontMatterDescriptor {
33 return &pagemeta.FrontMatterDescriptor{
34 PageConfigEarly: &pagemeta.PageConfigEarly{
35 Frontmatter: make(map[string]any),
36 },
37 PageConfigLate: &pagemeta.PageConfigLate{
38 Params: make(map[string]any),
39 },
40 Location: time.UTC,
41 }
42 }
43
44 func TestFrontMatterNewConfig(t *testing.T) {
45 c := qt.New(t)
46
47 cfg := config.New()
48
49 cfg.Set("frontmatter", map[string]any{
50 "date": []string{"publishDate", "LastMod"},
51 "Lastmod": []string{"publishDate"},
52 "expiryDate": []string{"lastMod"},
53 "publishDate": []string{"date"},
54 })
55
56 fc, err := pagemeta.DecodeFrontMatterConfig(cfg)
57 c.Assert(err, qt.IsNil)
58 c.Assert(fc.Date, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "lastmod", "modified"})
59 c.Assert(fc.Lastmod, qt.DeepEquals, []string{"publishdate", "pubdate", "published"})
60 c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"lastmod", "modified"})
61 c.Assert(fc.PublishDate, qt.DeepEquals, []string{"date"})
62
63 // Default
64 cfg = config.New()
65 fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
66 c.Assert(err, qt.IsNil)
67 c.Assert(fc.Date, qt.DeepEquals, []string{"date", "publishdate", "pubdate", "published", "lastmod", "modified"})
68 c.Assert(fc.Lastmod, qt.DeepEquals, []string{":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
69 c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"expirydate", "unpublishdate"})
70 c.Assert(fc.PublishDate, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "date"})
71
72 // :default keyword
73 cfg.Set("frontmatter", map[string]any{
74 "date": []string{"d1", ":default"},
75 "lastmod": []string{"d2", ":default"},
76 "expiryDate": []string{"d3", ":default"},
77 "publishDate": []string{"d4", ":default"},
78 })
79 fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
80 c.Assert(err, qt.IsNil)
81 c.Assert(fc.Date, qt.DeepEquals, []string{"d1", "date", "publishdate", "pubdate", "published", "lastmod", "modified"})
82 c.Assert(fc.Lastmod, qt.DeepEquals, []string{"d2", ":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
83 c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"d3", "expirydate", "unpublishdate"})
84 c.Assert(fc.PublishDate, qt.DeepEquals, []string{"d4", "publishdate", "pubdate", "published", "date"})
85 }
86
87 func TestFrontMatterDatesHandlers(t *testing.T) {
88 c := qt.New(t)
89
90 for _, handlerID := range []string{":filename", ":fileModTime", ":git"} {
91
92 cfg := config.New()
93
94 cfg.Set("frontmatter", map[string]any{
95 "date": []string{handlerID, "date"},
96 })
97 conf := testconfig.GetTestConfig(nil, cfg)
98 handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
99 c.Assert(err, qt.IsNil)
100
101 d1, _ := time.Parse("2006-01-02", "2018-02-01")
102 d2, _ := time.Parse("2006-01-02", "2018-02-02")
103
104 d := newTestFd()
105 switch strings.ToLower(handlerID) {
106 case ":filename":
107 d.BaseFilename = "2018-02-01-page.md"
108 case ":filemodtime":
109 d.ModTime = d1
110 case ":git":
111 d.GitAuthorDate = d1
112 }
113 d.PageConfigEarly.Frontmatter["date"] = d2
114 c.Assert(handler.HandleDates(d), qt.IsNil)
115 c.Assert(d.PageConfigLate.Dates.Date, qt.Equals, d1)
116 c.Assert(d.PageConfigLate.Params["date"], qt.Equals, d2)
117
118 d = newTestFd()
119 d.PageConfigEarly.Frontmatter["date"] = d2
120 c.Assert(handler.HandleDates(d), qt.IsNil)
121 c.Assert(d.PageConfigLate.Dates.Date, qt.Equals, d2)
122 c.Assert(d.PageConfigLate.Params["date"], qt.Equals, d2)
123
124 }
125 }
126
127 func TestFrontMatterDatesDefaultKeyword(t *testing.T) {
128 t.Parallel()
129
130 c := qt.New(t)
131
132 cfg := config.New()
133
134 cfg.Set("frontmatter", map[string]any{
135 "date": []string{"mydate", ":default"},
136 "publishdate": []string{":default", "mypubdate"},
137 })
138
139 conf := testconfig.GetTestConfig(nil, cfg)
140 handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
141 c.Assert(err, qt.IsNil)
142
143 testDate, _ := time.Parse("2006-01-02", "2018-02-01")
144 d := newTestFd()
145 d.PageConfigEarly.Frontmatter["mydate"] = testDate
146 d.PageConfigEarly.Frontmatter["date"] = testDate.Add(1 * 24 * time.Hour)
147 d.PageConfigEarly.Frontmatter["mypubdate"] = testDate.Add(2 * 24 * time.Hour)
148 d.PageConfigEarly.Frontmatter["publishdate"] = testDate.Add(3 * 24 * time.Hour)
149
150 c.Assert(handler.HandleDates(d), qt.IsNil)
151
152 c.Assert(d.PageConfigLate.Dates.Date.Day(), qt.Equals, 1)
153 c.Assert(d.PageConfigLate.Dates.Lastmod.Day(), qt.Equals, 2)
154 c.Assert(d.PageConfigLate.Dates.PublishDate.Day(), qt.Equals, 4)
155 c.Assert(d.PageConfigLate.Dates.ExpiryDate.IsZero(), qt.Equals, true)
156 }
157
158 // Issue 14684
159 // Each call to the opener must return an independent reader. With the old
160 // implementation, opener() returned the same shared reader seeked to 0, so a
161 // second open would reset the position of a reader already in use.
162 func TestSourceValueAsOpenReadSeekCloserIsIndependent(t *testing.T) {
163 c := qt.New(t)
164 s := pagemeta.Source{Value: "abcdefgh"}
165 opener := s.ValueAsOpenReadSeekCloser()
166
167 r1, err := opener()
168 c.Assert(err, qt.IsNil)
169 defer r1.Close()
170
171 // Partially consume r1.
172 buf := make([]byte, 4)
173 _, err = io.ReadFull(r1, buf)
174 c.Assert(err, qt.IsNil)
175 c.Assert(string(buf), qt.Equals, "abcd")
176
177 // Open a second reader and fully consume it.
178 r2, err := opener()
179 c.Assert(err, qt.IsNil)
180 defer r2.Close()
181 all, err := io.ReadAll(r2)
182 c.Assert(err, qt.IsNil)
183 c.Assert(string(all), qt.Equals, "abcdefgh")
184
185 // r1's position must be unaffected; it should yield the remaining half.
186 rest, err := io.ReadAll(r1)
187 c.Assert(err, qt.IsNil)
188 c.Assert(string(rest), qt.Equals, "efgh")
189 }
190
191 func TestContentMediaTypeFromMarkup(t *testing.T) {
192 c := qt.New(t)
193 logger := loggers.NewDefault()
194
195 for _, test := range []struct {
196 in string
197 expected string
198 }{
199 {"", "text/markdown"},
200 {"md", "text/markdown"},
201 {"markdown", "text/markdown"},
202 {"mdown", "text/markdown"},
203 {"goldmark", "text/markdown"},
204 {"html", "text/html"},
205 {"htm", "text/html"},
206 {"asciidoc", "text/asciidoc"},
207 {"asciidocext", "text/asciidoc"},
208 {"adoc", "text/asciidoc"},
209 {"pandoc", "text/pandoc"},
210 {"pdc", "text/pandoc"},
211 {"rst", "text/rst"},
212 } {
213 var pc pagemeta.PageConfigEarly
214 pc.Content.Markup = test.in
215 c.Assert(pc.CompileForPagesFromDataPre("", logger, media.DefaultTypes), qt.IsNil)
216 c.Assert(pc.ContentMediaType.Type, qt.Equals, test.expected)
217 }
218 }