aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-07 00:06:07 +0700
committerShulhan <ms@kilabit.info>2023-11-07 00:31:16 +0700
commit064fa7766564bfacafee3c091e364fd264eb62c1 (patch)
tree01360c1fa226491a272ac0b6d7fde798aa704a15 /lib
parent7c1e3582dc14a0de8722f1c6fd8d615dfee8d8ae (diff)
downloadpakakeh.go-064fa7766564bfacafee3c091e364fd264eb62c1.tar.xz
lib/memfs: add test for DirWatcher with symlink to file
Diffstat (limited to 'lib')
-rw-r--r--lib/memfs/.gitignore2
-rw-r--r--lib/memfs/dirwatcher_test.go65
2 files changed, 67 insertions, 0 deletions
diff --git a/lib/memfs/.gitignore b/lib/memfs/.gitignore
new file mode 100644
index 00000000..d1708448
--- /dev/null
+++ b/lib/memfs/.gitignore
@@ -0,0 +1,2 @@
+/testdata/symlinkSource
+/testdata/dirwatcher/withSymlink/symlinkDest
diff --git a/lib/memfs/dirwatcher_test.go b/lib/memfs/dirwatcher_test.go
index 16e94aa4..de379f34 100644
--- a/lib/memfs/dirwatcher_test.go
+++ b/lib/memfs/dirwatcher_test.go
@@ -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)
+}