hugolib: Fix static filesystem for themed multihost sites
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 12 Jul 2018 08:09:32 +0000 (10:09 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 12 Jul 2018 10:18:56 +0000 (12:18 +0200)
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

hugolib/filesystems/basefs.go
hugolib/filesystems/basefs_test.go

index f453e411e885420e3eb506656280ff373366cf9d..1a99da17b4abebc7a877aa3c0b809c5f7f65115d 100644 (file)
@@ -499,7 +499,6 @@ func (b *sourceFilesystemsBuilder) createRootMappingFs(dirKey, themeFolder strin
        s.Fs = afero.NewReadOnlyFs(fs)
 
        return s, nil
-
 }
 
 func (b *sourceFilesystemsBuilder) existsInSource(abspath string) bool {
@@ -536,6 +535,14 @@ func (b *sourceFilesystemsBuilder) createStaticFs() error {
                                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
                        ms[l.Lang] = s
 
index 3e043966fbcb90c7fa5b6da438f560fa6eddecb9..c4da0be8316c72392f1d34951e15c876e972c16e 100644 (file)
@@ -20,6 +20,8 @@ import (
        "path/filepath"
        "testing"
 
+       "github.com/gohugoio/hugo/langs"
+
        "github.com/spf13/afero"
 
        "github.com/gohugoio/hugo/hugofs"
@@ -167,9 +169,6 @@ func TestRealDirs(t *testing.T) {
        }()
 
        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,12 +210,94 @@ func TestRealDirs(t *testing.T) {
 
 }
 
+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) {
        if fs == nil {
                return 0, nil, errors.New("no fs")