github.com/Hellyna/notify@v0.0.0-20210101060149-8ebdd4ef22cf/watcher_test.go (about)

     1  // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be
     3  // found in the LICENSE file.
     4  
     5  // +build darwin linux freebsd dragonfly netbsd openbsd windows solaris
     6  
     7  package notify
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  // NOTE Set NOTIFY_DEBUG env var or debug build tag for extra debugging info.
    16  
    17  func TestWatcher(t *testing.T) {
    18  	w := NewWatcherTest(t, "testdata/vfs.txt")
    19  	defer w.Close()
    20  
    21  	cases := [...]WCase{
    22  		create(w, "src/github.com/ppknap/link/include/coost/.link.hpp.swp"),
    23  		create(w, "src/github.com/rjeczalik/fs/fs_test.go"),
    24  		create(w, "src/github.com/rjeczalik/fs/binfs/"),
    25  		create(w, "src/github.com/rjeczalik/fs/binfs.go"),
    26  		create(w, "src/github.com/rjeczalik/fs/binfs_test.go"),
    27  		remove(w, "src/github.com/rjeczalik/fs/binfs/"),
    28  		create(w, "src/github.com/rjeczalik/fs/binfs/"),
    29  		create(w, "src/github.com/rjeczalik/fs/virfs"),
    30  		remove(w, "src/github.com/rjeczalik/fs/virfs"),
    31  		create(w, "file"),
    32  		create(w, "dir/"),
    33  	}
    34  
    35  	w.ExpectAny(cases[:])
    36  }
    37  
    38  // Simulates the scenario, where outside of the programs control the base dir
    39  // is removed. This is detected and the watch removed. Then the directory is
    40  // restored and a new watch set up.
    41  func TestStopPathNotExists(t *testing.T) {
    42  	w := NewWatcherTest(t, "testdata/vfs.txt")
    43  	defer w.Close()
    44  
    45  	if err := os.RemoveAll(w.root); err != nil {
    46  		panic(err)
    47  	}
    48  	Sync()
    49  
    50  	// Don't check the returned error, as the public function (notify.Stop)
    51  	// does not return a potential error. As long as everything later on
    52  	// works as inteded, that's fine
    53  	time.Sleep(time.Duration(100) * time.Millisecond)
    54  	w.Watcher.Unwatch(w.root)
    55  	time.Sleep(time.Duration(100) * time.Millisecond)
    56  
    57  	if err := os.Mkdir(w.root, 0777); err != nil {
    58  		panic(err)
    59  	}
    60  	Sync()
    61  	w.Watch("", All)
    62  
    63  	drainall(w.C)
    64  	cases := [...]WCase{
    65  		create(w, "file"),
    66  		create(w, "dir/"),
    67  	}
    68  	w.ExpectAny(cases[:])
    69  }
    70  
    71  func TestWatcherUnwatch(t *testing.T) {
    72  	w := NewWatcherTest(t, "testdata/vfs.txt")
    73  	defer w.Close()
    74  
    75  	remove(w, "src/github.com/ppknap/link/test/test_circular_calls.cpp").Action()
    76  	w.Unwatch("")
    77  
    78  	w.Watch("", All)
    79  
    80  	drainall(w.C)
    81  	cases := [...]WCase{
    82  		create(w, "file"),
    83  		create(w, "dir/"),
    84  	}
    85  	w.ExpectAny(cases[:])
    86  }