| 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 |
"encoding/json" |
| 18 |
"path/filepath" |
| 19 |
"runtime" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/spf13/afero" |
| 24 |
|
| 25 |
"github.com/gohugoio/hugo/cache/filecache" |
| 26 |
"github.com/gohugoio/hugo/config" |
| 27 |
"github.com/gohugoio/hugo/config/testconfig" |
| 28 |
|
| 29 |
qt "github.com/frankban/quicktest" |
| 30 |
) |
| 31 |
|
| 32 |
func TestDecodeConfig(t *testing.T) { |
| 33 |
t.Parallel() |
| 34 |
|
| 35 |
c := qt.New(t) |
| 36 |
|
| 37 |
configStr := ` |
| 38 |
resourceDir = "myresources" |
| 39 |
contentDir = "content" |
| 40 |
dataDir = "data" |
| 41 |
i18nDir = "i18n" |
| 42 |
layoutDir = "layouts" |
| 43 |
assetDir = "assets" |
| 44 |
archetypeDir = "archetypes" |
| 45 |
|
| 46 |
[caches] |
| 47 |
[caches.misc] |
| 48 |
maxAge = "10m" |
| 49 |
dir = "/path/to/c1" |
| 50 |
[caches.images] |
| 51 |
dir = "/path/to/c3" |
| 52 |
[caches.getResource] |
| 53 |
dir = "/path/to/c4" |
| 54 |
` |
| 55 |
|
| 56 |
cfg, err := config.FromConfigString(configStr, "toml") |
| 57 |
c.Assert(err, qt.IsNil) |
| 58 |
fs := afero.NewMemMapFs() |
| 59 |
decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches |
| 60 |
c.Assert(len(decoded), qt.Equals, 7) |
| 61 |
|
| 62 |
c2 := decoded["misc"] |
| 63 |
c.Assert(c2.MaxAge.String(), qt.Equals, "10m0s") |
| 64 |
c.Assert(c2.DirCompiled, qt.Equals, filepath.FromSlash("/path/to/c1/filecache/misc")) |
| 65 |
|
| 66 |
c3 := decoded["images"] |
| 67 |
c.Assert(c3.MaxAge, qt.Equals, time.Duration(-1)) |
| 68 |
c.Assert(c3.DirCompiled, qt.Equals, filepath.FromSlash("/path/to/c3/filecache/images")) |
| 69 |
|
| 70 |
c4 := decoded["getresource"] |
| 71 |
c.Assert(c4.MaxAge, qt.Equals, time.Duration(-1)) |
| 72 |
c.Assert(c4.DirCompiled, qt.Equals, filepath.FromSlash("/path/to/c4/filecache/getresource")) |
| 73 |
} |
| 74 |
|
| 75 |
func TestDecodeConfigIgnoreCache(t *testing.T) { |
| 76 |
t.Parallel() |
| 77 |
|
| 78 |
c := qt.New(t) |
| 79 |
|
| 80 |
configStr := ` |
| 81 |
resourceDir = "myresources" |
| 82 |
contentDir = "content" |
| 83 |
dataDir = "data" |
| 84 |
i18nDir = "i18n" |
| 85 |
layoutDir = "layouts" |
| 86 |
assetDir = "assets" |
| 87 |
archeTypedir = "archetypes" |
| 88 |
|
| 89 |
ignoreCache = true |
| 90 |
[caches] |
| 91 |
[caches.misc] |
| 92 |
maxAge = 1234 |
| 93 |
dir = "/path/to/c1" |
| 94 |
[caches.images] |
| 95 |
dir = "/path/to/c3" |
| 96 |
[caches.getResource] |
| 97 |
dir = "/path/to/c4" |
| 98 |
` |
| 99 |
|
| 100 |
cfg, err := config.FromConfigString(configStr, "toml") |
| 101 |
c.Assert(err, qt.IsNil) |
| 102 |
fs := afero.NewMemMapFs() |
| 103 |
decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches |
| 104 |
c.Assert(len(decoded), qt.Equals, 7) |
| 105 |
|
| 106 |
for _, v := range decoded { |
| 107 |
c.Assert(v.MaxAge, qt.Equals, time.Duration(0)) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestDecodeConfigDefault(t *testing.T) { |
| 112 |
c := qt.New(t) |
| 113 |
cfg := config.New() |
| 114 |
|
| 115 |
if runtime.GOOS == "windows" { |
| 116 |
cfg.Set("resourceDir", "c:\\cache\\resources") |
| 117 |
cfg.Set("cacheDir", "c:\\cache\\thecache") |
| 118 |
|
| 119 |
} else { |
| 120 |
cfg.Set("resourceDir", "/cache/resources") |
| 121 |
cfg.Set("cacheDir", "/cache/thecache") |
| 122 |
} |
| 123 |
cfg.Set("workingDir", filepath.FromSlash("/my/cool/hugoproject")) |
| 124 |
|
| 125 |
fs := afero.NewMemMapFs() |
| 126 |
decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches |
| 127 |
c.Assert(len(decoded), qt.Equals, 7) |
| 128 |
|
| 129 |
imgConfig := decoded[filecache.CacheKeyImages] |
| 130 |
miscConfig := decoded[filecache.CacheKeyMisc] |
| 131 |
|
| 132 |
if runtime.GOOS == "windows" { |
| 133 |
c.Assert(imgConfig.DirCompiled, qt.Equals, filepath.FromSlash("_gen/images")) |
| 134 |
} else { |
| 135 |
c.Assert(imgConfig.DirCompiled, qt.Equals, "_gen/images") |
| 136 |
c.Assert(miscConfig.DirCompiled, qt.Equals, "/cache/thecache/hugoproject/filecache/misc") |
| 137 |
} |
| 138 |
|
| 139 |
c.Assert(imgConfig.IsResourceDir, qt.Equals, true) |
| 140 |
c.Assert(miscConfig.IsResourceDir, qt.Equals, false) |
| 141 |
} |
| 142 |
|
| 143 |
func TestFileCacheConfigMarshalJSON(t *testing.T) { |
| 144 |
c := qt.New(t) |
| 145 |
|
| 146 |
cfg := config.New() |
| 147 |
cfg.Set("cacheDir", "/cache") |
| 148 |
cfg.Set("workingDir", "/my/project") |
| 149 |
|
| 150 |
fs := afero.NewMemMapFs() |
| 151 |
decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches |
| 152 |
|
| 153 |
moduleQueriesConfig := decoded[filecache.CacheKeyModuleQueries] |
| 154 |
c.Assert(moduleQueriesConfig.MaxAge, qt.Equals, 24*time.Hour) |
| 155 |
|
| 156 |
// Also verify the new moduleGitInfo cache. |
| 157 |
moduleGitInfoConfig := decoded[filecache.CacheKeyModuleGitInfo] |
| 158 |
c.Assert(moduleGitInfoConfig.MaxAge, qt.Equals, 24*time.Hour) |
| 159 |
|
| 160 |
b, err := json.Marshal(moduleQueriesConfig) |
| 161 |
c.Assert(err, qt.IsNil) |
| 162 |
|
| 163 |
c.Assert(string(b), qt.Contains, `"maxAge":"24h"`) |
| 164 |
c.Assert(string(b), qt.Not(qt.Contains), "86400000000000") |
| 165 |
c.Assert(string(b), qt.Not(qt.Contains), "8.64e") |
| 166 |
|
| 167 |
moduleQueriesConfig.MaxAge = -1 |
| 168 |
b, err = json.Marshal(moduleQueriesConfig) |
| 169 |
c.Assert(err, qt.IsNil) |
| 170 |
c.Assert(string(b), qt.Contains, `"maxAge":-1`) |
| 171 |
} |