Fix default values when loading from config dir
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 15 Jul 2021 13:31:50 +0000 (15:31 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 15 Jul 2021 14:15:32 +0000 (16:15 +0200)
By waiting until we've loaded the config dir config before applying the default values.

Fixes #8763

hugolib/config.go
hugolib/config_test.go

index 65b269ab6a0eb1f679564881bc355302105a554f..694f346156aaf487658d38db5f5c848222541443 100644 (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 != "" {
                dcfg, dirnames, err := config.LoadConfigFromDir(l.Fs, d.AbsConfigDir, l.Environment)
                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()
 
        // We create languages based on the settings, so we need to make sure that
index 6de2747179dc8a70ad74a65d140b233f0347ad37..575931975308e74a27ff509b291912d93a25d916 100644 (file)
@@ -20,6 +20,8 @@ import (
        "strings"
        "testing"
 
+       "github.com/gohugoio/hugo/config"
+
        "github.com/gohugoio/hugo/media"
        "github.com/google/go-cmp/cmp"
 
@@ -29,24 +31,49 @@ import (
 )
 
 func TestLoadConfig(t *testing.T) {
-       t.Parallel()
 
        c := qt.New(t)
 
-       // Add a random config variable for testing.
-       // side = page in Norwegian.
-       configContent := `
-       PaginatePath = "side"
-       `
-
-       mm := afero.NewMemMapFs()
-
-       writeToFs(t, mm, "hugo.toml", configContent)
+       loadConfig := func(c *qt.C, configContent string, fromDir bool) config.Provider {
+               mm := afero.NewMemMapFs()
+               filename := "config.toml"
+               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
+       }
 
-       cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Filename: "hugo.toml"})
-       c.Assert(err, qt.IsNil)
+       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")
+       })
 
-       c.Assert(cfg.GetString("paginatePath"), qt.Equals, "side")
+       // 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)
+
+                       c.Assert(cfg.Get("taxonomies"), qt.DeepEquals, maps.Params{
+                               "appellation": "appellations",
+                               "vigneron":    "vignerons",
+                       })
+               })
+       }
 }
 
 func TestLoadMultiConfig(t *testing.T) {