summaryrefslogtreecommitdiffstats
path: root/hugolib/filesystems
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-09 08:09:15 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-09 10:18:28 +0200
commit166a394a2fef6f2990e264cc8dfb722af2cc6a67 (patch)
treeaee96bc2e8a9fe588d9eab40ce5a2a27511a4ba2 /hugolib/filesystems
parent824395204680496d528684587a1f2977394aff3d (diff)
Fix static sync issue with virtual mounts
Fixes #6165
Diffstat (limited to 'hugolib/filesystems')
-rw-r--r--hugolib/filesystems/basefs.go8
-rw-r--r--hugolib/filesystems/basefs_test.go39
2 files changed, 46 insertions, 1 deletions
diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go
index 2d8f54d65..2a1f994b2 100644
--- a/hugolib/filesystems/basefs.go
+++ b/hugolib/filesystems/basefs.go
@@ -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)
}
}
diff --git a/hugolib/filesystems/basefs_test.go b/hugolib/filesystems/basefs_test.go
index 3b4e4cd16..62f968684 100644
--- a/hugolib/filesystems/basefs_test.go
+++ b/hugolib/filesystems/basefs_test.go
@@ -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)