1- // Copyright 2019 The Hugo Authors. All rights reserved.
1+ // Copyright 2025 The Hugo Authors. All rights reserved.
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
1414package hugolib
1515
1616import (
17- "os"
1817 "path/filepath"
1918 "runtime"
19+ "strings"
2020 "testing"
2121
22- qt "github.com/frankban/quicktest"
2322 "github.com/gohugoio/hugo/common/loggers"
24- )
25-
26- const pageWithAlias = `---
27- title: Has Alias
28- aliases: ["/foo/bar/", "rel"]
29- ---
30- For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
31- `
32-
33- const pageWithAliasMultipleOutputs = `---
34- title: Has Alias for HTML and AMP
35- aliases: ["/foo/bar/"]
36- outputs: ["HTML", "AMP", "JSON"]
37- ---
38- For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
39- `
40-
41- const (
42- basicTemplate = "<html><body>{{.Content}}</body></html>"
23+ "github.com/gohugoio/hugo/config"
4324)
4425
4526func TestAlias (t * testing.T ) {
4627 t .Parallel ()
47- c := qt .New (t )
4828
4929 tests := []struct {
5030 fileSuffix string
@@ -60,15 +40,26 @@ func TestAlias(t *testing.T) {
6040 }
6141
6242 for _ , test := range tests {
63- b := newTestSitesBuilder (t )
64- b .WithSimpleConfigFileAndSettings (test .settings ).WithContent ("blog/page.md" , pageWithAlias )
65- b .CreateSites ().Build (BuildCfg {})
43+ files := `
44+ -- hugo.toml --
45+ disableKinds = ["rss", "sitemap", "taxonomy", "term"]
46+ CONFIG
47+ -- content/blog/page.md --
48+ ---
49+ title: Has Alias
50+ aliases: ["/foo/bar/", "rel"]
51+ ---
52+ For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
53+ -- layouts/all.html --
54+ Title: {{ .Title }}|Content: {{ .Content }}|
55+ `
56+ files = strings .Replace (files , "CONFIG" , config .FromMapToTOMLString (test .settings ), 1 )
6657
67- c .Assert (len (b .H .Sites ), qt .Equals , 1 )
68- c .Assert (len (b .H .Sites [0 ].RegularPages ()), qt .Equals , 1 )
58+ b := Test (t , files )
6959
7060 // the real page
7161 b .AssertFileContent ("public/blog/page" + test .fileSuffix , "For some moments the old man" )
62+
7263 // the alias redirectors
7364 b .AssertFileContent ("public/foo/bar" + test .fileSuffix , "<meta http-equiv=\" refresh\" content=\" 0; url=" + test .urlPrefix + "/blog/page" + test .urlSuffix + "\" >" )
7465 b .AssertFileContent ("public/blog/rel" + test .fileSuffix , "<meta http-equiv=\" refresh\" content=\" 0; url=" + test .urlPrefix + "/blog/page" + test .urlSuffix + "\" >" )
@@ -78,19 +69,25 @@ func TestAlias(t *testing.T) {
7869func TestAliasMultipleOutputFormats (t * testing.T ) {
7970 t .Parallel ()
8071
81- c := qt .New (t )
82-
83- b := newTestSitesBuilder (t )
84- b .WithSimpleConfigFile ().WithContent ("blog/page.md" , pageWithAliasMultipleOutputs )
85-
86- b .WithTemplates (
87- "_default/single.html" , basicTemplate ,
88- "_default/single.amp.html" , basicTemplate ,
89- "_default/single.json" , basicTemplate )
90-
91- b .CreateSites ().Build (BuildCfg {})
72+ files := `
73+ -- hugo.toml --
74+ baseURL = "http://example.com"
75+ -- layouts/_default/single.html --
76+ {{ .Content }}
77+ -- layouts/_default/single.amp.html --
78+ {{ .Content }}
79+ -- layouts/_default/single.json --
80+ {{ .Content }}
81+ -- content/blog/page.md --
82+ ---
83+ title: Has Alias for HTML and AMP
84+ aliases: ["/foo/bar/"]
85+ outputs: ["html", "amp", "json"]
86+ ---
87+ For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
88+ `
9289
93- b . H . Sites [ 0 ]. pageMap . debugPrint ( "" , 999 , os . Stdout )
90+ b := Test ( t , files )
9491
9592 // the real pages
9693 b .AssertFileContent ("public/blog/page/index.html" , "For some moments the old man" )
@@ -100,7 +97,7 @@ func TestAliasMultipleOutputFormats(t *testing.T) {
10097 // the alias redirectors
10198 b .AssertFileContent ("public/foo/bar/index.html" , "<meta http-equiv=\" refresh\" content=\" 0; " )
10299 b .AssertFileContent ("public/amp/foo/bar/index.html" , "<meta http-equiv=\" refresh\" content=\" 0; " )
103- c . Assert ( b . CheckExists ("public/foo/bar/index.json" ), qt . Equals , false )
100+ b . AssertFileExists ("public/foo/bar/index.json" , false )
104101}
105102
106103func TestAliasTemplate (t * testing.T ) {
0 commit comments