github.com/jekamas/notify@v0.9.1/watcher_recursive_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,!kqueue windows
     6  
     7  package notify
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  )
    13  
    14  // noevent stripts test-case from expected event list, used when action is not
    15  // expected to trigger any events.
    16  func noevent(cas WCase) WCase {
    17  	return WCase{Action: cas.Action}
    18  }
    19  
    20  func TestWatcherRecursiveRewatch(t *testing.T) {
    21  	w := newWatcherTest(t, "testdata/vfs.txt")
    22  	defer w.Close()
    23  
    24  	cases := []WCase{
    25  		create(w, "src/github.com/rjeczalik/file"),
    26  		create(w, "src/github.com/rjeczalik/dir/"),
    27  		noevent(create(w, "src/github.com/rjeczalik/fs/dir/")),
    28  		noevent(create(w, "src/github.com/dir/")),
    29  		noevent(write(w, "src/github.com/rjeczalik/file", []byte("XD"))),
    30  		noevent(rename(w, "src/github.com/rjeczalik/fs/LICENSE", "src/LICENSE")),
    31  	}
    32  
    33  	w.Watch("src/github.com/rjeczalik", Create)
    34  	w.ExpectAny(cases)
    35  
    36  	cases = []WCase{
    37  		create(w, "src/github.com/rjeczalik/fs/file"),
    38  		create(w, "src/github.com/rjeczalik/fs/cmd/gotree/file"),
    39  		create(w, "src/github.com/rjeczalik/fs/cmd/dir/"),
    40  		create(w, "src/github.com/rjeczalik/fs/cmd/gotree/dir/"),
    41  		noevent(write(w, "src/github.com/rjeczalik/fs/file", []byte("XD"))),
    42  		noevent(create(w, "src/github.com/anotherdir/")),
    43  	}
    44  
    45  	w.RecursiveRewatch("src/github.com/rjeczalik", "src/github.com/rjeczalik", Create, Create)
    46  	w.ExpectAny(cases)
    47  
    48  	cases = []WCase{
    49  		create(w, "src/github.com/rjeczalik/1"),
    50  		create(w, "src/github.com/rjeczalik/2/"),
    51  		noevent(create(w, "src/github.com/rjeczalik/fs/cmd/1")),
    52  		noevent(create(w, "src/github.com/rjeczalik/fs/1/")),
    53  		noevent(write(w, "src/github.com/rjeczalik/fs/file", []byte("XD"))),
    54  	}
    55  
    56  	w.Rewatch("src/github.com/rjeczalik", Create, Create)
    57  	w.ExpectAny(cases)
    58  }
    59  
    60  // TODO(rjeczalik): move to watcher_test.go after #5
    61  func TestIsDirCreateEvent(t *testing.T) {
    62  	w := NewWatcherTest(t, "testdata/vfs.txt")
    63  	defer w.Close()
    64  
    65  	cases := [...]WCase{
    66  		// i=0
    67  		create(w, "src/github.com/jszwec/"),
    68  		// i=1
    69  		create(w, "src/github.com/jszwec/gojunitxml/"),
    70  		// i=2
    71  		create(w, "src/github.com/jszwec/gojunitxml/README.md"),
    72  		// i=3
    73  		create(w, "src/github.com/jszwec/gojunitxml/LICENSE"),
    74  		// i=4
    75  		create(w, "src/github.com/jszwec/gojunitxml/cmd/"),
    76  	}
    77  
    78  	dirs := [...]bool{
    79  		true,  // i=0
    80  		true,  // i=1
    81  		false, // i=2
    82  		false, // i=3
    83  		true,  // i=4
    84  	}
    85  
    86  	fn := func(i int, _ WCase, ei EventInfo) error {
    87  		d, ok := ei.(isDirer)
    88  		if !ok {
    89  			return fmt.Errorf("received EventInfo does not implement isDirer")
    90  		}
    91  		switch ok, err := d.isDir(); {
    92  		case err != nil:
    93  			return err
    94  		case ok != dirs[i]:
    95  			return fmt.Errorf("want ok=%v; got %v", dirs[i], ok)
    96  		default:
    97  			return nil
    98  		}
    99  	}
   100  
   101  	w.ExpectAnyFunc(cases[:], fn)
   102  }