all: Refactor to nonglobal Viper, i18n etc.

This is a final rewrite that removes all the global state in Hugo, which also enables
the use if `t.Parallel` in tests.

Updates #2701
Fixes #3016
This commit is contained in:
Bjørn Erik Pedersen
2017-02-05 10:20:06 +07:00
parent e34af6ee30
commit 93ca7c9e95
99 changed files with 2843 additions and 2458 deletions

View File

@@ -16,7 +16,7 @@ package hugofs
import (
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/spf13/hugo/config"
)
// Os points to an Os Afero file system.
@@ -39,30 +39,37 @@ type Fs struct {
// NewDefault creates a new Fs with the OS file system
// as source and destination file systems.
func NewDefault() *Fs {
func NewDefault(cfg config.Provider) *Fs {
fs := &afero.OsFs{}
return newFs(fs)
return newFs(fs, cfg)
}
// NewDefault creates a new Fs with the MemMapFs
// as source and destination file systems.
// Useful for testing.
func NewMem() *Fs {
func NewMem(cfg config.Provider) *Fs {
fs := &afero.MemMapFs{}
return newFs(fs)
return newFs(fs, cfg)
}
func newFs(base afero.Fs) *Fs {
// NewFrom creates a new Fs based on the provided Afero Fs
// as source and destination file systems.
// Useful for testing.
func NewFrom(fs afero.Fs, cfg config.Provider) *Fs {
return newFs(fs, cfg)
}
func newFs(base afero.Fs, cfg config.Provider) *Fs {
return &Fs{
Source: base,
Destination: base,
Os: &afero.OsFs{},
WorkingDir: getWorkingDirFs(base),
WorkingDir: getWorkingDirFs(base, cfg),
}
}
func getWorkingDirFs(base afero.Fs) *afero.BasePathFs {
workingDir := viper.GetString("workingDir")
func getWorkingDirFs(base afero.Fs, cfg config.Provider) *afero.BasePathFs {
workingDir := cfg.GetString("workingDir")
if workingDir != "" {
return afero.NewBasePathFs(afero.NewReadOnlyFs(base), workingDir).(*afero.BasePathFs)