2018-10-03 08:58:09 -04:00
package hugolib
import (
"fmt"
2022-05-02 10:07:52 -04:00
"os"
2018-10-21 06:20:21 -04:00
"path/filepath"
2018-10-03 08:58:09 -04:00
"strings"
"testing"
2019-01-02 06:33:26 -05:00
2020-12-23 13:47:20 -05:00
"github.com/gohugoio/hugo/htesting"
2018-10-03 08:58:09 -04:00
2019-08-10 15:05:17 -04:00
qt "github.com/frankban/quicktest"
2018-10-03 08:58:09 -04:00
"github.com/gohugoio/hugo/common/herrors"
)
type testSiteBuildErrorAsserter struct {
2019-08-10 15:05:17 -04:00
name string
c * qt . C
2018-10-03 08:58:09 -04:00
}
2022-05-02 10:07:52 -04:00
func ( t testSiteBuildErrorAsserter ) getFileError ( err error ) herrors . FileError {
2019-08-10 15:05:17 -04:00
t . c . Assert ( err , qt . Not ( qt . IsNil ) , qt . Commentf ( t . name ) )
2022-05-02 10:07:52 -04:00
fe := herrors . UnwrapFileError ( err )
t . c . Assert ( fe , qt . Not ( qt . IsNil ) )
return fe
2018-10-03 08:58:09 -04:00
}
func ( t testSiteBuildErrorAsserter ) assertLineNumber ( lineNumber int , err error ) {
2022-05-02 10:07:52 -04:00
t . c . Helper ( )
2018-10-03 08:58:09 -04:00
fe := t . getFileError ( err )
2020-01-15 09:59:56 -05:00
t . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , lineNumber , qt . Commentf ( err . Error ( ) ) )
2018-10-21 06:20:21 -04:00
}
func ( t testSiteBuildErrorAsserter ) assertErrorMessage ( e1 , e2 string ) {
// The error message will contain filenames with OS slashes. Normalize before compare.
e1 , e2 = filepath . ToSlash ( e1 ) , filepath . ToSlash ( e2 )
2019-08-10 15:05:17 -04:00
t . c . Assert ( e2 , qt . Contains , e1 )
2018-10-03 08:58:09 -04:00
}
func TestSiteBuildErrors ( t * testing . T ) {
const (
yamlcontent = "yamlcontent"
2018-10-21 06:20:21 -04:00
tomlcontent = "tomlcontent"
2018-11-01 06:28:30 -04:00
jsoncontent = "jsoncontent"
2018-10-03 08:58:09 -04:00
shortcode = "shortcode"
base = "base"
single = "single"
)
// TODO(bep) add content tests after https://github.com/gohugoio/hugo/issues/5324
// is implemented.
tests := [ ] struct {
name string
fileType string
fileFixer func ( content string ) string
assertCreateError func ( a testSiteBuildErrorAsserter , err error )
assertBuildError func ( a testSiteBuildErrorAsserter , err error )
} {
{
name : "Base template parse failed" ,
fileType : base ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title }}" , ".Title }" , 1 )
} ,
2020-07-13 07:40:35 -04:00
// Base templates gets parsed at build time.
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
a . assertLineNumber ( 4 , err )
2018-10-03 08:58:09 -04:00
} ,
} ,
{
name : "Base template execute failed" ,
fileType : base ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Titles" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
a . assertLineNumber ( 4 , err )
2018-10-03 08:58:09 -04:00
} ,
} ,
{
name : "Single template parse failed" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title }}" , ".Title }" , 1 )
} ,
assertCreateError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 1 )
2020-07-13 07:40:35 -04:00
a . assertErrorMessage ( "\"layouts/foo/single.html:5:1\": parse failed: template: foo/single.html:5: unexpected \"}\" in operand" , fe . Error ( ) )
2018-10-03 08:58:09 -04:00
} ,
} ,
{
name : "Single template execute failed" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Titles" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 14 )
2018-10-22 14:20:48 -04:00
a . assertErrorMessage ( "\"layouts/_default/single.html:5:14\": execute of template failed" , fe . Error ( ) )
2018-10-03 08:58:09 -04:00
} ,
} ,
2018-10-24 07:32:46 -04:00
{
name : "Single template execute failed, long keyword" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".ThisIsAVeryLongTitle" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 14 )
2018-10-24 07:32:46 -04:00
a . assertErrorMessage ( "\"layouts/_default/single.html:5:14\": execute of template failed" , fe . Error ( ) )
} ,
} ,
2018-10-03 08:58:09 -04:00
{
name : "Shortcode parse failed" ,
fileType : shortcode ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title }}" , ".Title }" , 1 )
} ,
assertCreateError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
a . assertLineNumber ( 4 , err )
2018-10-03 08:58:09 -04:00
} ,
} ,
2018-10-20 13:09:03 -04:00
{
2022-05-02 10:07:52 -04:00
name : "Shortcode execute failed" ,
2018-10-20 13:09:03 -04:00
fileType : shortcode ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Titles" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-22 14:20:48 -04:00
fe := a . getFileError ( err )
// Make sure that it contains both the content file and template
2022-05-02 10:07:52 -04:00
a . assertErrorMessage ( ` "content/myyaml.md:7:10": failed to render shortcode "sc": failed to process shortcode: "layouts/shortcodes/sc.html:4:22": execute of template failed: template: shortcodes/sc.html:4:22: executing "shortcodes/sc.html" at <.Page.Titles>: can't evaluate field Titles in type page.Page ` , fe . Error ( ) )
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 7 )
2018-10-20 13:09:03 -04:00
} ,
} ,
2018-10-21 06:20:21 -04:00
{
name : "Shortode does not exist" ,
fileType : yamlcontent ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , "{{< sc >}}" , "{{< nono >}}" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 7 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 10 )
2019-01-02 06:33:26 -05:00
a . assertErrorMessage ( ` "content/myyaml.md:7:10": failed to extract shortcode: template for shortcode "nono" not found ` , fe . Error ( ) )
2018-10-21 06:20:21 -04:00
} ,
} ,
{
name : "Invalid YAML front matter" ,
fileType : yamlcontent ,
fileFixer : func ( content string ) string {
2022-05-02 10:07:52 -04:00
return ` -- -
title : "My YAML Content"
foo bar
-- -
`
2018-10-21 06:20:21 -04:00
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2022-05-02 10:07:52 -04:00
a . assertLineNumber ( 3 , err )
2018-10-21 06:20:21 -04:00
} ,
} ,
{
name : "Invalid TOML front matter" ,
fileType : tomlcontent ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , "description = " , "description &" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 6 )
2018-10-21 06:20:21 -04:00
} ,
} ,
2018-10-23 02:54:10 -04:00
{
name : "Invalid JSON front matter" ,
2018-11-01 06:28:30 -04:00
fileType : jsoncontent ,
2018-10-23 02:54:10 -04:00
fileFixer : func ( content string ) string {
return strings . Replace ( content , "\"description\":" , "\"description\"" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 3 )
2018-10-23 02:54:10 -04:00
} ,
} ,
2018-10-17 02:24:45 -04:00
{
2018-10-24 08:02:34 -04:00
// See https://github.com/gohugoio/hugo/issues/5327
2018-10-17 02:24:45 -04:00
name : "Panic in template Execute" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Parent.Parent.Parent" , 1 )
} ,
2018-10-24 08:02:34 -04:00
2018-10-17 02:24:45 -04:00
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2019-08-10 15:05:17 -04:00
a . c . Assert ( err , qt . Not ( qt . IsNil ) )
2019-09-04 03:50:32 -04:00
fe := a . getFileError ( err )
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 21 )
2018-10-17 02:24:45 -04:00
} ,
} ,
2018-10-03 08:58:09 -04:00
}
for _ , test := range tests {
2022-05-02 10:07:52 -04:00
if test . name != "Invalid JSON front matter" {
continue
}
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
test := test
2019-01-02 06:33:26 -05:00
t . Run ( test . name , func ( t * testing . T ) {
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
t . Parallel ( )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
2019-01-02 06:33:26 -05:00
errorAsserter := testSiteBuildErrorAsserter {
2019-08-10 15:05:17 -04:00
c : c ,
name : test . name ,
2019-01-02 06:33:26 -05:00
}
2018-10-03 08:58:09 -04:00
2019-01-02 06:33:26 -05:00
b := newTestSitesBuilder ( t ) . WithSimpleConfigFile ( )
2018-10-03 08:58:09 -04:00
2019-01-02 06:33:26 -05:00
f := func ( fileType , content string ) string {
if fileType != test . fileType {
return content
}
return test . fileFixer ( content )
2018-10-03 08:58:09 -04:00
}
2019-01-02 06:33:26 -05:00
b . WithTemplatesAdded ( "layouts/shortcodes/sc.html" , f ( shortcode , ` SHORTCODE L1
2018-10-03 08:58:09 -04:00
SHORTCODE L2
SHORTCODE L3 :
SHORTCODE L4 : { { . Page . Title } }
` ) )
2019-01-02 06:33:26 -05:00
b . WithTemplatesAdded ( "layouts/_default/baseof.html" , f ( base , ` BASEOF L1
2018-10-03 08:58:09 -04:00
BASEOF L2
BASEOF L3
BASEOF L4 { { if . Title } } { { end } }
{ { block "main" . } } This is the main content . { { end } }
BASEOF L6
` ) )
2019-01-02 06:33:26 -05:00
b . WithTemplatesAdded ( "layouts/_default/single.html" , f ( single , ` { { define "main" } }
2018-10-03 08:58:09 -04:00
SINGLE L2 :
SINGLE L3 :
SINGLE L4 :
SINGLE L5 : { { . Title } } { { . Content } }
{ { end } }
2020-01-15 09:59:56 -05:00
` ) )
b . WithTemplatesAdded ( "layouts/foo/single.html" , f ( single , `
SINGLE L2 :
SINGLE L3 :
SINGLE L4 :
SINGLE L5 : { { . Title } } { { . Content } }
2018-10-03 08:58:09 -04:00
` ) )
2019-01-02 06:33:26 -05:00
b . WithContent ( "myyaml.md" , f ( yamlcontent , ` -- -
2018-10-03 08:58:09 -04:00
title : "The YAML"
-- -
Some content .
2018-10-21 06:20:21 -04:00
{ { < sc > } }
2018-10-03 08:58:09 -04:00
Some more text .
The end .
2018-10-21 06:20:21 -04:00
` ) )
2019-01-02 06:33:26 -05:00
b . WithContent ( "mytoml.md" , f ( tomlcontent , ` ++ +
2018-10-21 06:20:21 -04:00
title = "The TOML"
p1 = "v"
p2 = "v"
p3 = "v"
description = "Descriptioon"
++ +
Some content .
2018-10-23 02:54:10 -04:00
` ) )
2019-01-02 06:33:26 -05:00
b . WithContent ( "myjson.md" , f ( jsoncontent , ` {
2018-10-23 02:54:10 -04:00
"title" : "This is a title" ,
"description" : "This is a description."
}
Some content .
2018-10-03 08:58:09 -04:00
` ) )
2019-01-02 06:33:26 -05:00
createErr := b . CreateSitesE ( )
if test . assertCreateError != nil {
test . assertCreateError ( errorAsserter , createErr )
2018-10-03 08:58:09 -04:00
} else {
2019-08-10 15:05:17 -04:00
c . Assert ( createErr , qt . IsNil )
2018-10-03 08:58:09 -04:00
}
2019-01-02 06:33:26 -05:00
if createErr == nil {
buildErr := b . BuildE ( BuildCfg { } )
if test . assertBuildError != nil {
test . assertBuildError ( errorAsserter , buildErr )
} else {
2019-08-10 15:05:17 -04:00
c . Assert ( buildErr , qt . IsNil )
2019-01-02 06:33:26 -05:00
}
}
} )
2018-10-03 08:58:09 -04:00
}
2022-05-02 10:07:52 -04:00
}
// Issue 9852
func TestErrorMinify ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
minify = true
-- layouts / index . html --
< body >
< script >= ; < / script >
< / body >
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
fe := herrors . UnwrapFileError ( err )
b . Assert ( fe , qt . IsNotNil )
b . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 2 )
b . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 9 )
b . Assert ( fe . Error ( ) , qt . Contains , "unexpected = in expression on line 2 and column 9" )
b . Assert ( filepath . ToSlash ( fe . Position ( ) . Filename ) , qt . Contains , "hugo-transform-error" )
b . Assert ( os . Remove ( fe . Position ( ) . Filename ) , qt . IsNil )
}
func TestErrorNested ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
-- layouts / index . html --
line 1
12 { { partial "foo.html" . } }
line 4
line 5
-- layouts / partials / foo . html --
line 1
line 2
123 { { . ThisDoesNotExist } }
line 4
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
errors := herrors . UnwrapFileErrorsWithErrorContext ( err )
b . Assert ( errors , qt . HasLen , 2 )
fmt . Println ( errors [ 0 ] )
b . Assert ( errors [ 0 ] . Position ( ) . LineNumber , qt . Equals , 2 )
b . Assert ( errors [ 0 ] . Position ( ) . ColumnNumber , qt . Equals , 5 )
b . Assert ( errors [ 0 ] . Error ( ) , qt . Contains , filepath . FromSlash ( ` "/layouts/index.html:2:5": execute of template failed ` ) )
b . Assert ( errors [ 0 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "line 1" , "12{{ partial \"foo.html\" . }}" , "line 4" , "line 5" } )
b . Assert ( errors [ 1 ] . Position ( ) . LineNumber , qt . Equals , 3 )
b . Assert ( errors [ 1 ] . Position ( ) . ColumnNumber , qt . Equals , 6 )
b . Assert ( errors [ 1 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "line 1" , "line 2" , "123{{ .ThisDoesNotExist }}" , "line 4" } )
2018-10-03 08:58:09 -04:00
}
2018-10-30 06:15:15 -04:00
// https://github.com/gohugoio/hugo/issues/5375
func TestSiteBuildTimeout ( t * testing . T ) {
2020-12-23 13:47:20 -05:00
if ! htesting . IsCI ( ) {
2021-06-09 04:58:18 -04:00
//defer leaktest.CheckTimeout(t, 10*time.Second)()
2019-01-02 06:33:26 -05:00
}
2018-10-30 06:15:15 -04:00
b := newTestSitesBuilder ( t )
b . WithConfigFile ( "toml" , `
timeout = 5
` )
b . WithTemplatesAdded ( "_default/single.html" , `
{ { . WordCount } }
` , "shortcodes/c.html", `
{ { range . Page . Site . RegularPages } }
{ { . WordCount } }
{ { end } }
` )
for i := 1 ; i < 100 ; i ++ {
b . WithContent ( fmt . Sprintf ( "page%d.md" , i ) , ` -- -
title : "A page"
-- -
{ { < c > } } ` )
}
2019-01-02 06:33:26 -05:00
b . CreateSites ( ) . BuildFail ( BuildCfg { } )
2018-10-30 06:15:15 -04:00
}