github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/site_stats_test.go (about)

     1  // Copyright 2017 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  	"bytes"
    18  	"fmt"
    19  	"io"
    20  	"testing"
    21  
    22  	"github.com/gohugoio/hugo/helpers"
    23  
    24  	qt "github.com/frankban/quicktest"
    25  )
    26  
    27  func TestSiteStats(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	c := qt.New(t)
    31  
    32  	siteConfig := `
    33  baseURL = "http://example.com/blog"
    34  
    35  paginate = 1
    36  defaultContentLanguage = "nn"
    37  
    38  [languages]
    39  [languages.nn]
    40  languageName = "Nynorsk"
    41  weight = 1
    42  title = "Hugo på norsk"
    43  
    44  [languages.en]
    45  languageName = "English"
    46  weight = 2
    47  title = "Hugo in English"
    48  
    49  `
    50  
    51  	pageTemplate := `---
    52  title: "T%d"
    53  tags:
    54  %s
    55  categories:
    56  %s
    57  aliases: [/Ali%d]
    58  ---
    59  # Doc
    60  `
    61  
    62  	b := newTestSitesBuilder(t).WithConfigFile("toml", siteConfig)
    63  
    64  	b.WithTemplates(
    65  		"_default/single.html", "Single|{{ .Title }}|{{ .Content }}",
    66  		"_default/list.html", `List|{{ .Title }}|Pages: {{ .Paginator.TotalPages }}|{{ .Content }}`,
    67  		"_default/terms.html", "Terms List|{{ .Title }}|{{ .Content }}",
    68  	)
    69  
    70  	for i := 0; i < 2; i++ {
    71  		for j := 0; j < 2; j++ {
    72  			pageID := i + j + 1
    73  			b.WithContent(fmt.Sprintf("content/sect/p%d.md", pageID),
    74  				fmt.Sprintf(pageTemplate, pageID, fmt.Sprintf("- tag%d", j), fmt.Sprintf("- category%d", j), pageID))
    75  		}
    76  	}
    77  
    78  	for i := 0; i < 5; i++ {
    79  		b.WithContent(fmt.Sprintf("assets/image%d.png", i+1), "image")
    80  	}
    81  
    82  	b.Build(BuildCfg{})
    83  	h := b.H
    84  
    85  	stats := []*helpers.ProcessingStats{
    86  		h.Sites[0].PathSpec.ProcessingStats,
    87  		h.Sites[1].PathSpec.ProcessingStats,
    88  	}
    89  
    90  	stats[0].Table(io.Discard)
    91  	stats[1].Table(io.Discard)
    92  
    93  	var buff bytes.Buffer
    94  
    95  	helpers.ProcessingStatsTable(&buff, stats...)
    96  
    97  	c.Assert(buff.String(), qt.Contains, "Pages            | 19 |  6")
    98  }