Debian Codegraph @

resources/page/pagemeta/pagemeta_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 pagemeta
15
16 import (
17 "fmt"
18 "testing"
19 "time"
20
21 "github.com/gohugoio/hugo/htesting/hqt"
22
23 "github.com/gohugoio/hugo/config"
24
25 qt "github.com/frankban/quicktest"
26 )
27
28 func TestDecodeBuildConfig(t *testing.T) {
29 t.Parallel()
30
31 c := qt.New(t)
32
33 configTempl := `
34 [build]
35 render = %s
36 list = %s
37 publishResources = true`
38
39 for _, test := range []struct {
40 args []any
41 expect BuildConfig
42 }{
43 {
44 []any{"true", "true"},
45 BuildConfig{
46 Render: Always,
47 List: Always,
48 PublishResources: true,
49 },
50 },
51 {[]any{"true", "false"}, BuildConfig{
52 Render: Always,
53 List: Never,
54 PublishResources: true,
55 }},
56 {[]any{`"always"`, `"always"`}, BuildConfig{
57 Render: Always,
58 List: Always,
59 PublishResources: true,
60 }},
61 {[]any{`"never"`, `"never"`}, BuildConfig{
62 Render: Never,
63 List: Never,
64 PublishResources: true,
65 }},
66 {[]any{`"link"`, `"local"`}, BuildConfig{
67 Render: Link,
68 List: ListLocally,
69 PublishResources: true,
70 }},
71 {[]any{`"always"`, `"asdfadf"`}, BuildConfig{
72 Render: Always,
73 List: Always,
74 PublishResources: true,
75 }},
76 } {
77 cfg, err := config.FromConfigString(fmt.Sprintf(configTempl, test.args...), "toml")
78 c.Assert(err, qt.IsNil)
79 bcfg, err := DecodeBuildConfig(cfg.Get("build"))
80 c.Assert(err, qt.IsNil)
81
82 eq := qt.CmpEquals(hqt.DeepAllowUnexported(BuildConfig{}))
83
84 c.Assert(bcfg, eq, test.expect)
85
86 }
87 }
88
89 func TestDateAndSlugFromBaseFilename(t *testing.T) {
90 t.Parallel()
91
92 c := qt.New(t)
93
94 tests := []struct {
95 name string
96 date string
97 slug string
98 }{
99 // date
100 {"2025-07-04 page.md", "2025-07-04T00:00:00+02:00", "page"},
101 {"2025-07-04-page.md", "2025-07-04T00:00:00+02:00", "page"},
102 {"2025-07-04_page.md", "2025-07-04T00:00:00+02:00", "page"},
103 {"2025-07-04page.md", "2025-07-04T00:00:00+02:00", "page"},
104 {"2025-07-04", "2025-07-04T00:00:00+02:00", ""},
105 {"2025-07-04-.md", "2025-07-04T00:00:00+02:00", ""},
106 {"2025-07-04.md", "2025-07-04T00:00:00+02:00", ""},
107 // date and time
108 {"2025-07-04-22-17-13 page.md", "2025-07-04T22:17:13+02:00", "page"},
109 {"2025-07-04-22-17-13-page.md", "2025-07-04T22:17:13+02:00", "page"},
110 {"2025-07-04-22-17-13_page.md", "2025-07-04T22:17:13+02:00", "page"},
111 {"2025-07-04-22-17-13page.md", "2025-07-04T22:17:13+02:00", "page"},
112 {"2025-07-04-22-17-13", "2025-07-04T22:17:13+02:00", ""},
113 {"2025-07-04-22-17-13-.md", "2025-07-04T22:17:13+02:00", ""},
114 {"2025-07-04-22-17-13.md", "2025-07-04T22:17:13+02:00", ""},
115 // date and time with other separators between the two
116 {"2025-07-04T22-17-13.md", "2025-07-04T22:17:13+02:00", ""},
117 {"2025-07-04 22-17-13.md", "2025-07-04T22:17:13+02:00", ""},
118 // no date or time
119 {"something.md", "0001-01-01T00:00:00+00:00", ""}, // 9 chars
120 {"some-thing-.md", "0001-01-01T00:00:00+00:00", ""}, // 10 chars
121 {"somethingsomething.md", "0001-01-01T00:00:00+00:00", ""}, // 18 chars
122 {"something-something.md", "0001-01-01T00:00:00+00:00", ""}, // 19 chars
123 {"something-something-else.md", "0001-01-01T00:00:00+00:00", ""}, // 27 chars
124 // invalid
125 {"2025-07-4-page.md", "0001-01-01T00:00:00+00:00", ""},
126 {"2025-07-4-22-17-13-page.md", "0001-01-01T00:00:00+00:00", ""},
127 {"asdfasdf.md", "0001-01-01T00:00:00+00:00", ""},
128 }
129
130 location, err := time.LoadLocation("Europe/Oslo")
131 if err != nil {
132 t.Error("Unable to determine location from given time zone")
133 }
134 for _, test := range tests {
135
136 gotDate, gotSlug := dateAndSlugFromBaseFilename(location, test.name)
137
138 c.Assert(gotDate.Format("2006-01-02T15:04:05-07:00"), qt.Equals, test.date)
139 c.Assert(gotSlug, qt.Equals, test.slug)
140
141 }
142 }
143
144 func TestExpandDefaultValues(t *testing.T) {
145 c := qt.New(t)
146 c.Assert(expandDefaultValues([]string{"a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"a", "b", "c", "d"})
147 c.Assert(expandDefaultValues([]string{"a", "b", "c"}, []string{"a", "b", "c"}), qt.DeepEquals, []string{"a", "b", "c"})
148 c.Assert(expandDefaultValues([]string{":default", "a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"b", "c", "a", "b", "c", "d"})
149 }