2018-11-14 09:44:04 +00:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package filecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2018-11-14 16:18:32 +00:00
|
|
|
"strings"
|
2018-11-14 09:44:04 +00:00
|
|
|
"testing"
|
2018-11-14 09:51:41 +00:00
|
|
|
"time"
|
2018-11-14 09:44:04 +00:00
|
|
|
|
2018-11-23 08:16:42 +00:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
|
2018-11-14 09:44:04 +00:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeConfig(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := require.New(t)
|
|
|
|
|
|
|
|
configStr := `
|
2018-11-14 10:53:45 +00:00
|
|
|
resourceDir = "myresources"
|
2018-11-23 08:16:42 +00:00
|
|
|
contentDir = "content"
|
|
|
|
dataDir = "data"
|
|
|
|
i18nDir = "i18n"
|
|
|
|
layoutDir = "layouts"
|
|
|
|
assetDir = "assets"
|
|
|
|
archetypeDir = "archetypes"
|
|
|
|
|
2018-11-14 09:44:04 +00:00
|
|
|
[caches]
|
|
|
|
[caches.getJSON]
|
2018-11-14 09:51:41 +00:00
|
|
|
maxAge = "10m"
|
2018-11-14 09:44:04 +00:00
|
|
|
dir = "/path/to/c1"
|
|
|
|
[caches.getCSV]
|
2018-11-14 09:51:41 +00:00
|
|
|
maxAge = "11h"
|
2018-11-14 09:44:04 +00:00
|
|
|
dir = "/path/to/c2"
|
|
|
|
[caches.images]
|
|
|
|
dir = "/path/to/c3"
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
cfg, err := config.FromConfigString(configStr, "toml")
|
|
|
|
assert.NoError(err)
|
|
|
|
fs := hugofs.NewMem(cfg)
|
2018-11-23 08:16:42 +00:00
|
|
|
p, err := helpers.NewPathSpec(fs, cfg)
|
2018-11-14 09:44:04 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
decoded, err := decodeConfig(p)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
assert.Equal(4, len(decoded))
|
|
|
|
|
|
|
|
c2 := decoded["getcsv"]
|
2018-11-14 09:51:41 +00:00
|
|
|
assert.Equal("11h0m0s", c2.MaxAge.String())
|
2018-11-14 09:44:04 +00:00
|
|
|
assert.Equal(filepath.FromSlash("/path/to/c2"), c2.Dir)
|
|
|
|
|
|
|
|
c3 := decoded["images"]
|
2018-11-14 09:51:41 +00:00
|
|
|
assert.Equal(time.Duration(-1), c3.MaxAge)
|
2018-11-14 09:44:04 +00:00
|
|
|
assert.Equal(filepath.FromSlash("/path/to/c3"), c3.Dir)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeConfigIgnoreCache(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := require.New(t)
|
|
|
|
|
|
|
|
configStr := `
|
2018-11-14 10:53:45 +00:00
|
|
|
resourceDir = "myresources"
|
2018-11-23 08:16:42 +00:00
|
|
|
contentDir = "content"
|
|
|
|
dataDir = "data"
|
|
|
|
i18nDir = "i18n"
|
|
|
|
layoutDir = "layouts"
|
|
|
|
assetDir = "assets"
|
|
|
|
archeTypedir = "archetypes"
|
|
|
|
|
2018-11-14 09:44:04 +00:00
|
|
|
ignoreCache = true
|
|
|
|
[caches]
|
|
|
|
[caches.getJSON]
|
|
|
|
maxAge = 1234
|
|
|
|
dir = "/path/to/c1"
|
|
|
|
[caches.getCSV]
|
|
|
|
maxAge = 3456
|
|
|
|
dir = "/path/to/c2"
|
|
|
|
[caches.images]
|
|
|
|
dir = "/path/to/c3"
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
cfg, err := config.FromConfigString(configStr, "toml")
|
|
|
|
assert.NoError(err)
|
|
|
|
fs := hugofs.NewMem(cfg)
|
2018-11-23 08:16:42 +00:00
|
|
|
p, err := helpers.NewPathSpec(fs, cfg)
|
2018-11-14 09:44:04 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
decoded, err := decodeConfig(p)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
assert.Equal(4, len(decoded))
|
|
|
|
|
|
|
|
for _, v := range decoded {
|
2018-11-14 09:51:41 +00:00
|
|
|
assert.Equal(time.Duration(0), v.MaxAge)
|
2018-11-14 09:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeConfigDefault(t *testing.T) {
|
|
|
|
assert := require.New(t)
|
2018-11-23 08:16:42 +00:00
|
|
|
cfg := newTestConfig()
|
2018-11-14 16:18:32 +00:00
|
|
|
|
2018-11-14 09:44:04 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
cfg.Set("resourceDir", "c:\\cache\\resources")
|
|
|
|
cfg.Set("cacheDir", "c:\\cache\\thecache")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cfg.Set("resourceDir", "/cache/resources")
|
|
|
|
cfg.Set("cacheDir", "/cache/thecache")
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := hugofs.NewMem(cfg)
|
2018-11-23 08:16:42 +00:00
|
|
|
p, err := helpers.NewPathSpec(fs, cfg)
|
2018-11-14 09:44:04 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
decoded, err := decodeConfig(p)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
assert.Equal(4, len(decoded))
|
|
|
|
|
2018-11-23 08:16:42 +00:00
|
|
|
imgConfig := decoded[cacheKeyImages]
|
|
|
|
jsonConfig := decoded[cacheKeyGetJSON]
|
|
|
|
|
2018-11-14 09:44:04 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2018-11-23 08:16:42 +00:00
|
|
|
assert.Equal("_gen", imgConfig.Dir)
|
2018-11-14 09:44:04 +00:00
|
|
|
} else {
|
2018-11-23 08:16:42 +00:00
|
|
|
assert.Equal("_gen", imgConfig.Dir)
|
|
|
|
assert.Equal("/cache/thecache/hugoproject", jsonConfig.Dir)
|
2018-11-14 16:18:32 +00:00
|
|
|
}
|
2018-11-23 08:16:42 +00:00
|
|
|
|
|
|
|
assert.True(imgConfig.isResourceDir)
|
|
|
|
assert.False(jsonConfig.isResourceDir)
|
2018-11-14 16:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeConfigInvalidDir(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := require.New(t)
|
|
|
|
|
|
|
|
configStr := `
|
|
|
|
resourceDir = "myresources"
|
2018-11-23 08:16:42 +00:00
|
|
|
contentDir = "content"
|
|
|
|
dataDir = "data"
|
|
|
|
i18nDir = "i18n"
|
|
|
|
layoutDir = "layouts"
|
|
|
|
assetDir = "assets"
|
|
|
|
archeTypedir = "archetypes"
|
|
|
|
|
2018-11-14 16:18:32 +00:00
|
|
|
[caches]
|
|
|
|
[caches.getJSON]
|
|
|
|
maxAge = "10m"
|
|
|
|
dir = "/"
|
|
|
|
|
|
|
|
`
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
configStr = strings.Replace(configStr, "/", "c:\\\\", 1)
|
2018-11-14 09:44:04 +00:00
|
|
|
}
|
2018-11-14 16:18:32 +00:00
|
|
|
|
|
|
|
cfg, err := config.FromConfigString(configStr, "toml")
|
|
|
|
assert.NoError(err)
|
|
|
|
fs := hugofs.NewMem(cfg)
|
2018-11-23 08:16:42 +00:00
|
|
|
p, err := helpers.NewPathSpec(fs, cfg)
|
2018-11-14 16:18:32 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
_, err = decodeConfig(p)
|
|
|
|
assert.Error(err)
|
|
|
|
|
2018-11-14 09:44:04 +00:00
|
|
|
}
|
2018-11-23 08:16:42 +00:00
|
|
|
|
|
|
|
func newTestConfig() *viper.Viper {
|
|
|
|
cfg := viper.New()
|
|
|
|
cfg.Set("workingDir", filepath.FromSlash("/my/cool/hugoproject"))
|
|
|
|
cfg.Set("contentDir", "content")
|
|
|
|
cfg.Set("dataDir", "data")
|
|
|
|
cfg.Set("resourceDir", "resources")
|
|
|
|
cfg.Set("i18nDir", "i18n")
|
|
|
|
cfg.Set("layoutDir", "layouts")
|
|
|
|
cfg.Set("archetypeDir", "archetypes")
|
|
|
|
cfg.Set("assetDir", "assets")
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|