shithub: hugo

Download patch

ref: 35ccf06daeaf86176c1341dde4207c3b11653d7a
parent: 4ddcf52ccc7af3e23109ebaac1f0486087a212ba
author: Bjørn Erik Pedersen <[email protected]>
date: Wed May 23 06:03:11 EDT 2018

Fix some recently broken embedded templates

And add tests for them.

Fixes #4757

--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -84,11 +84,11 @@
 	}
 
 	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)
 			}
--- a/deps/deps.go
+++ b/deps/deps.go
@@ -23,6 +23,9 @@
 	// 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 @@
 		timeoutms = 3000
 	}
 
+	distinctErrorLogger := helpers.NewDistinctLogger(logger.ERROR)
+
 	d := &Deps{
 		Fs:                  fs,
 		Log:                 logger,
+		DistinctErrorLog: distinctErrorLogger,
 		templateProvider:    cfg.TemplateProvider,
 		translationProvider: cfg.TranslationProvider,
 		WithTemplate:        cfg.WithTemplate,
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -260,7 +260,8 @@
 	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 @@
 // DistinctLogger ignores duplicate log statements.
 type DistinctLogger struct {
 	sync.RWMutex
-	logger logPrinter
+	logger LogPrinter
 	m      map[string]bool
 }
 
@@ -307,6 +308,11 @@
 // NewDistinctErrorLogger creates a new DistinctLogger that logs ERRORs
 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
--- /dev/null
+++ b/hugolib/embedded_templates_test.go
@@ -1,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')", "<script async src='//www.google-analytics.com/analytics.js'>")
+
+	// Disqus
+	b.AssertFileContent("public/index.html", "\"disqus_shortname\" + '.disqus.com/embed.js';")
+}
--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -29,6 +29,7 @@
 	"github.com/gohugoio/hugo/i18n"
 	"github.com/gohugoio/hugo/tpl"
 	"github.com/gohugoio/hugo/tpl/tplimpl"
+	jww "github.com/spf13/jwalterweatherman"
 )
 
 // HugoSites represents the sites to build. Each site represents a language.
@@ -54,6 +55,13 @@
 
 func (h *HugoSites) IsMultihost() bool {
 	return h != nil && h.multihost
+}
+
+func (h *HugoSites) NumLogErrors() int {
+	if h == nil {
+		return 0
+	}
+	return int(h.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError))
 }
 
 func (h *HugoSites) PrintProcessingStats(w io.Writer) {
--- a/hugolib/page_output.go
+++ b/hugolib/page_output.go
@@ -24,7 +24,6 @@
 
 	"github.com/gohugoio/hugo/media"
 
-	"github.com/gohugoio/hugo/helpers"
 	"github.com/gohugoio/hugo/output"
 )
 
@@ -115,7 +114,7 @@
 func (p *PageOutput) Render(layout ...string) template.HTML {
 	l, err := p.layouts(layout...)
 	if err != nil {
-		helpers.DistinctErrorLog.Printf("in .Render: Failed to resolve layout %q for page %q", layout, p.pathOrTitle())
+		p.s.DistinctErrorLog.Printf("in .Render: Failed to resolve layout %q for page %q", layout, p.pathOrTitle())
 		return ""
 	}
 
@@ -130,7 +129,7 @@
 		if templ != nil {
 			res, err := templ.ExecuteToString(p)
 			if err != nil {
-				helpers.DistinctErrorLog.Printf("in .Render: Failed to execute template %q: %s", layout, err)
+				p.s.DistinctErrorLog.Printf("in .Render: Failed to execute template %q: %s", layout, err)
 				return template.HTML("")
 			}
 			return template.HTML(res)
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1746,7 +1746,7 @@
 	}
 	transformer := transform.NewChain(transform.AbsURLInXML)
 	if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil {
-		helpers.DistinctErrorLog.Println(err)
+		s.DistinctErrorLog.Println(err)
 		return nil
 	}
 
@@ -1805,7 +1805,7 @@
 
 	transformer := transform.NewChain(transformLinks...)
 	if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil {
-		helpers.DistinctErrorLog.Println(err)
+		s.DistinctErrorLog.Println(err)
 		return nil
 	}
 
@@ -1821,7 +1821,7 @@
 			if templ != nil {
 				templName = templ.Name()
 			}
-			helpers.DistinctErrorLog.Printf("Failed to render %q: %s", templName, r)
+			s.DistinctErrorLog.Printf("Failed to render %q: %s", templName, r)
 			// TOD(bep) we really need to fix this. Also see below.
 			if !s.running() && !testMode {
 				os.Exit(-1)
@@ -1838,12 +1838,12 @@
 		// Behavior here should be dependent on if running in server or watch mode.
 		if p, ok := d.(*PageOutput); ok {
 			if p.File != nil {
-				helpers.DistinctErrorLog.Printf("Error while rendering %q in %q: %s", name, p.File.Dir(), err)
+				s.DistinctErrorLog.Printf("Error while rendering %q in %q: %s", name, p.File.Dir(), err)
 			} else {
-				helpers.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err)
+				s.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err)
 			}
 		} else {
-			helpers.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err)
+			s.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err)
 		}
 		if !s.running() && !testMode {
 			// TODO(bep) check if this can be propagated
--- a/hugolib/testhelpers_test.go
+++ b/hugolib/testhelpers_test.go
@@ -135,10 +135,21 @@
 	return s
 }
 
