github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/cache/filecache/filecache_config_test.go (about)

     1  // Copyright 2018 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 filecache_test
    15  
    16  import (
    17  	"path/filepath"
    18  	"runtime"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/spf13/afero"
    23  
    24  	"github.com/gohugoio/hugo/cache/filecache"
    25  	"github.com/gohugoio/hugo/config"
    26  	"github.com/gohugoio/hugo/config/testconfig"
    27  
    28  	qt "github.com/frankban/quicktest"
    29  )
    30  
    31  func TestDecodeConfig(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	c := qt.New(t)
    35  
    36  	configStr := `
    37  resourceDir = "myresources"
    38  contentDir = "content"
    39  dataDir = "data"
    40  i18nDir = "i18n"
    41  layoutDir = "layouts"
    42  assetDir = "assets"
    43  archetypeDir = "archetypes"
    44  
    45  [caches]
    46  [caches.getJSON]
    47  maxAge = "10m"
    48  dir = "/path/to/c1"
    49  [caches.getCSV]
    50  maxAge = "11h"
    51  dir = "/path/to/c2"
    52  [caches.images]
    53  dir = "/path/to/c3"
    54  [caches.getResource]
    55  dir = "/path/to/c4"
    56  `
    57  
    58  	cfg, err := config.FromConfigString(configStr, "toml")
    59  	c.Assert(err, qt.IsNil)
    60  	fs := afero.NewMemMapFs()
    61  	decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches
    62  	c.Assert(len(decoded), qt.Equals, 6)
    63  
    64  	c2 := decoded["getcsv"]
    65  	c.Assert(c2.MaxAge.String(), qt.Equals, "11h0m0s")
    66  	c.Assert(c2.DirCompiled, qt.Equals, filepath.FromSlash("/path/to/c2/filecache/getcsv"))
    67  
    68  	c3 := decoded["images"]
    69  	c.Assert(c3.MaxAge, qt.Equals, time.Duration(-1))
    70  	c.Assert(c3.DirCompiled, qt.Equals, filepath.FromSlash("/path/to/c3/filecache/images"))
    71  
    72  	c4 := decoded["getresource"]
    73  	c.Assert(c4.MaxAge, qt.Equals, time.Duration(-1))
    74  	c.Assert(c4.DirCompiled, qt.Equals, filepath.FromSlash("/path/to/c4/filecache/getresource"))
    75  }
    76  
    77  func TestDecodeConfigIgnoreCache(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	c := qt.New(t)
    81  
    82  	configStr := `
    83  resourceDir = "myresources"
    84  contentDir = "content"
    85  dataDir = "data"
    86  i18nDir = "i18n"
    87  layoutDir = "layouts"
    88  assetDir = "assets"
    89  archeTypedir = "archetypes"
    90  
    91  ignoreCache = true
    92  [caches]
    93  [caches.getJSON]
    94  maxAge = 1234
    95  dir = "/path/to/c1"
    96  [caches.getCSV]
    97  maxAge = 3456
    98  dir = "/path/to/c2"
    99  [caches.images]
   100  dir = "/path/to/c3"
   101  [caches.getResource]
   102  dir = "/path/to/c4"
   103  `
   104  
   105  	cfg, err := config.FromConfigString(configStr, "toml")
   106  	c.Assert(err, qt.IsNil)
   107  	fs := afero.NewMemMapFs()
   108  	decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches
   109  	c.Assert(len(decoded), qt.Equals, 6)
   110  
   111  	for _, v := range decoded {
   112  		c.Assert(v.MaxAge, qt.Equals, time.Duration(0))
   113  	}
   114  }
   115  
   116  func TestDecodeConfigDefault(t *testing.T) {
   117  	c := qt.New(t)
   118  	cfg := config.New()
   119  
   120  	if runtime.GOOS == "windows" {
   121  		cfg.Set("resourceDir", "c:\\cache\\resources")
   122  		cfg.Set("cacheDir", "c:\\cache\\thecache")
   123  
   124  	} else {
   125  		cfg.Set("resourceDir", "/cache/resources")
   126  		cfg.Set("cacheDir", "/cache/thecache")
   127  	}
   128  	cfg.Set("workingDir", filepath.FromSlash("/my/cool/hugoproject"))
   129  
   130  	fs := afero.NewMemMapFs()
   131  	decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches
   132  	c.Assert(len(decoded), qt.Equals, 6)
   133  
   134  	imgConfig := decoded[filecache.CacheKeyImages]
   135  	jsonConfig := decoded[filecache.CacheKeyGetJSON]
   136  
   137  	if runtime.GOOS == "windows" {
   138  		c.Assert(imgConfig.DirCompiled, qt.Equals, filepath.FromSlash("_gen/images"))
   139  	} else {
   140  		c.Assert(imgConfig.DirCompiled, qt.Equals, "_gen/images")
   141  		c.Assert(jsonConfig.DirCompiled, qt.Equals, "/cache/thecache/hugoproject/filecache/getjson")
   142  	}
   143  
   144  	c.Assert(imgConfig.IsResourceDir, qt.Equals, true)
   145  	c.Assert(jsonConfig.IsResourceDir, qt.Equals, false)
   146  }