2016-10-30 12:59:24 -04:00
// Copyright 2016 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 hugolib
import (
"fmt"
"path/filepath"
2016-11-26 09:50:32 -05:00
"strings"
2016-10-30 12:59:24 -04:00
"testing"
2017-01-10 04:55:03 -05:00
2016-11-11 05:35:55 -05:00
"time"
2016-10-30 12:59:24 -04:00
2017-02-04 22:20:06 -05:00
"github.com/spf13/afero"
2017-01-10 04:55:03 -05:00
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/hugofs"
2016-10-30 12:59:24 -04:00
"github.com/stretchr/testify/require"
)
/ *
This file will test the "making everything a page" transition .
See https : //github.com/spf13/hugo/issues/2297
* /
2016-10-31 13:03:02 -04:00
func TestNodesAsPage ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-12-26 13:30:57 -05:00
for _ , preserveTaxonomyNames := range [ ] bool { false , true } {
for _ , ugly := range [ ] bool { true , false } {
doTestNodeAsPage ( t , ugly , preserveTaxonomyNames )
}
2016-11-26 09:50:32 -05:00
}
}
2016-12-26 13:30:57 -05:00
func doTestNodeAsPage ( t * testing . T , ugly , preserveTaxonomyNames bool ) {
2016-10-31 13:03:02 -04:00
2016-10-30 12:59:24 -04:00
/ * Will have to decide what to name the node content files , but :
Home page should have :
Content , shortcode support
Metadata ( title , dates etc . )
Params
Taxonomies ( categories , tags )
* /
2017-02-04 22:20:06 -05:00
var (
cfg , fs = newTestCfg ( )
2017-02-17 14:52:50 -05:00
th = testHelper { cfg , fs , t }
2017-02-04 22:20:06 -05:00
)
2016-10-31 05:23:01 -04:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "uglyURLs" , ugly )
cfg . Set ( "preserveTaxonomyNames" , preserveTaxonomyNames )
2016-10-30 12:59:24 -04:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "paginate" , 1 )
cfg . Set ( "title" , "Hugo Rocks" )
cfg . Set ( "rssURI" , "customrss.xml" )
2017-01-10 04:55:03 -05:00
2017-02-04 22:20:06 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
writeNodePagesForNodeAsPageTests ( t , fs , "" )
2017-01-10 04:55:03 -05:00
2017-02-04 22:20:06 -05:00
writeRegularPagesForNodeAsPageTests ( t , fs )
2017-01-10 04:55:03 -05:00
2017-02-04 22:20:06 -05:00
sites , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2017-01-10 04:55:03 -05:00
require . NoError ( t , err )
require . NoError ( t , sites . Build ( BuildCfg { } ) )
2016-10-30 12:59:24 -04:00
2016-11-11 05:35:55 -05:00
// date order: home, sect1, sect2, cat/hugo, cat/web, categories
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "index.html" ) ,
2016-10-30 12:59:24 -04:00
"Index Title: Home Sweet Home!" ,
"Home <strong>Content!</strong>" ,
2016-11-17 04:29:11 -05:00
"# Pages: 4" ,
2016-11-11 05:35:55 -05:00
"Date: 2009-01-02" ,
"Lastmod: 2009-01-03" ,
2016-11-12 11:30:21 -05:00
"GetPage: Section1 " ,
2016-11-11 05:35:55 -05:00
)
2016-10-31 05:23:01 -04:00
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "sect1" , "regular1" ) , "Single Title: Page 01" , "Content Page 01" )
2016-10-30 12:59:24 -04:00
2017-01-10 04:55:03 -05:00
nodes := sites . findAllPagesByKindNotIn ( KindPage )
2016-12-26 13:30:57 -05:00
2017-03-01 12:44:30 -05:00
require . Len ( t , nodes , 8 )
2016-10-30 12:59:24 -04:00
2017-03-03 19:00:11 -05:00
home := nodes [ 7 ] // oldest
2016-10-30 12:59:24 -04:00
require . True ( t , home . IsHome ( ) )
require . True ( t , home . IsNode ( ) )
require . False ( t , home . IsPage ( ) )
2016-11-13 08:27:10 -05:00
require . True ( t , home . Path ( ) != "" )
2016-10-30 12:59:24 -04:00
2017-03-03 19:00:11 -05:00
section2 := nodes [ 5 ]
2016-11-11 05:35:55 -05:00
require . Equal ( t , "Section2" , section2 . Title )
2017-01-10 04:55:03 -05:00
pages := sites . findAllPagesByKind ( KindPage )
2016-10-31 13:03:02 -04:00
require . Len ( t , pages , 4 )
2016-10-30 12:59:24 -04:00
first := pages [ 0 ]
2016-11-11 05:35:55 -05:00
2016-10-30 12:59:24 -04:00
require . False ( t , first . IsHome ( ) )
require . False ( t , first . IsNode ( ) )
require . True ( t , first . IsPage ( ) )
2016-10-31 13:03:02 -04:00
// Check Home paginator
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "page" , "2" ) ,
2016-10-31 13:03:02 -04:00
"Pag: Page 02" )
// Check Sections
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "sect1" ) ,
2016-11-11 05:35:55 -05:00
"Section Title: Section" , "Section1 <strong>Content!</strong>" ,
"Date: 2009-01-04" ,
"Lastmod: 2009-01-05" ,
)
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "sect2" ) ,
2016-11-11 05:35:55 -05:00
"Section Title: Section" , "Section2 <strong>Content!</strong>" ,
"Date: 2009-01-06" ,
"Lastmod: 2009-01-07" ,
)
2016-10-31 13:03:02 -04:00
// Check Sections paginator
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "sect1" , "page" , "2" ) ,
2016-10-31 13:03:02 -04:00
"Pag: Page 02" )
2017-01-10 04:55:03 -05:00
sections := sites . findAllPagesByKind ( KindSection )
2016-11-10 06:26:23 -05:00
2016-10-31 13:03:02 -04:00
require . Len ( t , sections , 2 )
2016-10-31 05:23:01 -04:00
2016-11-01 11:47:15 -04:00
// Check taxonomy lists
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "categories" , "hugo" ) ,
2016-11-11 05:35:55 -05:00
"Taxonomy Title: Taxonomy Hugo" , "Taxonomy Hugo <strong>Content!</strong>" ,
"Date: 2009-01-08" ,
"Lastmod: 2009-01-09" ,
)
2016-10-31 14:53:33 -04:00
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "categories" , "hugo-rocks" ) ,
2017-01-02 06:28:26 -05:00
"Taxonomy Title: Taxonomy Hugo Rocks" ,
)
2017-01-10 04:55:03 -05:00
s := sites . Sites [ 0 ]
2016-12-26 13:30:57 -05:00
web := s . getPage ( KindTaxonomy , "categories" , "web" )
require . NotNil ( t , web )
require . Len ( t , web . Data [ "Pages" ] . ( Pages ) , 4 )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "categories" , "web" ) ,
2016-11-11 05:35:55 -05:00
"Taxonomy Title: Taxonomy Web" ,
"Taxonomy Web <strong>Content!</strong>" ,
"Date: 2009-01-10" ,
"Lastmod: 2009-01-11" ,
)
2016-11-01 11:47:15 -04:00
2016-10-31 14:53:33 -04:00
// Check taxonomy list paginator
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "categories" , "hugo" , "page" , "2" ) ,
2016-10-31 14:53:33 -04:00
"Taxonomy Title: Taxonomy Hugo" ,
"Pag: Page 02" )
2016-11-01 11:47:15 -04:00
// Check taxonomy terms
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "categories" ) ,
2016-11-11 05:35:55 -05:00
"Taxonomy Terms Title: Taxonomy Term Categories" , "Taxonomy Term Categories <strong>Content!</strong>" , "k/v: hugo" ,
2017-01-02 06:28:26 -05:00
"Date: 2009-01-14" ,
"Lastmod: 2009-01-15" ,
2016-11-11 05:35:55 -05:00
)
2016-11-01 11:47:15 -04:00
// There are no pages to paginate over in the taxonomy terms.
2016-11-02 16:34:19 -04:00
// RSS
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "customrss.xml" ) , "Recent content in Home Sweet Home! on Hugo Rocks" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "sect1" , "customrss.xml" ) , "Recent content in Section1 on Hugo Rocks" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "sect2" , "customrss.xml" ) , "Recent content in Section2 on Hugo Rocks" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "categories" , "hugo" , "customrss.xml" ) , "Recent content in Taxonomy Hugo on Hugo Rocks" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "categories" , "web" , "customrss.xml" ) , "Recent content in Taxonomy Web on Hugo Rocks" , "<rss" )
2016-11-02 16:34:19 -04:00
2016-10-30 12:59:24 -04:00
}
2016-11-01 17:39:24 -04:00
func TestNodesWithNoContentFile ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-11-26 09:50:32 -05:00
for _ , ugly := range [ ] bool { false , true } {
doTestNodesWithNoContentFile ( t , ugly )
}
}
func doTestNodesWithNoContentFile ( t * testing . T , ugly bool ) {
2016-11-01 17:39:24 -04:00
2017-02-04 22:20:06 -05:00
var (
cfg , fs = newTestCfg ( )
2017-02-17 14:52:50 -05:00
th = testHelper { cfg , fs , t }
2017-02-04 22:20:06 -05:00
)
2016-11-01 17:39:24 -04:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "uglyURLs" , ugly )
cfg . Set ( "paginate" , 1 )
cfg . Set ( "title" , "Hugo Rocks!" )
cfg . Set ( "rssURI" , "customrss.xml" )
2016-11-01 17:39:24 -04:00
2017-01-10 04:55:03 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
writeRegularPagesForNodeAsPageTests ( t , fs )
2017-02-04 22:20:06 -05:00
sites , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2017-01-10 04:55:03 -05:00
require . NoError ( t , err )
require . NoError ( t , sites . Build ( BuildCfg { } ) )
s := sites . Sites [ 0 ]
2016-11-01 17:39:24 -04:00
// Home page
2016-11-16 11:52:03 -05:00
homePages := s . findPagesByKind ( KindHome )
2016-11-01 17:39:24 -04:00
require . Len ( t , homePages , 1 )
homePage := homePages [ 0 ]
2016-11-17 04:29:11 -05:00
require . Len ( t , homePage . Data [ "Pages" ] , 4 )
require . Len ( t , homePage . Pages , 4 )
2016-11-13 08:27:10 -05:00
require . True ( t , homePage . Path ( ) == "" )
2016-11-01 17:39:24 -04:00
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "index.html" ) ,
2016-11-11 05:35:55 -05:00
"Index Title: Hugo Rocks!" ,
"Date: 2010-06-12" ,
"Lastmod: 2010-06-13" ,
)
2016-11-01 17:39:24 -04:00
// Taxonomy list
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "categories" , "hugo" ) ,
2016-11-11 05:35:55 -05:00
"Taxonomy Title: Hugo" ,
"Date: 2010-06-12" ,
"Lastmod: 2010-06-13" ,
)
2016-11-01 17:39:24 -04:00
// Taxonomy terms
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "categories" ) ,
2016-11-11 05:35:55 -05:00
"Taxonomy Terms Title: Categories" ,
)
2016-11-01 17:39:24 -04:00
2016-12-26 21:36:08 -05:00
pages := s . findPagesByKind ( KindTaxonomyTerm )
for _ , p := range pages {
var want string
if ugly {
2017-01-10 04:55:03 -05:00
want = "/" + p . s . PathSpec . URLize ( p . Title ) + ".html"
2016-12-26 21:36:08 -05:00
} else {
2017-01-10 04:55:03 -05:00
want = "/" + p . s . PathSpec . URLize ( p . Title ) + "/"
2016-12-26 21:36:08 -05:00
}
if p . URL ( ) != want {
t . Errorf ( "Taxonomy term URL mismatch: want %q, got %q" , want , p . URL ( ) )
}
}
2016-11-01 17:39:24 -04:00
// Sections
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "sect1" ) ,
2016-11-11 05:35:55 -05:00
"Section Title: Sect1s" ,
"Date: 2010-06-12" ,
"Lastmod: 2010-06-13" ,
)
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "sect2" ) ,
2016-11-11 05:35:55 -05:00
"Section Title: Sect2s" ,
"Date: 2008-07-06" ,
"Lastmod: 2008-07-09" ,
)
2016-11-01 17:39:24 -04:00
2016-11-02 16:34:19 -04:00
// RSS
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "customrss.xml" ) , "Hugo Rocks!" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "sect1" , "customrss.xml" ) , "Recent content in Sect1s on Hugo Rocks!" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "sect2" , "customrss.xml" ) , "Recent content in Sect2s on Hugo Rocks!" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "categories" , "hugo" , "customrss.xml" ) , "Recent content in Hugo on Hugo Rocks!" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "categories" , "web" , "customrss.xml" ) , "Recent content in Web on Hugo Rocks!" , "<rss" )
2016-11-02 16:34:19 -04:00
2016-11-01 17:39:24 -04:00
}
2016-11-07 14:24:37 -05:00
func TestNodesAsPageMultilingual ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2017-01-10 04:55:03 -05:00
for _ , ugly := range [ ] bool { false , true } {
2016-11-26 09:50:32 -05:00
doTestNodesAsPageMultilingual ( t , ugly )
}
}
func doTestNodesAsPageMultilingual ( t * testing . T , ugly bool ) {
2016-11-07 14:24:37 -05:00
2017-02-04 22:20:06 -05:00
mf := afero . NewMemMapFs ( )
2016-11-07 14:24:37 -05:00
2017-02-04 22:20:06 -05:00
writeToFs ( t , mf , "config.toml" ,
2016-11-07 14:24:37 -05:00
`
paginage = 1
title = "Hugo Multilingual Rocks!"
rssURI = "customrss.xml"
2016-11-21 04:11:34 -05:00
defaultContentLanguage = "nn"
defaultContentLanguageInSubdir = true
2016-11-07 14:24:37 -05:00
[ languages ]
[ languages . nn ]
languageName = "Nynorsk"
weight = 1
title = "Hugo på norsk"
[ languages . en ]
languageName = "English"
weight = 2
title = "Hugo in English"
2016-11-21 04:11:34 -05:00
[ languages . de ]
languageName = "Deutsch"
weight = 3
title = "Deutsche Hugo"
2016-11-07 14:24:37 -05:00
` )
2017-02-04 22:20:06 -05:00
cfg , err := LoadConfig ( mf , "" , "config.toml" )
require . NoError ( t , err )
cfg . Set ( "uglyURLs" , ugly )
fs := hugofs . NewFrom ( mf , cfg )
2017-01-10 04:55:03 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
2016-11-07 14:24:37 -05:00
for _ , lang := range [ ] string { "nn" , "en" } {
2017-01-10 04:55:03 -05:00
writeRegularPagesForNodeAsPageTestsWithLang ( t , fs , lang )
2016-11-07 14:24:37 -05:00
}
2017-02-17 14:52:50 -05:00
th := testHelper { cfg , fs , t }
2016-11-07 14:24:37 -05:00
2017-02-04 22:20:06 -05:00
sites , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2016-11-07 14:24:37 -05:00
if err != nil {
t . Fatalf ( "Failed to create sites: %s" , err )
}
2016-11-21 04:11:34 -05:00
if len ( sites . Sites ) != 3 {
2016-11-07 14:24:37 -05:00
t . Fatalf ( "Got %d sites" , len ( sites . Sites ) )
}
2017-01-10 04:55:03 -05:00
// Only write node pages for the English and Deutsch
writeNodePagesForNodeAsPageTests ( t , fs , "en" )
writeNodePagesForNodeAsPageTests ( t , fs , "de" )
2016-11-07 14:24:37 -05:00
err = sites . Build ( BuildCfg { } )
if err != nil {
t . Fatalf ( "Failed to build sites: %s" , err )
}
2016-11-21 04:11:34 -05:00
// The en and de language have content pages
enHome := sites . Sites [ 1 ] . getPage ( "home" )
require . NotNil ( t , enHome )
require . Equal ( t , "en" , enHome . Language ( ) . Lang )
require . Contains ( t , enHome . Content , "l-en" )
deHome := sites . Sites [ 2 ] . getPage ( "home" )
require . NotNil ( t , deHome )
require . Equal ( t , "de" , deHome . Language ( ) . Lang )
require . Contains ( t , deHome . Content , "l-de" )
require . Len ( t , deHome . Translations ( ) , 2 , deHome . Translations ( ) [ 0 ] . Language ( ) . Lang )
require . Equal ( t , "en" , deHome . Translations ( ) [ 1 ] . Language ( ) . Lang )
require . Equal ( t , "nn" , deHome . Translations ( ) [ 0 ] . Language ( ) . Lang )
2016-11-26 09:50:32 -05:00
require . Equal ( t , expetedPermalink ( ugly , "/de/" ) , deHome . Permalink ( ) )
2016-11-21 04:11:34 -05:00
enSect := sites . Sites [ 1 ] . getPage ( "section" , "sect1" )
require . NotNil ( t , enSect )
require . Equal ( t , "en" , enSect . Language ( ) . Lang )
require . Len ( t , enSect . Translations ( ) , 2 , enSect . Translations ( ) [ 0 ] . Language ( ) . Lang )
require . Equal ( t , "de" , enSect . Translations ( ) [ 1 ] . Language ( ) . Lang )
require . Equal ( t , "nn" , enSect . Translations ( ) [ 0 ] . Language ( ) . Lang )
2016-11-07 14:24:37 -05:00
2016-11-26 09:50:32 -05:00
require . Equal ( t , expetedPermalink ( ugly , "/en/sect1/" ) , enSect . Permalink ( ) )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "nn" , "index.html" ) ,
2016-11-07 14:24:37 -05:00
"Index Title: Hugo på norsk" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "en" , "index.html" ) ,
2016-11-07 14:24:37 -05:00
"Index Title: Home Sweet Home!" , "<strong>Content!</strong>" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "de" , "index.html" ) ,
2016-11-21 04:11:34 -05:00
"Index Title: Home Sweet Home!" , "<strong>Content!</strong>" )
2016-11-07 14:24:37 -05:00
// Taxonomy list
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "nn" , "categories" , "hugo" ) ,
2016-11-07 14:24:37 -05:00
"Taxonomy Title: Hugo" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "en" , "categories" , "hugo" ) ,
2016-11-07 14:24:37 -05:00
"Taxonomy Title: Taxonomy Hugo" )
// Taxonomy terms
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "nn" , "categories" ) ,
2016-11-07 14:24:37 -05:00
"Taxonomy Terms Title: Categories" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "en" , "categories" ) ,
2016-11-07 14:24:37 -05:00
"Taxonomy Terms Title: Taxonomy Term Categories" )
// Sections
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "nn" , "sect1" ) ,
2016-11-07 14:24:37 -05:00
"Section Title: Sect1s" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "nn" , "sect2" ) ,
2016-11-07 14:24:37 -05:00
"Section Title: Sect2s" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "en" , "sect1" ) ,
2016-11-07 14:24:37 -05:00
"Section Title: Section1" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "en" , "sect2" ) ,
2016-11-07 14:24:37 -05:00
"Section Title: Section2" )
2016-11-26 09:50:32 -05:00
// Regular pages
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "en" , "sect1" , "regular1" ) ,
2016-11-26 09:50:32 -05:00
"Single Title: Page 01" )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( expectedFilePath ( ugly , "public" , "nn" , "sect1" , "regular2" ) ,
2016-11-26 09:50:32 -05:00
"Single Title: Page 02" )
2016-11-07 14:24:37 -05:00
// RSS
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "nn" , "customrss.xml" ) , "Hugo på norsk" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "nn" , "sect1" , "customrss.xml" ) , "Recent content in Sect1s on Hugo på norsk" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "nn" , "sect2" , "customrss.xml" ) , "Recent content in Sect2s on Hugo på norsk" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "nn" , "categories" , "hugo" , "customrss.xml" ) , "Recent content in Hugo on Hugo på norsk" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "nn" , "categories" , "web" , "customrss.xml" ) , "Recent content in Web on Hugo på norsk" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "en" , "customrss.xml" ) , "Recent content in Home Sweet Home! on Hugo in English" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "en" , "sect1" , "customrss.xml" ) , "Recent content in Section1 on Hugo in English" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "en" , "sect2" , "customrss.xml" ) , "Recent content in Section2 on Hugo in English" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "en" , "categories" , "hugo" , "customrss.xml" ) , "Recent content in Taxonomy Hugo on Hugo in English" , "<rss" )
th . assertFileContent ( filepath . Join ( "public" , "en" , "categories" , "web" , "customrss.xml" ) , "Recent content in Taxonomy Web on Hugo in English" , "<rss" )
2016-11-07 14:24:37 -05:00
}
2016-11-09 14:55:42 -05:00
func TestNodesWithTaxonomies ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
var (
cfg , fs = newTestCfg ( )
2017-02-17 14:52:50 -05:00
th = testHelper { cfg , fs , t }
2017-02-04 22:20:06 -05:00
)
2017-01-10 04:55:03 -05:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "paginate" , 1 )
cfg . Set ( "title" , "Hugo Rocks!" )
2016-11-09 14:55:42 -05:00
2017-01-10 04:55:03 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
writeRegularPagesForNodeAsPageTests ( t , fs )
writeSource ( t , fs , filepath . Join ( "content" , "_index.md" ) , ` -- -
2016-11-09 14:55:42 -05:00
title : Home With Taxonomies
categories : [
2017-01-10 04:55:03 -05:00
"Hugo" ,
2016-11-09 14:55:42 -05:00
"Home"
]
-- -
` )
2017-02-04 22:20:06 -05:00
h , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2016-11-09 14:55:42 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , err )
2016-11-09 14:55:42 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , h . Build ( BuildCfg { } ) )
2016-11-09 14:55:42 -05:00
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "categories" , "hugo" , "index.html" ) , "Taxonomy Title: Hugo" , "# Pages: 5" )
th . assertFileContent ( filepath . Join ( "public" , "categories" , "home" , "index.html" ) , "Taxonomy Title: Home" , "# Pages: 1" )
2016-11-09 14:55:42 -05:00
}
2016-11-10 04:53:12 -05:00
func TestNodesWithMenu ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
var (
cfg , fs = newTestCfg ( )
2017-02-17 14:52:50 -05:00
th = testHelper { cfg , fs , t }
2017-02-04 22:20:06 -05:00
)
2017-01-10 04:55:03 -05:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "paginate" , 1 )
cfg . Set ( "title" , "Hugo Rocks!" )
2016-11-10 04:53:12 -05:00
2017-01-10 04:55:03 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
writeRegularPagesForNodeAsPageTests ( t , fs )
writeSource ( t , fs , filepath . Join ( "content" , "_index.md" ) , ` -- -
2016-11-10 04:53:12 -05:00
title : Home With Menu
menu :
mymenu :
name : "Go Home!"
-- -
2016-12-11 07:01:11 -05:00
` )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "sect1" , "_index.md" ) , ` -- -
2016-12-11 07:01:11 -05:00
title : Sect1 With Menu
menu :
mymenu :
name : "Go Sect1!"
-- -
` )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "categories" , "hugo" , "_index.md" ) , ` -- -
2016-12-11 07:01:11 -05:00
title : Taxonomy With Menu
menu :
mymenu :
name : "Go Tax Hugo!"
-- -
2016-11-10 04:53:12 -05:00
` )
2017-02-04 22:20:06 -05:00
h , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2016-11-10 04:53:12 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , err )
2016-11-10 04:53:12 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , h . Build ( BuildCfg { } ) )
2016-11-10 04:53:12 -05:00
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "index.html" ) , "Home With Menu" , "Home Menu Item: Go Home!: /" )
th . assertFileContent ( filepath . Join ( "public" , "sect1" , "index.html" ) , "Sect1 With Menu" , "Section Menu Item: Go Sect1!: /sect1/" )
th . assertFileContent ( filepath . Join ( "public" , "categories" , "hugo" , "index.html" ) , "Taxonomy With Menu" , "Taxonomy Menu Item: Go Tax Hugo!: /categories/hugo/" )
2016-11-10 04:53:12 -05:00
}
2016-11-10 05:01:58 -05:00
func TestNodesWithAlias ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
var (
cfg , fs = newTestCfg ( )
2017-02-17 14:52:50 -05:00
th = testHelper { cfg , fs , t }
2017-02-04 22:20:06 -05:00
)
2017-01-10 04:55:03 -05:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "paginate" , 1 )
cfg . Set ( "baseURL" , "http://base/" )
cfg . Set ( "title" , "Hugo Rocks!" )
2017-01-10 04:55:03 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
writeRegularPagesForNodeAsPageTests ( t , fs )
2016-11-10 05:01:58 -05:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "_index.md" ) , ` -- -
2016-11-10 05:01:58 -05:00
title : Home With Alias
aliases :
- / my / new / home . html
-- -
` )
2017-02-04 22:20:06 -05:00
h , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2016-11-10 05:01:58 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , err )
2016-11-10 05:01:58 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , h . Build ( BuildCfg { } ) )
2016-11-10 05:01:58 -05:00
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "index.html" ) , "Home With Alias" )
th . assertFileContent ( filepath . Join ( "public" , "my" , "new" , "home.html" ) , "content=\"0; url=http://base/" )
2016-11-10 05:01:58 -05:00
}
2016-11-10 06:26:23 -05:00
func TestNodesWithSectionWithIndexPageOnly ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
var (
cfg , fs = newTestCfg ( )
2017-02-17 14:52:50 -05:00
th = testHelper { cfg , fs , t }
2017-02-04 22:20:06 -05:00
)
2016-11-10 06:26:23 -05:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "paginate" , 1 )
cfg . Set ( "title" , "Hugo Rocks!" )
2017-01-10 04:55:03 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
writeSource ( t , fs , filepath . Join ( "content" , "sect" , "_index.md" ) , ` -- -
2016-11-10 06:26:23 -05:00
title : MySection
-- -
My Section Content
` )
2017-02-04 22:20:06 -05:00
h , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2016-11-10 06:26:23 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , err )
2016-11-10 06:26:23 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , h . Build ( BuildCfg { } ) )
2016-11-10 06:26:23 -05:00
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "sect" , "index.html" ) , "My Section" )
2016-11-10 06:26:23 -05:00
}
2016-11-15 04:43:49 -05:00
func TestNodesWithURLs ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
var (
cfg , fs = newTestCfg ( )
2017-02-17 14:52:50 -05:00
th = testHelper { cfg , fs , t }
2017-02-04 22:20:06 -05:00
)
2016-11-15 04:43:49 -05:00
2017-02-04 22:20:06 -05:00
cfg . Set ( "paginate" , 1 )
cfg . Set ( "title" , "Hugo Rocks!" )
cfg . Set ( "baseURL" , "http://bep.is/base/" )
2017-01-10 04:55:03 -05:00
writeLayoutsForNodeAsPageTests ( t , fs )
writeRegularPagesForNodeAsPageTests ( t , fs )
2016-11-15 04:43:49 -05:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "sect" , "_index.md" ) , ` -- -
2016-11-15 04:43:49 -05:00
title : MySection
url : foo . html
-- -
My Section Content
` )
2017-02-04 22:20:06 -05:00
h , err := NewHugoSites ( deps . DepsCfg { Fs : fs , Cfg : cfg } )
2016-11-15 04:43:49 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , err )
2016-11-15 04:43:49 -05:00
2017-01-10 04:55:03 -05:00
require . NoError ( t , h . Build ( BuildCfg { } ) )
2017-02-17 15:14:52 -05:00
th . assertFileContent ( filepath . Join ( "public" , "sect" , "index.html" ) , "My Section" )
2016-11-15 04:43:49 -05:00
2017-01-10 04:55:03 -05:00
s := h . Sites [ 0 ]
2016-11-15 04:43:49 -05:00
p := s . RegularPages [ 0 ]
require . Equal ( t , "/base/sect1/regular1/" , p . URL ( ) )
// Section with front matter and url set (which should not be used)
sect := s . getPage ( KindSection , "sect" )
require . Equal ( t , "/base/sect/" , sect . URL ( ) )
require . Equal ( t , "http://bep.is/base/sect/" , sect . Permalink ( ) )
require . Equal ( t , "/base/sect/" , sect . RelPermalink ( ) )
// Home page without front matter
require . Equal ( t , "/base/" , s . getPage ( KindHome ) . URL ( ) )
}
2017-01-10 04:55:03 -05:00
func writeRegularPagesForNodeAsPageTests ( t * testing . T , fs * hugofs . Fs ) {
writeRegularPagesForNodeAsPageTestsWithLang ( t , fs , "" )
2016-11-09 14:55:42 -05:00
}
2017-01-10 04:55:03 -05:00
func writeRegularPagesForNodeAsPageTestsWithLang ( t * testing . T , fs * hugofs . Fs , lang string ) {
2016-11-09 14:55:42 -05:00
var langStr string
if lang != "" {
langStr = lang + "."
}
2016-11-11 05:35:55 -05:00
format := "2006-01-02"
date , _ := time . Parse ( format , "2010-06-15" )
2016-11-09 14:55:42 -05:00
for i := 1 ; i <= 4 ; i ++ {
sect := "sect1"
if i > 2 {
sect = "sect2"
2016-11-11 05:35:55 -05:00
date , _ = time . Parse ( format , "2008-07-15" ) // Nodes are placed in 2009
2016-11-09 14:55:42 -05:00
}
2016-11-11 05:35:55 -05:00
date = date . Add ( - 24 * time . Duration ( i ) * time . Hour )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , sect , fmt . Sprintf ( "regular%d.%smd" , i , langStr ) ) , fmt . Sprintf ( ` -- -
2016-11-09 14:55:42 -05:00
title : Page % 02 d
2016-11-11 05:35:55 -05:00
lastMod : % q
date : % q
2016-11-09 14:55:42 -05:00
categories : [
"Hugo" ,
2017-01-02 06:28:26 -05:00
"Web" ,
"Hugo Rocks!"
2016-11-09 14:55:42 -05:00
]
-- -
Content Page % 02 d
2016-11-11 05:35:55 -05:00
` , i , date . Add ( time . Duration ( i ) * - 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( time . Duration ( i ) * - 2 * 24 * time . Hour ) . Format ( time . RFC822 ) , i ) )
2016-11-09 14:55:42 -05:00
}
}
2017-01-10 04:55:03 -05:00
func writeNodePagesForNodeAsPageTests ( t * testing . T , fs * hugofs . Fs , lang string ) {
2016-11-07 14:24:37 -05:00
filename := "_index.md"
if lang != "" {
filename = fmt . Sprintf ( "_index.%s.md" , lang )
}
2016-11-11 05:35:55 -05:00
format := "2006-01-02"
date , _ := time . Parse ( format , "2009-01-01" )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , filename ) , fmt . Sprintf ( ` -- -
2016-11-07 14:24:37 -05:00
title : Home Sweet Home !
2016-11-11 05:35:55 -05:00
date : % q
lastMod : % q
2016-11-07 14:24:37 -05:00
-- -
2016-11-21 04:11:34 -05:00
l - % s Home * * Content ! * *
` , date . Add ( 1 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 2 * 24 * time . Hour ) . Format ( time . RFC822 ) , lang ) )
2016-11-07 14:24:37 -05:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "sect1" , filename ) , fmt . Sprintf ( ` -- -
2016-11-07 14:24:37 -05:00
title : Section1
2016-11-11 05:35:55 -05:00
date : % q
lastMod : % q
2016-11-07 14:24:37 -05:00
-- -
Section1 * * Content ! * *
2016-11-11 05:35:55 -05:00
` , date . Add ( 3 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 4 * 24 * time . Hour ) . Format ( time . RFC822 ) ) )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "sect2" , filename ) , fmt . Sprintf ( ` -- -
2016-11-07 14:24:37 -05:00
title : Section2
2016-11-11 05:35:55 -05:00
date : % q
lastMod : % q
2016-11-07 14:24:37 -05:00
-- -
Section2 * * Content ! * *
2016-11-11 05:35:55 -05:00
` , date . Add ( 5 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 6 * 24 * time . Hour ) . Format ( time . RFC822 ) ) )
2016-11-07 14:24:37 -05:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "categories" , "hugo" , filename ) , fmt . Sprintf ( ` -- -
2016-11-07 14:24:37 -05:00
title : Taxonomy Hugo
2016-11-11 05:35:55 -05:00
date : % q
lastMod : % q
2016-11-07 14:24:37 -05:00
-- -
Taxonomy Hugo * * Content ! * *
2016-11-11 05:35:55 -05:00
` , date . Add ( 7 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 8 * 24 * time . Hour ) . Format ( time . RFC822 ) ) )
2016-11-07 14:24:37 -05:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "categories" , "web" , filename ) , fmt . Sprintf ( ` -- -
2016-11-07 14:24:37 -05:00
title : Taxonomy Web
2016-11-11 05:35:55 -05:00
date : % q
lastMod : % q
2016-11-07 14:24:37 -05:00
-- -
Taxonomy Web * * Content ! * *
2016-11-11 05:35:55 -05:00
` , date . Add ( 9 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 10 * 24 * time . Hour ) . Format ( time . RFC822 ) ) )
2016-11-07 14:24:37 -05:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "categories" , "hugo-rocks" , filename ) , fmt . Sprintf ( ` -- -
2017-01-02 06:28:26 -05:00
title : Taxonomy Hugo Rocks
date : % q
lastMod : % q
-- -
Taxonomy Hugo Rocks * * Content ! * *
` , date . Add ( 11 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 12 * 24 * time . Hour ) . Format ( time . RFC822 ) ) )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "categories" , filename ) , fmt . Sprintf ( ` -- -
2016-11-07 14:24:37 -05:00
title : Taxonomy Term Categories
2016-11-11 05:35:55 -05:00
date : % q
lastMod : % q
2016-11-07 14:24:37 -05:00
-- -
Taxonomy Term Categories * * Content ! * *
2017-01-02 06:28:26 -05:00
` , date . Add ( 13 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 14 * 24 * time . Hour ) . Format ( time . RFC822 ) ) )
2017-03-03 19:00:11 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "tags" , filename ) , fmt . Sprintf ( ` -- -
title : Taxonomy Term Tags
date : % q
lastMod : % q
-- -
Taxonomy Term Tags * * Content ! * *
` , date . Add ( 15 * 24 * time . Hour ) . Format ( time . RFC822 ) , date . Add ( 16 * 24 * time . Hour ) . Format ( time . RFC822 ) ) )
2016-11-07 14:24:37 -05:00
}
2017-01-10 04:55:03 -05:00
func writeLayoutsForNodeAsPageTests ( t * testing . T , fs * hugofs . Fs ) {
writeSource ( t , fs , filepath . Join ( "layouts" , "index.html" ) , `
2016-11-01 17:39:24 -04:00
Index Title : { { . Title } }
Index Content : { { . Content } }
# Pages : { { len . Data . Pages } }
{ { range . Paginator . Pages } }
Pag : { { . Title } }
{ { end } }
2016-11-10 04:53:12 -05:00
{ { with . Site . Menus . mymenu } }
{ { range . } }
2016-12-11 07:01:11 -05:00
Home Menu Item : { { . Name } } : { { . URL } }
2016-11-10 04:53:12 -05:00
{ { end } }
{ { end } }
2016-11-11 05:35:55 -05:00
Date : { { . Date . Format "2006-01-02" } }
Lastmod : { { . Lastmod . Format "2006-01-02" } }
2016-11-12 11:30:21 -05:00
GetPage : { { with . Site . GetPage "section" "sect1" } } { { . Title } } { { end } }
2016-11-01 17:39:24 -04:00
` )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "layouts" , "_default" , "single.html" ) , `
2016-11-01 17:39:24 -04:00
Single Title : { { . Title } }
Single Content : { { . Content } }
2016-11-11 05:35:55 -05:00
Date : { { . Date . Format "2006-01-02" } }
Lastmod : { { . Lastmod . Format "2006-01-02" } }
2016-11-01 17:39:24 -04:00
` )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "layouts" , "_default" , "section.html" ) , `
2016-11-01 17:39:24 -04:00
Section Title : { { . Title } }
Section Content : { { . Content } }
# Pages : { { len . Data . Pages } }
{ { range . Paginator . Pages } }
Pag : { { . Title } }
{ { end } }
2016-12-11 07:01:11 -05:00
{ { with . Site . Menus . mymenu } }
{ { range . } }
Section Menu Item : { { . Name } } : { { . URL } }
{ { end } }
{ { end } }
2016-11-11 05:35:55 -05:00
Date : { { . Date . Format "2006-01-02" } }
Lastmod : { { . Lastmod . Format "2006-01-02" } }
2016-11-01 17:39:24 -04:00
` )
// Taxonomy lists
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "layouts" , "_default" , "taxonomy.html" ) , `
2016-11-01 17:39:24 -04:00
Taxonomy Title : { { . Title } }
Taxonomy Content : { { . Content } }
# Pages : { { len . Data . Pages } }
{ { range . Paginator . Pages } }
Pag : { { . Title } }
{ { end } }
2016-12-11 07:01:11 -05:00
{ { with . Site . Menus . mymenu } }
{ { range . } }
Taxonomy Menu Item : { { . Name } } : { { . URL } }
{ { end } }
{ { end } }
2016-11-11 05:35:55 -05:00
Date : { { . Date . Format "2006-01-02" } }
Lastmod : { { . Lastmod . Format "2006-01-02" } }
2016-11-01 17:39:24 -04:00
` )
// Taxonomy terms
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "layouts" , "_default" , "terms.html" ) , `
2016-11-01 17:39:24 -04:00
Taxonomy Terms Title : { { . Title } }
Taxonomy Terms Content : { { . Content } }
{ { range $ key , $ value := . Data . Terms } }
2016-12-26 13:30:57 -05:00
k / v : { { $ key | lower } } / { { printf "%s" $ value } }
2016-11-01 17:39:24 -04:00
{ { end } }
2016-12-11 07:01:11 -05:00
{ { with . Site . Menus . mymenu } }
{ { range . } }
Taxonomy Terms Menu Item : { { . Name } } : { { . URL } }
{ { end } }
{ { end } }
2016-11-11 05:35:55 -05:00
Date : { { . Date . Format "2006-01-02" } }
Lastmod : { { . Lastmod . Format "2006-01-02" } }
2016-11-01 17:39:24 -04:00
` )
}
2016-11-26 09:50:32 -05:00
func expectedFilePath ( ugly bool , path ... string ) string {
if ugly {
return filepath . Join ( append ( path [ 0 : len ( path ) - 1 ] , path [ len ( path ) - 1 ] + ".html" ) ... )
}
return filepath . Join ( append ( path , "index.html" ) ... )
}
func expetedPermalink ( ugly bool , path string ) string {
if ugly {
return strings . TrimSuffix ( path , "/" ) + ".html"
}
return path
}