| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package notify |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 61 |
func TestIsDirCreateEvent(t *testing.T) { |
| 62 |
w := NewWatcherTest(t, "testdata/vfs.txt") |
| 63 |
defer w.Close() |
| 64 |
|
| 65 |
cases := [...]WCase{ |
| 66 |
|
| 67 |
create(w, "src/github.com/jszwec/"), |
| 68 |
|
| 69 |
create(w, "src/github.com/jszwec/gojunitxml/"), |
| 70 |
|
| 71 |
create(w, "src/github.com/jszwec/gojunitxml/README.md"), |
| 72 |
|
| 73 |
create(w, "src/github.com/jszwec/gojunitxml/LICENSE"), |
| 74 |
|
| 75 |
create(w, "src/github.com/jszwec/gojunitxml/cmd/"), |
| 76 |
} |
| 77 |
|
| 78 |
dirs := [...]bool{ |
| 79 |
true, |
| 80 |
true, |
| 81 |
false, |
| 82 |
false, |
| 83 |
true, |
| 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 |
} |