1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright 2017-present The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package output
import (
"path/filepath"
"strings"
"testing"
qt "github.com/frankban/quicktest"
)
func TestLayoutBase(t *testing.T) {
c := qt.New(t)
var (
workingDir = "/sites/mysite/"
layoutPath1 = "_default/single.html"
layoutPathAmp = "_default/single.amp.html"
layoutPathJSON = "_default/single.json"
)
for _, this := range []struct {
name string
d TemplateLookupDescriptor
needsBase bool
basePathMatchStrings string
expect TemplateNames
}{
{"No base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPath1}, false, "",
TemplateNames{
Name: "_default/single.html",
OverlayFilename: "_default/single.html",
}},
{"Base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPath1}, true, "",
TemplateNames{
Name: "_default/single.html",
OverlayFilename: "_default/single.html",
MasterFilename: "_default/single-baseof.html",
}},
// Issue #3893
{"Base Lang, Default Base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "_default/list.en.html"}, true, "_default/baseof.html",
TemplateNames{
Name: "_default/list.en.html",
OverlayFilename: "_default/list.en.html",
MasterFilename: "_default/baseof.html",
}},
{"Base Lang, Lang Base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "_default/list.en.html"}, true, "_default/baseof.html|_default/baseof.en.html",
TemplateNames{
Name: "_default/list.en.html",
OverlayFilename: "_default/list.en.html",
MasterFilename: "_default/baseof.en.html",
}},
// Issue #3856
{"Base Taxonomy Term", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "taxonomy/tag.terms.html"}, true, "_default/baseof.html",
TemplateNames{
Name: "taxonomy/tag.terms.html",
OverlayFilename: "taxonomy/tag.terms.html",
MasterFilename: "_default/baseof.html",
}},
{"Partial", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "partials/menu.html"}, true,
"mytheme/layouts/_default/baseof.html",
TemplateNames{
Name: "partials/menu.html",
OverlayFilename: "partials/menu.html",
}},
{"Partial in subfolder", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "/partials/sub/menu.html"}, true,
"_default/baseof.html",
TemplateNames{
Name: "partials/sub/menu.html",
OverlayFilename: "/partials/sub/menu.html",
}},
{"Shortcode in subfolder", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "shortcodes/sub/menu.html"}, true,
"_default/baseof.html",
TemplateNames{
Name: "shortcodes/sub/menu.html",
OverlayFilename: "shortcodes/sub/menu.html",
}},
{"AMP, no base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathAmp}, false, "",
TemplateNames{
Name: "_default/single.amp.html",
OverlayFilename: "_default/single.amp.html",
}},
{"JSON, no base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathJSON}, false, "",
TemplateNames{
Name: "_default/single.json",
OverlayFilename: "_default/single.json",
}},
{"AMP with base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathAmp}, true, "single-baseof.html|single-baseof.amp.html",
TemplateNames{
Name: "_default/single.amp.html",
OverlayFilename: "_default/single.amp.html",
MasterFilename: "_default/single-baseof.amp.html",
}},
{"AMP with no AMP base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathAmp}, true, "single-baseof.html",
TemplateNames{
Name: "_default/single.amp.html",
OverlayFilename: "_default/single.amp.html",
MasterFilename: "_default/single-baseof.html",
}},
{"JSON with base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathJSON}, true, "single-baseof.json",
TemplateNames{
Name: "_default/single.json",
OverlayFilename: "_default/single.json",
MasterFilename: "_default/single-baseof.json",
}},
} {
c.Run(this.name, func(c *qt.C) {
this.basePathMatchStrings = filepath.FromSlash(this.basePathMatchStrings)
fileExists := func(filename string) (bool, error) {
stringsToMatch := strings.Split(this.basePathMatchStrings, "|")
for _, s := range stringsToMatch {
if strings.Contains(filename, s) {
return true, nil
}
}
return false, nil
}
needsBase := func(filename string, subslices [][]byte) (bool, error) {
return this.needsBase, nil
}
this.d.OutputFormats = Formats{AMPFormat, HTMLFormat, RSSFormat, JSONFormat}
this.d.WorkingDir = filepath.FromSlash(this.d.WorkingDir)
this.d.RelPath = filepath.FromSlash(this.d.RelPath)
this.d.ContainsAny = needsBase
this.d.FileExists = fileExists
this.expect.MasterFilename = filepath.FromSlash(this.expect.MasterFilename)
this.expect.OverlayFilename = filepath.FromSlash(this.expect.OverlayFilename)
if strings.Contains(this.d.RelPath, "json") {
// currently the only plain text templates in this test.
this.expect.Name = "_text/" + this.expect.Name
}
id, err := CreateTemplateNames(this.d)
c.Assert(err, qt.IsNil)
msg := qt.Commentf(this.name)
c.Assert(id, qt.Equals, this.expect, msg)
})
}
}
|