github.com/mattolenik/notify@v0.9.1/watcher_kqueue_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 dragonfly freebsd netbsd openbsd
     6  
     7  package notify
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  func kqremove(w *W, path string, files []string) WCase {
    16  	cas := remove(w, path)
    17  	cas.Events[0] = &Call{P: path, E: NoteDelete}
    18  	for _, f := range files {
    19  		cas.Events = append(cas.Events, &Call{P: f, E: NoteDelete})
    20  	}
    21  	return cas
    22  }
    23  
    24  func kqwrite(w *W, path string, p []byte) WCase {
    25  	cas := write(w, path, p)
    26  	path = cas.Events[0].Path()
    27  	cas.Events[0] = &Call{P: path, E: NoteExtend | NoteWrite}
    28  	return cas
    29  }
    30  
    31  func kqrename(w *W, path string, files []string) WCase {
    32  	const ext = ".notify"
    33  	cas := WCase{
    34  		Action: func() {
    35  			file := filepath.Join(w.root, path)
    36  			if err := os.Rename(file, file+ext); err != nil {
    37  				w.Fatalf("Rename(%q, %q)=%v", path, path+ext, err)
    38  			}
    39  		},
    40  		Events: []EventInfo{
    41  			&Call{P: path + ext, E: osSpecificCreate},
    42  			&Call{P: path, E: NoteRename},
    43  		},
    44  	}
    45  	for _, f := range files {
    46  		cas.Events = append(cas.Events, &Call{P: f, E: NoteRename})
    47  	}
    48  	return cas
    49  }
    50  
    51  func kqlink(w *W, path string) WCase {
    52  	const ext = ".notify"
    53  	return WCase{
    54  		Action: func() {
    55  			file := filepath.Join(w.root, path)
    56  			if err := os.Link(file, file+ext); err != nil {
    57  				w.Fatalf("Link(%q, %q)=%v", path, path+ext, err)
    58  			}
    59  		},
    60  		Events: []EventInfo{
    61  			&Call{P: path, E: NoteLink},
    62  			&Call{P: path + ext, E: osSpecificCreate},
    63  		},
    64  	}
    65  }
    66  
    67  var events = []Event{
    68  	NoteWrite,
    69  	NoteAttrib,
    70  	NoteRename,
    71  	osSpecificCreate,
    72  	NoteDelete,
    73  	NoteExtend,
    74  	NoteLink,
    75  }
    76  
    77  func TestWatcherKqueue(t *testing.T) {
    78  	w := NewWatcherTest(t, "testdata/vfs.txt", events...)
    79  	defer w.Close()
    80  
    81  	cases := [...]WCase{
    82  		kqremove(w, "src/github.com/ppknap/link/include/coost/link", []string{
    83  			"src/github.com/ppknap/link/include/coost/link/definitions.hpp",
    84  			"src/github.com/ppknap/link/include/coost/link/detail/bundle.hpp",
    85  			"src/github.com/ppknap/link/include/coost/link/detail/container_invoker.hpp",
    86  			"src/github.com/ppknap/link/include/coost/link/detail/container_value_trait.hpp",
    87  			"src/github.com/ppknap/link/include/coost/link/detail/dummy_type.hpp",
    88  			"src/github.com/ppknap/link/include/coost/link/detail/function_trait.hpp",
    89  			"src/github.com/ppknap/link/include/coost/link/detail/immediate_invoker.hpp",
    90  			"src/github.com/ppknap/link/include/coost/link/detail/stdhelpers/always_same.hpp",
    91  			"src/github.com/ppknap/link/include/coost/link/detail/stdhelpers/make_unique.hpp",
    92  			"src/github.com/ppknap/link/include/coost/link/detail/stdhelpers",
    93  			"src/github.com/ppknap/link/include/coost/link/detail/vertex.hpp",
    94  			"src/github.com/ppknap/link/include/coost/link/detail/wire.hpp",
    95  			"src/github.com/ppknap/link/include/coost/link/detail",
    96  			"src/github.com/ppknap/link/include/coost/link/link.hpp",
    97  		},
    98  		),
    99  		kqwrite(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")),
   100  		kqremove(w, "src/github.com/ppknap/link/README.md", nil),
   101  		kqlink(w, "src/github.com/rjeczalik/fs/LICENSE"),
   102  		kqrename(w, "src/github.com/rjeczalik/fs/fs.go", nil),
   103  		kqrename(w, "src/github.com/rjeczalik/fs/cmd/gotree", []string{
   104  			"src/github.com/rjeczalik/fs/cmd/gotree/go.go",
   105  			"src/github.com/rjeczalik/fs/cmd/gotree/main.go",
   106  		},
   107  		),
   108  	}
   109  
   110  	w.ExpectAll(cases[:])
   111  }