github.com/dominikszabo/hugo-ds-clean@v0.47.1/hugolib/disableKinds_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  package hugolib
    14  
    15  import (
    16  	"strings"
    17  	"testing"
    18  
    19  	"fmt"
    20  
    21  	"github.com/gohugoio/hugo/deps"
    22  	"github.com/spf13/afero"
    23  
    24  	"github.com/gohugoio/hugo/helpers"
    25  	"github.com/gohugoio/hugo/hugofs"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestDisableKindsNoneDisabled(t *testing.T) {
    30  	t.Parallel()
    31  	doTestDisableKinds(t)
    32  }
    33  
    34  func TestDisableKindsSomeDisabled(t *testing.T) {
    35  	t.Parallel()
    36  	doTestDisableKinds(t, KindSection, kind404)
    37  }
    38  
    39  func TestDisableKindsOneDisabled(t *testing.T) {
    40  	t.Parallel()
    41  	for _, kind := range allKinds {
    42  		if kind == KindPage {
    43  			// Turning off regular page generation have some side-effects
    44  			// not handled by the assertions below (no sections), so
    45  			// skip that for now.
    46  			continue
    47  		}
    48  		doTestDisableKinds(t, kind)
    49  	}
    50  }
    51  
    52  func TestDisableKindsAllDisabled(t *testing.T) {
    53  	t.Parallel()
    54  	doTestDisableKinds(t, allKinds...)
    55  }
    56  
    57  func doTestDisableKinds(t *testing.T, disabled ...string) {
    58  	siteConfigTemplate := `
    59  baseURL = "http://example.com/blog"
    60  enableRobotsTXT = true
    61  disableKinds = %s
    62  
    63  paginate = 1
    64  defaultContentLanguage = "en"
    65  
    66  [Taxonomies]
    67  tag = "tags"
    68  category = "categories"
    69  `
    70  
    71  	pageTemplate := `---
    72  title: "%s"
    73  tags:
    74  %s
    75  categories:
    76  - Hugo
    77  ---
    78  # Doc
    79  `
    80  
    81  	mf := afero.NewMemMapFs()
    82  
    83  	disabledStr := "[]"
    84  
    85  	if len(disabled) > 0 {
    86  		disabledStr = strings.Replace(fmt.Sprintf("%#v", disabled), "[]string{", "[", -1)
    87  		disabledStr = strings.Replace(disabledStr, "}", "]", -1)
    88  	}
    89  
    90  	siteConfig := fmt.Sprintf(siteConfigTemplate, disabledStr)
    91  	writeToFs(t, mf, "config.toml", siteConfig)
    92  
    93  	cfg, err := LoadConfigDefault(mf)
    94  	require.NoError(t, err)
    95  
    96  	fs := hugofs.NewFrom(mf, cfg)
    97  	th := testHelper{cfg, fs, t}
    98  
    99  	writeSource(t, fs, "layouts/index.html", "Home|{{ .Title }}|{{ .Content }}")
   100  	writeSource(t, fs, "layouts/_default/single.html", "Single|{{ .Title }}|{{ .Content }}")
   101  	writeSource(t, fs, "layouts/_default/list.html", "List|{{ .Title }}|{{ .Content }}")
   102  	writeSource(t, fs, "layouts/_default/terms.html", "Terms List|{{ .Title }}|{{ .Content }}")
   103  	writeSource(t, fs, "layouts/404.html", "Page Not Found")
   104  
   105  	writeSource(t, fs, "content/sect/p1.md", fmt.Sprintf(pageTemplate, "P1", "- tag1"))
   106  
   107  	writeNewContentFile(t, fs.Source, "Category Terms", "2017-01-01", "content/categories/_index.md", 10)
   108  	writeNewContentFile(t, fs.Source, "Tag1 List", "2017-01-01", "content/tags/tag1/_index.md", 10)
   109  
   110  	h, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
   111  
   112  	require.NoError(t, err)
   113  	require.Len(t, h.Sites, 1)
   114  
   115  	err = h.Build(BuildCfg{})
   116  
   117  	require.NoError(t, err)
   118  
   119  	assertDisabledKinds(th, h.Sites[0], disabled...)
   120  
   121  }
   122  
   123  func assertDisabledKinds(th testHelper, s *Site, disabled ...string) {
   124  	assertDisabledKind(th,
   125  		func(isDisabled bool) bool {
   126  			if isDisabled {
   127  				return len(s.RegularPages) == 0
   128  			}
   129  			return len(s.RegularPages) > 0
   130  		}, disabled, KindPage, "public/sect/p1/index.html", "Single|P1")
   131  	assertDisabledKind(th,
   132  		func(isDisabled bool) bool {
   133  			p := s.getPage(KindHome)
   134  			if isDisabled {
   135  				return p == nil
   136  			}
   137  			return p != nil
   138  		}, disabled, KindHome, "public/index.html", "Home")
   139  	assertDisabledKind(th,
   140  		func(isDisabled bool) bool {
   141  			p := s.getPage(KindSection, "sect")
   142  			if isDisabled {
   143  				return p == nil
   144  			}
   145  			return p != nil
   146  		}, disabled, KindSection, "public/sect/index.html", "Sects")
   147  	assertDisabledKind(th,
   148  		func(isDisabled bool) bool {
   149  			p := s.getPage(KindTaxonomy, "tags", "tag1")
   150  
   151  			if isDisabled {
   152  				return p == nil
   153  			}
   154  			return p != nil
   155  
   156  		}, disabled, KindTaxonomy, "public/tags/tag1/index.html", "Tag1")
   157  	assertDisabledKind(th,
   158  		func(isDisabled bool) bool {
   159  			p := s.getPage(KindTaxonomyTerm, "tags")
   160  			if isDisabled {
   161  				return p == nil
   162  			}
   163  			return p != nil
   164  
   165  		}, disabled, KindTaxonomyTerm, "public/tags/index.html", "Tags")
   166  	assertDisabledKind(th,
   167  		func(isDisabled bool) bool {
   168  			p := s.getPage(KindTaxonomyTerm, "categories")
   169  
   170  			if isDisabled {
   171  				return p == nil
   172  			}
   173  			return p != nil
   174  
   175  		}, disabled, KindTaxonomyTerm, "public/categories/index.html", "Category Terms")
   176  	assertDisabledKind(th,
   177  		func(isDisabled bool) bool {
   178  			p := s.getPage(KindTaxonomy, "categories", "hugo")
   179  			if isDisabled {
   180  				return p == nil
   181  			}
   182  			return p != nil
   183  
   184  		}, disabled, KindTaxonomy, "public/categories/hugo/index.html", "Hugo")
   185  	// The below have no page in any collection.
   186  	assertDisabledKind(th, func(isDisabled bool) bool { return true }, disabled, kindRSS, "public/index.xml", "<link>")
   187  	assertDisabledKind(th, func(isDisabled bool) bool { return true }, disabled, kindSitemap, "public/sitemap.xml", "sitemap")
   188  	assertDisabledKind(th, func(isDisabled bool) bool { return true }, disabled, kindRobotsTXT, "public/robots.txt", "User-agent")
   189  	assertDisabledKind(th, func(isDisabled bool) bool { return true }, disabled, kind404, "public/404.html", "Page Not Found")
   190  }
   191  
   192  func assertDisabledKind(th testHelper, kindAssert func(bool) bool, disabled []string, kind, path, matcher string) {
   193  	isDisabled := stringSliceContains(kind, disabled...)
   194  	require.True(th.T, kindAssert(isDisabled), fmt.Sprintf("%s: %t", kind, isDisabled))
   195  
   196  	if kind == kindRSS && !isDisabled {
   197  		// If the home page is also disabled, there is not RSS to look for.
   198  		if stringSliceContains(KindHome, disabled...) {
   199  			isDisabled = true
   200  		}
   201  	}
   202  
   203  	if isDisabled {
   204  		// Path should not exist
   205  		fileExists, err := helpers.Exists(path, th.Fs.Destination)
   206  		require.False(th.T, fileExists)
   207  		require.NoError(th.T, err)
   208  
   209  	} else {
   210  		th.assertFileContent(path, matcher)
   211  	}
   212  }
   213  
   214  func stringSliceContains(k string, values ...string) bool {
   215  	for _, v := range values {
   216  		if k == v {
   217  			return true
   218  		}
   219  	}
   220  	return false
   221  }