Skip to content

Commit

Permalink
Make hugo.toml the new default config name
Browse files Browse the repository at this point in the history
Note that the old, e.g. config.toml, still works fine.

See #8979
  • Loading branch information
bep committed Sep 19, 2021
1 parent 13ad840 commit 2be3080
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestExecute(t *testing.T) {
siteDir := filepath.Join(dir, "mysite")
resp := Execute([]string{"new", "site", siteDir, "-e=staging"})
c.Assert(resp.Err, qt.IsNil)
config := readFileFrom(c, filepath.Join(siteDir, "config.toml"))
config := readFileFrom(c, filepath.Join(siteDir, "hugo.toml"))
c.Assert(config, qt.Contains, "baseURL = 'http://example.org/'")
checkNewSiteInited(c, siteDir)
})
Expand All @@ -133,7 +133,7 @@ func checkNewSiteInited(c *qt.C, basepath string) {
filepath.Join(basepath, "archetypes"),
filepath.Join(basepath, "static"),
filepath.Join(basepath, "data"),
filepath.Join(basepath, "config.toml"),
filepath.Join(basepath, "hugo.toml"),
}

for _, path := range paths {
Expand Down
2 changes: 1 addition & 1 deletion commands/new_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func createConfig(fs *hugofs.Fs, inpath string, kind string) (err error) {
return err
}

return helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), &buf, fs.Source)
return helpers.WriteToDisk(filepath.Join(inpath, "hugo."+kind), &buf, fs.Source)
}

func nextStepsText() string {
Expand Down
2 changes: 1 addition & 1 deletion config/configLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func LoadConfigFromDir(sourceFs afero.Fs, configDir, environment string) (Provid

var keyPath []string

if name != "config" {
if name != "hugo" && name != "config" {
// Can be params.jp, menus.en etc.
name, lang := paths.FileAndExtNoDelimiter(name)

Expand Down
14 changes: 10 additions & 4 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ 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() {
configFiles, loadAll := d.configFilenames()
for _, name := range configFiles {
var filename string
filename, err := l.loadConfig(name)
if err == nil {
configFiles = append(configFiles, filename)
if !loadAll {
break
}
} else if err != ErrNoConfigFile {
return nil, nil, err
}
Expand Down Expand Up @@ -211,11 +215,13 @@ func (d ConfigSourceDescriptor) configFileDir() string {
return d.WorkingDir
}

func (d ConfigSourceDescriptor) configFilenames() []string {
func (d ConfigSourceDescriptor) configFilenames() ([]string, bool) {
if d.Filename == "" {
return []string{"config"}
// Pick the first one of these.
// We switched from config.toml etc. to hugo.toml in Hugo 0.89.
return []string{"hugo", "config"}, false
}
return strings.Split(d.Filename, ",")
return strings.Split(d.Filename, ","), true
}

// SiteConfig represents the config in .Site.Config.
Expand Down
34 changes: 26 additions & 8 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,46 @@ func TestLoadConfig(t *testing.T) {

c := qt.New(t)

loadConfig := func(c *qt.C, configContent string, fromDir bool) config.Provider {
loadConfig := func(c *qt.C, configFilename, configContent string, fromDir bool) config.Provider {
mm := afero.NewMemMapFs()
filename := "config.toml"
descriptor := ConfigSourceDescriptor{Fs: mm}
if fromDir {
filename = filepath.Join("config", "_default", filename)
configFilename = filepath.Join("config", "_default", configFilename)
descriptor.AbsConfigDir = "config"
}
writeToFs(t, mm, filename, configContent)
writeToFs(t, mm, configFilename, configContent)
cfg, _, err := LoadConfig(descriptor)
c.Assert(err, qt.IsNil)
return cfg
}

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)
cfg := loadConfig(c, "hugo.toml", `PaginatePath = "side"`, false)
c.Assert(cfg.GetString("paginatePath"), qt.Equals, "side")
})

c.Run("Basic, old config name", func(c *qt.C) {
c.Parallel()
cfg := loadConfig(c, "config.toml", `PaginatePath = "side"`, false)
c.Assert(cfg.GetString("paginatePath"), qt.Equals, "side")
})

c.Run("Both hugo.toml and config.toml", func(c *qt.C) {
c.Parallel()
mm := afero.NewMemMapFs()
descriptor := ConfigSourceDescriptor{Fs: mm}
writeToFs(t, mm, "hugo.toml", `title = "Hugo Rocks"`)
writeToFs(t, mm, "config.toml", `paginatePath = "side"`)

cfg, _, err := LoadConfig(descriptor)
c.Assert(err, qt.IsNil)

c.Assert(cfg.GetString("title"), qt.Equals, "Hugo Rocks")
c.Assert(cfg.GetString("paginatePath"), qt.Equals, "page")

})

// Issue #8763
for _, fromDir := range []bool{false, true} {
testName := "Taxonomy overrides"
Expand All @@ -64,7 +82,7 @@ func TestLoadConfig(t *testing.T) {
}
c.Run(testName, func(c *qt.C) {
c.Parallel()
cfg := loadConfig(c, `[taxonomies]
cfg := loadConfig(c, "hugo.toml", `[taxonomies]
appellation = "appellations"
vigneron = "vignerons"`, fromDir)

Expand Down

0 comments on commit 2be3080

Please sign in to comment.