File: watcher_inotify_test.go

package info (click to toggle)
golang-github-syncthing-notify 0.0~git20180806.b76b458-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 440 kB
  • sloc: makefile: 3
file content (132 lines) | stat: -rw-r--r-- 2,887 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

// +build linux

package notify

import (
	"os"
	"path/filepath"
	"testing"
)

func icreate(w *W, path string) WCase {
	cas := create(w, path)
	cas.Events = append(cas.Events,
		&Call{P: path, E: InCreate},
	)
	return cas
}

func iremove(w *W, path string) WCase {
	cas := remove(w, path)
	cas.Events = append(cas.Events,
		&Call{P: path, E: InDelete},
	)
	return cas
}

func iopen(w *W, path string) WCase {
	return WCase{
		Action: func() {
			f, err := os.OpenFile(filepath.Join(w.root, path), os.O_RDWR, 0644)
			if err != nil {
				w.Fatalf("OpenFile(%q)=%v", path, err)
			}
			if err := f.Close(); err != nil {
				w.Fatalf("Close(%q)=%v", path, err)
			}
		},
		Events: []EventInfo{
			&Call{P: path, E: InAccess},
			&Call{P: path, E: InOpen},
			&Call{P: path, E: InCloseNowrite},
		},
	}
}

func iread(w *W, path string, p []byte) WCase {
	return WCase{
		Action: func() {
			f, err := os.OpenFile(filepath.Join(w.root, path), os.O_RDWR, 0644)
			if err != nil {
				w.Fatalf("OpenFile(%q)=%v", path, err)
			}
			if _, err := f.Read(p); err != nil {
				w.Fatalf("Read(%q)=%v", path, err)
			}
			if err := f.Close(); err != nil {
				w.Fatalf("Close(%q)=%v", path, err)
			}
		},
		Events: []EventInfo{
			&Call{P: path, E: InAccess},
			&Call{P: path, E: InOpen},
			&Call{P: path, E: InModify},
			&Call{P: path, E: InCloseNowrite},
		},
	}
}

func iwrite(w *W, path string, p []byte) WCase {
	cas := write(w, path, p)
	path = cas.Events[0].Path()
	cas.Events = append(cas.Events,
		&Call{P: path, E: InAccess},
		&Call{P: path, E: InOpen},
		&Call{P: path, E: InModify},
		&Call{P: path, E: InCloseWrite},
	)
	return cas
}

func irename(w *W, path string) WCase {
	const ext = ".notify"
	return WCase{
		Action: func() {
			file := filepath.Join(w.root, path)
			if err := os.Rename(file, file+ext); err != nil {
				w.Fatalf("Rename(%q, %q)=%v", path, path+ext, err)
			}
		},
		Events: []EventInfo{
			&Call{P: path, E: InMovedFrom},
			&Call{P: path + ext, E: InMovedTo},
			&Call{P: path, E: InOpen},
			&Call{P: path, E: InAccess},
			&Call{P: path, E: InCreate},
		},
	}
}

var events = []Event{
	InAccess,
	InModify,
	InAttrib,
	InCloseWrite,
	InCloseNowrite,
	InOpen,
	InMovedFrom,
	InMovedTo,
	InCreate,
	InDelete,
	InDeleteSelf,
	InMoveSelf,
}

func TestWatcherInotify(t *testing.T) {
	w := NewWatcherTest(t, "testdata/vfs.txt", events...)
	defer w.Close()

	cases := [...]WCase{
		iopen(w, "src/github.com/rjeczalik/fs/fs.go"),
		iwrite(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")),
		iread(w, "src/github.com/rjeczalik/fs/fs.go", []byte("XD")),
		iremove(w, "src/github.com/ppknap/link/README.md"),
		irename(w, "src/github.com/rjeczalik/fs/LICENSE"),
	}

	w.ExpectAny(cases[:])
}