diff options
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/config.go | 37 | ||||
-rw-r--r-- | hugolib/config_test.go | 52 | ||||
-rw-r--r-- | hugolib/integrationtest_builder.go | 15 |
3 files changed, 89 insertions, 15 deletions
diff --git a/hugolib/config.go b/hugolib/config.go index 8e73a35ec..059424e85 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -69,18 +69,35 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid // use a partial configuration to do its job. defer l.deleteMergeStrategies() - for _, name := range d.configFilenames() { - var filename string - filename, err := l.loadConfig(name) - if err == nil { - configFiles = append(configFiles, filename) - } else if err != ErrNoConfigFile { - return nil, nil, l.wrapFileError(err, filename) + names := d.configFilenames() + + if names != nil { + for _, name := range names { + var filename string + filename, err := l.loadConfig(name) + if err == nil { + configFiles = append(configFiles, filename) + } else if err != ErrNoConfigFile { + return nil, nil, l.wrapFileError(err, filename) + } + } + } else { + for _, name := range config.DefaultConfigNames { + var filename string + filename, err := l.loadConfig(name) + if err == nil { + configFiles = append(configFiles, filename) + break + } else if err != ErrNoConfigFile { + return nil, nil, l.wrapFileError(err, filename) + } } } if d.AbsConfigDir != "" { + dcfg, dirnames, err := config.LoadConfigFromDir(l.Fs, d.AbsConfigDir, l.Environment) + if err == nil { if len(dirnames) > 0 { l.cfg.Set("", dcfg.Get("")) @@ -162,9 +179,9 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid return l.cfg, configFiles, err } -// LoadConfigDefault is a convenience method to load the default "config.toml" config. +// LoadConfigDefault is a convenience method to load the default "hugo.toml" config. func LoadConfigDefault(fs afero.Fs) (config.Provider, error) { - v, _, err := LoadConfig(ConfigSourceDescriptor{Fs: fs, Filename: "config.toml"}) + v, _, err := LoadConfig(ConfigSourceDescriptor{Fs: fs}) return v, err } @@ -202,7 +219,7 @@ func (d ConfigSourceDescriptor) configFileDir() string { func (d ConfigSourceDescriptor) configFilenames() []string { if d.Filename == "" { - return []string{"config"} + return nil } return strings.Split(d.Filename, ",") } diff --git a/hugolib/config_test.go b/hugolib/config_test.go index 882d83c8d..37605b4c2 100644 --- a/hugolib/config_test.go +++ b/hugolib/config_test.go @@ -782,3 +782,55 @@ defaultMarkdownHandler = 'blackfriday' b.Assert(err.Error(), qt.Contains, "Configured defaultMarkdownHandler \"blackfriday\" not found. Did you mean to use goldmark? Blackfriday was removed in Hugo v0.100.0.") } + +// Issue 8979 +func TestHugoConfig(t *testing.T) { + filesTemplate := ` +-- hugo.toml -- +theme = "mytheme" +[params] +rootparam = "rootvalue" +-- config/_default/hugo.toml -- +[params] +rootconfigparam = "rootconfigvalue" +-- themes/mytheme/config/_default/hugo.toml -- +[params] +themeconfigdirparam = "themeconfigdirvalue" +-- themes/mytheme/hugo.toml -- +[params] +themeparam = "themevalue" +-- layouts/index.html -- +rootparam: {{ site.Params.rootparam }} +rootconfigparam: {{ site.Params.rootconfigparam }} +themeparam: {{ site.Params.themeparam }} +themeconfigdirparam: {{ site.Params.themeconfigdirparam }} + + +` + + for _, configName := range []string{"hugo.toml", "config.toml"} { + configName := configName + t.Run(configName, func(t *testing.T) { + t.Parallel() + + files := strings.ReplaceAll(filesTemplate, "hugo.toml", configName) + + b, err := NewIntegrationTestBuilder( + IntegrationTestConfig{ + T: t, + TxtarString: files, + }, + ).BuildE() + + b.Assert(err, qt.IsNil) + b.AssertFileContent("public/index.html", + "rootparam: rootvalue", + "rootconfigparam: rootconfigvalue", + "themeparam: themevalue", + "themeconfigdirparam: themeconfigdirvalue", + ) + + }) + } + +} diff --git a/hugolib/integrationtest_builder.go b/hugolib/integrationtest_builder.go index 9dcfe4830..5b457893d 100644 --- a/hugolib/integrationtest_builder.go +++ b/hugolib/integrationtest_builder.go @@ -301,13 +301,18 @@ func (s *IntegrationTestBuilder) initBuilder() error { s.Assert(afero.WriteFile(afs, filename, data, 0666), qt.IsNil) } + configDirFilename := filepath.Join(s.Cfg.WorkingDir, "config") + if _, err := afs.Stat(configDirFilename); err != nil { + configDirFilename = "" + } + cfg, _, err := LoadConfig( ConfigSourceDescriptor{ - WorkingDir: s.Cfg.WorkingDir, - Fs: afs, - Logger: logger, - Environ: []string{}, - Filename: "config.toml", + WorkingDir: s.Cfg.WorkingDir, + AbsConfigDir: configDirFilename, + Fs: afs, + Logger: logger, + Environ: []string{}, }, func(cfg config.Provider) error { return nil |