| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package notify |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestSplitflags(t *testing.T) { |
| 15 |
cases := [...]struct { |
| 16 |
set uint32 |
| 17 |
flags []uint32 |
| 18 |
}{ |
| 19 |
{0, nil}, |
| 20 |
{0xD, []uint32{0x1, 0x4, 0x8}}, |
| 21 |
{0x0010 | 0x0040 | 0x0080 | 0x01000, []uint32{0x0010, 0x0040, 0x0080, 0x01000}}, |
| 22 |
{0x40000 | 0x00100 | 0x00200, []uint32{0x00100, 0x00200, 0x40000}}, |
| 23 |
} |
| 24 |
for i, cas := range cases { |
| 25 |
if flags := splitflags(cas.set); !reflect.DeepEqual(flags, cas.flags) { |
| 26 |
t.Errorf("want flags=%v; got %v (i=%d)", cas.flags, flags, i) |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestWatchStrip(t *testing.T) { |
| 32 |
const ( |
| 33 |
create = uint32(FSEventsCreated) |
| 34 |
remove = uint32(FSEventsRemoved) |
| 35 |
rename = uint32(FSEventsRenamed) |
| 36 |
write = uint32(FSEventsModified) |
| 37 |
inode = uint32(FSEventsInodeMetaMod) |
| 38 |
owner = uint32(FSEventsChangeOwner) |
| 39 |
) |
| 40 |
cases := [...][]struct { |
| 41 |
path string |
| 42 |
flag uint32 |
| 43 |
diff uint32 |
| 44 |
}{ |
| 45 |
|
| 46 |
{ |
| 47 |
{"file", create | write, create | write}, |
| 48 |
{"file", create | write | inode, write | inode}, |
| 49 |
}, |
| 50 |
|
| 51 |
{ |
| 52 |
{"file", create, create}, |
| 53 |
{"file", create | remove, remove}, |
| 54 |
{"file", create | remove, create}, |
| 55 |
}, |
| 56 |
|
| 57 |
{ |
| 58 |
{"file", create | write, create | write}, |
| 59 |
{"file", create | write | owner, write | owner}, |
| 60 |
}, |
| 61 |
|
| 62 |
{ |
| 63 |
{"file", create | write, create | write}, |
| 64 |
{"file", write | inode, write | inode}, |
| 65 |
{"file", remove | write | inode, remove}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
{"file", remove | write | inode, remove}, |
| 69 |
}, |
| 70 |
} |
| 71 |
Test: |
| 72 |
for i, cas := range cases { |
| 73 |
if len(cas) == 0 { |
| 74 |
t.Log("skipped") |
| 75 |
continue |
| 76 |
} |
| 77 |
w := &watch{prev: make(map[string]uint32)} |
| 78 |
for j, cas := range cas { |
| 79 |
if diff := w.strip(cas.path, cas.flag); diff != cas.diff { |
| 80 |
t.Errorf("want diff=%v; got %v (i=%d, j=%d)", Event(cas.diff), |
| 81 |
Event(diff), i, j) |
| 82 |
continue Test |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
func TestWatcherShadowedWriteCreate(t *testing.T) { |
| 92 |
w := NewWatcherTest(t, "testdata/vfs.txt") |
| 93 |
defer w.Close() |
| 94 |
|
| 95 |
cases := [...]WCase{ |
| 96 |
|
| 97 |
create(w, "src/github.com/rjeczalik/fs/.fs.go.swp"), |
| 98 |
|
| 99 |
write(w, "src/github.com/rjeczalik/fs/.fs.go.swp", []byte("XD")), |
| 100 |
|
| 101 |
write(w, "src/github.com/rjeczalik/fs/.fs.go.swp", []byte("XD")), |
| 102 |
|
| 103 |
remove(w, "src/github.com/rjeczalik/fs/.fs.go.swp"), |
| 104 |
|
| 105 |
create(w, "src/github.com/rjeczalik/fs/.fs.go.swp"), |
| 106 |
|
| 107 |
write(w, "src/github.com/rjeczalik/fs/.fs.go.swp", []byte("XD")), |
| 108 |
} |
| 109 |
|
| 110 |
w.ExpectAny(cases[:5]) |
| 111 |
} |