// Copyright 2016-present The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugolib
import (
"bytes"
"fmt"
"path/filepath"
"strings"
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/media"
"github.com/google/go-cmp/cmp"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/common/maps"
"github.com/spf13/afero"
)
func TestLoadConfig(t *testing.T) {
c := qt.New(t)
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
}
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")
})
// 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) {
t.Parallel()
c := qt.New(t)
// Add a random config variable for testing.
// side = page in Norwegian.
configContentBase := `
DontChange = "same"
PaginatePath = "side"
`
configContentSub := `
PaginatePath = "top"
`
mm := afero.NewMemMapFs()
writeToFs(t, mm, "base.toml", configContentBase)
writeToFs(t, mm, "override.toml", configContentSub)
cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Filename: "base.toml,override.toml"})
c.Assert(err, qt.IsNil)
c.Assert(cfg.GetString("paginatePath"), qt.Equals, "top")
c.Assert(cfg.GetString("DontChange"), qt.Equals, "same")
}
func TestLoadConfigFromThemes(t *testing.T) {
t.Parallel()
c := qt.New(t)
mainConfigTemplate := `
theme = "test-theme"
baseURL = "https://example.com/"
[frontmatter]
date = ["date","publishDate"]
[params]
MERGE_PARAMS
p1 = "p1 main"
[params.b]
b1 = "b1 main"
[params.b.c]
bc1 = "bc1 main"
[mediaTypes]
[mediaTypes."text/m1"]
suffixes = ["m1main"]
[outputFormats.o1]
mediaType = "text/m1"
baseName = "o1main"
[languages]
[languages.en]
languageName = "English"
[languages.en.params]
pl1 = "p1-en-main"
[languages.nb]
languageName = "Norsk"
[languages.nb.params]
pl1 = "p1-nb-main"
[[menu.main]]
name = "menu-main-main"
[[menu.top]]
name = "menu-top-main"
`
themeConfig := `
baseURL = "http://bep.is/"
# Can not be set in theme.
disableKinds = ["taxonomy", "term"]
# Can not be set in theme.
[frontmatter]