2018-11-14 04:44:04 -05: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 11:18:32 -05:00
|
|
|
"strings"
|
2018-11-14 04:44:04 -05:00
|
|
|
"testing"
|
2018-11-14 04:51:41 -05:00
|
|
|
"time"
|
2018-11-14 04:44:04 -05:00
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
"github.com/spf13/afero"
|
2018-11-23 03:16:42 -05:00
|
|
|
|
2018-11-14 04:44:04 -05:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-11-14 04:44:04 -05:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeConfig(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
|
|
|
configStr := `
|
2018-11-14 05:53:45 -05:00
|
|
|
resourceDir = "myresources"
|
2018-11-23 03:16:42 -05:00
|
|
|
contentDir = "content"
|
|
|
|
dataDir = "data"
|
|
|
|
i18nDir = "i18n"
|
|
|
|
layoutDir = "layouts"
|
|
|
|
assetDir = "assets"
|
|
|
|
archetypeDir = "archetypes"
|
|
|
|
|
2018-11-14 04:44:04 -05:00
|
|
|
[caches]
|
|
|
|
[caches.getJSON]
|
2018-11-14 04:51:41 -05:00
|
|
|
maxAge = "10m"
|
2018-11-14 04:44:04 -05:00
|
|
|
dir = "/path/to/c1"
|
|
|
|
[caches.getCSV]
|
2018-11-14 04:51:41 -05:00
|
|
|
maxAge = "11h"
|
2018-11-14 04:44:04 -05:00
|
|
|
dir = "/path/to/c2"
|
|
|
|
[caches.images]
|
|
|
|
dir = "/path/to/c3"
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
cfg, err := config.FromConfigString(configStr, "toml")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
decoded, err := DecodeConfig(fs, cfg)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(decoded), qt.Equals, 5)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
|
|
|
c2 := decoded["getcsv"]
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(c2.MaxAge.String(), qt.Equals, "11h0m0s")
|
|
|
|
c.Assert(c2.Dir, qt.Equals, filepath.FromSlash("/path/to/c2/filecache/getcsv"))
|
2018-11-14 04:44:04 -05:00
|
|
|
|
|
|
|
c3 := decoded["images"]
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(c3.MaxAge, qt.Equals, time.Duration(-1))
|
|
|
|
c.Assert(c3.Dir, qt.Equals, filepath.FromSlash("/path/to/c3/filecache/images"))
|
2018-11-14 04:44:04 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeConfigIgnoreCache(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
|
|
|
configStr := `
|
2018-11-14 05:53:45 -05:00
|
|
|
resourceDir = "myresources"
|
2018-11-23 03:16:42 -05:00
|
|
|
contentDir = "content"
|
|
|
|
dataDir = "data"
|
|
|
|
i18nDir = "i18n"
|
|
|
|
layoutDir = "layouts"
|
|
|
|
assetDir = "assets"
|
|
|
|
archeTypedir = "archetypes"
|
|
|
|
|
2018-11-14 04:44:04 -05: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")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
decoded, err := DecodeConfig(fs, cfg)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(decoded), qt.Equals, 5)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
|
|
|
for _, v := range decoded {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(v.MaxAge, qt.Equals, time.Duration(0))
|
2018-11-14 04:44:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeConfigDefault(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-11-23 03:16:42 -05:00
|
|
|
cfg := newTestConfig()
|
2018-11-14 11:18:32 -05:00
|
|
|
|
2018-11-14 04:44:04 -05: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")
|
|
|
|
}
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
2018-11-14 04:44:04 -05:00
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
decoded, err := DecodeConfig(fs, cfg)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(decoded), qt.Equals, 5)
|
2018-11-14 04:44:04 -05:00
|
|
|
|
2018-11-23 03:16:42 -05:00
|
|
|
imgConfig := decoded[cacheKeyImages]
|
|
|
|
jsonConfig := decoded[cacheKeyGetJSON]
|
|
|
|
|
2018-11-14 04:44:04 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(imgConfig.Dir, qt.Equals, filepath.FromSlash("_gen/images"))
|
2018-11-14 04:44:04 -05:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(imgConfig.Dir, qt.Equals, "_gen/images")
|
|
|
|
c.Assert(jsonConfig.Dir, qt.Equals, "/cache/thecache/hugoproject/filecache/getjson")
|
2018-11-14 11:18:32 -05:00
|
|
|
}
|
2018-11-23 03:16:42 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(imgConfig.isResourceDir, qt.Equals, true)
|
|
|
|
c.Assert(jsonConfig.isResourceDir, qt.Equals, false)
|
2018-11-14 11:18:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeConfigInvalidDir(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-11-14 11:18:32 -05:00
|
|
|
|
|
|
|
configStr := `
|
|
|
|
resourceDir = "myresources"
|
2018-11-23 03:16:42 -05:00
|
|
|
contentDir = "content"
|
|
|
|
dataDir = "data"
|
|
|
|
i18nDir = "i18n"
|
|
|
|
layoutDir = "layouts"
|
|
|
|
assetDir = "assets"
|
|
|
|
archeTypedir = "archetypes"
|
|
|
|
|
2018-11-14 11:18:32 -05:00
|
|
|
[caches]
|
|
|
|
[caches.getJSON]
|
|
|
|
maxAge = "10m"
|
|
|
|
dir = "/"
|
|
|
|
|
|
|
|
`
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
configStr = strings.Replace(configStr, "/", "c:\\\\", 1)
|
2018-11-14 04:44:04 -05:00
|
|
|
}
|
2018-11-14 11:18:32 -05:00
|
|
|
|
|
|
|
cfg, err := config.FromConfigString(configStr, "toml")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
2018-11-14 11:18:32 -05:00
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
_, err = DecodeConfig(fs, cfg)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2018-11-14 11:18:32 -05:00
|
|
|
|
2018-11-14 04:44:04 -05:00
|
|
|
}
|
2018-11-23 03:16:42 -05: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
|
|
|
|
}
|