Skip to content

Commit

Permalink
Fix static sync issue with virtual mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 9, 2019
1 parent 7ff0a8e commit d0f1307
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions commands/static_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error {
fromPath := ev.Name

relPath := sourceFs.MakePathRelative(fromPath)

if relPath == "" {
// Not member of this virtual host.
continue
Expand Down
4 changes: 4 additions & 0 deletions hugofs/rootmapping_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ func TestRootMappingFsMountOverlap(t *testing.T) {
assert.Equal([]string{"b.txt", "c"}, getDirnames("static/b"))
assert.Equal([]string{"c.txt"}, getDirnames("static/b/c"))

fi, err := rfs.Stat(filepath.FromSlash("static/b/b.txt"))
assert.NoError(err)
assert.Equal("b.txt", fi.Name())

}

func TestRootMappingFsOs(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion hugolib/filesystems/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,14 @@ func (s SourceFilesystems) MakeStaticPathRelative(filename string) string {
// It will return an empty string if the filename is not a member of this filesystem.
func (d *SourceFilesystem) MakePathRelative(filename string) string {
for _, dir := range d.Dirs {
currentPath := dir.(hugofs.FileMetaInfo).Meta().Filename()
meta := dir.(hugofs.FileMetaInfo).Meta()
currentPath := meta.Filename()

if strings.HasPrefix(filename, currentPath) {
if path := meta.Path(); path != "" {
currentPath = strings.TrimRight(strings.TrimSuffix(currentPath, path), filePathSeparator)
}

return strings.TrimPrefix(filename, currentPath)
}
}
Expand Down
39 changes: 39 additions & 0 deletions hugolib/filesystems/basefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,45 @@ func TestStaticFsMultiHost(t *testing.T) {
checkFileContent(noFs, "f2.txt", assert, "Hugo Themes Still Rocks!")
}

func TestMakePathRelative(t *testing.T) {
assert := require.New(t)
v := createConfig()
fs := hugofs.NewMem(v)
workDir := "mywork"
v.Set("workingDir", workDir)

assert.NoError(fs.Source.MkdirAll(filepath.Join(workDir, "dist"), 0777))
assert.NoError(fs.Source.MkdirAll(filepath.Join(workDir, "static"), 0777))

moduleCfg := map[string]interface{}{
"mounts": []interface{}{
map[string]interface{}{
"source": "dist",
"target": "static/dist",
},
map[string]interface{}{
"source": "static",
"target": "static",
},
},
}

v.Set("module", moduleCfg)

assert.NoError(initConfig(fs.Source, v))

p, err := paths.New(fs, v)
assert.NoError(err)
bfs, err := NewBase(p, nil)
assert.NoError(err)

sfs := bfs.Static[""]
assert.NotNil(sfs)

assert.Equal(filepath.FromSlash("/foo.txt"), sfs.MakePathRelative(filepath.Join(workDir, "static", "foo.txt")))
assert.Equal(filepath.FromSlash("/dist/foo.txt"), sfs.MakePathRelative(filepath.Join(workDir, "dist", "foo.txt")))
}

func checkFileCount(fs afero.Fs, dirname string, assert *require.Assertions, expected int) {
count, fnames, err := countFileaAndGetFilenames(fs, dirname)
assert.NoError(err, fnames)
Expand Down

0 comments on commit d0f1307

Please sign in to comment.