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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
import re
import sys
import threading
from pathlib import Path
from time import sleep
import anyio
import pytest
from watchgod import AllWatcher, Change, DefaultWatcher, PythonWatcher, RegExpWatcher, awatch, watch
from .conftest import mktree
skip_on_windows = pytest.mark.skipif(sys.platform == 'win32', reason='fails on windows')
skip_unless_linux = pytest.mark.skipif(sys.platform != 'linux', reason='test only on linux')
tree = {
'foo': {
'bar.txt': 'bar',
'spam.py': 'whatever',
'spam.pyc': 'splosh',
'recursive_dir': {
'a.js': 'boom',
},
'.git': {
'x': 'y',
},
}
}
def test_add(tmp_path):
watcher = AllWatcher(tmp_path)
changes = watcher.check()
assert changes == set()
sleep(0.01)
(tmp_path / 'foo.txt').write_text('foobar')
changes = watcher.check()
assert changes == {(Change.added, str((tmp_path / 'foo.txt')))}
def test_add_watched_file(tmp_path):
file = tmp_path / 'bar.txt'
watcher = AllWatcher(file)
assert watcher.check() == set()
sleep(0.01)
file.write_text('foobar')
assert watcher.check() == {(Change.added, str(file))}
def test_modify(tmp_path: Path):
mktree(tmp_path, tree)
watcher = AllWatcher(tmp_path)
assert watcher.check() == set()
sleep(0.01)
(tmp_path / 'foo/bar.txt').write_text('foobar')
assert watcher.check() == {(Change.modified, str(tmp_path / 'foo/bar.txt'))}
@skip_on_windows
def test_ignore_root(tmp_path):
mktree(tmp_path, tree)
watcher = AllWatcher(tmp_path, ignored_paths={str(tmp_path / 'foo')})
assert watcher.check() == set()
sleep(0.01)
(tmp_path / 'foo/bar.txt').write_text('foobar')
assert watcher.check() == set()
@skip_on_windows
def test_ignore_file_path(tmp_path):
mktree(tmp_path, tree)
watcher = AllWatcher(tmp_path, ignored_paths={str(tmp_path / 'foo' / 'bar.txt')})
assert watcher.check() == set()
sleep(0.01)
(tmp_path / 'foo' / 'bar.txt').write_text('foobar')
(tmp_path / 'foo' / 'new_not_ignored.txt').write_text('foobar')
(tmp_path / 'foo' / 'spam.py').write_text('foobar')
assert watcher.check() == {
(Change.added, str(tmp_path / 'foo' / 'new_not_ignored.txt')),
(Change.modified, str(tmp_path / 'foo' / 'spam.py')),
}
@skip_on_windows
def test_ignore_subdir(tmp_path):
mktree(tmp_path, tree)
watcher = AllWatcher(tmp_path, ignored_paths={str(tmp_path / 'dir' / 'ignored')})
assert watcher.check() == set()
sleep(0.01)
(tmp_path / 'dir').mkdir()
(tmp_path / 'dir' / 'ignored').mkdir()
(tmp_path / 'dir' / 'not_ignored').mkdir()
(tmp_path / 'dir' / 'ignored' / 'file.txt').write_text('content')
(tmp_path / 'dir' / 'not_ignored' / 'file.txt').write_text('content')
assert watcher.check() == {(Change.added, str(tmp_path / 'dir' / 'not_ignored' / 'file.txt'))}
def test_modify_watched_file(tmp_path):
file = tmp_path / 'bar.txt'
file.write_text('foobar')
watcher = AllWatcher(file)
assert watcher.check() == set()
sleep(0.01)
file.write_text('foobar')
assert watcher.check() == {(Change.modified, str(file))} # same content but time updated
sleep(0.01)
file.write_text('baz')
assert watcher.check() == {(Change.modified, str(file))}
def test_delete(tmp_path):
mktree(tmp_path, tree)
watcher = AllWatcher(tmp_path)
sleep(0.01)
(tmp_path / 'foo/bar.txt').unlink()
assert watcher.check() == {(Change.deleted, str((tmp_path / 'foo/bar.txt')))}
def test_delete_watched_file(tmp_path):
file = tmp_path / 'bar.txt'
file.write_text('foobar')
watcher = AllWatcher(file)
assert watcher.check() == set()
sleep(0.01)
file.unlink()
assert watcher.check() == {(Change.deleted, str(file))}
def test_ignore_file(tmp_path):
mktree(tmp_path, tree)
watcher = DefaultWatcher(tmp_path)
sleep(0.01)
(tmp_path / 'foo/spam.pyc').write_text('foobar')
assert watcher.check() == set()
def test_ignore_dir(tmp_path):
mktree(tmp_path, tree)
watcher = DefaultWatcher(tmp_path)
sleep(0.01)
(tmp_path / 'foo/.git/abc').write_text('xxx')
assert watcher.check() == set()
def test_python(tmp_path):
mktree(tmp_path, tree)
watcher = PythonWatcher(tmp_path)
sleep(0.01)
(tmp_path / 'foo/spam.py').write_text('xxx')
(tmp_path / 'foo/bar.txt').write_text('xxx')
(tmp_path / 'foo/spam.md').write_text('xxx')
assert watcher.check() == {(Change.modified, str(tmp_path / 'foo/spam.py'))}
def test_python_extensions(tmp_path):
mktree(tmp_path, tree)
watcher = PythonWatcher(tmp_path, extra_extensions=('.md',))
sleep(0.01)
(tmp_path / 'foo/spam.py').write_text('xxx')
(tmp_path / 'foo/bar.txt').write_text('xxx')
(tmp_path / 'foo/spam.md').write_text('xxx')
assert watcher.check() == {
(Change.modified, str(tmp_path / 'foo/spam.py')),
(Change.added, str(tmp_path / 'foo/spam.md')),
}
def test_regexp(tmp_path):
mktree(tmp_path, tree)
re_files = r'^.*(\.txt|\.js)$'
re_dirs = r'^(?:(?!recursive_dir).)*$'
watcher = RegExpWatcher(tmp_path, re_files, re_dirs)
changes = watcher.check()
assert changes == set()
sleep(0.01)
(tmp_path / 'foo/spam.py').write_text('xxx')
(tmp_path / 'foo/bar.txt').write_text('change')
(tmp_path / 'foo/borec.txt').write_text('ahoy')
(tmp_path / 'foo/borec-js.js').write_text('peace')
(tmp_path / 'foo/recursive_dir/b.js').write_text('borec')
assert watcher.check() == {
(Change.modified, str((tmp_path / 'foo/bar.txt'))),
(Change.added, str((tmp_path / 'foo/borec.txt'))),
(Change.added, str((tmp_path / 'foo/borec-js.js'))),
}
def test_regexp_no_re_dirs(tmp_path):
mktree(tmp_path, tree)
re_files = r'^.*(\.txt|\.js)$'
watcher_no_re_dirs = RegExpWatcher(tmp_path, re_files)
changes = watcher_no_re_dirs.check()
assert changes == set()
sleep(0.01)
(tmp_path / 'foo/spam.py').write_text('xxx')
(tmp_path / 'foo/bar.txt').write_text('change')
(tmp_path / 'foo/recursive_dir/foo.js').write_text('change')
assert watcher_no_re_dirs.check() == {
(Change.modified, str((tmp_path / 'foo/bar.txt'))),
(Change.added, str((tmp_path / 'foo/recursive_dir/foo.js'))),
}
def test_regexp_no_re_files(tmp_path):
mktree(tmp_path, tree)
re_dirs = r'^(?:(?!recursive_dir).)*$'
watcher_no_re_files = RegExpWatcher(tmp_path, re_dirs=re_dirs)
changes = watcher_no_re_files.check()
assert changes == set()
sleep(0.01)
(tmp_path / 'foo/spam.py').write_text('xxx')
(tmp_path / 'foo/bar.txt').write_text('change')
(tmp_path / 'foo/recursive_dir/foo.js').write_text('change')
assert watcher_no_re_files.check() == {
(Change.modified, str((tmp_path / 'foo/spam.py'))),
(Change.modified, str((tmp_path / 'foo/bar.txt'))),
}
def test_regexp_no_args(tmp_path):
mktree(tmp_path, tree)
watcher_no_args = RegExpWatcher(tmp_path)
changes = watcher_no_args.check()
assert changes == set()
sleep(0.01)
(tmp_path / 'foo/spam.py').write_text('xxx')
(tmp_path / 'foo/bar.txt').write_text('change')
(tmp_path / 'foo/recursive_dir/foo.js').write_text('change')
assert watcher_no_args.check() == {
(Change.modified, str((tmp_path / 'foo/spam.py'))),
(Change.modified, str((tmp_path / 'foo/bar.txt'))),
(Change.added, str((tmp_path / 'foo/recursive_dir/foo.js'))),
}
@skip_on_windows
def test_does_not_exist(caplog, tmp_path):
p = tmp_path / 'missing'
AllWatcher(p)
assert f"error walking file system: FileNotFoundError [Errno 2] No such file or directory: '{p}'" in caplog.text
def test_watch(mocker):
class FakeWatcher:
def __init__(self, path):
self._results = iter(
[
{'r1'},
set(),
{'r2'},
set(),
]
)
def check(self):
return next(self._results)
iter_ = watch('xxx', watcher_cls=FakeWatcher, debounce=5, normal_sleep=2, min_sleep=1)
assert next(iter_) == {'r1'}
assert next(iter_) == {'r2'}
def test_watch_watcher_kwargs(mocker):
class FakeWatcher:
def __init__(self, path, arg1=None, arg2=None):
self._results = iter(
[
{arg1},
set(),
{arg2},
set(),
]
)
def check(self):
return next(self._results)
kwargs = dict(arg1='foo', arg2='bar')
iter_ = watch('xxx', watcher_cls=FakeWatcher, watcher_kwargs=kwargs, debounce=5, normal_sleep=2, min_sleep=1)
assert next(iter_) == {kwargs['arg1']}
assert next(iter_) == {kwargs['arg2']}
def test_watch_stop():
class FakeWatcher:
def __init__(self, path):
self._results = iter(
[
{'r1'},
set(),
{'r2'},
]
)
def check(self):
return next(self._results)
stop_event = threading.Event()
stop_event.set()
ans = []
for c in watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1, stop_event=stop_event):
ans.append(c)
assert ans == []
def test_watch_keyboard_error():
class FakeWatcher:
def __init__(self, path):
pass
def check(self):
raise KeyboardInterrupt()
iter = watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1)
assert list(iter) == []
def test_watch_log(mocker, caplog):
mock_log_enabled = mocker.patch('watchgod.main.logger.isEnabledFor')
mock_log_enabled.return_value = True
class FakeWatcher:
def __init__(self, path):
self.files = [1, 2, 3]
def check(self):
return {'r1'}
iter = watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=10)
assert next(iter) == {'r1'}
assert 'xxx time=Xms debounced=Xms files=3 changes=1 (1)' in re.sub(r'\dms', 'Xms', caplog.text)
async def test_awatch(mocker):
class FakeWatcher:
def __init__(self, path):
self._results = iter(
[
set(),
set(),
{'r1'},
set(),
{'r2'},
set(),
]
)
def check(self):
return next(self._results)
ans = []
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, normal_sleep=2, min_sleep=1):
ans.append(v)
if len(ans) == 2:
break
assert ans == [{'r1'}, {'r2'}]
async def test_awatch_stop():
class FakeWatcher:
def __init__(self, path):
self._results = iter(
[
{'r1'},
set(),
{'r2'},
]
)
def check(self):
return next(self._results)
stop_event = anyio.Event()
stop_event.set()
ans = []
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1, stop_event=stop_event):
ans.append(v)
assert ans == []
@skip_unless_linux
async def test_awatch_log(mocker, caplog):
mock_log_enabled = mocker.patch('watchgod.main.logger.isEnabledFor')
mock_log_enabled.return_value = True
class FakeWatcher:
def __init__(self, path):
self.files = [1, 2, 3]
def check(self):
return {'r1'}
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1):
assert v == {'r1'}
break
print(caplog.text)
assert caplog.text.count('DEBUG') > 3
assert 'xxx time=Xms debounced=Xms files=3 changes=1 (1)' in re.sub(r'\dms', 'Xms', caplog.text)
|