Fix static sync issue with virtual mounts
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 9 Aug 2019 06:09:15 +0000 (08:09 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 9 Aug 2019 08:18:28 +0000 (10:18 +0200)
Fixes #6165

commands/static_syncer.go
hugofs/rootmapping_fs_test.go
hugolib/filesystems/basefs.go
hugolib/filesystems/basefs_test.go

index 6103fe71c65ef0e49fef8cfeeda640645a37a1c6..ab7cc4fe2a937a8895ddb73e925781fa54337271 100644 (file)
@@ -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
index b3c491f177ddae6d38cee233ea283148f005ecc0..0b3f251f291344a531ef9c01f2b2a748f3136571 100644 (file)
@@ -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) {
index 2d8f54d6504dd926a87f063e126fe89c358f4145..2a1f994b299bc0aaf6279ef02f7e7fc5c70fac47 100644 (file)
@@ -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)
                }
        }
index 3b4e4cd163f74a26c73977bfb743496e73c67fb9..62f968684cc5a2bd70d9fa5b0292672dda90fe87 100644 (file)
@@ -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)