github.com/jmooring/hugo@v0.47.1/hugolib/site_test.go (about)

     1  // Copyright 2016 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"path/filepath"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/markbates/inflect"
    24  
    25  	"github.com/gohugoio/hugo/helpers"
    26  
    27  	"github.com/gohugoio/hugo/deps"
    28  	"github.com/gohugoio/hugo/hugofs"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  const (
    34  	templateMissingFunc = "{{ .Title | funcdoesnotexists }}"
    35  	templateWithURLAbs  = "<a href=\"/foobar.jpg\">Going</a>"
    36  )
    37  
    38  func init() {
    39  	testMode = true
    40  }
    41  
    42  func pageMust(p *Page, err error) *Page {
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	return p
    47  }
    48  
    49  func TestRenderWithInvalidTemplate(t *testing.T) {
    50  	t.Parallel()
    51  	cfg, fs := newTestCfg()
    52  
    53  	writeSource(t, fs, filepath.Join("content", "foo.md"), "foo")
    54  
    55  	withTemplate := createWithTemplateFromNameValues("missing", templateMissingFunc)
    56  
    57  	buildSingleSiteExpected(t, true, deps.DepsCfg{Fs: fs, Cfg: cfg, WithTemplate: withTemplate}, BuildCfg{})
    58  
    59  }
    60  
    61  func TestDraftAndFutureRender(t *testing.T) {
    62  	t.Parallel()
    63  	sources := [][2]string{
    64  		{filepath.FromSlash("sect/doc1.md"), "---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*"},
    65  		{filepath.FromSlash("sect/doc2.md"), "---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*"},
    66  		{filepath.FromSlash("sect/doc3.md"), "---\ntitle: doc3\ndraft: false\npublishdate: \"2414-05-29\"\n---\n# doc3\n*some content*"},
    67  		{filepath.FromSlash("sect/doc4.md"), "---\ntitle: doc4\ndraft: false\npublishdate: \"2012-05-29\"\n---\n# doc4\n*some content*"},
    68  	}
    69  
    70  	siteSetup := func(t *testing.T, configKeyValues ...interface{}) *Site {
    71  		cfg, fs := newTestCfg()
    72  
    73  		cfg.Set("baseURL", "http://auth/bub")
    74  
    75  		for i := 0; i < len(configKeyValues); i += 2 {
    76  			cfg.Set(configKeyValues[i].(string), configKeyValues[i+1])
    77  		}
    78  
    79  		for _, src := range sources {
    80  			writeSource(t, fs, filepath.Join("content", src[0]), src[1])
    81  
    82  		}
    83  
    84  		return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
    85  	}
    86  
    87  	// Testing Defaults.. Only draft:true and publishDate in the past should be rendered
    88  	s := siteSetup(t)
    89  	if len(s.RegularPages) != 1 {
    90  		t.Fatal("Draft or Future dated content published unexpectedly")
    91  	}
    92  
    93  	// only publishDate in the past should be rendered
    94  	s = siteSetup(t, "buildDrafts", true)
    95  	if len(s.RegularPages) != 2 {
    96  		t.Fatal("Future Dated Posts published unexpectedly")
    97  	}
    98  
    99  	//  drafts should not be rendered, but all dates should
   100  	s = siteSetup(t,
   101  		"buildDrafts", false,
   102  		"buildFuture", true)
   103  
   104  	if len(s.RegularPages) != 2 {
   105  		t.Fatal("Draft posts published unexpectedly")
   106  	}
   107  
   108  	// all 4 should be included
   109  	s = siteSetup(t,
   110  		"buildDrafts", true,
   111  		"buildFuture", true)
   112  
   113  	if len(s.RegularPages) != 4 {
   114  		t.Fatal("Drafts or Future posts not included as expected")
   115  	}
   116  
   117  }
   118  
   119  func TestFutureExpirationRender(t *testing.T) {
   120  	t.Parallel()
   121  	sources := [][2]string{
   122  		{filepath.FromSlash("sect/doc3.md"), "---\ntitle: doc1\nexpirydate: \"2400-05-29\"\n---\n# doc1\n*some content*"},
   123  		{filepath.FromSlash("sect/doc4.md"), "---\ntitle: doc2\nexpirydate: \"2000-05-29\"\n---\n# doc2\n*some content*"},
   124  	}
   125  
   126  	siteSetup := func(t *testing.T) *Site {
   127  		cfg, fs := newTestCfg()
   128  		cfg.Set("baseURL", "http://auth/bub")
   129  
   130  		for _, src := range sources {
   131  			writeSource(t, fs, filepath.Join("content", src[0]), src[1])
   132  
   133  		}
   134  
   135  		return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   136  	}
   137  
   138  	s := siteSetup(t)
   139  
   140  	if len(s.AllPages) != 1 {
   141  		if len(s.RegularPages) > 1 {
   142  			t.Fatal("Expired content published unexpectedly")
   143  		}
   144  
   145  		if len(s.RegularPages) < 1 {
   146  			t.Fatal("Valid content expired unexpectedly")
   147  		}
   148  	}
   149  
   150  	if s.AllPages[0].title == "doc2" {
   151  		t.Fatal("Expired content published unexpectedly")
   152  	}
   153  }
   154  
   155  func TestLastChange(t *testing.T) {
   156  	t.Parallel()
   157  
   158  	cfg, fs := newTestCfg()
   159  
   160  	writeSource(t, fs, filepath.Join("content", "sect/doc1.md"), "---\ntitle: doc1\nweight: 1\ndate: 2014-05-29\n---\n# doc1\n*some content*")
   161  	writeSource(t, fs, filepath.Join("content", "sect/doc2.md"), "---\ntitle: doc2\nweight: 2\ndate: 2015-05-29\n---\n# doc2\n*some content*")
   162  	writeSource(t, fs, filepath.Join("content", "sect/doc3.md"), "---\ntitle: doc3\nweight: 3\ndate: 2017-05-29\n---\n# doc3\n*some content*")
   163  	writeSource(t, fs, filepath.Join("content", "sect/doc4.md"), "---\ntitle: doc4\nweight: 4\ndate: 2016-05-29\n---\n# doc4\n*some content*")
   164  	writeSource(t, fs, filepath.Join("content", "sect/doc5.md"), "---\ntitle: doc5\nweight: 3\n---\n# doc5\n*some content*")
   165  
   166  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
   167  
   168  	require.False(t, s.Info.LastChange.IsZero(), "Site.LastChange is zero")
   169  	require.Equal(t, 2017, s.Info.LastChange.Year(), "Site.LastChange should be set to the page with latest Lastmod (year 2017)")
   170  }
   171  
   172  // Issue #_index
   173  func TestPageWithUnderScoreIndexInFilename(t *testing.T) {
   174  	t.Parallel()
   175  
   176  	cfg, fs := newTestCfg()
   177  
   178  	writeSource(t, fs, filepath.Join("content", "sect/my_index_file.md"), "---\ntitle: doc1\nweight: 1\ndate: 2014-05-29\n---\n# doc1\n*some content*")
   179  
   180  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
   181  
   182  	require.Len(t, s.RegularPages, 1)
   183  
   184  }
   185  
   186  // Issue #957
   187  func TestCrossrefs(t *testing.T) {
   188  	t.Parallel()
   189  	for _, uglyURLs := range []bool{true, false} {
   190  		for _, relative := range []bool{true, false} {
   191  			doTestCrossrefs(t, relative, uglyURLs)
   192  		}
   193  	}
   194  }
   195  
   196  func doTestCrossrefs(t *testing.T, relative, uglyURLs bool) {
   197  
   198  	baseURL := "http://foo/bar"
   199  
   200  	var refShortcode string
   201  	var expectedBase string
   202  	var expectedURLSuffix string
   203  	var expectedPathSuffix string
   204  
   205  	if relative {
   206  		refShortcode = "relref"
   207  		expectedBase = "/bar"
   208  	} else {
   209  		refShortcode = "ref"
   210  		expectedBase = baseURL
   211  	}
   212  
   213  	if uglyURLs {
   214  		expectedURLSuffix = ".html"
   215  		expectedPathSuffix = ".html"
   216  	} else {
   217  		expectedURLSuffix = "/"
   218  		expectedPathSuffix = "/index.html"
   219  	}
   220  
   221  	doc3Slashed := filepath.FromSlash("/sect/doc3.md")
   222  
   223  	sources := [][2]string{
   224  		{
   225  			filepath.FromSlash("sect/doc1.md"),
   226  			fmt.Sprintf(`Ref 2: {{< %s "sect/doc2.md" >}}`, refShortcode),
   227  		},
   228  		// Issue #1148: Make sure that no P-tags is added around shortcodes.
   229  		{
   230  			filepath.FromSlash("sect/doc2.md"),
   231  			fmt.Sprintf(`**Ref 1:**
   232  
   233  {{< %s "sect/doc1.md" >}}
   234  
   235  THE END.`, refShortcode),
   236  		},
   237  		// Issue #1753: Should not add a trailing newline after shortcode.
   238  		{
   239  			filepath.FromSlash("sect/doc3.md"),
   240  			fmt.Sprintf(`**Ref 1:**{{< %s "sect/doc3.md" >}}.`, refShortcode),
   241  		},
   242  		// Issue #3703
   243  		{
   244  			filepath.FromSlash("sect/doc4.md"),
   245  			fmt.Sprintf(`**Ref 1:**{{< %s "%s" >}}.`, refShortcode, doc3Slashed),
   246  		},
   247  	}
   248  
   249  	cfg, fs := newTestCfg()
   250  
   251  	cfg.Set("baseURL", baseURL)
   252  	cfg.Set("uglyURLs", uglyURLs)
   253  	cfg.Set("verbose", true)
   254  
   255  	for _, src := range sources {
   256  		writeSource(t, fs, filepath.Join("content", src[0]), src[1])
   257  	}
   258  
   259  	s := buildSingleSite(
   260  		t,
   261  		deps.DepsCfg{
   262  			Fs:           fs,
   263  			Cfg:          cfg,
   264  			WithTemplate: createWithTemplateFromNameValues("_default/single.html", "{{.Content}}")},
   265  		BuildCfg{})
   266  
   267  	require.Len(t, s.RegularPages, 4)
   268  
   269  	th := testHelper{s.Cfg, s.Fs, t}
   270  
   271  	tests := []struct {
   272  		doc      string
   273  		expected string
   274  	}{
   275  		{filepath.FromSlash(fmt.Sprintf("public/sect/doc1%s", expectedPathSuffix)), fmt.Sprintf("<p>Ref 2: %s/sect/doc2%s</p>\n", expectedBase, expectedURLSuffix)},
   276  		{filepath.FromSlash(fmt.Sprintf("public/sect/doc2%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong></p>\n\n%s/sect/doc1%s\n\n<p>THE END.</p>\n", expectedBase, expectedURLSuffix)},
   277  		{filepath.FromSlash(fmt.Sprintf("public/sect/doc3%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong>%s/sect/doc3%s.</p>\n", expectedBase, expectedURLSuffix)},
   278  		{filepath.FromSlash(fmt.Sprintf("public/sect/doc4%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong>%s/sect/doc3%s.</p>\n", expectedBase, expectedURLSuffix)},
   279  	}
   280  
   281  	for _, test := range tests {
   282  		th.assertFileContent(test.doc, test.expected)
   283  
   284  	}
   285  
   286  }
   287  
   288  // Issue #939
   289  // Issue #1923
   290  func TestShouldAlwaysHaveUglyURLs(t *testing.T) {
   291  	t.Parallel()
   292  	for _, uglyURLs := range []bool{true, false} {
   293  		doTestShouldAlwaysHaveUglyURLs(t, uglyURLs)
   294  	}
   295  }
   296  
   297  func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
   298  
   299  	cfg, fs := newTestCfg()
   300  
   301  	cfg.Set("verbose", true)
   302  	cfg.Set("baseURL", "http://auth/bub")
   303  	cfg.Set("rssURI", "index.xml")
   304  	cfg.Set("blackfriday",
   305  		map[string]interface{}{
   306  			"plainIDAnchors": true})
   307  
   308  	cfg.Set("uglyURLs", uglyURLs)
   309  
   310  	sources := [][2]string{
   311  		{filepath.FromSlash("sect/doc1.md"), "---\nmarkup: markdown\n---\n# title\nsome *content*"},
   312  		{filepath.FromSlash("sect/doc2.md"), "---\nurl: /ugly.html\nmarkup: markdown\n---\n# title\ndoc2 *content*"},
   313  	}
   314  
   315  	for _, src := range sources {
   316  		writeSource(t, fs, filepath.Join("content", src[0]), src[1])
   317  	}
   318  
   319  	writeSource(t, fs, filepath.Join("layouts", "index.html"), "Home Sweet {{ if.IsHome  }}Home{{ end }}.")
   320  	writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}{{ if.IsHome  }}This is not home!{{ end }}")
   321  	writeSource(t, fs, filepath.Join("layouts", "404.html"), "Page Not Found.{{ if.IsHome  }}This is not home!{{ end }}")
   322  	writeSource(t, fs, filepath.Join("layouts", "rss.xml"), "<root>RSS</root>")
   323  	writeSource(t, fs, filepath.Join("layouts", "sitemap.xml"), "<root>SITEMAP</root>")
   324  
   325  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   326  
   327  	var expectedPagePath string
   328  	if uglyURLs {
   329  		expectedPagePath = "public/sect/doc1.html"
   330  	} else {
   331  		expectedPagePath = "public/sect/doc1/index.html"
   332  	}
   333  
   334  	tests := []struct {
   335  		doc      string
   336  		expected string
   337  	}{
   338  		{filepath.FromSlash("public/index.html"), "Home Sweet Home."},
   339  		{filepath.FromSlash(expectedPagePath), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"},
   340  		{filepath.FromSlash("public/404.html"), "Page Not Found."},
   341  		{filepath.FromSlash("public/index.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>RSS</root>"},
   342  		{filepath.FromSlash("public/sitemap.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>SITEMAP</root>"},
   343  		// Issue #1923
   344  		{filepath.FromSlash("public/ugly.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>doc2 <em>content</em></p>\n"},
   345  	}
   346  
   347  	for _, p := range s.RegularPages {
   348  		assert.False(t, p.IsHome())
   349  	}
   350  
   351  	for _, test := range tests {
   352  		content := readDestination(t, fs, test.doc)
   353  
   354  		if content != test.expected {
   355  			t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content)
   356  		}
   357  	}
   358  
   359  }
   360  
   361  func TestNewSiteDefaultLang(t *testing.T) {
   362  	t.Parallel()
   363  	defer os.Remove("resources")
   364  	s, err := NewSiteDefaultLang()
   365  	require.NoError(t, err)
   366  	require.Equal(t, hugofs.Os, s.Fs.Source)
   367  	require.Equal(t, hugofs.Os, s.Fs.Destination)
   368  }
   369  
   370  // Issue #3355
   371  func TestShouldNotWriteZeroLengthFilesToDestination(t *testing.T) {
   372  	cfg, fs := newTestCfg()
   373  
   374  	writeSource(t, fs, filepath.Join("content", "simple.html"), "simple")
   375  	writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}")
   376  	writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "")
   377  
   378  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   379  	th := testHelper{s.Cfg, s.Fs, t}
   380  
   381  	th.assertFileNotExist(filepath.Join("public", "index.html"))
   382  }
   383  
   384  // Issue #1176
   385  func TestSectionNaming(t *testing.T) {
   386  	t.Parallel()
   387  	for _, canonify := range []bool{true, false} {
   388  		for _, uglify := range []bool{true, false} {
   389  			for _, pluralize := range []bool{true, false} {
   390  				t.Run(fmt.Sprintf("canonify=%t,uglify=%t,pluralize=%t", canonify, uglify, pluralize), func(t *testing.T) {
   391  					doTestSectionNaming(t, canonify, uglify, pluralize)
   392  				})
   393  			}
   394  		}
   395  	}
   396  }
   397  
   398  func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {
   399  
   400  	var expectedPathSuffix string
   401  
   402  	if uglify {
   403  		expectedPathSuffix = ".html"
   404  	} else {
   405  		expectedPathSuffix = "/index.html"
   406  	}
   407  
   408  	sources := [][2]string{
   409  		{filepath.FromSlash("sect/doc1.html"), "doc1"},
   410  		// Add one more page to sect to make sure sect is picked in mainSections
   411  		{filepath.FromSlash("sect/sect.html"), "sect"},
   412  		{filepath.FromSlash("Fish and Chips/doc2.html"), "doc2"},
   413  		{filepath.FromSlash("ラーメン/doc3.html"), "doc3"},
   414  	}
   415  
   416  	cfg, fs := newTestCfg()
   417  
   418  	cfg.Set("baseURL", "http://auth/sub/")
   419  	cfg.Set("uglyURLs", uglify)
   420  	cfg.Set("pluralizeListTitles", pluralize)
   421  	cfg.Set("canonifyURLs", canonify)
   422  
   423  	for _, src := range sources {
   424  		writeSource(t, fs, filepath.Join("content", src[0]), src[1])
   425  	}
   426  
   427  	writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}")
   428  	writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "{{.Title}}")
   429  
   430  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   431  
   432  	mainSections, err := s.Info.Param("mainSections")
   433  	require.NoError(t, err)
   434  	require.Equal(t, []string{"sect"}, mainSections)
   435  
   436  	th := testHelper{s.Cfg, s.Fs, t}
   437  	tests := []struct {
   438  		doc         string
   439  		pluralAware bool
   440  		expected    string
   441  	}{
   442  		{filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), false, "doc1"},
   443  		{filepath.FromSlash(fmt.Sprintf("sect%s", expectedPathSuffix)), true, "Sect"},
   444  		{filepath.FromSlash(fmt.Sprintf("fish-and-chips/doc2%s", expectedPathSuffix)), false, "doc2"},
   445  		{filepath.FromSlash(fmt.Sprintf("fish-and-chips%s", expectedPathSuffix)), true, "Fish and Chips"},
   446  		{filepath.FromSlash(fmt.Sprintf("ラーメン/doc3%s", expectedPathSuffix)), false, "doc3"},
   447  		{filepath.FromSlash(fmt.Sprintf("ラーメン%s", expectedPathSuffix)), true, "ラーメン"},
   448  	}
   449  
   450  	for _, test := range tests {
   451  
   452  		if test.pluralAware && pluralize {
   453  			test.expected = inflect.Pluralize(test.expected)
   454  		}
   455  
   456  		th.assertFileContent(filepath.Join("public", test.doc), test.expected)
   457  	}
   458  
   459  }
   460  func TestSkipRender(t *testing.T) {
   461  	t.Parallel()
   462  	sources := [][2]string{
   463  		{filepath.FromSlash("sect/doc1.html"), "---\nmarkup: markdown\n---\n# title\nsome *content*"},
   464  		{filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"},
   465  		{filepath.FromSlash("sect/doc3.md"), "# doc3\n*some* content"},
   466  		{filepath.FromSlash("sect/doc4.md"), "---\ntitle: doc4\n---\n# doc4\n*some content*"},
   467  		{filepath.FromSlash("sect/doc5.html"), "<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"},
   468  		{filepath.FromSlash("sect/doc6.html"), "<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"},
   469  		{filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"},
   470  		{filepath.FromSlash("sect/doc8.html"), "---\nmarkup: md\n---\n# title\nsome *content*"},
   471  		// Issue #3021
   472  		{filepath.FromSlash("doc9.html"), "<html><body>doc9: {{< myshortcode >}}</body></html>"},
   473  	}
   474  
   475  	cfg, fs := newTestCfg()
   476  
   477  	cfg.Set("verbose", true)
   478  	cfg.Set("canonifyURLs", true)
   479  	cfg.Set("uglyURLs", true)
   480  	cfg.Set("baseURL", "http://auth/bub")
   481  
   482  	for _, src := range sources {
   483  		writeSource(t, fs, filepath.Join("content", src[0]), src[1])
   484  
   485  	}
   486  
   487  	writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}")
   488  	writeSource(t, fs, filepath.Join("layouts", "head"), "<head><script src=\"script.js\"></script></head>")
   489  	writeSource(t, fs, filepath.Join("layouts", "head_abs"), "<head><script src=\"/script.js\"></script></head>")
   490  	writeSource(t, fs, filepath.Join("layouts", "shortcodes", "myshortcode.html"), "SHORT")
   491  
   492  	buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   493  
   494  	tests := []struct {
   495  		doc      string
   496  		expected string
   497  	}{
   498  		{filepath.FromSlash("public/sect/doc1.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"},
   499  		{filepath.FromSlash("public/sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"},
   500  		{filepath.FromSlash("public/sect/doc3.html"), "\n\n<h1 id=\"doc3\">doc3</h1>\n\n<p><em>some</em> content</p>\n"},
   501  		{filepath.FromSlash("public/sect/doc4.html"), "\n\n<h1 id=\"doc4\">doc4</h1>\n\n<p><em>some content</em></p>\n"},
   502  		{filepath.FromSlash("public/sect/doc5.html"), "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"},
   503  		{filepath.FromSlash("public/sect/doc6.html"), "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
   504  		{filepath.FromSlash("public/doc7.html"), "<html><body>doc7 content</body></html>"},
   505  		{filepath.FromSlash("public/sect/doc8.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"},
   506  		{filepath.FromSlash("public/doc9.html"), "<html><body>doc9: SHORT</body></html>"},
   507  	}
   508  
   509  	for _, test := range tests {
   510  		file, err := fs.Destination.Open(test.doc)
   511  		if err != nil {
   512  			t.Fatalf("Did not find %s in target.", test.doc)
   513  		}
   514  
   515  		content := helpers.ReaderToString(file)
   516  
   517  		if content != test.expected {
   518  			t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content)
   519  		}
   520  	}
   521  }
   522  
   523  func TestAbsURLify(t *testing.T) {
   524  	t.Parallel()
   525  	sources := [][2]string{
   526  		{filepath.FromSlash("sect/doc1.html"), "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
   527  		{filepath.FromSlash("blue/doc2.html"), "---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"},
   528  	}
   529  	for _, baseURL := range []string{"http://auth/bub", "http://base", "//base"} {
   530  		for _, canonify := range []bool{true, false} {
   531  
   532  			cfg, fs := newTestCfg()
   533  
   534  			cfg.Set("uglyURLs", true)
   535  			cfg.Set("canonifyURLs", canonify)
   536  			cfg.Set("baseURL", baseURL)
   537  
   538  			for _, src := range sources {
   539  				writeSource(t, fs, filepath.Join("content", src[0]), src[1])
   540  
   541  			}
   542  
   543  			writeSource(t, fs, filepath.Join("layouts", "blue/single.html"), templateWithURLAbs)
   544  
   545  			s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   546  			th := testHelper{s.Cfg, s.Fs, t}
   547  
   548  			tests := []struct {
   549  				file, expected string
   550  			}{
   551  				{"public/blue/doc2.html", "<a href=\"%s/foobar.jpg\">Going</a>"},
   552  				{"public/sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
   553  			}
   554  
   555  			for _, test := range tests {
   556  
   557  				expected := test.expected
   558  
   559  				if strings.Contains(expected, "%s") {
   560  					expected = fmt.Sprintf(expected, baseURL)
   561  				}
   562  
   563  				if !canonify {
   564  					expected = strings.Replace(expected, baseURL, "", -1)
   565  				}
   566  
   567  				th.assertFileContent(test.file, expected)
   568  
   569  			}
   570  		}
   571  	}
   572  }
   573  
   574  var weightedPage1 = `+++
   575  weight = "2"
   576  title = "One"
   577  my_param = "foo"
   578  my_date = 1979-05-27T07:32:00Z
   579  +++
   580  Front Matter with Ordered Pages`
   581  
   582  var weightedPage2 = `+++
   583  weight = "6"
   584  title = "Two"
   585  publishdate = "2012-03-05"
   586  my_param = "foo"
   587  +++
   588  Front Matter with Ordered Pages 2`
   589  
   590  var weightedPage3 = `+++
   591  weight = "4"
   592  title = "Three"
   593  date = "2012-04-06"
   594  publishdate = "2012-04-06"
   595  my_param = "bar"
   596  only_one = "yes"
   597  my_date = 2010-05-27T07:32:00Z
   598  +++
   599  Front Matter with Ordered Pages 3`
   600  
   601  var weightedPage4 = `+++
   602  weight = "4"
   603  title = "Four"
   604  date = "2012-01-01"
   605  publishdate = "2012-01-01"
   606  my_param = "baz"
   607  my_date = 2010-05-27T07:32:00Z
   608  categories = [ "hugo" ]
   609  +++
   610  Front Matter with Ordered Pages 4. This is longer content`
   611  
   612  var weightedSources = [][2]string{
   613  	{filepath.FromSlash("sect/doc1.md"), weightedPage1},
   614  	{filepath.FromSlash("sect/doc2.md"), weightedPage2},
   615  	{filepath.FromSlash("sect/doc3.md"), weightedPage3},
   616  	{filepath.FromSlash("sect/doc4.md"), weightedPage4},
   617  }
   618  
   619  func TestOrderedPages(t *testing.T) {
   620  	t.Parallel()
   621  	cfg, fs := newTestCfg()
   622  	cfg.Set("baseURL", "http://auth/bub")
   623  
   624  	for _, src := range weightedSources {
   625  		writeSource(t, fs, filepath.Join("content", src[0]), src[1])
   626  
   627  	}
   628  
   629  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
   630  
   631  	if s.getPage(KindSection, "sect").Pages[1].title != "Three" || s.getPage(KindSection, "sect").Pages[2].title != "Four" {
   632  		t.Error("Pages in unexpected order.")
   633  	}
   634  
   635  	bydate := s.RegularPages.ByDate()
   636  
   637  	if bydate[0].title != "One" {
   638  		t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bydate[0].title)
   639  	}
   640  
   641  	rev := bydate.Reverse()
   642  	if rev[0].title != "Three" {
   643  		t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rev[0].title)
   644  	}
   645  
   646  	bypubdate := s.RegularPages.ByPublishDate()
   647  
   648  	if bypubdate[0].title != "One" {
   649  		t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bypubdate[0].title)
   650  	}
   651  
   652  	rbypubdate := bypubdate.Reverse()
   653  	if rbypubdate[0].title != "Three" {
   654  		t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rbypubdate[0].title)
   655  	}
   656  
   657  	bylength := s.RegularPages.ByLength()
   658  	if bylength[0].title != "One" {
   659  		t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bylength[0].title)
   660  	}
   661  
   662  	rbylength := bylength.Reverse()
   663  	if rbylength[0].title != "Four" {
   664  		t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Four", rbylength[0].title)
   665  	}
   666  }
   667  
   668  var groupedSources = [][2]string{
   669  	{filepath.FromSlash("sect1/doc1.md"), weightedPage1},
   670  	{filepath.FromSlash("sect1/doc2.md"), weightedPage2},
   671  	{filepath.FromSlash("sect2/doc3.md"), weightedPage3},
   672  	{filepath.FromSlash("sect3/doc4.md"), weightedPage4},
   673  }
   674  
   675  func TestGroupedPages(t *testing.T) {
   676  	t.Parallel()
   677  	defer func() {
   678  		if r := recover(); r != nil {
   679  			fmt.Println("Recovered in f", r)
   680  		}
   681  	}()
   682  
   683  	cfg, fs := newTestCfg()
   684  	cfg.Set("baseURL", "http://auth/bub")
   685  
   686  	writeSourcesToSource(t, "content", fs, groupedSources...)
   687  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   688  
   689  	rbysection, err := s.RegularPages.GroupBy("Section", "desc")
   690  	if err != nil {
   691  		t.Fatalf("Unable to make PageGroup array: %s", err)
   692  	}
   693  
   694  	if rbysection[0].Key != "sect3" {
   695  		t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "sect3", rbysection[0].Key)
   696  	}
   697  	if rbysection[1].Key != "sect2" {
   698  		t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "sect2", rbysection[1].Key)
   699  	}
   700  	if rbysection[2].Key != "sect1" {
   701  		t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "sect1", rbysection[2].Key)
   702  	}
   703  	if rbysection[0].Pages[0].title != "Four" {
   704  		t.Errorf("PageGroup has an unexpected page. First group's pages should have '%s', got '%s'", "Four", rbysection[0].Pages[0].title)
   705  	}
   706  	if len(rbysection[2].Pages) != 2 {
   707  		t.Errorf("PageGroup has unexpected number of pages. Third group should have '%d' pages, got '%d' pages", 2, len(rbysection[2].Pages))
   708  	}
   709  
   710  	bytype, err := s.RegularPages.GroupBy("Type", "asc")
   711  	if err != nil {
   712  		t.Fatalf("Unable to make PageGroup array: %s", err)
   713  	}
   714  	if bytype[0].Key != "sect1" {
   715  		t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "sect1", bytype[0].Key)
   716  	}
   717  	if bytype[1].Key != "sect2" {
   718  		t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "sect2", bytype[1].Key)
   719  	}
   720  	if bytype[2].Key != "sect3" {
   721  		t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "sect3", bytype[2].Key)
   722  	}
   723  	if bytype[2].Pages[0].title != "Four" {
   724  		t.Errorf("PageGroup has an unexpected page. Third group's data should have '%s', got '%s'", "Four", bytype[0].Pages[0].title)
   725  	}
   726  	if len(bytype[0].Pages) != 2 {
   727  		t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(bytype[2].Pages))
   728  	}
   729  
   730  	bydate, err := s.RegularPages.GroupByDate("2006-01", "asc")
   731  	if err != nil {
   732  		t.Fatalf("Unable to make PageGroup array: %s", err)
   733  	}
   734  	if bydate[0].Key != "0001-01" {
   735  		t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "0001-01", bydate[0].Key)
   736  	}
   737  	if bydate[1].Key != "2012-01" {
   738  		t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "2012-01", bydate[1].Key)
   739  	}
   740  
   741  	bypubdate, err := s.RegularPages.GroupByPublishDate("2006")
   742  	if err != nil {
   743  		t.Fatalf("Unable to make PageGroup array: %s", err)
   744  	}
   745  	if bypubdate[0].Key != "2012" {
   746  		t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "2012", bypubdate[0].Key)
   747  	}
   748  	if bypubdate[1].Key != "0001" {
   749  		t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "0001", bypubdate[1].Key)
   750  	}
   751  	if bypubdate[0].Pages[0].title != "Three" {
   752  		t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", bypubdate[0].Pages[0].title)
   753  	}
   754  	if len(bypubdate[0].Pages) != 3 {
   755  		t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 3, len(bypubdate[0].Pages))
   756  	}
   757  
   758  	byparam, err := s.RegularPages.GroupByParam("my_param", "desc")
   759  	if err != nil {
   760  		t.Fatalf("Unable to make PageGroup array: %s", err)
   761  	}
   762  	if byparam[0].Key != "foo" {
   763  		t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "foo", byparam[0].Key)
   764  	}
   765  	if byparam[1].Key != "baz" {
   766  		t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "baz", byparam[1].Key)
   767  	}
   768  	if byparam[2].Key != "bar" {
   769  		t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "bar", byparam[2].Key)
   770  	}
   771  	if byparam[2].Pages[0].title != "Three" {
   772  		t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", byparam[2].Pages[0].title)
   773  	}
   774  	if len(byparam[0].Pages) != 2 {
   775  		t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(byparam[0].Pages))
   776  	}
   777  
   778  	_, err = s.RegularPages.GroupByParam("not_exist")
   779  	if err == nil {
   780  		t.Errorf("GroupByParam didn't return an expected error")
   781  	}
   782  
   783  	byOnlyOneParam, err := s.RegularPages.GroupByParam("only_one")
   784  	if err != nil {
   785  		t.Fatalf("Unable to make PageGroup array: %s", err)
   786  	}
   787  	if len(byOnlyOneParam) != 1 {
   788  		t.Errorf("PageGroup array has unexpected elements. Group length should be '%d', got '%d'", 1, len(byOnlyOneParam))
   789  	}
   790  	if byOnlyOneParam[0].Key != "yes" {
   791  		t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "yes", byOnlyOneParam[0].Key)
   792  	}
   793  
   794  	byParamDate, err := s.RegularPages.GroupByParamDate("my_date", "2006-01")
   795  	if err != nil {
   796  		t.Fatalf("Unable to make PageGroup array: %s", err)
   797  	}
   798  	if byParamDate[0].Key != "2010-05" {
   799  		t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "2010-05", byParamDate[0].Key)
   800  	}
   801  	if byParamDate[1].Key != "1979-05" {
   802  		t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "1979-05", byParamDate[1].Key)
   803  	}
   804  	if byParamDate[1].Pages[0].title != "One" {
   805  		t.Errorf("PageGroup has an unexpected page. Second group's pages should have '%s', got '%s'", "One", byParamDate[1].Pages[0].title)
   806  	}
   807  	if len(byParamDate[0].Pages) != 2 {
   808  		t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(byParamDate[2].Pages))
   809  	}
   810  }
   811  
   812  var pageWithWeightedTaxonomies1 = `+++
   813  tags = [ "a", "b", "c" ]
   814  tags_weight = 22
   815  categories = ["d"]
   816  title = "foo"
   817  categories_weight = 44
   818  +++
   819  Front Matter with weighted tags and categories`
   820  
   821  var pageWithWeightedTaxonomies2 = `+++
   822  tags = "a"
   823  tags_weight = 33
   824  title = "bar"
   825  categories = [ "d", "e" ]
   826  categories_weight = 11
   827  alias = "spf13"
   828  date = 1979-05-27T07:32:00Z
   829  +++
   830  Front Matter with weighted tags and categories`
   831  
   832  var pageWithWeightedTaxonomies3 = `+++
   833  title = "bza"
   834  categories = [ "e" ]
   835  categories_weight = 11
   836  alias = "spf13"
   837  date = 2010-05-27T07:32:00Z
   838  +++
   839  Front Matter with weighted tags and categories`
   840  
   841  func TestWeightedTaxonomies(t *testing.T) {
   842  	t.Parallel()
   843  	sources := [][2]string{
   844  		{filepath.FromSlash("sect/doc1.md"), pageWithWeightedTaxonomies2},
   845  		{filepath.FromSlash("sect/doc2.md"), pageWithWeightedTaxonomies1},
   846  		{filepath.FromSlash("sect/doc3.md"), pageWithWeightedTaxonomies3},
   847  	}
   848  	taxonomies := make(map[string]string)
   849  
   850  	taxonomies["tag"] = "tags"
   851  	taxonomies["category"] = "categories"
   852  
   853  	cfg, fs := newTestCfg()
   854  
   855  	cfg.Set("baseURL", "http://auth/bub")
   856  	cfg.Set("taxonomies", taxonomies)
   857  
   858  	writeSourcesToSource(t, "content", fs, sources...)
   859  	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   860  
   861  	if s.Taxonomies["tags"]["a"][0].Page.title != "foo" {
   862  		t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Taxonomies["tags"]["a"][0].Page.title)
   863  	}
   864  
   865  	if s.Taxonomies["categories"]["d"][0].Page.title != "bar" {
   866  		t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Taxonomies["categories"]["d"][0].Page.title)
   867  	}
   868  
   869  	if s.Taxonomies["categories"]["e"][0].Page.title != "bza" {
   870  		t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Taxonomies["categories"]["e"][0].Page.title)
   871  	}
   872  }
   873  
   874  func setupLinkingMockSite(t *testing.T) *Site {
   875  	sources := [][2]string{
   876  		{filepath.FromSlash("level2/unique.md"), ""},
   877  		{filepath.FromSlash("_index.md"), ""},
   878  		{filepath.FromSlash("common.md"), ""},
   879  		{filepath.FromSlash("rootfile.md"), ""},
   880  		{filepath.FromSlash("root-image.png"), ""},
   881  
   882  		{filepath.FromSlash("level2/2-root.md"), ""},
   883  		{filepath.FromSlash("level2/common.md"), ""},
   884  
   885  		{filepath.FromSlash("level2/2-image.png"), ""},
   886  		{filepath.FromSlash("level2/common.png"), ""},
   887  
   888  		{filepath.FromSlash("level2/level3/start.md"), ""},
   889  		{filepath.FromSlash("level2/level3/_index.md"), ""},
   890  		{filepath.FromSlash("level2/level3/3-root.md"), ""},
   891  		{filepath.FromSlash("level2/level3/common.md"), ""},
   892  		{filepath.FromSlash("level2/level3/3-image.png"), ""},
   893  		{filepath.FromSlash("level2/level3/common.png"), ""},
   894  
   895  		{filepath.FromSlash("level2/level3/embedded.dot.md"), ""},
   896  	}
   897  
   898  	cfg, fs := newTestCfg()
   899  
   900  	cfg.Set("baseURL", "http://auth/")
   901  	cfg.Set("uglyURLs", false)
   902  	cfg.Set("outputs", map[string]interface{}{
   903  		"page": []string{"HTML", "AMP"},
   904  	})
   905  	cfg.Set("pluralizeListTitles", false)
   906  	cfg.Set("canonifyURLs", false)
   907  	cfg.Set("blackfriday",
   908  		map[string]interface{}{})
   909  	writeSourcesToSource(t, "content", fs, sources...)
   910  	return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   911  
   912  }
   913  
   914  func TestRefLinking(t *testing.T) {
   915  	t.Parallel()
   916  	site := setupLinkingMockSite(t)
   917  
   918  	currentPage := site.getPage(KindPage, "level2/level3/start.md")
   919  	if currentPage == nil {
   920  		t.Fatalf("failed to find current page in site")
   921  	}
   922  
   923  	for i, test := range []struct {
   924  		link         string
   925  		outputFormat string
   926  		relative     bool
   927  		expected     string
   928  	}{
   929  		// different refs resolving to the same unique filename:
   930  		{"/level2/unique.md", "", true, "/level2/unique/"},
   931  		{"../unique.md", "", true, "/level2/unique/"},
   932  		{"unique.md", "", true, "/level2/unique/"},
   933  
   934  		{"level2/common.md", "", true, "/level2/common/"},
   935  		{"3-root.md", "", true, "/level2/level3/3-root/"},
   936  		{"../..", "", true, "/"},
   937  
   938  		// different refs resolving to the same ambiguous top-level filename:
   939  		{"../../common.md", "", true, "/common/"},
   940  		{"/common.md", "", true, "/common/"},
   941  
   942  		// different refs resolving to the same ambiguous level-2 filename:
   943  		{"/level2/common.md", "", true, "/level2/common/"},
   944  		{"../common.md", "", true, "/level2/common/"},
   945  		{"common.md", "", true, "/level2/level3/common/"},
   946  
   947  		// different refs resolving to the same section:
   948  		{"/level2", "", true, "/level2/"},
   949  		{"..", "", true, "/level2/"},
   950  		{"../", "", true, "/level2/"},
   951  
   952  		// different refs resolving to the same subsection:
   953  		{"/level2/level3", "", true, "/level2/level3/"},
   954  		{"/level2/level3/_index.md", "", true, "/level2/level3/"},
   955  		{".", "", true, "/level2/level3/"},
   956  		{"./", "", true, "/level2/level3/"},
   957  
   958  		// try to confuse parsing
   959  		{"embedded.dot.md", "", true, "/level2/level3/embedded.dot/"},
   960  
   961  		//test empty link, as well as fragment only link
   962  		{"", "", true, ""},
   963  	} {
   964  		checkLinkCase(site, test.link, currentPage, test.relative, test.outputFormat, test.expected, t, i)
   965  
   966  		//make sure fragment links are also handled
   967  		checkLinkCase(site, test.link+"#intro", currentPage, test.relative, test.outputFormat, test.expected+"#intro", t, i)
   968  	}
   969  
   970  	// TODO: and then the failure cases.
   971  }
   972  
   973  func checkLinkCase(site *Site, link string, currentPage *Page, relative bool, outputFormat string, expected string, t *testing.T, i int) {
   974  	if out, err := site.refLink(link, currentPage, relative, outputFormat); err != nil || out != expected {
   975  		t.Errorf("[%d] Expected %q from %q to resolve to %q, got %q - error: %s", i, link, currentPage.absoluteSourceRef(), expected, out, err)
   976  	}
   977  }