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