2017-03-19 20:09:31 +00:00
// 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"
"github.com/stretchr/testify/require"
)
func TestLayoutBase ( t * testing . T ) {
var (
workingDir = "/sites/mysite/"
2017-04-12 18:40:36 +00:00
themeDir = "/themes/mytheme/"
2017-03-19 20:09:31 +00:00
layoutBase1 = "layouts"
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
} {
2017-04-12 18:40:36 +00:00
{ "No base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPath1 } , false , "" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.html" ,
} } ,
2017-04-12 18:40:36 +00:00
{ "Base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPath1 } , true , "" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.html" ,
MasterFilename : "/sites/mysite/layouts/_default/single-baseof.html" ,
} } ,
2017-11-17 11:27:50 +00:00
// Issue #3893
{ "Base Lang, Default Base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : "layouts" , RelPath : "_default/list.en.html" } , true , "_default/baseof.html" ,
TemplateNames {
Name : "_default/list.en.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/list.en.html" ,
MasterFilename : "/sites/mysite/layouts/_default/baseof.html" ,
} } ,
{ "Base Lang, Lang Base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : "layouts" , RelPath : "_default/list.en.html" } , true , "_default/baseof.html|_default/baseof.en.html" ,
TemplateNames {
Name : "_default/list.en.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/list.en.html" ,
MasterFilename : "/sites/mysite/layouts/_default/baseof.en.html" ,
} } ,
2017-09-03 09:32:26 +00:00
// Issue #3856
{ "Base Taxonomy Term" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : "taxonomy/tag.terms.html" } , true , "_default/baseof.html" ,
TemplateNames {
Name : "taxonomy/tag.terms.html" ,
OverlayFilename : "/sites/mysite/layouts/taxonomy/tag.terms.html" ,
MasterFilename : "/sites/mysite/layouts/_default/baseof.html" ,
} } ,
2017-04-12 18:40:36 +00:00
{ "Base in theme" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPath1 , ThemeDir : themeDir } , true ,
2017-03-19 20:09:31 +00:00
"mytheme/layouts/_default/baseof.html" ,
TemplateNames {
Name : "_default/single.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.html" ,
2017-04-12 18:40:36 +00:00
MasterFilename : "/themes/mytheme/layouts/_default/baseof.html" ,
2017-03-19 20:09:31 +00:00
} } ,
2017-04-12 18:40:36 +00:00
{ "Template in theme, base in theme" , TemplateLookupDescriptor { TemplateDir : themeDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPath1 , ThemeDir : themeDir } , true ,
2017-03-19 20:09:31 +00:00
"mytheme/layouts/_default/baseof.html" ,
TemplateNames {
Name : "_default/single.html" ,
2017-04-12 18:40:36 +00:00
OverlayFilename : "/themes/mytheme/layouts/_default/single.html" ,
MasterFilename : "/themes/mytheme/layouts/_default/baseof.html" ,
2017-03-19 20:09:31 +00:00
} } ,
2017-04-12 18:40:36 +00:00
{ "Template in theme, base in site" , TemplateLookupDescriptor { TemplateDir : themeDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPath1 , ThemeDir : themeDir } , true ,
"/sites/mysite/layouts/_default/baseof.html" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.html" ,
2017-04-12 18:40:36 +00:00
OverlayFilename : "/themes/mytheme/layouts/_default/single.html" ,
MasterFilename : "/sites/mysite/layouts/_default/baseof.html" ,
2017-03-19 20:09:31 +00:00
} } ,
2017-04-12 18:40:36 +00:00
{ "Template in site, base in theme" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPath1 , ThemeDir : themeDir } , true ,
"/themes/mytheme" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.html" ,
2017-04-12 18:40:36 +00:00
MasterFilename : "/themes/mytheme/layouts/_default/single-baseof.html" ,
2017-03-19 20:09:31 +00:00
} } ,
2017-04-12 18:40:36 +00:00
{ "With prefix, base in theme" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPath1 ,
ThemeDir : themeDir , Prefix : "someprefix" } , true ,
2017-03-19 20:09:31 +00:00
"mytheme/layouts/_default/baseof.html" ,
TemplateNames {
Name : "someprefix/_default/single.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.html" ,
2017-04-12 18:40:36 +00:00
MasterFilename : "/themes/mytheme/layouts/_default/baseof.html" ,
2017-03-19 20:09:31 +00:00
} } ,
2017-04-12 18:40:36 +00:00
{ "Partial" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : "partials/menu.html" } , true ,
2017-03-19 20:09:31 +00:00
"mytheme/layouts/_default/baseof.html" ,
TemplateNames {
Name : "partials/menu.html" ,
OverlayFilename : "/sites/mysite/layouts/partials/menu.html" ,
} } ,
2017-04-12 18:40:36 +00:00
{ "AMP, no base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPathAmp } , false , "" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.amp.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.amp.html" ,
} } ,
2017-04-12 18:40:36 +00:00
{ "JSON, no base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPathJSON } , false , "" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.json" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.json" ,
} } ,
2017-04-12 18:40:36 +00:00
{ "AMP with base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPathAmp } , true , "single-baseof.html|single-baseof.amp.html" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.amp.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.amp.html" ,
MasterFilename : "/sites/mysite/layouts/_default/single-baseof.amp.html" ,
} } ,
2017-11-17 11:27:50 +00:00
{ "AMP with no AMP base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPathAmp } , true , "single-baseof.html" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.amp.html" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.amp.html" ,
2017-11-17 11:27:50 +00:00
MasterFilename : "/sites/mysite/layouts/_default/single-baseof.html" ,
2017-03-19 20:09:31 +00:00
} } ,
2017-04-12 18:40:36 +00:00
{ "JSON with base" , TemplateLookupDescriptor { TemplateDir : workingDir , WorkingDir : workingDir , LayoutDir : layoutBase1 , RelPath : layoutPathJSON } , true , "single-baseof.json" ,
2017-03-19 20:09:31 +00:00
TemplateNames {
Name : "_default/single.json" ,
OverlayFilename : "/sites/mysite/layouts/_default/single.json" ,
MasterFilename : "/sites/mysite/layouts/_default/single-baseof.json" ,
} } ,
} {
t . Run ( this . name , func ( t * testing . T ) {
2017-03-22 10:34:17 +00:00
this . basePathMatchStrings = filepath . FromSlash ( this . basePathMatchStrings )
2017-03-19 20:09:31 +00:00
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
}
2017-03-27 18:43:49 +00:00
this . d . OutputFormats = Formats { AMPFormat , HTMLFormat , RSSFormat , JSONFormat }
2017-03-19 20:09:31 +00:00
this . d . WorkingDir = filepath . FromSlash ( this . d . WorkingDir )
this . d . LayoutDir = filepath . FromSlash ( this . d . LayoutDir )
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 )
2017-03-27 18:43:49 +00:00
if strings . Contains ( this . d . RelPath , "json" ) {
// currently the only plain text templates in this test.
this . expect . Name = "_text/" + this . expect . Name
}
2017-03-22 10:34:17 +00:00
id , err := CreateTemplateNames ( this . d )
2017-03-19 20:09:31 +00:00
require . NoError ( t , err )
require . Equal ( t , this . expect , id , this . name )
} )
}
}