"strings"
"testing"
- "path/filepath"
-
"os"
+ "path/filepath"
+ "text/template"
"github.com/fsnotify/fsnotify"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)
+type testSiteConfig struct {
+ DefaultContentLanguage string
+}
+
func init() {
testCommonResetState()
jww.SetStdoutThreshold(jww.LevelCritical)
func doTestMultiSitesMainLangInRoot(t *testing.T, defaultInSubDir bool) {
testCommonResetState()
viper.Set("DefaultContentLanguageInSubdir", defaultInSubDir)
+ siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
- sites := createMultiTestSites(t, multiSiteTOMLConfig)
+ sites := createMultiTestSites(t, siteConfig, multiSiteTOMLConfigTemplate)
err := sites.Build(BuildCfg{})
content string
suffix string
}{
- {multiSiteTOMLConfig, "toml"},
+ {multiSiteTOMLConfigTemplate, "toml"},
{multiSiteYAMLConfig, "yml"},
{multiSiteJSONConfig, "json"},
} {
}
}
-func doTestMultiSitesBuild(t *testing.T, configContent, configSuffix string) {
+func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
testCommonResetState()
- sites := createMultiTestSitesForConfig(t, configContent, configSuffix)
+ siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
+ sites := createMultiTestSitesForConfig(t, siteConfig, configTemplate, configSuffix)
err := sites.Build(BuildCfg{})
func TestMultiSitesRebuild(t *testing.T) {
testCommonResetState()
- sites := createMultiTestSites(t, multiSiteTOMLConfig)
+ siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
+ sites := createMultiTestSites(t, siteConfig, multiSiteTOMLConfigTemplate)
cfg := BuildCfg{Watching: true}
err := sites.Build(cfg)
func TestAddNewLanguage(t *testing.T) {
testCommonResetState()
+ siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
- sites := createMultiTestSites(t, multiSiteTOMLConfig)
+ sites := createMultiTestSites(t, siteConfig, multiSiteTOMLConfigTemplate)
cfg := BuildCfg{}
err := sites.Build(cfg)
t.Fatalf("Failed to build sites: %s", err)
}
- newConfig := multiSiteTOMLConfig + `
+ newConfig := multiSiteTOMLConfigTemplate + `
[Languages.sv]
weight = 15
title = "Svenska"
`
+ newConfig = createConfig(t, siteConfig, newConfig)
+
writeNewContentFile(t, "Swedish Contentfile", "2016-01-01", "content/sect/doc1.sv.md", 10)
// replace the config
writeSource(t, "multilangconfig.toml", newConfig)
}
-var multiSiteTOMLConfig = `
+func TestChangeDefaultLanguage(t *testing.T) {
+ testCommonResetState()
+ viper.Set("DefaultContentLanguageInSubdir", false)
+
+ sites := createMultiTestSites(t, testSiteConfig{DefaultContentLanguage: "fr"}, multiSiteTOMLConfigTemplate)
+ cfg := BuildCfg{}
+
+ err := sites.Build(cfg)
+
+ if err != nil {
+ t.Fatalf("Failed to build sites: %s", err)
+ }
+
+ assertFileContent(t, "public/sect/doc1/index.html", true, "Single", "Bonjour")
+ assertFileContent(t, "public/en/sect/doc2/index.html", true, "Single", "Hello")
+
+ newConfig := createConfig(t, testSiteConfig{DefaultContentLanguage: "en"}, multiSiteTOMLConfigTemplate)
+
+ // replace the config
+ writeSource(t, "multilangconfig.toml", newConfig)
+
+ // Watching does not work with in-memory fs, so we trigger a reload manually
+ require.NoError(t, viper.ReadInConfig())
+ err = sites.Build(BuildCfg{CreateSitesFromConfig: true})
+
+ if err != nil {
+ t.Fatalf("Failed to rebuild sites: %s", err)
+ }
+
+ // Default language is now en, so that should now be the "root" language
+ assertFileContent(t, "public/fr/sect/doc1/index.html", true, "Single", "Bonjour")
+ assertFileContent(t, "public/sect/doc2/index.html", true, "Single", "Hello")
+
+}
+
+var multiSiteTOMLConfigTemplate = `
DefaultExtension = "html"
baseurl = "http://example.com/blog"
DisableSitemap = false
RSSUri = "index.xml"
paginate = 1
-DefaultContentLanguage = "fr"
+DefaultContentLanguage = "{{ .DefaultContentLanguage }}"
[permalinks]
other = "/somewhere/else/:filename"
}
`
-func createMultiTestSites(t *testing.T, tomlConfig string) *HugoSites {
- return createMultiTestSitesForConfig(t, tomlConfig, "toml")
+func createMultiTestSites(t *testing.T, siteConfig testSiteConfig, tomlConfigTemplate string) *HugoSites {
+ return createMultiTestSitesForConfig(t, siteConfig, tomlConfigTemplate, "toml")
}
-func createMultiTestSitesForConfig(t *testing.T, configContent, configSuffix string) *HugoSites {
+func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, configTemplate, configSuffix string) *HugoSites {
+
+ configContent := createConfig(t, siteConfig, configTemplate)
// Add some layouts
if err := afero.WriteFile(hugofs.Source(),
content := newTestPage(title, date, weight)
writeSource(t, filename, content)
}
+
+func createConfig(t *testing.T, config testSiteConfig, configTemplate string) string {
+ templ, err := template.New("test").Parse(configTemplate)
+ if err != nil {
+ t.Fatal("Template parse failed:", err)
+ }
+ var b bytes.Buffer
+ templ.Execute(&b, config)
+ return b.String()
+}