+const commonConfigSections = `
+
+[services]
+[services.disqus]
+shortname = "disqus_shortname"
+[services.googleAnalytics]
+id = "ga_id"
+
+`
+
 func (s *sitesBuilder) WithSimpleConfigFile() *sitesBuilder {
 	var config = `
 baseURL = "http://example.com/"
-`
+
+` + commonConfigSections
 	return s.WithConfigFile("toml", config)
 }
 
@@ -198,7 +209,7 @@
 paginatePath = "side"
 [Languages.nb.Taxonomies]
 lag = "lag"
-`
+` + commonConfigSections
 
 	return s.WithConfigFile("toml", defaultMultiSiteConfig)
 
@@ -311,6 +322,12 @@
 		s.CreateSites()
 	}
 	err := s.H.Build(cfg)
+	if err == nil {
+		logErrorCount := s.H.NumLogErrors()
+		if logErrorCount > 0 {
+			err = fmt.Errorf("logged %d errors", logErrorCount)
+		}
+	}
 	if err != nil && !shouldFail {
 		s.Fatalf("Build failed: %s", err)
 	} else if err == nil && shouldFail {
--- a/tpl/tplimpl/embedded/templates.autogen.go
+++ b/tpl/tplimpl/embedded/templates.autogen.go
@@ -99,7 +99,7 @@
 <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
 <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>{{end}}
 {{- end -}}`},
-	{`google_analytics.html`, `{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
+	{`google_analytics.html`, `{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
 {{- if not $pc.Disable -}}
 {{ with .Site.GoogleAnalytics }}
 <script>
@@ -116,7 +116,7 @@
 {{ end }}
 {{- end -}}
 {{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}}
-{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
+{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
 {{- if not $pc.RespectDoNotTrack -}}
 var doNotTrack = false;
 {{- else -}}
@@ -124,7 +124,7 @@
 var doNotTrack = (dnt == "1" || dnt == "yes");
 {{- end -}}
 {{- end -}}`},
-	{`google_analytics_async.html`, `{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
+	{`google_analytics_async.html`, `{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
 {{- if not $pc.Disable -}}
 {{ with .Site.GoogleAnalytics }}
 <script>
--- a/tpl/tplimpl/embedded/templates/google_analytics.html
+++ b/tpl/tplimpl/embedded/templates/google_analytics.html
@@ -1,4 +1,4 @@
-{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
+{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
 {{- if not $pc.Disable -}}
 {{ with .Site.GoogleAnalytics }}
 <script>
@@ -15,7 +15,7 @@
 {{ end }}
 {{- end -}}
 {{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}}
-{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
+{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
 {{- if not $pc.RespectDoNotTrack -}}
 var doNotTrack = false;
 {{- else -}}
--- a/tpl/tplimpl/embedded/templates/google_analytics_async.html
+++ b/tpl/tplimpl/embedded/templates/google_analytics_async.html
@@ -1,4 +1,4 @@
-{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
+{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
 {{- if not $pc.Disable -}}
 {{ with .Site.GoogleAnalytics }}
 <script>