Coverage for gws-app/gws/lib/watcher/_test.py: 100%
92 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
1import gws
2import gws.test.util as u
3import gws.lib.watcher as watcher
6def _mkdir(p):
7 p.mkdir(parents=True)
8 return p
11def _touch(p):
12 p.touch()
13 gws.u.sleep(0.05)
14 return p
17def _change(p):
18 p.write_text("test")
19 gws.u.sleep(0.05)
20 return p
23@u.fixture
24def cb():
25 class Callback:
26 def __init__(self):
27 self.changes = []
29 def __call__(self, *args):
30 self.changes.append(args)
32 def changed(self):
33 yes = bool(self.changes)
34 self.changes = []
35 return yes
37 return Callback()
40def test_simple(tmp_path, cb):
41 d1 = _mkdir(tmp_path / '1')
42 d2 = _mkdir(tmp_path / '2')
44 w = watcher.new(cb)
45 w.add_directory(d1)
46 w.add_directory(d2)
48 w.start()
50 _touch(d1 / 'a')
51 assert cb.changed()
53 _touch(d2 / 'b')
54 assert cb.changed()
56 w.stop()
59def test_patterns(tmp_path, cb):
60 d = _mkdir(tmp_path / '1')
62 w = watcher.new(cb)
63 w.add_directory(d, file_pattern=r'\.txt$')
65 w.start()
67 _touch(d / 'a.txt')
68 assert cb.changed()
70 _touch(d / 'a.foo')
71 assert not cb.changed()
73 w.stop()
76def test_exclude(tmp_path, cb):
77 d = _mkdir(tmp_path / '1')
79 w = watcher.new(cb)
80 w.add_directory(d)
81 w.exclude('ignore_me')
83 w.start()
85 a = _touch(d / 'not_ignored')
86 assert cb.changed()
87 _change(a)
88 assert cb.changed()
90 a = _touch(d / 'ignore_me')
91 assert not cb.changed()
92 _change(a)
93 assert not cb.changed()
96def test_files(tmp_path, cb):
97 d = _mkdir(tmp_path / '1')
99 w = watcher.new(cb)
100 w.add_file(d / 'watch_me_1')
101 w.add_file(d / 'watch_me_2')
103 w.start()
105 a = _touch(d / 'watch_me_1')
106 assert cb.changed()
107 _change(a)
108 assert cb.changed()
110 a = _touch(d / 'watch_me_2')
111 assert cb.changed()
112 _change(a)
113 assert cb.changed()
115 a = _touch(d / 'ignore_me')
116 assert not cb.changed()
117 _change(a)
118 assert not cb.changed()
120 w.stop()
123def test_recursive(tmp_path, cb):
124 a = _mkdir(tmp_path / 'aaa')
125 deep = _mkdir(a / 'bbb' / 'ccc')
127 w = watcher.new(cb)
128 w.add_directory(a, file_pattern=r'\.txt$', recursive=True)
130 w.start()
132 _touch(deep / 'a.txt')
133 assert cb.changed()
135 _touch(deep / 'a.foo')
136 assert not cb.changed()
138 w.stop()