def test_several(tmpdir): m = Monitor() m.add_watch(tmpdir, (EVENTS.ACCESS, EVENTS.CREATE)) async def main(): tmpdir.mkdir('xxx') async for event in m.next_events(): assert event.name == 'xxx' assert event.is_dir assert str(event) in ('CREATE: xxx', 'ACCESS: xxx') curio.run(main())
def test_symlinks(tmp): m1 = Monitor() m2 = Monitor() root_path = tmp watch_path = root_path / 'dir' watch_path.mkdir() f = watch_path / 'file' f.touch() link = watch_path / 'link' link.symlink_to(f) m1.add_watch(link, EVENTS.ALL_EVENTS, follow_symlinks=False) m2.add_watch(link, EVENTS.ALL_EVENTS) async def do_watch_m1(): async with m1: # pragma: no cover raise RuntimeError() async def do_watch_m2(t): async with m2: await t.cancel() async def main(): t1 = await curio.spawn(curio.timeout_after(2, do_watch_m1())) t2 = await curio.spawn(curio.timeout_after(2, do_watch_m2(t1))) with link.open('a') as wr: wr.write("Hello") await t2.join() with pytest.raises(curio.TaskError): await t1.join() curio.run(main(), with_monitor=True)
def test_remove(tmp): p = tmp subpaths = [] m = Monitor(error_empty=True) for i in '1234': subpath = p/i subpath.touch() m.add_watch(subpath, EVENTS.DELETE_SELF) subpaths.append(subpath) assert(len(m._watches) == len(subpaths) == len(m.active_watches())) wd = m.add_watch(p, EVENTS.DELETE_SELF) it = iter(subpaths) def delpath(): try: next(it).unlink() except StopIteration: m.remove_watch(wd) async def do_watch(): try: async for event in m: delpath() except NoMoreWatches: return async def main(): t = await curio.spawn(curio.timeout_after(2, do_watch())) delpath() await t.join() curio.run(main()) with pytest.raises(ValueError): m.remove_watch(wd) assert not m._watches
def test_remove(tmpdir): s = str(tmpdir) p = Path(s) subpaths = [] m = Monitor(error_empty=True) for i in '1234': subpath = p / i subpath.touch() m.add_watch(subpath, EVENTS.DELETE_SELF) subpaths.append(subpath) assert (len(m._watches) == len(subpaths) == len(m.active_watches())) wd = m.add_watch(p, EVENTS.DELETE_SELF) it = iter(subpaths) def delpath(): try: next(it).unlink() except StopIteration: m.remove_watch(wd) async def do_watch(): try: async for event in m: delpath() except NoMoreWatches: return async def main(): t = await curio.spawn(curio.timeout_after(2, do_watch())) delpath() await t.join() curio.run(main()) with pytest.raises(ValueError): m.remove_watch(wd) assert not m._watches
def test_symlinks(tmpdir): m1 = Monitor() m2 = Monitor() root_path = Path(tmpdir) watch_path = root_path / 'dir' watch_path.mkdir() f = watch_path / 'file' f.touch() link = watch_path / 'link' link.symlink_to(f) m1.add_watch(link, EVENTS.ALL_EVENTS, follow_symlinks=False) m2.add_watch(link, EVENTS.ALL_EVENTS) async def do_watch_m1(): async with m1: # pragma: no cover raise RuntimeError() async def do_watch_m2(t): async with m2: await t.cancel() async def main(): t1 = await curio.spawn(curio.timeout_after(2, do_watch_m1())) t2 = await curio.spawn(curio.timeout_after(2, do_watch_m2(t1))) with link.open('a') as wr: wr.write("Hello") await t2.join() with pytest.raises(curio.TaskError): await t1.join() curio.run(main(), with_monitor=True)
def test_bad(): m = Monitor() with pytest.raises(OSError): m.add_watch('/', 123212313) with pytest.raises(TypeError): m.add_watch('/', None)
def test_interface(tmpdir): m = Monitor() with pytest.raises(TypeError): m.add_watch(tmpdir, "xxxx") with pytest.raises(TypeError): m.add_watch(1234, 0) p = Path(tmpdir) m.add_watch(p, 1) m.add_watch(tmpdir, EVENTS.CREATE) m.add_watch(tmpdir, EVENTS.DELETE_SELF, replace_existing=True) async def do_watch(): async for event in m.next_events(): assert event.tp != EVENTS.CREATE async def main(): t = await curio.spawn(do_watch()) with (p/'xxx').open('a'): pass (p/'xxx').unlink() p.rmdir() await t.join() curio.run(main())
def test_interface(tmp): m = Monitor() with pytest.raises(TypeError): m.add_watch(tmp, "xxxx") with pytest.raises(TypeError): m.add_watch(1234, 0) p = tmp m.add_watch(p, 1) m.add_watch(tmp, EVENTS.CREATE) m.add_watch(tmp, EVENTS.DELETE_SELF, replace_existing=True) async def do_watch(): async for event in m.next_events(): assert event.tp != EVENTS.CREATE async def main(): t = await curio.spawn(do_watch()) with (p / 'xxx').open('a'): pass (p / 'xxx').unlink() p.rmdir() await t.join() curio.run(main())