Fix some recently broken embedded templates

And add tests for them.

Fixes #4757
This commit is contained in:
Bjørn Erik Pedersen 2018-05-23 10:03:11 +02:00
parent 4ddcf52ccc
commit 35ccf06dae
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
11 changed files with 111 additions and 21 deletions

View file

@ -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)
}

6
deps/deps.go vendored
View file

@ -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,

View file

@ -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}

View file

@ -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')", "<script async src='//www.google-analytics.com/analytics.js'>")
// Disqus
b.AssertFileContent("public/index.html", "\"disqus_shortname\" + '.disqus.com/embed.js';")
}

View file

@ -29,6 +29,7 @@ import (
"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.
@ -56,6 +57,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) {
stats := make([]*helpers.ProcessingStats, len(h.Sites))
for i := 0; i < len(h.Sites); i++ {

View file

@ -24,7 +24,6 @@ import (
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/output"
)
@ -115,7 +114,7 @@ func (p *PageOutput) layouts(layouts ...string) ([]string, error) {
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 @@ func (p *PageOutput) Render(layout ...string) template.HTML {
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)

View file

@ -1746,7 +1746,7 @@ func (s *Site) renderAndWriteXML(statCounter *uint64, name string, dest string,
}
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 @@ func (s *Site) renderAndWritePage(statCounter *uint64, name string, dest string,
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 @@ func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts
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 @@ func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts
// 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

View file

@ -135,10 +135,21 @@ func (s *sitesBuilder) WithThemeConfigFile(format, conf string) *sitesBuilder {
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 @@ languageName = "Bokmål"
paginatePath = "side"
[Languages.nb.Taxonomies]
lag = "lag"
`
` + commonConfigSections
return s.WithConfigFile("toml", defaultMultiSiteConfig)
@ -311,6 +322,12 @@ func (s *sitesBuilder) build(cfg BuildCfg, shouldFail bool) *sitesBuilder {
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 {

View file

@ -99,7 +99,7 @@ var EmbeddedTemplates = [][2]string{
<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 @@ if (!doNotTrack) {
{{ 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 dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
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>

View file

@ -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 @@ if (!doNotTrack) {
{{ 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 -}}

View file

@ -1,4 +1,4 @@
{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.Disable -}}
{{ with .Site.GoogleAnalytics }}
<script>