| 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 |
"strings" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/hugolib" |
| 21 |
) |
| 22 |
|
| 23 |
func TestLastModEq(t *testing.T) { |
| 24 |
files := ` |
| 25 |
-- hugo.toml -- |
| 26 |
timeZone = "Europe/London" |
| 27 |
-- content/p1.md -- |
| 28 |
--- |
| 29 |
title: p1 |
| 30 |
date: 2024-03-13T06:00:00 |
| 31 |
--- |
| 32 |
-- layouts/single.html -- |
| 33 |
Date: {{ .Date }} |
| 34 |
Lastmod: {{ .Lastmod }} |
| 35 |
Eq: {{ eq .Date .Lastmod }} |
| 36 |
|
| 37 |
` |
| 38 |
|
| 39 |
b := hugolib.Test(t, files) |
| 40 |
|
| 41 |
b.AssertFileContent("public/p1/index.html", ` |
| 42 |
Date: 2024-03-13 06:00:00 +0000 GMT |
| 43 |
Lastmod: 2024-03-13 06:00:00 +0000 GMT |
| 44 |
Eq: true |
| 45 |
`) |
| 46 |
} |
| 47 |
|
| 48 |
func TestDateValidation(t *testing.T) { |
| 49 |
t.Parallel() |
| 50 |
|
| 51 |
files := ` |
| 52 |
-- hugo.toml -- |
| 53 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 54 |
-- content/_index.md -- |
| 55 |
FRONT_MATTER |
| 56 |
-- layouts/home.html -- |
| 57 |
{{ .Date.UTC.Format "2006-01-02" }} |
| 58 |
-- |
| 59 |
` |
| 60 |
errorMsg := `ERROR the "date" front matter field is not a parsable date` |
| 61 |
|
| 62 |
// TOML: unquoted date/time (valid) |
| 63 |
f := strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 64 |
+++ |
| 65 |
date = 2024-10-01 |
| 66 |
+++ |
| 67 |
`) |
| 68 |
b := hugolib.Test(t, f) |
| 69 |
b.AssertFileContent("public/index.html", "2024-10-01") |
| 70 |
|
| 71 |
// TOML: string (valid) |
| 72 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 73 |
+++ |
| 74 |
date = "2024-10-01" |
| 75 |
+++ |
| 76 |
`) |
| 77 |
b = hugolib.Test(t, f) |
| 78 |
b.AssertFileContent("public/index.html", "2024-10-01") |
| 79 |
|
| 80 |
// TOML: empty string (valid) |
| 81 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 82 |
+++ |
| 83 |
date = "" |
| 84 |
+++ |
| 85 |
`) |
| 86 |
b = hugolib.Test(t, f) |
| 87 |
b.AssertFileContent("public/index.html", "0001-01-01") |
| 88 |
|
| 89 |
// TOML: int (valid) |
| 90 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 91 |
+++ |
| 92 |
date = 0 |
| 93 |
+++ |
| 94 |
`) |
| 95 |
b = hugolib.Test(t, f) |
| 96 |
b.AssertFileContent("public/index.html", "1970-01-01") |
| 97 |
|
| 98 |
// TOML: string (invalid) |
| 99 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 100 |
+++ |
| 101 |
date = "2024-42-42" |
| 102 |
+++ |
| 103 |
`) |
| 104 |
b, _ = hugolib.TestE(t, f) |
| 105 |
b.AssertLogContains(errorMsg) |
| 106 |
|
| 107 |
// TOML: bool (invalid) |
| 108 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 109 |
+++ |
| 110 |
date = true |
| 111 |
+++ |
| 112 |
`) |
| 113 |
b, _ = hugolib.TestE(t, f) |
| 114 |
b.AssertLogContains(errorMsg) |
| 115 |
|
| 116 |
// TOML: float (invalid) |
| 117 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 118 |
+++ |
| 119 |
date = 6.7 |
| 120 |
+++ |
| 121 |
`) |
| 122 |
b, _ = hugolib.TestE(t, f) |
| 123 |
b.AssertLogContains(errorMsg) |
| 124 |
|
| 125 |
// JSON: null (valid) |
| 126 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 127 |
{ |
| 128 |
"date": null |
| 129 |
} |
| 130 |
`) |
| 131 |
b = hugolib.Test(t, f) |
| 132 |
b.AssertFileContent("public/index.html", "0001-01-01") |
| 133 |
|
| 134 |
// YAML: null (valid) |
| 135 |
f = strings.ReplaceAll(files, "FRONT_MATTER", ` |
| 136 |
--- |
| 137 |
date: |
| 138 |
--- |
| 139 |
`) |
| 140 |
b = hugolib.Test(t, f) |
| 141 |
b.AssertFileContent("public/index.html", "0001-01-01") |
| 142 |
} |