github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/partials/integration_test.go (about)

     1  // Copyright 2022 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 partials_test
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  	"regexp"
    20  	"sort"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/gohugoio/hugo/htesting/hqt"
    25  	"github.com/gohugoio/hugo/hugolib"
    26  )
    27  
    28  func TestInclude(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	files := `
    32  -- config.toml --
    33  baseURL = 'http://example.com/'
    34  -- layouts/index.html --
    35  partial: {{ partials.Include "foo.html" . }}
    36  -- layouts/partials/foo.html --
    37  foo
    38    `
    39  
    40  	b := hugolib.NewIntegrationTestBuilder(
    41  		hugolib.IntegrationTestConfig{
    42  			T:           t,
    43  			TxtarString: files,
    44  		},
    45  	).Build()
    46  
    47  	b.AssertFileContent("public/index.html", `
    48  partial: foo
    49  `)
    50  }
    51  
    52  func TestIncludeCached(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	files := `
    56  -- config.toml --
    57  baseURL = 'http://example.com/'
    58  -- layouts/index.html --
    59  partialCached: {{ partials.IncludeCached "foo.html" . }}
    60  partialCached: {{ partials.IncludeCached "foo.html" . }}
    61  -- layouts/partials/foo.html --
    62  foo
    63    `
    64  
    65  	b := hugolib.NewIntegrationTestBuilder(
    66  		hugolib.IntegrationTestConfig{
    67  			T:           t,
    68  			TxtarString: files,
    69  		},
    70  	).Build()
    71  
    72  	b.AssertFileContent("public/index.html", `
    73  partialCached: foo
    74  partialCached: foo
    75  `)
    76  }
    77  
    78  // Issue 9519
    79  func TestIncludeCachedRecursion(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	files := `
    83  -- config.toml --
    84  baseURL = 'http://example.com/'
    85  -- layouts/index.html --
    86  {{ partials.IncludeCached "p1.html" . }}
    87  -- layouts/partials/p1.html --
    88  {{ partials.IncludeCached "p2.html" . }}
    89  -- layouts/partials/p2.html --
    90  P2
    91  
    92    `
    93  
    94  	b := hugolib.NewIntegrationTestBuilder(
    95  		hugolib.IntegrationTestConfig{
    96  			T:           t,
    97  			TxtarString: files,
    98  		},
    99  	).Build()
   100  
   101  	b.AssertFileContent("public/index.html", `
   102  P2
   103  `)
   104  }
   105  
   106  func TestIncludeCacheHints(t *testing.T) {
   107  	t.Parallel()
   108  
   109  	files := `
   110  -- config.toml --
   111  baseURL = 'http://example.com/'
   112  templateMetrics=true
   113  templateMetricsHints=true
   114  disableKinds = ["page", "section", "taxonomy", "term", "sitemap"]
   115  [outputs]
   116  home = ["HTML"]
   117  -- layouts/index.html --
   118  {{ partials.IncludeCached "static1.html" . }}
   119  {{ partials.IncludeCached "static1.html" . }}
   120  {{ partials.Include "static2.html" . }}
   121  
   122  D1I: {{ partials.Include "dynamic1.html" . }}
   123  D1C: {{ partials.IncludeCached "dynamic1.html" . }}
   124  D1C: {{ partials.IncludeCached "dynamic1.html" . }}
   125  D1C: {{ partials.IncludeCached "dynamic1.html" . }}
   126  H1I: {{ partials.Include "halfdynamic1.html" . }}
   127  H1C: {{ partials.IncludeCached "halfdynamic1.html" . }}
   128  H1C: {{ partials.IncludeCached "halfdynamic1.html" . }}
   129  
   130  -- layouts/partials/static1.html --
   131  P1
   132  -- layouts/partials/static2.html --
   133  P2
   134  -- layouts/partials/dynamic1.html --
   135  {{ math.Counter }}
   136  -- layouts/partials/halfdynamic1.html --
   137  D1
   138  {{ math.Counter }}
   139  
   140  
   141    `
   142  
   143  	b := hugolib.NewIntegrationTestBuilder(
   144  		hugolib.IntegrationTestConfig{
   145  			T:           t,
   146  			TxtarString: files,
   147  		},
   148  	).Build()
   149  
   150  	// fmt.Println(b.FileContent("public/index.html"))
   151  
   152  	var buf bytes.Buffer
   153  	b.H.Metrics.WriteMetrics(&buf)
   154  
   155  	got := buf.String()
   156  
   157  	// Get rid of all the durations, they are never the same.
   158  	durationRe := regexp.MustCompile(`\b[\.\d]*(ms|µs|s)\b`)
   159  
   160  	normalize := func(s string) string {
   161  		s = durationRe.ReplaceAllString(s, "")
   162  		linesIn := strings.Split(s, "\n")[3:]
   163  		var lines []string
   164  		for _, l := range linesIn {
   165  			l = strings.TrimSpace(l)
   166  			if l == "" {
   167  				continue
   168  			}
   169  			lines = append(lines, l)
   170  		}
   171  
   172  		sort.Strings(lines)
   173  
   174  		return strings.Join(lines, "\n")
   175  	}
   176  
   177  	got = normalize(got)
   178  
   179  	expect := `
   180  	0        0       0      1  index.html
   181  	100        0       0      1  partials/static2.html
   182  	100       50       1      2  partials/static1.html
   183  	25       50       2      4  partials/dynamic1.html
   184  	66       33       1      3  partials/halfdynamic1.html
   185  	`
   186  
   187  	b.Assert(got, hqt.IsSameString, expect)
   188  }
   189  
   190  //  gobench --package ./tpl/partials
   191  func BenchmarkIncludeCached(b *testing.B) {
   192  	files := `
   193  -- config.toml --
   194  baseURL = 'http://example.com/'
   195  -- layouts/index.html --
   196  -- layouts/_default/single.html --
   197  {{ partialCached "heavy.html" "foo" }}
   198  {{ partialCached "easy1.html" "bar" }}
   199  {{ partialCached "easy1.html" "baz" }}
   200  {{ partialCached "easy2.html" "baz" }}
   201  -- layouts/partials/easy1.html --
   202  ABCD
   203  -- layouts/partials/easy2.html --
   204  ABCDE
   205  -- layouts/partials/heavy.html --
   206  {{ $result := slice }}
   207  {{ range site.RegularPages }}
   208  {{ $result = $result | append (dict "title" .Title "link" .RelPermalink "readingTime" .ReadingTime) }}
   209  {{ end }}
   210  {{ range $result }}
   211  * {{ .title }} {{ .link }} {{ .readingTime }}
   212  {{ end }}
   213  
   214  
   215  `
   216  
   217  	for i := 1; i < 100; i++ {
   218  		files += fmt.Sprintf("\n-- content/p%d.md --\n---\ntitle: page\n---\n"+strings.Repeat("FOO ", i), i)
   219  	}
   220  
   221  	cfg := hugolib.IntegrationTestConfig{
   222  		T:           b,
   223  		TxtarString: files,
   224  	}
   225  	builders := make([]*hugolib.IntegrationTestBuilder, b.N)
   226  
   227  	for i, _ := range builders {
   228  		builders[i] = hugolib.NewIntegrationTestBuilder(cfg)
   229  	}
   230  
   231  	b.ResetTimer()
   232  
   233  	for i := 0; i < b.N; i++ {
   234  		builders[i].Build()
   235  	}
   236  }