From 845a7ba4fc30c61842148d67d31d0fa3db8f40b9 Mon Sep 17 00:00:00 2001 From: satotake Date: Mon, 24 May 2021 21:59:02 +0900 Subject: [PATCH] Catch incomplete shortcode error Currently, no name shortcodes (`{{< >}}`) enter unexpected branch and throw `BUG: template info not set`. This patch checks if shortcode has name or not earlier and throws specific error. Closes #6866 --- hugolib/shortcode.go | 16 +++++++++------- hugolib/shortcode_test.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index 483fad2e..21d65de3 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -33,8 +33,6 @@ import ( "github.com/gohugoio/hugo/parser/pageparser" "github.com/gohugoio/hugo/resources/page" - _errors "github.com/pkg/errors" - "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/common/urls" @@ -310,7 +308,7 @@ func renderShortcode( var found bool tmpl, found = s.TextTmpl().Lookup(templName) if !found { - return "", false, _errors.Errorf("no earlier definition of shortcode %q found", sc.name) + return "", false, errors.Errorf("no earlier definition of shortcode %q found", sc.name) } } } else { @@ -417,7 +415,7 @@ func (s *shortcodeHandler) renderShortcodesForPage(p *pageState, f output.Format for _, v := range s.shortcodes { s, more, err := renderShortcode(0, s.s, tplVariants, v, nil, p) if err != nil { - err = p.parseError(_errors.Wrapf(err, "failed to render shortcode %q", v.name), p.source.parsed.Input(), v.pos) + err = p.parseError(errors.Wrapf(err, "failed to render shortcode %q", v.name), p.source.parsed.Input(), v.pos) return nil, false, err } hasVariants = hasVariants || more @@ -460,6 +458,10 @@ Loop: switch { case currItem.IsLeftShortcodeDelim(): next := pt.Peek() + if next.IsRightShortcodeDelim() { + // no name: {{< >}} or {{% %}} + return sc, errors.New("shortcode has no name") + } if next.IsShortcodeClose() { continue } @@ -506,7 +508,7 @@ Loop: // return that error, more specific continue } - return sc, fail(_errors.Errorf("shortcode %q has no .Inner, yet a closing tag was provided", next.Val), next) + return sc, fail(errors.Errorf("shortcode %q has no .Inner, yet a closing tag was provided", next.Val), next) } } if next.IsRightShortcodeDelim() { @@ -536,7 +538,7 @@ Loop: // Used to check if the template expects inner content. templs := s.s.Tmpl().LookupVariants(sc.name) if templs == nil { - return nil, _errors.Errorf("template for shortcode %q not found", sc.name) + return nil, errors.Errorf("template for shortcode %q not found", sc.name) } sc.info = templs[0].(tpl.Info) @@ -637,7 +639,7 @@ func renderShortcodeWithPage(h tpl.TemplateHandler, tmpl tpl.Template, data *Sho err := h.Execute(tmpl, buffer, data) if err != nil { - return "", _errors.Wrap(err, "failed to process shortcode") + return "", errors.Wrap(err, "failed to process shortcode") } return buffer.String(), nil } diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index 41f4eddb..51187a00 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -65,8 +65,9 @@ title: "Title" t.Fatalf("Shortcode rendered error %s.", err) } - if err == nil && expectError { - t.Fatalf("No error from shortcode") + if expectError { + c.Assert(err, qt.ErrorMatches, expected) + return } h := b.H @@ -341,6 +342,12 @@ func TestShortcodeWrappedInPIssue(t *testing.T) { `, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", wt) } +// #6866 +func TestShortcodeIncomplete(t *testing.T) { + t.Parallel() + CheckShortCodeMatchAndError(t, `{{< >}}`, ".*shortcode has no name.*", nil, true) +} + func TestExtractShortcodes(t *testing.T) { b := newTestSitesBuilder(t).WithSimpleConfigFile()