shithub: hugo

Download patch

ref: 80c8f3b81a9849080e64bf877288ede28d960d3f
parent: 6b6dcb44a014699c289bf32fe57d4c4216777be0
author: Bjørn Erik Pedersen <[email protected]>
date: Thu Jul 12 06:09:32 EDT 2018

hugolib: Fix static filesystem for themed multihost sites

Multihost is where each language has its own `baseURL`. In this configuration, static files from the theme was not picked up.

This was a regression in Hugo `0.42`. This commit also adds proper tests for this, so that does not happen again.

Fixes #4929

--- a/hugolib/filesystems/basefs.go
+++ b/hugolib/filesystems/basefs.go
@@ -499,7 +499,6 @@
 	s.Fs = afero.NewReadOnlyFs(fs)
 
 	return s, nil
-
 }
 
 func (b *sourceFilesystemsBuilder) existsInSource(abspath string) bool {
@@ -534,6 +533,14 @@
 			fs, err := createOverlayFs(b.p.Fs.Source, s.Dirnames)
 			if err != nil {
 				return err
+			}
+
+			if b.hasTheme {
+				themeFolder := "static"
+				fs = afero.NewCopyOnWriteFs(newRealBase(afero.NewBasePathFs(b.themeFs, themeFolder)), fs)
+				for _, absThemeDir := range b.absThemeDirs {
+					s.Dirnames = append(s.Dirnames, filepath.Join(absThemeDir, themeFolder))
+				}
 			}
 
 			s.Fs = fs
--- a/hugolib/filesystems/basefs_test.go
+++ b/hugolib/filesystems/basefs_test.go
@@ -20,6 +20,8 @@
 	"path/filepath"
 	"testing"
 
+	"github.com/gohugoio/hugo/langs"
+
 	"github.com/spf13/afero"
 
 	"github.com/gohugoio/hugo/hugofs"
@@ -167,9 +169,6 @@
 	}()
 
 	v.Set("workingDir", root)
-	v.Set("contentDir", "content")
-	v.Set("resourceDir", "resources")
-	v.Set("publishDir", "public")
 	v.Set("themesDir", themesDir)
 	v.Set("theme", "mytheme")
 
@@ -211,10 +210,92 @@
 
 }
 
+func TestStaticFs(t *testing.T) {
+	assert := require.New(t)
+	v := createConfig()
+	workDir := "mywork"
+	v.Set("workingDir", workDir)
+	v.Set("themesDir", "themes")
+	v.Set("theme", "t1")
+
+	fs := hugofs.NewMem(v)
+
+	themeStaticDir := filepath.Join(workDir, "themes", "t1", "static")
+
+	afero.WriteFile(fs.Source, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0755)
+	afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0755)
+	afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0755)
+
+	p, err := paths.New(fs, v)
+	assert.NoError(err)
+	bfs, err := NewBase(p)
+	sfs := bfs.StaticFs("en")
+	checkFileContent(sfs, "f1.txt", assert, "Hugo Rocks!")
+	checkFileContent(sfs, "f2.txt", assert, "Hugo Themes Still Rocks!")
+
+}
+
+func TestStaticFsMultiHost(t *testing.T) {
+	assert := require.New(t)
+	v := createConfig()
+	workDir := "mywork"
+	v.Set("workingDir", workDir)
+	v.Set("themesDir", "themes")
+	v.Set("theme", "t1")
+	v.Set("multihost", true)
+
+	vn := viper.New()
+	vn.Set("staticDir", "nn_static")
+
+	en := langs.NewLanguage("en", v)
+	no := langs.NewLanguage("no", v)
+	no.Set("staticDir", "static_no")
+
+	languages := langs.Languages{
+		en,
+		no,
+	}
+
+	v.Set("languagesSorted", languages)
+
+	fs := hugofs.NewMem(v)
+
+	themeStaticDir := filepath.Join(workDir, "themes", "t1", "static")
+
+	afero.WriteFile(fs.Source, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0755)
+	afero.WriteFile(fs.Source, filepath.Join(workDir, "static_no", "f1.txt"), []byte("Hugo Rocks in Norway!"), 0755)
+
+	afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0755)
+	afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0755)
+
+	p, err := paths.New(fs, v)
+	assert.NoError(err)
+	bfs, err := NewBase(p)
+	enFs := bfs.StaticFs("en")
+	checkFileContent(enFs, "f1.txt", assert, "Hugo Rocks!")
+	checkFileContent(enFs, "f2.txt", assert, "Hugo Themes Still Rocks!")
+
+	noFs := bfs.StaticFs("no")
+	checkFileContent(noFs, "f1.txt", assert, "Hugo Rocks in Norway!")
+	checkFileContent(noFs, "f2.txt", assert, "Hugo Themes Still Rocks!")
+}
+
 func checkFileCount(fs afero.Fs, dirname string, assert *require.Assertions, expected int) {
 	count, _, err := countFileaAndGetDirs(fs, dirname)
 	assert.NoError(err)
 	assert.Equal(expected, count)
+}
+
+func checkFileContent(fs afero.Fs, filename string, assert *require.Assertions, expected ...string) {
+
+	b, err := afero.ReadFile(fs, filename)
+	assert.NoError(err)
+
+	content := string(b)
+
+	for _, e := range expected {
+		assert.Contains(content, e)
+	}
 }
 
 func countFileaAndGetDirs(fs afero.Fs, dirname string) (int, []string, error) {