File: watcher_recursive_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 (102 lines) | stat: -rw-r--r-- 2,784 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
// 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 darwin,!kqueue windows

package notify

import (
	"fmt"
	"testing"
)

// noevent stripts test-case from expected event list, used when action is not
// expected to trigger any events.
func noevent(cas WCase) WCase {
	return WCase{Action: cas.Action}
}

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

	cases := []WCase{
		create(w, "src/github.com/rjeczalik/file"),
		create(w, "src/github.com/rjeczalik/dir/"),
		noevent(create(w, "src/github.com/rjeczalik/fs/dir/")),
		noevent(create(w, "src/github.com/dir/")),
		noevent(write(w, "src/github.com/rjeczalik/file", []byte("XD"))),
		noevent(rename(w, "src/github.com/rjeczalik/fs/LICENSE", "src/LICENSE")),
	}

	w.Watch("src/github.com/rjeczalik", Create)
	w.ExpectAny(cases)

	cases = []WCase{
		create(w, "src/github.com/rjeczalik/fs/file"),
		create(w, "src/github.com/rjeczalik/fs/cmd/gotree/file"),
		create(w, "src/github.com/rjeczalik/fs/cmd/dir/"),
		create(w, "src/github.com/rjeczalik/fs/cmd/gotree/dir/"),
		noevent(write(w, "src/github.com/rjeczalik/fs/file", []byte("XD"))),
		noevent(create(w, "src/github.com/anotherdir/")),
	}

	w.RecursiveRewatch("src/github.com/rjeczalik", "src/github.com/rjeczalik", Create, Create)
	w.ExpectAny(cases)

	cases = []WCase{
		create(w, "src/github.com/rjeczalik/1"),
		create(w, "src/github.com/rjeczalik/2/"),
		noevent(create(w, "src/github.com/rjeczalik/fs/cmd/1")),
		noevent(create(w, "src/github.com/rjeczalik/fs/1/")),
		noevent(write(w, "src/github.com/rjeczalik/fs/file", []byte("XD"))),
	}

	w.Rewatch("src/github.com/rjeczalik", Create, Create)
	w.ExpectAny(cases)
}

// TODO(rjeczalik): move to watcher_test.go after #5
func TestIsDirCreateEvent(t *testing.T) {
	w := NewWatcherTest(t, "testdata/vfs.txt")
	defer w.Close()

	cases := [...]WCase{
		// i=0
		create(w, "src/github.com/jszwec/"),
		// i=1
		create(w, "src/github.com/jszwec/gojunitxml/"),
		// i=2
		create(w, "src/github.com/jszwec/gojunitxml/README.md"),
		// i=3
		create(w, "src/github.com/jszwec/gojunitxml/LICENSE"),
		// i=4
		create(w, "src/github.com/jszwec/gojunitxml/cmd/"),
	}

	dirs := [...]bool{
		true,  // i=0
		true,  // i=1
		false, // i=2
		false, // i=3
		true,  // i=4
	}

	fn := func(i int, _ WCase, ei EventInfo) error {
		d, ok := ei.(isDirer)
		if !ok {
			return fmt.Errorf("received EventInfo does not implement isDirer")
		}
		switch ok, err := d.isDir(); {
		case err != nil:
			return err
		case ok != dirs[i]:
			return fmt.Errorf("want ok=%v; got %v", dirs[i], ok)
		default:
			return nil
		}
	}

	w.ExpectAnyFunc(cases[:], fn)
}