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