Fix live reload mount logic with sub paths
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 15 Aug 2019 14:44:44 +0000 (16:44 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 15 Aug 2019 17:14:22 +0000 (19:14 +0200)
Fixes #6209

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

index 2a1f994b299bc0aaf6279ef02f7e7fc5c70fac47..de6baa130d721cabdee18eb830935fa92593db79 100644 (file)
@@ -243,11 +243,11 @@ func (d *SourceFilesystem) MakePathRelative(filename string) string {
                currentPath := meta.Filename()
 
                if strings.HasPrefix(filename, currentPath) {
-                       if path := meta.Path(); path != "" {
-                               currentPath = strings.TrimRight(strings.TrimSuffix(currentPath, path), filePathSeparator)
+                       rel := strings.TrimPrefix(filename, currentPath)
+                       if mp := meta.Path(); mp != "" {
+                               rel = filepath.Join(mp, rel)
                        }
-
-                       return strings.TrimPrefix(filename, currentPath)
+                       return strings.TrimPrefix(rel, filePathSeparator)
                }
        }
        return ""
index 47aef63cea2d9c14f41995f9c62b385f2560417d..3cac4f11a672b88d8494fb644c4f930024a70be0 100644 (file)
@@ -360,14 +360,19 @@ func TestMakePathRelative(t *testing.T) {
        workDir := "mywork"
        v.Set("workingDir", workDir)
 
-       c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "dist"), 0777), qt.IsNil)
-       c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "static"), 0777), qt.IsNil)
+       c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "dist", "d1"), 0777), qt.IsNil)
+       c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "static", "d2"), 0777), qt.IsNil)
+       c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "dust", "d2"), 0777), qt.IsNil)
 
        moduleCfg := map[string]interface{}{
                "mounts": []interface{}{
                        map[string]interface{}{
                                "source": "dist",
-                               "target": "static/dist",
+                               "target": "static/mydist",
+                       },
+                       map[string]interface{}{
+                               "source": "dust",
+                               "target": "static/foo/bar",
                        },
                        map[string]interface{}{
                                "source": "static",
@@ -388,8 +393,10 @@ func TestMakePathRelative(t *testing.T) {
        sfs := bfs.Static[""]
        c.Assert(sfs, qt.Not(qt.IsNil))
 
-       c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "static", "foo.txt")), qt.Equals, filepath.FromSlash("/foo.txt"))
-       c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "dist", "foo.txt")), qt.Equals, filepath.FromSlash("/dist/foo.txt"))
+       c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "dist", "d1", "foo.txt")), qt.Equals, filepath.FromSlash("mydist/d1/foo.txt"))
+       c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "static", "d2", "foo.txt")), qt.Equals, filepath.FromSlash("d2/foo.txt"))
+       c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "dust", "d3", "foo.txt")), qt.Equals, filepath.FromSlash("foo/bar/d3/foo.txt"))
+
 }
 
 func checkFileCount(fs afero.Fs, dirname string, c *qt.C, expected int) {