Add TestChangeDefaultLanguage

This commit is contained in:
Bjørn Erik Pedersen 2016-09-21 14:09:41 +02:00
parent 2071d470e6
commit 73894cca56

View file

@ -6,9 +6,9 @@ import (
"strings" "strings"
"testing" "testing"
"path/filepath"
"os" "os"
"path/filepath"
"text/template"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/spf13/afero" "github.com/spf13/afero"
@ -21,6 +21,10 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type testSiteConfig struct {
DefaultContentLanguage string
}
func init() { func init() {
testCommonResetState() testCommonResetState()
jww.SetStdoutThreshold(jww.LevelCritical) jww.SetStdoutThreshold(jww.LevelCritical)
@ -50,8 +54,9 @@ func TestMultiSitesMainLangInRoot(t *testing.T) {
func doTestMultiSitesMainLangInRoot(t *testing.T, defaultInSubDir bool) { func doTestMultiSitesMainLangInRoot(t *testing.T, defaultInSubDir bool) {
testCommonResetState() testCommonResetState()
viper.Set("DefaultContentLanguageInSubdir", defaultInSubDir) viper.Set("DefaultContentLanguageInSubdir", defaultInSubDir)
siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
sites := createMultiTestSites(t, multiSiteTOMLConfig) sites := createMultiTestSites(t, siteConfig, multiSiteTOMLConfigTemplate)
err := sites.Build(BuildCfg{}) err := sites.Build(BuildCfg{})
@ -171,7 +176,7 @@ func TestMultiSitesBuild(t *testing.T) {
content string content string
suffix string suffix string
}{ }{
{multiSiteTOMLConfig, "toml"}, {multiSiteTOMLConfigTemplate, "toml"},
{multiSiteYAMLConfig, "yml"}, {multiSiteYAMLConfig, "yml"},
{multiSiteJSONConfig, "json"}, {multiSiteJSONConfig, "json"},
} { } {
@ -179,9 +184,10 @@ func TestMultiSitesBuild(t *testing.T) {
} }
} }
func doTestMultiSitesBuild(t *testing.T, configContent, configSuffix string) { func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
testCommonResetState() testCommonResetState()
sites := createMultiTestSitesForConfig(t, configContent, configSuffix) siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
sites := createMultiTestSitesForConfig(t, siteConfig, configTemplate, configSuffix)
err := sites.Build(BuildCfg{}) err := sites.Build(BuildCfg{})
@ -342,7 +348,8 @@ func doTestMultiSitesBuild(t *testing.T, configContent, configSuffix string) {
func TestMultiSitesRebuild(t *testing.T) { func TestMultiSitesRebuild(t *testing.T) {
testCommonResetState() testCommonResetState()
sites := createMultiTestSites(t, multiSiteTOMLConfig) siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
sites := createMultiTestSites(t, siteConfig, multiSiteTOMLConfigTemplate)
cfg := BuildCfg{Watching: true} cfg := BuildCfg{Watching: true}
err := sites.Build(cfg) err := sites.Build(cfg)
@ -554,8 +561,9 @@ func assertShouldNotBuild(t *testing.T, sites *HugoSites) {
func TestAddNewLanguage(t *testing.T) { func TestAddNewLanguage(t *testing.T) {
testCommonResetState() testCommonResetState()
siteConfig := testSiteConfig{DefaultContentLanguage: "fr"}
sites := createMultiTestSites(t, multiSiteTOMLConfig) sites := createMultiTestSites(t, siteConfig, multiSiteTOMLConfigTemplate)
cfg := BuildCfg{} cfg := BuildCfg{}
err := sites.Build(cfg) err := sites.Build(cfg)
@ -564,13 +572,15 @@ func TestAddNewLanguage(t *testing.T) {
t.Fatalf("Failed to build sites: %s", err) t.Fatalf("Failed to build sites: %s", err)
} }
newConfig := multiSiteTOMLConfig + ` newConfig := multiSiteTOMLConfigTemplate + `
[Languages.sv] [Languages.sv]
weight = 15 weight = 15
title = "Svenska" title = "Svenska"
` `
newConfig = createConfig(t, siteConfig, newConfig)
writeNewContentFile(t, "Swedish Contentfile", "2016-01-01", "content/sect/doc1.sv.md", 10) writeNewContentFile(t, "Swedish Contentfile", "2016-01-01", "content/sect/doc1.sv.md", 10)
// replace the config // replace the config
writeSource(t, "multilangconfig.toml", newConfig) writeSource(t, "multilangconfig.toml", newConfig)
@ -612,7 +622,42 @@ title = "Svenska"
} }
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" DefaultExtension = "html"
baseurl = "http://example.com/blog" baseurl = "http://example.com/blog"
DisableSitemap = false DisableSitemap = false
@ -620,7 +665,7 @@ DisableRSS = false
RSSUri = "index.xml" RSSUri = "index.xml"
paginate = 1 paginate = 1
DefaultContentLanguage = "fr" DefaultContentLanguage = "{{ .DefaultContentLanguage }}"
[permalinks] [permalinks]
other = "/somewhere/else/:filename" other = "/somewhere/else/:filename"
@ -805,11 +850,13 @@ var multiSiteJSONConfig = `
} }
` `
func createMultiTestSites(t *testing.T, tomlConfig string) *HugoSites { func createMultiTestSites(t *testing.T, siteConfig testSiteConfig, tomlConfigTemplate string) *HugoSites {
return createMultiTestSitesForConfig(t, tomlConfig, "toml") 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 // Add some layouts
if err := afero.WriteFile(hugofs.Source(), if err := afero.WriteFile(hugofs.Source(),
@ -1071,3 +1118,13 @@ func writeNewContentFile(t *testing.T, title, date, filename string, weight int)
content := newTestPage(title, date, weight) content := newTestPage(title, date, weight)
writeSource(t, filename, content) 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()
}