mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code. Also, * Lower case the default output format names; this is in line with the custom ones (map keys) and how it's treated all the places. This avoids doing `stringds.EqualFold` everywhere. Closes #10896 Closes #10620
This commit is contained in:
@@ -20,13 +20,11 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/common/types"
|
||||
|
||||
"github.com/gohugoio/hugo/modules"
|
||||
"github.com/gohugoio/hugo/config/testconfig"
|
||||
|
||||
"github.com/gohugoio/hugo/tpl/tplimpl"
|
||||
|
||||
"github.com/gohugoio/hugo/common/loggers"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/gohugoio/hugo/resources/page"
|
||||
"github.com/spf13/afero"
|
||||
|
||||
@@ -34,7 +32,6 @@ import (
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
)
|
||||
|
||||
var logger = loggers.NewErrorLogger()
|
||||
@@ -394,26 +391,22 @@ other = "{{ . }} miesiąca"
|
||||
} {
|
||||
|
||||
c.Run(test.name, func(c *qt.C) {
|
||||
cfg := getConfig()
|
||||
cfg := config.New()
|
||||
cfg.Set("enableMissingTranslationPlaceholders", true)
|
||||
fs := hugofs.NewMem(cfg)
|
||||
cfg.Set("publishDir", "public")
|
||||
afs := afero.NewMemMapFs()
|
||||
|
||||
err := afero.WriteFile(fs.Source, filepath.Join("i18n", test.lang+".toml"), []byte(test.templ), 0755)
|
||||
err := afero.WriteFile(afs, filepath.Join("i18n", test.lang+".toml"), []byte(test.templ), 0755)
|
||||
c.Assert(err, qt.IsNil)
|
||||
|
||||
tp := NewTranslationProvider()
|
||||
depsCfg := newDepsConfig(tp, cfg, fs)
|
||||
depsCfg.Logger = loggers.NewWarningLogger()
|
||||
d, err := deps.New(depsCfg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(d.LoadResources(), qt.IsNil)
|
||||
d, tp := prepareDeps(afs, cfg)
|
||||
|
||||
f := tp.t.Func(test.lang)
|
||||
ctx := context.Background()
|
||||
|
||||
for _, variant := range test.variants {
|
||||
c.Assert(f(ctx, test.id, variant.Key), qt.Equals, variant.Value, qt.Commentf("input: %v", variant.Key))
|
||||
c.Assert(int(depsCfg.Logger.LogCounters().WarnCounter.Count()), qt.Equals, 0)
|
||||
c.Assert(int(d.Log.LogCounters().WarnCounter.Count()), qt.Equals, 0)
|
||||
}
|
||||
|
||||
})
|
||||
@@ -471,52 +464,33 @@ func TestGetPluralCount(t *testing.T) {
|
||||
|
||||
func prepareTranslationProvider(t testing.TB, test i18nTest, cfg config.Provider) *TranslationProvider {
|
||||
c := qt.New(t)
|
||||
fs := hugofs.NewMem(cfg)
|
||||
afs := afero.NewMemMapFs()
|
||||
|
||||
for file, content := range test.data {
|
||||
err := afero.WriteFile(fs.Source, filepath.Join("i18n", file), []byte(content), 0755)
|
||||
err := afero.WriteFile(afs, filepath.Join("i18n", file), []byte(content), 0755)
|
||||
c.Assert(err, qt.IsNil)
|
||||
}
|
||||
|
||||
tp := NewTranslationProvider()
|
||||
depsCfg := newDepsConfig(tp, cfg, fs)
|
||||
d, err := deps.New(depsCfg)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(d.LoadResources(), qt.IsNil)
|
||||
|
||||
_, tp := prepareDeps(afs, cfg)
|
||||
return tp
|
||||
}
|
||||
|
||||
func newDepsConfig(tp *TranslationProvider, cfg config.Provider, fs *hugofs.Fs) deps.DepsCfg {
|
||||
l := langs.NewLanguage("en", cfg)
|
||||
l.Set("i18nDir", "i18n")
|
||||
return deps.DepsCfg{
|
||||
Language: l,
|
||||
Site: page.NewDummyHugoSite(cfg),
|
||||
Cfg: cfg,
|
||||
Fs: fs,
|
||||
Logger: logger,
|
||||
TemplateProvider: tplimpl.DefaultTemplateProvider,
|
||||
TranslationProvider: tp,
|
||||
}
|
||||
}
|
||||
|
||||
func getConfig() config.Provider {
|
||||
v := config.NewWithTestDefaults()
|
||||
langs.LoadLanguageSettings(v, nil)
|
||||
mod, err := modules.CreateProjectModule(v)
|
||||
if err != nil {
|
||||
func prepareDeps(afs afero.Fs, cfg config.Provider) (*deps.Deps, *TranslationProvider) {
|
||||
d := testconfig.GetTestDeps(afs, cfg)
|
||||
translationProvider := NewTranslationProvider()
|
||||
d.TemplateProvider = tplimpl.DefaultTemplateProvider
|
||||
d.TranslationProvider = translationProvider
|
||||
d.Site = page.NewDummyHugoSite(cfg)
|
||||
if err := d.Compile(nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
v.Set("allModules", modules.Modules{mod})
|
||||
|
||||
return v
|
||||
return d, translationProvider
|
||||
}
|
||||
|
||||
func TestI18nTranslate(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
var actual, expected string
|
||||
v := getConfig()
|
||||
v := config.New()
|
||||
|
||||
// Test without and with placeholders
|
||||
for _, enablePlaceholders := range []bool{false, true} {
|
||||
@@ -537,7 +511,7 @@ func TestI18nTranslate(t *testing.T) {
|
||||
}
|
||||
|
||||
func BenchmarkI18nTranslate(b *testing.B) {
|
||||
v := getConfig()
|
||||
v := config.New()
|
||||
for _, test := range i18nTests {
|
||||
b.Run(test.name, func(b *testing.B) {
|
||||
tp := prepareTranslationProvider(b, test, v)
|
||||
|
Reference in New Issue
Block a user