Fix the shortcode ref tests
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 7 Aug 2016 20:29:27 +0000 (22:29 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 6 Sep 2016 15:32:18 +0000 (18:32 +0300)
See #2309

hugolib/embedded_shortcodes_test.go
hugolib/site.go

index cebef0b8b381bbebd1fb44aff8d880bfbea2fa3f..7dda21d41f20a8b847a95635428ca30db26f6b9f 100644 (file)
@@ -19,11 +19,13 @@ import (
        "os"
        "path/filepath"
        "regexp"
+       "strings"
        "testing"
 
        "github.com/spf13/hugo/helpers"
        "github.com/spf13/hugo/tpl"
        "github.com/spf13/viper"
+       "github.com/stretchr/testify/require"
 )
 
 const (
@@ -37,6 +39,9 @@ func TestShortcodeCrossrefs(t *testing.T) {
 }
 
 func doTestShortcodeCrossrefs(t *testing.T, relative bool) {
+       testCommonResetState()
+       viper.Set("baseURL", baseURL)
+
        var refShortcode string
        var expectedBase string
 
@@ -50,21 +55,20 @@ func doTestShortcodeCrossrefs(t *testing.T, relative bool) {
 
        path := filepath.FromSlash("blog/post.md")
        in := fmt.Sprintf(`{{< %s "%s" >}}`, refShortcode, path)
+
+       writeSource(t, "content/"+path, simplePageWithURL+": "+in)
+
        expected := fmt.Sprintf(`%s/simple/url/`, expectedBase)
 
-       templ := tpl.New()
-       p, _ := pageFromString(simplePageWithURL, path)
-       p.Node.Site = newSiteInfoDefaultLanguage(
-               helpers.SanitizeURLKeepTrailingSlash(baseURL),
-               p)
+       sites, err := newHugoSitesDefaultLanguage()
+       require.NoError(t, err)
 
-       output, err := HandleShortcodes(in, p, templ)
+       require.NoError(t, sites.Build(BuildCfg{}))
+       require.Len(t, sites.Sites[0].Pages, 1)
 
-       if err != nil {
-               t.Fatal("Handle shortcode error", err)
-       }
+       output := string(sites.Sites[0].Pages[0].Content)
 
-       if output != expected {
+       if !strings.Contains(output, expected) {
                t.Errorf("Got\n%q\nExpected\n%q", output, expected)
        }
 }
index cfbe75f70695027982ee2a8b61d449cf9e616d37..8ae3cd16f8b21a56b8a1ba381fc33f964b5efa12 100644 (file)
@@ -247,16 +247,7 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool) (string, error
        var link string
 
        if refURL.Path != "" {
-               // We may be in a shortcode and a not finished site, so look it the
-               // "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
-                       }
+               for _, page := range []*Page(*s.AllPages) {
                        refPath := filepath.FromSlash(refURL.Path)
                        if page.Source.Path() == refPath || page.Source.LogicalName() == refPath {
                                target = page