// Copyright 2016 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 (
"fmt"
"path/filepath"
"reflect"
"regexp"
"sort"
"strings"
"testing"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/hugo/tpl"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
// TODO(bep) remove
func pageFromString(in, filename string) (*Page, error) {
return NewPageFrom(strings.NewReader(in), filename)
}
func CheckShortCodeMatch(t *testing.T, input, expected string, withTemplate func(templ tpl.Template) error) {
CheckShortCodeMatchAndError(t, input, expected, withTemplate, false)
}
func CheckShortCodeMatchAndError(t *testing.T, input, expected string, withTemplate func(templ tpl.Template) error, expectError bool) {
testCommonResetState()
// Need some front matter, see https://github.com/spf13/hugo/issues/2337
contentFile := `---
title: "Title"
---
` + input
writeSource(t, "content/simple.md", contentFile)
h, err := newHugoSitesDefaultLanguage()
if err != nil {
t.Fatalf("Failed to create sites: %s", err)
}
cfg := BuildCfg{SkipRender: true, withTemplate: withTemplate}
err = h.Build(cfg)
if err != nil && !expectError {
t.Fatalf("Shortcode rendered error %s.", err)
}
if err == nil && expectError {
t.Fatalf("No error from shortcode")
}
require.Len(t, h.Sites[0].Pages, 1)
output := strings.TrimSpace(string(h.Sites[0].Pages[0].Content))
if strings.HasPrefix(output, "<p>") {
output = output[3:]
}
if strings.HasSuffix(output, "</p>") {
output = output[:len(output)-4]
}
expected = strings.TrimSpace(expected)
if output != expected {
t.Fatalf("Shortcode render didn't match. got \n%q but expected \n%q", output, expected)
}
}
func TestShortcodeGoFuzzReports(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("sc.html", `foo`)
p, _ := pageFromString(simplePage, "simple.md")
for i, this := range []struct {
data string
expectErr bool
}{
{"{{</*/", true},
} {
output, err := HandleShortcodes(this.data, p, tem)
if this.expectErr && err == nil {
t.Errorf("[%d] should have errored", i)
}
if !this.expectErr && err != nil {
t.Errorf("[%d] should not have errored: %s", i, err)
}
if !this.expectErr && err == nil && len(output) == 0 {
t.Errorf("[%d] empty result", i)
}
}
}
func TestNonSC(t *testing.T) {
// notice the syntax diff from 0.12, now comment delims must be added
CheckShortCodeMatch(t, "{{%/* movie 47238zzb */%}}", "{{% movie 47238zzb %}}", nil)
}
// Issue #929
func TestHyphenatedSC(t *testing.T) {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("hyphenated-video.html", `Playing Video {{ .Get 0 }}`)
return nil
}
CheckShortCodeMatch(t, "{{< hyphenated-video 47238zzb >}}", "Playing Video 47238zzb", wt)
}
// Issue #1753
func TestNoTrailingNewline(t