// 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"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/source"
"github.com/gohugoio/hugo/tpl"
"github.com/stretchr/testify/require"
)
// TODO(bep) remove
func pageFromString(in, filename string, withTemplate ...func(templ tpl.TemplateHandler) error) (*Page, error) {
s := newTestSite(nil)
if len(withTemplate) > 0 {
// Have to create a new site
var err error
cfg, fs := newTestCfg()
d := deps.DepsCfg{Language: helpers.NewLanguage("en", cfg), Cfg: cfg, Fs: fs, WithTemplate: withTemplate[0]}
s, err = NewSiteForCfg(d)
if err != nil {
return nil, err
}
}
return s.NewPageFrom(strings.NewReader(in), filename)
}
func CheckShortCodeMatch(t *testing.T, input, expected string, withTemplate func(templ tpl.TemplateHandler) error) {
CheckShortCodeMatchAndError(t, input, expected, withTemplate, false)
}
func CheckShortCodeMatchAndError(t *testing.T, input, expected string, withTemplate func(templ tpl.TemplateHandler) error, expectError bool) {
cfg, fs := newTestCfg()
// Need some front matter, see https://github.com/gohugoio/hugo/issues/2337
contentFile := `---
title: "Title"
---
` + input
writeSource(t, fs, "content/simple.md", contentFile)
h, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg, WithTemplate: withTemplate})
require.NoError(t, err)
require.Len(t, h.Sites, 1)
err = h.Build(BuildCfg{})
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].RegularPages, 1)
output := strings.TrimSpace(string(h.Sites[0].RegularPages[0].Content))
output = strings.TrimPrefix(output, "<p>")
output = strings.TrimSuffix(output, "</p>")
expected = strings.TrimSpace(expected)
if output != expected {
t.Fatalf("Shortcode render didn't match. got \n%q but expected \n%q", output, expected)
}
}
func TestNonSC(t *testi