github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/hugolib/hugo_sites_rebuild_test.go (about)

     1  // Copyright 2019 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  	"testing"
    18  
    19  	qt "github.com/frankban/quicktest"
    20  )
    21  
    22  func TestSitesRebuild(t *testing.T) {
    23  	configFile := `
    24  baseURL = "https://example.com"
    25  title = "Rebuild this"
    26  contentDir = "content"
    27  enableInlineShortcodes = true
    28  timeout = "5s"
    29  
    30  
    31  `
    32  
    33  	var (
    34  		contentFilename = "content/blog/page1.md"
    35  		dataFilename    = "data/mydata.toml"
    36  	)
    37  
    38  	createSiteBuilder := func(t testing.TB) *sitesBuilder {
    39  		b := newTestSitesBuilder(t).WithConfigFile("toml", configFile).Running()
    40  
    41  		b.WithSourceFile(dataFilename, `hugo = "Rocks!"`)
    42  
    43  		b.WithContent("content/_index.md", `---
    44  title: Home, Sweet Home!
    45  ---
    46  
    47  `)
    48  
    49  		b.WithContent(contentFilename, `
    50  ---
    51  title: "Page 1"
    52  summary: "Initial summary"
    53  paginate: 3
    54  ---
    55  
    56  Content.
    57  
    58  {{< badge.inline >}}
    59  Data Inline: {{ site.Data.mydata.hugo }}
    60  {{< /badge.inline >}}
    61  `)
    62  
    63  		// For .Page.Render tests
    64  		b.WithContent("prender.md", `---
    65  title: Page 1
    66  ---
    67  
    68  Content for Page 1.
    69  
    70  {{< dorender >}}
    71  
    72  `)
    73  
    74  		b.WithTemplatesAdded(
    75  			"layouts/shortcodes/dorender.html", `
    76  {{ $p := .Page }}
    77  Render {{ $p.RelPermalink }}: {{ $p.Render "single" }}
    78  
    79  `)
    80  
    81  		b.WithTemplatesAdded("index.html", `
    82  {{ range (.Paginate .Site.RegularPages).Pages }}
    83  * Page Paginate: {{ .Title }}|Summary: {{ .Summary }}|Content: {{ .Content }}
    84  {{ end }}
    85  {{ range .Site.RegularPages }}
    86  * Page Pages: {{ .Title }}|Summary: {{ .Summary }}|Content: {{ .Content }}
    87  {{ end }}
    88  Content: {{ .Content }}
    89  Data: {{ site.Data.mydata.hugo }}
    90  `)
    91  
    92  		b.WithTemplatesAdded("layouts/partials/mypartial1.html", `Mypartial1`)
    93  		b.WithTemplatesAdded("layouts/partials/mypartial2.html", `Mypartial2`)
    94  		b.WithTemplatesAdded("layouts/partials/mypartial3.html", `Mypartial3`)
    95  		b.WithTemplatesAdded("_default/single.html", `{{ define "main" }}Single Main: {{ .Title }}|Mypartial1: {{ partial "mypartial1.html" }}{{ end }}`)
    96  		b.WithTemplatesAdded("_default/list.html", `{{ define "main" }}List Main: {{ .Title }}{{ end }}`)
    97  		b.WithTemplatesAdded("_default/baseof.html", `Baseof:{{ block "main" . }}Baseof Main{{ end }}|Mypartial3: {{ partial "mypartial3.html" }}:END`)
    98  
    99  		return b
   100  	}
   101  
   102  	t.Run("Refresh paginator on edit", func(t *testing.T) {
   103  		b := createSiteBuilder(t)
   104  
   105  		b.Build(BuildCfg{})
   106  
   107  		b.AssertFileContent("public/index.html", "* Page Paginate: Page 1|Summary: Initial summary|Content: <p>Content.</p>")
   108  
   109  		b.EditFiles(contentFilename, `
   110  ---
   111  title: "Page 1 edit"
   112  summary: "Edited summary"
   113  ---
   114  
   115  Edited content.
   116  
   117  `)
   118  
   119  		b.Build(BuildCfg{})
   120  
   121  		b.AssertFileContent("public/index.html", "* Page Paginate: Page 1 edit|Summary: Edited summary|Content: <p>Edited content.</p>")
   122  		// https://github.com/gohugoio/hugo/issues/5833
   123  		b.AssertFileContent("public/index.html", "* Page Pages: Page 1 edit|Summary: Edited summary|Content: <p>Edited content.</p>")
   124  	})
   125  
   126  	// https://github.com/gohugoio/hugo/issues/6768
   127  	t.Run("Edit data", func(t *testing.T) {
   128  		b := createSiteBuilder(t)
   129  
   130  		b.Build(BuildCfg{})
   131  
   132  		b.AssertFileContent("public/index.html", `
   133  Data: Rocks!
   134  Data Inline: Rocks!
   135  `)
   136  
   137  		b.EditFiles(dataFilename, `hugo = "Rules!"`)
   138  
   139  		b.Build(BuildCfg{})
   140  
   141  		b.AssertFileContent("public/index.html", `
   142  Data: Rules!
   143  Data Inline: Rules!`)
   144  	})
   145  
   146  	// https://github.com/gohugoio/hugo/issues/6968
   147  	t.Run("Edit single.html with base", func(t *testing.T) {
   148  		b := newTestSitesBuilder(t).Running()
   149  
   150  		b.WithTemplates(
   151  			"_default/single.html", `{{ define "main" }}Single{{ end }}`,
   152  			"_default/baseof.html", `Base: {{ block "main"  .}}Block{{ end }}`,
   153  		)
   154  
   155  		b.WithContent("p1.md", "---\ntitle: Page\n---")
   156  
   157  		b.Build(BuildCfg{})
   158  
   159  		b.EditFiles("layouts/_default/single.html", `Single Edit: {{ define "main" }}Single{{ end }}`)
   160  
   161  		counters := &testCounters{}
   162  
   163  		b.Build(BuildCfg{testCounters: counters})
   164  
   165  		b.Assert(int(counters.contentRenderCounter), qt.Equals, 0)
   166  	})
   167  
   168  	t.Run("Page.Render, edit baseof", func(t *testing.T) {
   169  		b := createSiteBuilder(t)
   170  
   171  		b.WithTemplatesAdded("index.html", `
   172  {{ $p := site.GetPage "prender.md" }}
   173  prender: {{ $p.Title }}|{{ $p.Content }}
   174  
   175  `)
   176  
   177  		b.Build(BuildCfg{})
   178  
   179  		b.AssertFileContent("public/index.html", `
   180   Render /prender/: Baseof:Single Main: Page 1|Mypartial1: Mypartial1|Mypartial3: Mypartial3:END
   181  `)
   182  
   183  		b.EditFiles("layouts/_default/baseof.html", `Baseof Edited:{{ block "main" . }}Baseof Main{{ end }}:END`)
   184  
   185  		b.Build(BuildCfg{})
   186  
   187  		b.AssertFileContent("public/index.html", `
   188  Render /prender/: Baseof Edited:Single Main: Page 1|Mypartial1: Mypartial1:END
   189  `)
   190  	})
   191  
   192  	t.Run("Page.Render, edit partial in baseof", func(t *testing.T) {
   193  		b := createSiteBuilder(t)
   194  
   195  		b.WithTemplatesAdded("index.html", `
   196  {{ $p := site.GetPage "prender.md" }}
   197  prender: {{ $p.Title }}|{{ $p.Content }}
   198  
   199  `)
   200  
   201  		b.Build(BuildCfg{})
   202  
   203  		b.AssertFileContent("public/index.html", `
   204   Render /prender/: Baseof:Single Main: Page 1|Mypartial1: Mypartial1|Mypartial3: Mypartial3:END
   205  `)
   206  
   207  		b.EditFiles("layouts/partials/mypartial3.html", `Mypartial3 Edited`)
   208  
   209  		b.Build(BuildCfg{})
   210  
   211  		b.AssertFileContent("public/index.html", `
   212  Render /prender/: Baseof:Single Main: Page 1|Mypartial1: Mypartial1|Mypartial3: Mypartial3 Edited:END
   213  `)
   214  	})
   215  
   216  	t.Run("Edit RSS shortcode", func(t *testing.T) {
   217  		b := createSiteBuilder(t)
   218  
   219  		b.WithContent("output.md", `---
   220  title: Output
   221  outputs: ["HTML", "AMP"]
   222  layout: output
   223  ---
   224  
   225  Content for Output.
   226  
   227  {{< output >}}
   228  
   229  `)
   230  
   231  		b.WithTemplates(
   232  			"layouts/_default/output.html", `Output HTML: {{ .RelPermalink }}|{{ .Content }}`,
   233  			"layouts/_default/output.amp.html", `Output AMP: {{ .RelPermalink }}|{{ .Content }}`,
   234  			"layouts/shortcodes/output.html", `Output Shortcode HTML`,
   235  			"layouts/shortcodes/output.amp.html", `Output Shortcode AMP`)
   236  
   237  		b.Build(BuildCfg{})
   238  
   239  		b.AssertFileContent("public/output/index.html", `
   240  Output Shortcode HTML
   241  `)
   242  		b.AssertFileContent("public/amp/output/index.html", `
   243  Output Shortcode AMP
   244  `)
   245  
   246  		b.EditFiles("layouts/shortcodes/output.amp.html", `Output Shortcode AMP Edited`)
   247  
   248  		b.Build(BuildCfg{})
   249  
   250  		b.AssertFileContent("public/amp/output/index.html", `
   251  Output Shortcode AMP Edited
   252  `)
   253  	})
   254  }
   255  
   256  // Issues #7623 #7625
   257  func TestSitesRebuildOnFilesIncludedWithGetPage(t *testing.T) {
   258  	b := newTestSitesBuilder(t).Running()
   259  	b.WithContent("pages/p1.md", `---
   260  title: p1
   261  ---
   262  P3: {{< GetPage "pages/p3" >}}
   263  `)
   264  
   265  	b.WithContent("pages/p2.md", `---
   266  title: p2
   267  ---
   268  P4: {{< site_GetPage "pages/p4" >}}
   269  P5: {{< site_GetPage "p5" >}}
   270  P6: {{< dot_site_GetPage "p6" >}}
   271  `)
   272  
   273  	b.WithContent("pages/p3/index.md", "---\ntitle: p3\nheadless: true\n---\nP3 content")
   274  	b.WithContent("pages/p4/index.md", "---\ntitle: p4\nheadless: true\n---\nP4 content")
   275  	b.WithContent("pages/p5.md", "---\ntitle: p5\n---\nP5 content")
   276  	b.WithContent("pages/p6.md", "---\ntitle: p6\n---\nP6 content")
   277  
   278  	b.WithTemplates(
   279  		"_default/single.html", `{{ .Content }}`,
   280  		"shortcodes/GetPage.html", `
   281  {{ $arg := .Get 0 }}
   282  {{ $p := .Page.GetPage $arg }}
   283  {{ $p.Content }}
   284  	`,
   285  		"shortcodes/site_GetPage.html", `
   286  {{ $arg := .Get 0 }}
   287  {{ $p := site.GetPage $arg }}
   288  {{ $p.Content }}
   289  	`, "shortcodes/dot_site_GetPage.html", `
   290  {{ $arg := .Get 0 }}
   291  {{ $p := .Site.GetPage $arg }}
   292  {{ $p.Content }}
   293  	`,
   294  	)
   295  
   296  	b.Build(BuildCfg{})
   297  
   298  	b.AssertFileContent("public/pages/p1/index.html", "P3 content")
   299  	b.AssertFileContent("public/pages/p2/index.html", `P4 content
   300  P5 content
   301  P6 content
   302  `)
   303  
   304  	b.EditFiles("content/pages/p3/index.md", "---\ntitle: p3\n---\nP3 changed content")
   305  	b.EditFiles("content/pages/p4/index.md", "---\ntitle: p4\n---\nP4 changed content")
   306  	b.EditFiles("content/pages/p5.md", "---\ntitle: p5\n---\nP5 changed content")
   307  	b.EditFiles("content/pages/p6.md", "---\ntitle: p6\n---\nP6 changed content")
   308  
   309  	b.Build(BuildCfg{})
   310  
   311  	b.AssertFileContent("public/pages/p1/index.html", "P3 changed content")
   312  	b.AssertFileContent("public/pages/p2/index.html", `P4 changed content
   313  P5 changed content
   314  P6 changed content
   315  `)
   316  }