Skip to content

Commit

Permalink
lib/memfs: add test for DirWatcher with symlink to file
Browse files Browse the repository at this point in the history
  • Loading branch information
shuLhan committed Nov 6, 2023
1 parent 7c1e358 commit 064fa77
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/memfs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/testdata/symlinkSource
/testdata/dirwatcher/withSymlink/symlinkDest
65 changes: 65 additions & 0 deletions lib/memfs/dirwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,68 @@ func TestDirWatcher_removeDirSymlink(t *testing.T) {
test.Assert(t, `RemoveAll state`, FileStateDeleted, got.State)
test.Assert(t, `RemoveAll path`, `/sub/index.html`, got.Node.Path)
}

func TestDirWatcher_withSymlink(t *testing.T) {
// Initialize the file and directory for symlink.

var (
dirSource = t.TempDir()
dirDest = t.TempDir()
symlinkSource = filepath.Join(dirSource, `symlinkSource`)
symlinkDest = filepath.Join(dirDest, `symlinkDest`)
data = []byte(`content of symlink`)

err error
)

err = os.WriteFile(symlinkSource, data, 0600)
if err != nil {
t.Fatal(err)
}

err = os.Symlink(symlinkSource, symlinkDest)
if err != nil {
t.Fatal(err)
}

// Create the DirWatcher instance and start watching the changes.

var dw = DirWatcher{
Options: Options{
Root: dirDest,
},
Delay: 100 * time.Millisecond,
}

err = dw.Start()
if err != nil {
t.Fatal(err)
}

// Wait for all watcher started.
time.Sleep(500 * time.Millisecond)

var gotns NodeState

// Write to symlink file source.
data = []byte(`new content of symlink`)
err = os.WriteFile(symlinkSource, data, 0600)
if err != nil {
t.Fatal(err)
}

gotns = <-dw.C
test.Assert(t, `path`, `/symlinkDest`, gotns.Node.Path)
test.Assert(t, `state`, FileStateUpdateContent, gotns.State)

// Write to symlink file destination.
data = []byte(`new content of symlink destination`)
err = os.WriteFile(symlinkDest, data, 0600)
if err != nil {
t.Fatal(err)
}

gotns = <-dw.C
test.Assert(t, `path`, `/symlinkDest`, gotns.Node.Path)
test.Assert(t, `state`, FileStateUpdateContent, gotns.State)
}

0 comments on commit 064fa77

Please sign in to comment.