github.com/Hellyna/notify@v0.0.0-20210101060149-8ebdd4ef22cf/watcher_fsevents_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,!ios,cgo
     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  		// 1.
    46  		{
    47  			{"file", create | write, create | write},
    48  			{"file", create | write | inode, write | inode},
    49  		},
    50  		// 2.
    51  		{
    52  			{"file", create, create},
    53  			{"file", create | remove, remove},
    54  			{"file", create | remove, create},
    55  		},
    56  		// 3.
    57  		{
    58  			{"file", create | write, create | write},
    59  			{"file", create | write | owner, write | owner},
    60  		},
    61  		// 4.
    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  // Test for cases 3) and 5) with shadowed write&create events.
    89  //
    90  // See comment for (flagdiff).diff method.
    91  func TestWatcherShadowedWriteCreate(t *testing.T) {
    92  	w := NewWatcherTest(t, "testdata/vfs.txt")
    93  	defer w.Close()
    94  
    95  	cases := [...]WCase{
    96  		// i=0
    97  		create(w, "src/github.com/rjeczalik/fs/.fs.go.swp"),
    98  		// i=1
    99  		write(w, "src/github.com/rjeczalik/fs/.fs.go.swp", []byte("XD")),
   100  		// i=2
   101  		write(w, "src/github.com/rjeczalik/fs/.fs.go.swp", []byte("XD")),
   102  		// i=3
   103  		remove(w, "src/github.com/rjeczalik/fs/.fs.go.swp"),
   104  		// i=4
   105  		create(w, "src/github.com/rjeczalik/fs/.fs.go.swp"),
   106  		// i=5
   107  		write(w, "src/github.com/rjeczalik/fs/.fs.go.swp", []byte("XD")),
   108  	}
   109  
   110  	w.ExpectAny(cases[:5]) // BUG(rjeczalik): #62
   111  }