Fix default values when loading from config dir

By waiting until we've loaded the config dir config before applying the default values.

Fixes #8763
This commit is contained in:
Bjørn Erik Pedersen 2021-07-15 15:31:50 +02:00
parent a70da2b74a
commit ae6cf93c84
2 changed files with 43 additions and 16 deletions

View file

@ -78,10 +78,6 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
} }
} }
if err := l.applyConfigDefaults(); err != nil {
return l.cfg, configFiles, err
}
if d.AbsConfigDir != "" { if d.AbsConfigDir != "" {
dcfg, dirnames, err := config.LoadConfigFromDir(l.Fs, d.AbsConfigDir, l.Environment) dcfg, dirnames, err := config.LoadConfigFromDir(l.Fs, d.AbsConfigDir, l.Environment)
if err == nil { if err == nil {
@ -97,6 +93,10 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
} }
} }
if err := l.applyConfigDefaults(); err != nil {
return l.cfg, configFiles, err
}
l.cfg.SetDefaultMergeStrategy() l.cfg.SetDefaultMergeStrategy()
// We create languages based on the settings, so we need to make sure that // We create languages based on the settings, so we need to make sure that

View file

@ -20,6 +20,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/media"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
@ -29,24 +31,49 @@ import (
) )
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {
t.Parallel()
c := qt.New(t) c := qt.New(t)
// Add a random config variable for testing. loadConfig := func(c *qt.C, configContent string, fromDir bool) config.Provider {
// side = page in Norwegian. mm := afero.NewMemMapFs()
configContent := ` filename := "config.toml"
PaginatePath = "side" descriptor := ConfigSourceDescriptor{Fs: mm}
` if fromDir {
filename = filepath.Join("config", "_default", filename)
descriptor.AbsConfigDir = "config"
}
writeToFs(t, mm, filename, configContent)
cfg, _, err := LoadConfig(descriptor)
c.Assert(err, qt.IsNil)
return cfg
}
mm := afero.NewMemMapFs() c.Run("Basic", func(c *qt.C) {
c.Parallel()
// Add a random config variable for testing.
// side = page in Norwegian.
cfg := loadConfig(c, `PaginatePath = "side"`, false)
c.Assert(cfg.GetString("paginatePath"), qt.Equals, "side")
})
writeToFs(t, mm, "hugo.toml", configContent) // Issue #8763
for _, fromDir := range []bool{false, true} {
testName := "Taxonomy overrides"
if fromDir {
testName += " from dir"
}
c.Run(testName, func(c *qt.C) {
c.Parallel()
cfg := loadConfig(c, `[taxonomies]
appellation = "appellations"
vigneron = "vignerons"`, fromDir)
cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Filename: "hugo.toml"}) c.Assert(cfg.Get("taxonomies"), qt.DeepEquals, maps.Params{
c.Assert(err, qt.IsNil) "appellation": "appellations",
"vigneron": "vignerons",
c.Assert(cfg.GetString("paginatePath"), qt.Equals, "side") })
})
}
} }
func TestLoadMultiConfig(t *testing.T) { func TestLoadMultiConfig(t *testing.T) {