def test_watch_modules(self, tmpdir, monkeypatch): mod1 = tmpdir.join("module1.py").ensure(file=True) mod2 = tmpdir.join("module2.py").ensure(file=True) monkeypatch.syspath_prepend(str(tmpdir)) iterator = watch_for_changes(modules=["module1", "module2"], period=0.1) # Initial iteration hits all known modules paths, mods = next(iterator) assert not paths assert set(mods) == {"module1", "module2"} # Later iterations only yield modules that are new or changed timer = threading.Timer(0.15, lambda: mod1.setmtime()) timer.start() paths, mods = next(iterator) assert not paths assert set(mods) == {"module1"} # Deleting some original modules causes no errors mod1.remove() timer = threading.Timer(0.15, mod2.setmtime) timer.start() paths, mods = next(iterator) assert set(mods) == {"module2"} # If those modules are readded, next iteration picks them up mod1.ensure(file=True) mod2.setmtime() paths, mods = next(iterator) assert set(mods) == {"module1", "module2"}
def test_errors_path_does_not_exist(self, tmpdir): missing = str(tmpdir.join("not-real")) with pytest.raises(TerminalError) as exc: for _, _ in watch_for_changes(paths=[missing]): assert False, "Should never get here" assert repr(missing) in str(exc.value)
def test_watch_paths(self, tmpdir): dir_path = str(tmpdir.join("dir").mkdir()) path1 = str(tmpdir.join("test1.py").ensure(file=True)) path2 = str(tmpdir.join("test2.py").ensure(file=True)) path3 = os.path.join(dir_path, "test3.py") path4 = os.path.join(dir_path, "test4.something") iterator = watch_for_changes(paths=[dir_path, path1, path2], period=0.1) # Initial iteration hits all known paths paths, mods = next(iterator) assert not mods assert set(paths) == {path1, path2} # Later iterations only yield paths that are new or changed def update(): with open(path3, "wb"): pass with open(path4, "wb"): pass os.utime(path1) timer = threading.Timer(0.15, update) timer.start() paths, mods = next(iterator) # path4 isn't included, since only `*.py` files are watched assert set(paths) == {path1, path3} # Deleting some original paths causes no errors os.remove(path1) os.remove(path3) timer = threading.Timer(0.15, lambda: os.utime(path2)) timer.start() paths, mods = next(iterator) assert set(paths) == {path2} # If those paths are readded, next iteration picks them up with open(path1, "wb"): pass os.utime(path2) paths, mods = next(iterator) assert set(paths) == {path1, path2}
def test_errors_module_does_not_exist(self): name = "not_a_real_module_name" with pytest.raises(TerminalError, match=name): for _, _ in watch_for_changes(modules=[name]): assert False, "Should never get here"