Skip to content

Commit

Permalink
Fix some recently broken embedded templates
Browse files Browse the repository at this point in the history
And add tests for them.

Fixes #4757
  • Loading branch information
bep committed May 23, 2018
1 parent 4ddcf52 commit 35ccf06
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 21 deletions.
4 changes: 2 additions & 2 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions helpers/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,16 @@ 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{})
}

// DistinctLogger ignores duplicate log statements.
type DistinctLogger struct {
sync.RWMutex
logger logPrinter
logger LogPrinter
m map[string]bool
}

Expand Down Expand Up @@ -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}
Expand Down
54 changes: 54 additions & 0 deletions hugolib/embedded_templates_test.go
Original file line number Diff line number Diff line change
@@ -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';")
}
8 changes: 8 additions & 0 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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++ {
Expand Down
5 changes: 2 additions & 3 deletions hugolib/page_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/gohugoio/hugo/media"

"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/output"
)

Expand Down Expand Up @@ -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 ""
}

Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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
Expand Down
21 changes: 19 additions & 2 deletions hugolib/testhelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -198,7 +209,7 @@ languageName = "Bokmål"
paginatePath = "side"
[Languages.nb.Taxonomies]
lag = "lag"
`
` + commonConfigSections

return s.WithConfigFile("toml", defaultMultiSiteConfig)

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tpl/tplimpl/embedded/templates.autogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -116,15 +116,15 @@ 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 -}}
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>
Expand Down
4 changes: 2 additions & 2 deletions tpl/tplimpl/embedded/templates/google_analytics.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.Disable -}}
{{ with .Site.GoogleAnalytics }}
<script>
Expand All @@ -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 -}}
Expand Down
2 changes: 1 addition & 1 deletion tpl/tplimpl/embedded/templates/google_analytics_async.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}}
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.Disable -}}
{{ with .Site.GoogleAnalytics }}
<script>
Expand Down

0 comments on commit 35ccf06

Please sign in to comment.