github.com/jmigpin/editor@v1.6.0/core/fswatcher/gwatcher_test.go (about)

     1  package fswatcher
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func TestGWatcher1(t *testing.T) {
    10  	tmpDir := tmpDir()
    11  	defer os.RemoveAll(tmpDir)
    12  
    13  	w := NewGWatcher(mustNewFsnWatcher(t))
    14  	defer w.Close()
    15  
    16  	dir := tmpDir
    17  	dir2 := filepath.Join(dir, "dir2")
    18  	dir3 := filepath.Join(dir2, "dir3")
    19  	dir4 := filepath.Join(dir3, "dir4")
    20  	file1 := filepath.Join(dir4, "file1.txt")
    21  
    22  	mustAddWatch(t, w, dir4)
    23  	mustMkdirAll(t, dir4)
    24  
    25  	readEvent(t, w, true, func(ev *Event) bool {
    26  		return ev.Name == dir4 && ev.Op.HasAny(Create)
    27  	})
    28  
    29  	mustRemoveAll(t, dir2)
    30  
    31  	readEvent(t, w, true, func(ev *Event) bool {
    32  		return ev.Name == dir4 && ev.Op.HasAny(Remove)
    33  	})
    34  
    35  	mustRemoveWatch(t, w, dir4)
    36  	mustAddWatch(t, w, file1)
    37  	mustMkdirAll(t, dir4)
    38  	mustCreateFile(t, file1)
    39  
    40  	readEvent(t, w, true, func(ev *Event) bool {
    41  		return ev.Name == file1 && ev.Op.HasAny(Create)
    42  	})
    43  
    44  	mustRemoveWatch(t, w, file1)
    45  
    46  	s := w.root.n.SprintFlatTree()
    47  	if s != "{/:}" {
    48  		t.Fatalf(s)
    49  	}
    50  }
    51  
    52  func TestGWatcher2(t *testing.T) {
    53  	tmpDir := tmpDir()
    54  	defer os.RemoveAll(tmpDir)
    55  
    56  	w := NewGWatcher(mustNewFsnWatcher(t))
    57  	defer w.Close()
    58  
    59  	dir := tmpDir
    60  	dir2 := filepath.Join(dir, "dir2")
    61  	dir3 := filepath.Join(dir2, "dir3")
    62  	dir4 := filepath.Join(dir3, "dir4")
    63  	file1 := filepath.Join(dir4, "file1.txt")
    64  	file2 := filepath.Join(dir4, "file2.txt")
    65  
    66  	mustAddWatch(t, w, file2)
    67  	mustMkdirAll(t, dir4)
    68  	mustCreateFile(t, file1)
    69  	mustRenameFile(t, file1, file2)
    70  
    71  	readEvent(t, w, true, func(ev *Event) bool {
    72  		return ev.Name == file2 && ev.Op.HasAny(Create)
    73  	})
    74  
    75  	mustRemoveWatch(t, w, file2)
    76  
    77  	mustRemoveAll(t, dir2)
    78  	mustMkdirAll(t, dir4)
    79  	mustCreateFile(t, file1)
    80  
    81  	mustAddWatch(t, w, file2)
    82  	mustRenameFile(t, file1, file2)
    83  
    84  	readEvent(t, w, true, func(ev *Event) bool {
    85  		return ev.Name == file2 && ev.Op.HasAny(Create|Rename)
    86  	})
    87  
    88  	mustRemoveWatch(t, w, file2)
    89  
    90  	s := w.root.n.SprintFlatTree()
    91  	if s != "{/:}" {
    92  		t.Fatalf(s)
    93  	}
    94  }
    95  
    96  func TestGWatcher3(t *testing.T) {
    97  	tmpDir := tmpDir()
    98  	defer os.RemoveAll(tmpDir)
    99  
   100  	w := NewGWatcher(mustNewFsnWatcher(t))
   101  	defer w.Close()
   102  
   103  	dir := tmpDir
   104  	dir2 := filepath.Join(dir, "dir2")
   105  	dir3 := filepath.Join(dir2, "dir3")
   106  	dir4 := filepath.Join(dir3, "dir4")
   107  	file1 := filepath.Join(dir4, "file1.txt")
   108  
   109  	mustAddWatch(t, w, file1)
   110  	mustMkdirAll(t, dir4)
   111  	mustCreateFile(t, file1)
   112  
   113  	readEvent(t, w, true, func(ev *Event) bool {
   114  		return ev.Name == file1 && ev.Op.HasAny(Create)
   115  	})
   116  
   117  	mustWriteFile(t, file1)
   118  
   119  	readEvent(t, w, true, func(ev *Event) bool {
   120  		return ev.Name == file1 && ev.Op.HasAny(Modify)
   121  	})
   122  	// second modify event (truncate/close?)
   123  	readEvent(t, w, false, func(ev *Event) bool {
   124  		return ev.Name == file1 && ev.Op.HasAny(Modify)
   125  	})
   126  
   127  	mustRemoveWatch(t, w, file1)
   128  
   129  	s := w.root.n.SprintFlatTree()
   130  	if s != "{/:}" {
   131  		t.Fatalf(s)
   132  	}
   133  }
   134  
   135  func TestGWatcher4(t *testing.T) {
   136  	tmpDir := tmpDir()
   137  	defer os.RemoveAll(tmpDir)
   138  
   139  	w := NewGWatcher(mustNewFsnWatcher(t))
   140  	defer w.Close()
   141  
   142  	dir := tmpDir
   143  	dir2 := filepath.Join(dir, "dir2")
   144  	dir3 := filepath.Join(dir2, "dir3")
   145  	dir4 := filepath.Join(dir3, "dir4")
   146  	file1 := filepath.Join(dir4, "file1.txt")
   147  	file2 := filepath.Join(dir4, "file2.txt")
   148  
   149  	dir3_ := filepath.Join(dir2, "dir3_")
   150  
   151  	mustMkdirAll(t, dir4)
   152  	mustCreateFile(t, file1)
   153  	mustCreateFile(t, file2)
   154  	mustAddWatch(t, w, file1)
   155  	mustAddWatch(t, w, file2)
   156  	mustRenameFile(t, dir3, dir3_)
   157  
   158  	readEvent(t, w, true, func(ev *Event) bool {
   159  		return (ev.Name == file1 || ev.Name == file2) &&
   160  			ev.Op.HasAny(Remove)
   161  	})
   162  	readEvent(t, w, true, func(ev *Event) bool {
   163  		return (ev.Name == file1 || ev.Name == file2) &&
   164  			ev.Op.HasAny(Remove)
   165  	})
   166  
   167  	mustRenameFile(t, dir3_, dir3)
   168  
   169  	readEvent(t, w, true, func(ev *Event) bool {
   170  		return (ev.Name == file1 || ev.Name == file2) &&
   171  			ev.Op.HasAny(Create)
   172  	})
   173  	readEvent(t, w, true, func(ev *Event) bool {
   174  		return (ev.Name == file1 || ev.Name == file2) &&
   175  			ev.Op.HasAny(Create)
   176  	})
   177  
   178  	mustRemoveWatch(t, w, file1)
   179  	mustRemoveWatch(t, w, file2)
   180  
   181  	s := w.root.n.SprintFlatTree()
   182  	if s != "{/:}" {
   183  		t.Fatalf(s)
   184  	}
   185  }
   186  
   187  func TestGWatcher5(t *testing.T) {
   188  	tmpDir := tmpDir()
   189  	defer os.RemoveAll(tmpDir)
   190  
   191  	w := NewGWatcher(mustNewFsnWatcher(t))
   192  	defer w.Close()
   193  
   194  	dir := tmpDir
   195  	dir2 := filepath.Join(dir, "dir2")
   196  	dir3 := filepath.Join(dir2, "dir3")
   197  	dir4 := filepath.Join(dir3, "dir4")
   198  	file1 := filepath.Join(dir4, "file1.txt")
   199  
   200  	mustMkdirAll(t, dir4)
   201  	mustCreateFile(t, file1)
   202  
   203  	mustAddWatch(t, w, file1)
   204  	mustAddWatch(t, w, file1)
   205  	mustAddWatch(t, w, file1)
   206  
   207  	mustRemoveWatch(t, w, file1)
   208  
   209  	mustRemoveAll(t, dir2)
   210  
   211  	// should have no "remove" events here (watcher added more then once)
   212  
   213  	mustAddWatch(t, w, file1)
   214  	mustMkdirAll(t, dir4)
   215  	mustCreateFile(t, file1)
   216  
   217  	readEvent(t, w, true, func(ev *Event) bool {
   218  		return ev.Name == file1 && ev.Op.HasAny(Create)
   219  	})
   220  
   221  	mustRemoveWatch(t, w, file1)
   222  
   223  	s := w.root.n.SprintFlatTree()
   224  	if s != "{/:}" {
   225  		t.Fatalf(s)
   226  	}
   227  }
   228  
   229  func TestGWatcher6(t *testing.T) {
   230  	tmpDir := tmpDir()
   231  	defer os.RemoveAll(tmpDir)
   232  
   233  	w := NewGWatcher(mustNewFsnWatcher(t))
   234  	defer w.Close()
   235  
   236  	dir := tmpDir
   237  	dir2 := filepath.Join(dir, "dir2")
   238  	dir3 := filepath.Join(dir2, "dir3")
   239  	dir4 := filepath.Join(dir3, "dir4")
   240  	file1 := filepath.Join(dir4, "file1.txt")
   241  
   242  	mustMkdirAll(t, dir4)
   243  	mustCreateFile(t, file1)
   244  
   245  	mustAddWatch(t, w, file1)
   246  	mustRemoveWatch(t, w, file1)
   247  
   248  	s := w.root.n.SprintFlatTree()
   249  	if s != "{/:}" {
   250  		t.Fatalf(s)
   251  	}
   252  }