From 35ccf06daeaf86176c1341dde4207c3b11653d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 23 May 2018 10:03:11 +0200 Subject: [PATCH] Fix some recently broken embedded templates And add tests for them. Fixes #4757 --- commands/hugo.go | 4 +- deps/deps.go | 6 +++ helpers/general.go | 10 +++- hugolib/embedded_templates_test.go | 54 +++++++++++++++++++ hugolib/hugo_sites.go | 8 +++ hugolib/page_output.go | 5 +- hugolib/site.go | 12 ++--- hugolib/testhelpers_test.go | 21 +++++++- tpl/tplimpl/embedded/templates.autogen.go | 6 +-- .../embedded/templates/google_analytics.html | 4 +- .../templates/google_analytics_async.html | 2 +- 11 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 hugolib/embedded_templates_test.go diff --git a/commands/hugo.go b/commands/hugo.go index e2d447768..b898a9b7b 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -84,11 +84,11 @@ func Execute(args []string) Response { } if err == nil { - errCount := jww.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) + errCount := int(jww.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError)) if errCount > 0 { err = fmt.Errorf("logged %d errors", errCount) } else if resp.Result != nil { - errCount = resp.Result.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) + errCount = resp.Result.NumLogErrors() if errCount > 0 { err = fmt.Errorf("logged %d errors", errCount) } diff --git a/deps/deps.go b/deps/deps.go index 475d678a9..9a502b589 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -23,6 +23,9 @@ type Deps struct { // The logger to use. Log *jww.Notepad `json:"-"` + // Used to log errors that may repeat itself many times. + DistinctErrorLog *helpers.DistinctLogger + // The templates to use. This will usually implement the full tpl.TemplateHandler. Tmpl tpl.TemplateFinder `json:"-"` @@ -137,9 +140,12 @@ func New(cfg DepsCfg) (*Deps, error) { timeoutms = 3000 } + distinctErrorLogger := helpers.NewDistinctLogger(logger.ERROR) + d := &Deps{ Fs: fs, Log: logger, + DistinctErrorLog: distinctErrorLogger, templateProvider: cfg.TemplateProvider, translationProvider: cfg.TranslationProvider, WithTemplate: cfg.WithTemplate, diff --git a/helpers/general.go b/helpers/general.go index e6c0ec8e5..5b46520e5 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -260,7 +260,8 @@ func (p *PathSpec) ThemeSet() bool { return p.theme != "" } -type logPrinter interface { +// LogPrinter is the common interface of the JWWs loggers. +type LogPrinter interface { // Println is the only common method that works in all of JWWs loggers. Println(a ...interface{}) } @@ -268,7 +269,7 @@ type logPrinter interface { // DistinctLogger ignores duplicate log statements. type DistinctLogger struct { sync.RWMutex - logger logPrinter + logger LogPrinter m map[string]bool } @@ -309,6 +310,11 @@ func NewDistinctErrorLogger() *DistinctLogger { return &DistinctLogger{m: make(map[string]bool), logger: jww.ERROR} } +// NewDistinctLogger creates a new DistinctLogger that logs to the provided logger. +func NewDistinctLogger(logger LogPrinter) *DistinctLogger { + return &DistinctLogger{m: make(map[string]bool), logger: logger} +} + // NewDistinctWarnLogger creates a new DistinctLogger that logs WARNs func NewDistinctWarnLogger() *DistinctLogger { return &DistinctLogger{m: make(map[string]bool), logger: jww.WARN} diff --git a/hugolib/embedded_templates_test.go b/hugolib/embedded_templates_test.go new file mode 100644 index 000000000..fd620bd15 --- /dev/null +++ b/hugolib/embedded_templates_test.go @@ -0,0 +1,54 @@ +// Copyright 2018 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 ( + "testing" + + "github.com/stretchr/testify/require" +) + +// Just some simple test of the embedded templates to avoid +// https://github.com/gohugoio/hugo/issues/4757 and similar. +func TestEmbeddedTemplates(t *testing.T) { + t.Parallel() + + assert := require.New(t) + assert.True(true) + + home := []string{"index.html", ` +GA: +{{ template "_internal/google_analytics.html" . }} + +GA async: + +{{ template "_internal/google_analytics_async.html" . }} + +Disqus: + +{{ template "_internal/disqus.html" . }} + +`} + + b := newTestSitesBuilder(t) + b.WithSimpleConfigFile().WithTemplatesAdded(home...) + + b.Build(BuildCfg{}) + + // Gheck GA regular and async + b.AssertFileContent("public/index.html", "'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'ga_id', 'auto')", "