| 1 |
// Copyright 2025 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 hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
func Test404(t *testing.T) { |
| 22 |
t.Parallel() |
| 23 |
|
| 24 |
files := ` |
| 25 |
-- hugo.toml -- |
| 26 |
disableKinds = ["rss", "sitemap", "taxonomy", "term"] |
| 27 |
baseURL = "http://example.com/" |
| 28 |
-- layouts/all.html -- |
| 29 |
All. {{ .Kind }}. {{ .Title }}|Lastmod: {{ .Lastmod.Format "2006-01-02" }}| |
| 30 |
-- layouts/404.html -- |
| 31 |
{{ $home := site.Home }} |
| 32 |
404: |
| 33 |
Parent: {{ .Parent.Kind }}|{{ .Parent.Path }}| |
| 34 |
IsAncestor: {{ .IsAncestor $home }}/{{ $home.IsAncestor . }} |
| 35 |
IsDescendant: {{ .IsDescendant $home }}/{{ $home.IsDescendant . }} |
| 36 |
CurrentSection: {{ .CurrentSection.Kind }}| |
| 37 |
FirstSection: {{ .FirstSection.Kind }}| |
| 38 |
InSection: {{ .InSection $home.Section }}|{{ $home.InSection . }} |
| 39 |
Sections: {{ len .Sections }}| |
| 40 |
Page: {{ .Page.RelPermalink }}| |
| 41 |
Data: {{ len .Data }}| |
| 42 |
` |
| 43 |
|
| 44 |
b := Test(t, files) |
| 45 |
|
| 46 |
b.AssertFileContent("public/index.html", "All. home. |") |
| 47 |
|
| 48 |
// Note: We currently have only 1 404 page. One might think that we should have |
| 49 |
// multiple, to follow the Custom Output scheme, but I don't see how that would work |
| 50 |
// right now. |
| 51 |
b.AssertFileContent("public/404.html", ` |
| 52 |
404: |
| 53 |
Parent: home |
| 54 |
IsAncestor: false/true |
| 55 |
IsDescendant: true/false |
| 56 |
CurrentSection: home| |
| 57 |
FirstSection: home| |
| 58 |
InSection: false|true |
| 59 |
Sections: 0| |
| 60 |
Page: /404.html| |
| 61 |
Data: 1| |
| 62 |
|
| 63 |
`) |
| 64 |
} |
| 65 |
|
| 66 |
func Test404EditTemplate(t *testing.T) { |
| 67 |
t.Parallel() |
| 68 |
|
| 69 |
files := ` |
| 70 |
-- hugo.toml -- |
| 71 |
baseURL = "http://example.com/" |
| 72 |
disableLiveReload = true |
| 73 |
[internal] |
| 74 |
fastRenderMode = true |
| 75 |
-- layouts/baseof.html -- |
| 76 |
Base: {{ block "main" . }}{{ end }} |
| 77 |
-- layouts/404.html -- |
| 78 |
{{ define "main" }} |
| 79 |
Not found. |
| 80 |
{{ end }} |
| 81 |
|
| 82 |
` |
| 83 |
|
| 84 |
b := TestRunning(t, files) |
| 85 |
|
| 86 |
b.AssertFileContent("public/404.html", `Not found.`) |
| 87 |
|
| 88 |
b.EditFiles("layouts/404.html", `Not found. Updated.`).Build() |
| 89 |
|
| 90 |
fmt.Println("Rebuilding") |
| 91 |
b.BuildPartial("/does-not-exist") |
| 92 |
|
| 93 |
b.AssertFileContent("public/404.html", `Not found. Updated.`) |
| 94 |
} |
| 95 |
|
| 96 |
func Test404Panic14283(t *testing.T) { |
| 97 |
t.Parallel() |
| 98 |
|
| 99 |
files := ` |
| 100 |
-- hugo.toml -- |
| 101 |
baseURL = "http://example.com/" |
| 102 |
-- layouts/all.html -- |
| 103 |
All. {{ .Kind }}. {{ .Title }}| |
| 104 |
-- content/404/_index.md -- |
| 105 |
--- |
| 106 |
title: "404 branch" |
| 107 |
--- |
| 108 |
This is the 404 branch. |
| 109 |
|
| 110 |
` |
| 111 |
|
| 112 |
b := Test(t, files) // panic. |
| 113 |
|
| 114 |
b.AssertFileContent("public/404/index.html", "All. section. 404 branch|") |
| 115 |
} |