package hugolib import ( "bytes" "fmt" "strings" "testing" "path/filepath" "os" "github.com/fsnotify/fsnotify" "github.com/spf13/afero" "github.com/spf13/hugo/helpers" "github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/source" jww "github.com/spf13/jwalterweatherman" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func init() { testCommonResetState() jww.SetStdoutThreshold(jww.LevelCritical) } func testCommonResetState() { hugofs.InitMemFs() viper.Reset() viper.SetFs(hugofs.Source()) loadDefaultSettings() // Default is false, but true is easier to use as default in tests viper.Set("DefaultContentLanguageInSubdir", true) if err := hugofs.Source().Mkdir("content", 0755); err != nil { panic("Content folder creation failed.") } } func TestMultiSitesMainLangInRoot(t *testing.T) { for _, b := range []bool{false, true} { doTestMultiSitesMainLangInRoot(t, b) } } func doTestMultiSitesMainLangInRoot(t *testing.T, defaultInSubDir bool) { testCommonResetState() viper.Set("DefaultContentLanguageInSubdir", defaultInSubDir) sites := createMultiTestSites(t, multiSiteTomlConfig) err := sites.Build(BuildCfg{}) if err != nil { t.Fatalf("Failed to build sites: %s", err) } require.Len(t, sites.Sites, 4) enSite := sites.Sites[0] frSite := sites.Sites[1] require.Equal(t, "/en", enSite.Info.LanguagePrefix) if defaultInSubDir { require.Equal(t, "/fr", frSite.Info.LanguagePrefix) } else { require.Equal(t, "", frSite.Info.LanguagePrefix) } doc1en := enSite.Pages[0] doc1fr := frSite.Pages[0] enPerm, _ := doc1en.Permalink() enRelPerm, _ := doc1en.RelPermalink() require.Equal(t, "http://example.com/blog/en/sect/doc1-slug/", enPerm) require.Equal(t, "/blog/en/sect/doc1-slug/", enRelPerm) frPerm, _ := doc1fr.Permalink() frRelPerm, _ := doc1fr.RelPermalink() // Main language in root require.Equal(t, replaceDefaultContentLanguageValue("http://example.com/blog/fr/sect/doc1/", defaultInSubDir), frPerm) require.Equal(t, replaceDefaultContentLanguageValue("/blog/fr/sect/doc1/", defaultInSubDir), frRelPerm) assertFileContent(t, "public/fr/sect/doc1/index.html", defaultInSubDir, "Single", "Bonjour") assertFileContent(t, "public/en/sect/doc1-slug/index.html", defaultInSubDir, "Single", "Hello") // Check home if defaultInSubDir { // should have a redirect on top level. assertFileContent(t, "public/index.html", true, ``) } assertFileContent(t, "public/fr/index.html", defaultInSubDir, "Home", "Bonjour") assertFileContent(t, "public/en/index.html", defaultInSubDir, "Home", "Hello") // Check list pages assertFileContent(t, "public/fr/sect/index.html", defaultInSubDir, "List", "Bonjour") assertFileContent(t, "public/en/sect/index.html", defaultInSubDir, "List", "Hello") assertFileContent(t, "public/fr/plaques/frtag1/index.html", defaultInSubDir, "List", "Bonjour") assertFileContent(t, "public/en/tags/tag1/index.html", defaultInSubDir, "List", "Hello") // Check sitemaps // Sitemaps behaves different: In a multilanguage setup there will always be a index file and // one sitemap in each lang folder. assertFileContent(t, "public/sitemap.xml", true, "http:/example.com/blog/en/sitemap.xml", "http:/example.com/blog/fr/sitemap.xml") if defaultInSubDir { assertFileContent(t, "public/fr/sitemap.xml", true, "http://example.com/blog/fr/") } else { assertFileContent(t, "public/fr/sitemap.xml", true, "http://example.com/blog/") } assertFileContent(t, "public/en/sitemap.xml", true, "http://example.com/blog/en/") // Check rss assertFileContent(t, "public/fr/index.xml", defaultInSubDir, `http:/example.com/blog/en/sitemap.xml"), sitemapIndex) require.True(t, strings.Contains(sitemapIndex, "http:/example.com/blog/fr/sitemap.xml"), sitemapIndex) sitemapEn := readDestination(t, "public/en/sitemap.xml") sitemapFr := readDestination(t, "public/fr/sitemap.xml") require.True(t, strings.Contains(sitemapEn, "http://example.com/blog/en/sect/doc2/"), sitemapEn) require.True(t, strings.Contains(sitemapFr, "http://example.com/blog/fr/sect/doc1/"), sitemapFr) // Check taxonomies enTags := enSite.Taxonomies["tags"] frTags := frSite.Taxonomies["plaques"] require.Len(t, enTags, 2, fmt.Sprintf("Tags in en: %v", enTags)) require.Len(t, frTags, 2, fmt.Sprintf("Tags in fr: %v", frTags)) require.NotNil(t, enTags["tag1"]) require.NotNil(t, frTags["frtag1"]) readDestination(t, "public/fr/plaques/frtag1/index.html") readDestination(t, "public/en/tags/tag1/index.html") // Check Blackfriday config assert.True(t, strings.Contains(string(doc1fr.Content), "«"), string(doc1fr.Content)) assert.False(t, strings.Contains(string(doc1en.Content), "«"), string(doc1en.Content)) assert.True(t, strings.Contains(string(doc1en.Content), "“"), string(doc1en.Content)) // Check that the drafts etc. are not built/processed/rendered. assertShouldNotBuild(t, sites) } func TestMultiSitesRebuild(t *testing.T) { testCommonResetState() sites := createMultiTestSites(t, multiSiteTomlConfig) cfg := BuildCfg{} err := sites.Build(cfg) if err != nil { t.Fatalf("Failed to build sites: %s", err) } _, err = hugofs.Destination().Open("public/en/sect/doc2/index.html") if err != nil { t.Fatalf("Unable to locate file") } enSite := sites.Sites[0] frSite := sites.Sites[1] assert.Len(t, enSite.Pages, 3) assert.Len(t, frSite.Pages, 3) // Verify translations docEn := readDestination(t, "public/en/sect/doc1-slug/index.html") assert.True(t, strings.Contains(docEn, "Hello"), "No Hello") docFr := readDestination(t, "public/fr/sect/doc1/index.html") assert.True(t, strings.Contains(docFr, "Bonjour"), "No Bonjour") for i, this := range []struct { preFunc func(t *testing.T) events []fsnotify.Event assertFunc func(t *testing.T) }{ // * Remove doc // * Add docs existing languages // (Add doc new language: TODO(bep) we should load config.toml as part of these so we can add languages). // * Rename file // * Change doc // * Change a template // * Change language file { nil, []fsnotify.Event{{Name: "content/sect/doc2.en.md", Op: fsnotify.Remove}}, func(t *testing.T) { assert.Len(t, enSite.Pages, 2, "1 en removed") // Check build stats assert.Equal(t, 1, enSite.draftCount, "Draft") assert.Equal(t, 1, enSite.futureCount, "Future") assert.Equal(t, 1, enSite.expiredCount, "Expired") assert.Equal(t, 0, frSite.draftCount, "Draft") assert.Equal(t, 1, frSite.futureCount, "Future") assert.Equal(t, 1, frSite.expiredCount, "Expired") }, }, { func(t *testing.T) { writeNewContentFile(t, "new_en_1", "2016-07-31", "content/new1.en.md", -5) writeNewContentFile(t, "new_en_2", "1989-07-30", "content/new2.en.md", -10) writeNewContentFile(t, "new_fr_1", "2016-07-30", "content/new1.fr.md", 10) }, []fsnotify.Event{ {Name: "content/new1.en.md", Op: fsnotify.Create}, {Name: "content/new2.en.md", Op: fsnotify.Create}, {Name: "content/new1.fr.md", Op: fsnotify.Create}, }, func(t *testing.T) { assert.Len(t, enSite.Pages, 4) assert.Len(t, enSite.AllPages, 10) assert.Len(t, frSite.Pages, 4) assert.Equal(t, "new_fr_1", frSite.Pages[3].Title) assert.Equal(t, "new_en_2", enSite.Pages[0].Title) assert.Equal(t, "new_en_1", enSite.Pages[1].Title) rendered := readDestination(t, "public/en/new1/index.html") assert.True(t, strings.Contains(rendered, "new_en_1"), rendered) }, }, { func(t *testing.T) { p := "content/sect/doc1.en.md" doc1 := readSource(t, p) doc1 += "CHANGED" writeSource(t, p, doc1) }, []fsnotify.Event{{Name: "content/sect/doc1.en.md", Op: fsnotify.Write}}, func(t *testing.T) { assert.Len(t, enSite.Pages, 4) doc1 := readDestination(t, "public/en/sect/doc1-slug/index.html") assert.True(t, strings.Contains(doc1, "CHANGED"), doc1) }, }, // Rename a file { func(t *testing.T) { if err := hugofs.Source().Rename("content/new1.en.md", "content/new1renamed.en.md"); err != nil { t.Fatalf("Rename failed: %s", err) } }, []fsnotify.Event{ {Name: "content/new1renamed.en.md", Op: fsnotify.Rename}, {Name: "content/new1.en.md", Op: fsnotify.Rename}, }, func(t *testing.T) { assert.Len(t, enSite.Pages, 4, "Rename") assert.Equal(t, "new_en_1", enSite.Pages[1].Title) rendered := readDestination(t, "public/en/new1renamed/index.html") assert.True(t, strings.Contains(rendered, "new_en_1"), rendered) }}, { // Change a template func(t *testing.T) { template := "layouts/_default/single.html" templateContent := readSource(t, template) templateContent += "{{ print \"Template Changed\"}}" writeSource(t, template, templateContent) }, []fsnotify.Event{{Name: "layouts/_default/single.html", Op: fsnotify.Write}}, func(t *testing.T) { assert.Len(t, enSite.Pages, 4) assert.Len(t, enSite.AllPages, 10) assert.Len(t, frSite.Pages, 4) doc1 := readDestination(t, "public/en/sect/doc1-slug/index.html") assert.True(t, strings.Contains(doc1, "Template Changed"), doc1) }, }, { // Change a language file func(t *testing.T) { languageFile := "i18n/fr.yaml" langContent := readSource(t, languageFile) langContent = strings.Replace(langContent, "Bonjour", "Salut", 1) writeSource(t, languageFile, langContent) }, []fsnotify.Event{{Name: "i18n/fr.yaml", Op: fsnotify.Write}}, func(t *testing.T) { assert.Len(t, enSite.Pages, 4) assert.Len(t, enSite.AllPages, 10) assert.Len(t, frSite.Pages, 4) docEn := readDestination(t, "public/en/sect/doc1-slug/index.html") assert.True(t, strings.Contains(docEn, "Hello"), "No Hello") docFr := readDestination(t, "public/fr/sect/doc1/index.html") assert.True(t, strings.Contains(docFr, "Salut"), "No Salut") homeEn := enSite.getNode("home-0") require.NotNil(t, homeEn) require.Len(t, homeEn.Translations(), 3) require.Equal(t, "fr", homeEn.Translations()[0].Lang()) }, }, } { if this.preFunc != nil { this.preFunc(t) } err = sites.Rebuild(cfg, this.events...) if err != nil { t.Fatalf("[%d] Failed to rebuild sites: %s", i, err) } this.assertFunc(t) } // Check that the drafts etc. are not built/processed/rendered. assertShouldNotBuild(t, sites) } func assertShouldNotBuild(t *testing.T, sites *HugoSites) { s := sites.Sites[0] for _, p := range s.rawAllPages { // No HTML when not processed require.Equal(t, p.shouldBuild(), bytes.Contains(p.rawContent, []byte("