Fix the shortcode ref tests

See #2309
This commit is contained in:
Bjørn Erik Pedersen 2016-08-07 22:29:27 +02:00
parent 54141f71dd
commit 506e131f28
2 changed files with 15 additions and 20 deletions

View file

@ -19,11 +19,13 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"testing" "testing"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/tpl" "github.com/spf13/hugo/tpl"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/require"
) )
const ( const (
@ -37,6 +39,9 @@ func TestShortcodeCrossrefs(t *testing.T) {
} }
func doTestShortcodeCrossrefs(t *testing.T, relative bool) { func doTestShortcodeCrossrefs(t *testing.T, relative bool) {
testCommonResetState()
viper.Set("baseURL", baseURL)
var refShortcode string var refShortcode string
var expectedBase string var expectedBase string
@ -50,21 +55,20 @@ func doTestShortcodeCrossrefs(t *testing.T, relative bool) {
path := filepath.FromSlash("blog/post.md") path := filepath.FromSlash("blog/post.md")
in := fmt.Sprintf(`{{< %s "%s" >}}`, refShortcode, path) in := fmt.Sprintf(`{{< %s "%s" >}}`, refShortcode, path)
writeSource(t, "content/"+path, simplePageWithURL+": "+in)
expected := fmt.Sprintf(`%s/simple/url/`, expectedBase) expected := fmt.Sprintf(`%s/simple/url/`, expectedBase)
templ := tpl.New() sites, err := newHugoSitesDefaultLanguage()
p, _ := pageFromString(simplePageWithURL, path) require.NoError(t, err)
p.Node.Site = newSiteInfoDefaultLanguage(
helpers.SanitizeURLKeepTrailingSlash(baseURL),
p)
output, err := HandleShortcodes(in, p, templ) require.NoError(t, sites.Build(BuildCfg{}))
require.Len(t, sites.Sites[0].Pages, 1)
if err != nil { output := string(sites.Sites[0].Pages[0].Content)
t.Fatal("Handle shortcode error", err)
}
if output != expected { if !strings.Contains(output, expected) {
t.Errorf("Got\n%q\nExpected\n%q", output, expected) t.Errorf("Got\n%q\nExpected\n%q", output, expected)
} }
} }

View file

@ -247,16 +247,7 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool) (string, error
var link string var link string
if refURL.Path != "" { if refURL.Path != "" {
// We may be in a shortcode and a not finished site, so look it the for _, page := range []*Page(*s.AllPages) {
// "raw page" collection.
// This works, but it also means AllPages and Pages will be empty for other
// shortcode use, which may be a slap in the face for many.
// TODO(bep) ml move shortcode handling to a "pre-render" handler, which also
// will fix a few other problems.
for _, page := range []*Page(*s.rawAllPages) {
if !page.shouldBuild() {
continue
}
refPath := filepath.FromSlash(refURL.Path) refPath := filepath.FromSlash(refURL.Path)
if page.Source.Path() == refPath || page.Source.LogicalName() == refPath { if page.Source.Path() == refPath || page.Source.LogicalName() == refPath {
target = page target = page