github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/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 15 16 import ( 17 "path/filepath" 18 "runtime" 19 "strings" 20 "testing" 21 "time" 22 23 "github.com/spf13/afero" 24 25 "github.com/gohugoio/hugo/config" 26 27 qt "github.com/frankban/quicktest" 28 ) 29 30 func TestDecodeConfig(t *testing.T) { 31 t.Parallel() 32 33 c := qt.New(t) 34 35 configStr := ` 36 resourceDir = "myresources" 37 contentDir = "content" 38 dataDir = "data" 39 i18nDir = "i18n" 40 layoutDir = "layouts" 41 assetDir = "assets" 42 archetypeDir = "archetypes" 43 44 [caches] 45 [caches.getJSON] 46 maxAge = "10m" 47 dir = "/path/to/c1" 48 [caches.getCSV] 49 maxAge = "11h" 50 dir = "/path/to/c2" 51 [caches.images] 52 dir = "/path/to/c3" 53 54 ` 55 56 cfg, err := config.FromConfigString(configStr, "toml") 57 c.Assert(err, qt.IsNil) 58 fs := afero.NewMemMapFs() 59 decoded, err := DecodeConfig(fs, cfg) 60 c.Assert(err, qt.IsNil) 61 62 c.Assert(len(decoded), qt.Equals, 5) 63 64 c2 := decoded["getcsv"] 65 c.Assert(c2.MaxAge.String(), qt.Equals, "11h0m0s") 66 c.Assert(c2.Dir, 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.Dir, qt.Equals, filepath.FromSlash("/path/to/c3/filecache/images")) 71 } 72 73 func TestDecodeConfigIgnoreCache(t *testing.T) { 74 t.Parallel() 75 76 c := qt.New(t) 77 78 configStr := ` 79 resourceDir = "myresources" 80 contentDir = "content" 81 dataDir = "data" 82 i18nDir = "i18n" 83 layoutDir = "layouts" 84 assetDir = "assets" 85 archeTypedir = "archetypes" 86 87 ignoreCache = true 88 [caches] 89 [caches.getJSON] 90 maxAge = 1234 91 dir = "/path/to/c1" 92 [caches.getCSV] 93 maxAge = 3456 94 dir = "/path/to/c2" 95 [caches.images] 96 dir = "/path/to/c3" 97 98 ` 99 100 cfg, err := config.FromConfigString(configStr, "toml") 101 c.Assert(err, qt.IsNil) 102 fs := afero.NewMemMapFs() 103 decoded, err := DecodeConfig(fs, cfg) 104 c.Assert(err, qt.IsNil) 105 106 c.Assert(len(decoded), qt.Equals, 5) 107 108 for _, v := range decoded { 109 c.Assert(v.MaxAge, qt.Equals, time.Duration(0)) 110 } 111 } 112 113 func TestDecodeConfigDefault(t *testing.T) { 114 c := qt.New(t) 115 cfg := newTestConfig() 116 117 if runtime.GOOS == "windows" { 118 cfg.Set("resourceDir", "c:\\cache\\resources") 119 cfg.Set("cacheDir", "c:\\cache\\thecache") 120 121 } else { 122 cfg.Set("resourceDir", "/cache/resources") 123 cfg.Set("cacheDir", "/cache/thecache") 124 } 125 126 fs := afero.NewMemMapFs() 127 128 decoded, err := DecodeConfig(fs, cfg) 129 130 c.Assert(err, qt.IsNil) 131 132 c.Assert(len(decoded), qt.Equals, 5) 133 134 imgConfig := decoded[cacheKeyImages] 135 jsonConfig := decoded[cacheKeyGetJSON] 136 137 if runtime.GOOS == "windows" { 138 c.Assert(imgConfig.Dir, qt.Equals, filepath.FromSlash("_gen/images")) 139 } else { 140 c.Assert(imgConfig.Dir, qt.Equals, "_gen/images") 141 c.Assert(jsonConfig.Dir, 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 } 147 148 func TestDecodeConfigInvalidDir(t *testing.T) { 149 t.Parallel() 150 151 c := qt.New(t) 152 153 configStr := ` 154 resourceDir = "myresources" 155 contentDir = "content" 156 dataDir = "data" 157 i18nDir = "i18n" 158 layoutDir = "layouts" 159 assetDir = "assets" 160 archeTypedir = "archetypes" 161 162 [caches] 163 [caches.getJSON] 164 maxAge = "10m" 165 dir = "/" 166 167 ` 168 if runtime.GOOS == "windows" { 169 configStr = strings.Replace(configStr, "/", "c:\\\\", 1) 170 } 171 172 cfg, err := config.FromConfigString(configStr, "toml") 173 c.Assert(err, qt.IsNil) 174 fs := afero.NewMemMapFs() 175 176 _, err = DecodeConfig(fs, cfg) 177 c.Assert(err, qt.Not(qt.IsNil)) 178 } 179 180 func newTestConfig() config.Provider { 181 cfg := config.New() 182 cfg.Set("workingDir", filepath.FromSlash("/my/cool/hugoproject")) 183 cfg.Set("contentDir", "content") 184 cfg.Set("dataDir", "data") 185 cfg.Set("resourceDir", "resources") 186 cfg.Set("i18nDir", "i18n") 187 cfg.Set("layoutDir", "layouts") 188 cfg.Set("archetypeDir", "archetypes") 189 cfg.Set("assetDir", "assets") 190 191 return cfg 192 }