filecache_config_test.go (4547B)
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 [caches.getResource] 54 dir = "/path/to/c4" 55 ` 56 57 cfg, err := config.FromConfigString(configStr, "toml") 58 c.Assert(err, qt.IsNil) 59 fs := afero.NewMemMapFs() 60 decoded, err := DecodeConfig(fs, cfg) 61 c.Assert(err, qt.IsNil) 62 63 c.Assert(len(decoded), qt.Equals, 6) 64 65 c2 := decoded["getcsv"] 66 c.Assert(c2.MaxAge.String(), qt.Equals, "11h0m0s") 67 c.Assert(c2.Dir, qt.Equals, filepath.FromSlash("/path/to/c2/filecache/getcsv")) 68 69 c3 := decoded["images"] 70 c.Assert(c3.MaxAge, qt.Equals, time.Duration(-1)) 71 c.Assert(c3.Dir, qt.Equals, filepath.FromSlash("/path/to/c3/filecache/images")) 72 73 c4 := decoded["getresource"] 74 c.Assert(c4.MaxAge, qt.Equals, time.Duration(-1)) 75 c.Assert(c4.Dir, qt.Equals, filepath.FromSlash("/path/to/c4/filecache/getresource")) 76 } 77 78 func TestDecodeConfigIgnoreCache(t *testing.T) { 79 t.Parallel() 80 81 c := qt.New(t) 82 83 configStr := ` 84 resourceDir = "myresources" 85 contentDir = "content" 86 dataDir = "data" 87 i18nDir = "i18n" 88 layoutDir = "layouts" 89 assetDir = "assets" 90 archeTypedir = "archetypes" 91 92 ignoreCache = true 93 [caches] 94 [caches.getJSON] 95 maxAge = 1234 96 dir = "/path/to/c1" 97 [caches.getCSV] 98 maxAge = 3456 99 dir = "/path/to/c2" 100 [caches.images] 101 dir = "/path/to/c3" 102 [caches.getResource] 103 dir = "/path/to/c4" 104 ` 105 106 cfg, err := config.FromConfigString(configStr, "toml") 107 c.Assert(err, qt.IsNil) 108 fs := afero.NewMemMapFs() 109 decoded, err := DecodeConfig(fs, cfg) 110 c.Assert(err, qt.IsNil) 111 112 c.Assert(len(decoded), qt.Equals, 6) 113 114 for _, v := range decoded { 115 c.Assert(v.MaxAge, qt.Equals, time.Duration(0)) 116 } 117 } 118 119 func TestDecodeConfigDefault(t *testing.T) { 120 c := qt.New(t) 121 cfg := newTestConfig() 122 123 if runtime.GOOS == "windows" { 124 cfg.Set("resourceDir", "c:\\cache\\resources") 125 cfg.Set("cacheDir", "c:\\cache\\thecache") 126 127 } else { 128 cfg.Set("resourceDir", "/cache/resources") 129 cfg.Set("cacheDir", "/cache/thecache") 130 } 131 132 fs := afero.NewMemMapFs() 133 134 decoded, err := DecodeConfig(fs, cfg) 135 136 c.Assert(err, qt.IsNil) 137 138 c.Assert(len(decoded), qt.Equals, 6) 139 140 imgConfig := decoded[cacheKeyImages] 141 jsonConfig := decoded[cacheKeyGetJSON] 142 143 if runtime.GOOS == "windows" { 144 c.Assert(imgConfig.Dir, qt.Equals, filepath.FromSlash("_gen/images")) 145 } else { 146 c.Assert(imgConfig.Dir, qt.Equals, "_gen/images") 147 c.Assert(jsonConfig.Dir, qt.Equals, "/cache/thecache/hugoproject/filecache/getjson") 148 } 149 150 c.Assert(imgConfig.isResourceDir, qt.Equals, true) 151 c.Assert(jsonConfig.isResourceDir, qt.Equals, false) 152 } 153 154 func TestDecodeConfigInvalidDir(t *testing.T) { 155 t.Parallel() 156 157 c := qt.New(t) 158 159 configStr := ` 160 resourceDir = "myresources" 161 contentDir = "content" 162 dataDir = "data" 163 i18nDir = "i18n" 164 layoutDir = "layouts" 165 assetDir = "assets" 166 archeTypedir = "archetypes" 167 168 [caches] 169 [caches.getJSON] 170 maxAge = "10m" 171 dir = "/" 172 173 ` 174 if runtime.GOOS == "windows" { 175 configStr = strings.Replace(configStr, "/", "c:\\\\", 1) 176 } 177 178 cfg, err := config.FromConfigString(configStr, "toml") 179 c.Assert(err, qt.IsNil) 180 fs := afero.NewMemMapFs() 181 182 _, err = DecodeConfig(fs, cfg) 183 c.Assert(err, qt.Not(qt.IsNil)) 184 } 185 186 func newTestConfig() config.Provider { 187 cfg := config.NewWithTestDefaults() 188 cfg.Set("workingDir", filepath.FromSlash("/my/cool/hugoproject")) 189 cfg.Set("contentDir", "content") 190 cfg.Set("dataDir", "data") 191 cfg.Set("resourceDir", "resources") 192 cfg.Set("i18nDir", "i18n") 193 cfg.Set("layoutDir", "layouts") 194 cfg.Set("archetypeDir", "archetypes") 195 cfg.Set("assetDir", "assets") 196 197 return cfg 198 }