Fix crossrefs on Windows
authorbep <bjorn.erik.pedersen@gmail.com>
Tue, 17 Mar 2015 14:38:48 +0000 (15:38 +0100)
committerbep <bjorn.erik.pedersen@gmail.com>
Tue, 17 Mar 2015 15:29:10 +0000 (16:29 +0100)
Have to convert path slashes to file path slashes before the URL path is compared to a file path.

Fixes #957

hugolib/site.go
hugolib/site_test.go

index c9914b9712e4d38f8d2297e72affef3ecdfc775b..39fe888b557d6e12d4a614aee921867b6abf9dbb 100644 (file)
@@ -172,7 +172,8 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool) (string, error
 
        if refURL.Path != "" {
                for _, page := range []*Page(*s.Pages) {
-                       if page.Source.Path() == refURL.Path || page.Source.LogicalName() == refURL.Path {
+                       refPath := filepath.FromSlash(refURL.Path)
+                       if page.Source.Path() == refPath || page.Source.LogicalName() == refPath {
                                target = page
                                break
                        }
index 4eb90ae1a606861dc292f7c53a6da82039977dfd..79921e1106c5a0d7862d2da146f12052f4b77f8f 100644 (file)
@@ -307,6 +307,83 @@ func TestDraftAndFutureRender(t *testing.T) {
        viper.Set("BuildFuture", false)
 }
 
+// Issue #957
+func TestCrossrefs(t *testing.T) {
+       for _, uglyUrls := range []bool{true, false} {
+               for _, relative := range []bool{true, false} {
+                       doTestCrossrefs(t, relative, uglyUrls)
+               }
+       }
+}
+
+func doTestCrossrefs(t *testing.T, relative, uglyUrls bool) {
+       baseUrl := "http://foo/bar"
+       viper.Set("baseurl", baseUrl)
+       viper.Set("UglyURLs", uglyUrls)
+       viper.Set("verbose", true)
+
+       var refShortcode string
+       var expectedBase string
+       var expectedUrlSuffix string
+       var expectedPathSuffix string
+
+       if relative {
+               refShortcode = "relref"
+               expectedBase = "/bar"
+       } else {
+               refShortcode = "ref"
+               expectedBase = baseUrl
+       }
+
+       if uglyUrls {
+               expectedUrlSuffix = ".html"
+               expectedPathSuffix = ".html"
+       } else {
+               expectedUrlSuffix = "/"
+               expectedPathSuffix = "/index.html"
+       }
+
+       sources := []source.ByteSource{
+               {filepath.FromSlash("sect/doc1.md"),
+                       []byte(fmt.Sprintf(`Ref 2: {{< %s "sect/doc2.md" >}}`, refShortcode))},
+               {filepath.FromSlash("sect/doc2.md"),
+                       []byte(fmt.Sprintf(`Ref 1: {{< %s "sect/doc1.md" >}}`, refShortcode))},
+       }
+
+       s := &Site{
+               Source:  &source.InMemorySource{ByteSource: sources},
+               Targets: targetList{Page: &target.PagePub{UglyURLs: uglyUrls}},
+       }
+
+       s.initializeSiteInfo()
+       templatePrep(s)
+
+       must(s.addTemplate("_default/single.html", "{{.Content}}"))
+
+       createAndRenderPages(t, s)
+
+       tests := []struct {
+               doc      string
+               expected string
+       }{
+               {filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), fmt.Sprintf("<p>Ref 2: %s/sect/doc2%s</p>\n", expectedBase, expectedUrlSuffix)},
+               {filepath.FromSlash(fmt.Sprintf("sect/doc2%s", expectedPathSuffix)), fmt.Sprintf("<p>Ref 1: %s/sect/doc1%s</p>\n", expectedBase, expectedUrlSuffix)},
+       }
+
+       for _, test := range tests {
+               file, err := hugofs.DestinationFS.Open(test.doc)
+               if err != nil {
+                       t.Fatalf("Did not find %s in target: %s", test.doc, err)
+               }
+               content := helpers.ReaderToBytes(file)
+
+               if !bytes.Equal(content, []byte(test.expected)) {
+                       t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content))
+               }
+       }
+
+}
+
 // Issue #939
 func Test404ShouldAlwaysHaveUglyUrls(t *testing.T) {
        for _, uglyURLs := range []bool{true, false} {