import pylint if __name__ == "__main__": pylint.modify_sys_path() pylint.run_pylint()
def test_modify_sys_path() -> None: @contextlib.contextmanager def test_sys_path() -> Generator[None, None, None]: original_path = sys.path try: yield finally: sys.path = original_path @contextlib.contextmanager def test_environ_pythonpath( new_pythonpath: Optional[str], ) -> Generator[None, None, None]: original_pythonpath = os.environ.get("PYTHONPATH") if new_pythonpath: os.environ["PYTHONPATH"] = new_pythonpath elif new_pythonpath is None and original_pythonpath is not None: # If new_pythonpath is None, make sure to delete PYTHONPATH if present del os.environ["PYTHONPATH"] try: yield finally: if original_pythonpath: os.environ["PYTHONPATH"] = original_pythonpath elif new_pythonpath is not None: # Only delete PYTHONPATH if new_pythonpath wasn't None del os.environ["PYTHONPATH"] with test_sys_path(), patch("os.getcwd") as mock_getcwd: cwd = "/tmp/pytest-of-root/pytest-0/test_do_not_import_files_from_0" mock_getcwd.return_value = cwd default_paths = [ "/usr/local/lib/python39.zip", "/usr/local/lib/python3.9", "/usr/local/lib/python3.9/lib-dynload", "/usr/local/lib/python3.9/site-packages", ] paths = [ cwd, *default_paths, ] sys.path = copy(paths) with test_environ_pythonpath(None): modify_sys_path() assert sys.path == paths[1:] paths = [ cwd, cwd, *default_paths, ] sys.path = copy(paths) with test_environ_pythonpath("."): modify_sys_path() assert sys.path == paths[1:] paths = [ cwd, "/custom_pythonpath", *default_paths, ] sys.path = copy(paths) with test_environ_pythonpath("/custom_pythonpath"): modify_sys_path() assert sys.path == paths[1:] paths = [ cwd, "/custom_pythonpath", cwd, *default_paths, ] sys.path = copy(paths) with test_environ_pythonpath("/custom_pythonpath:"): modify_sys_path() assert sys.path == [paths[1]] + paths[3:] paths = [ "", cwd, "/custom_pythonpath", *default_paths, ] sys.path = copy(paths) with test_environ_pythonpath(":/custom_pythonpath"): modify_sys_path() assert sys.path == paths[2:] paths = [ cwd, cwd, "/custom_pythonpath", *default_paths, ] sys.path = copy(paths) with test_environ_pythonpath(":/custom_pythonpath:"): modify_sys_path() assert sys.path == paths[2:] paths = [ cwd, cwd, *default_paths, ] sys.path = copy(paths) with test_environ_pythonpath(":."): modify_sys_path() assert sys.path == paths[1:] sys.path = copy(paths) with test_environ_pythonpath(f":{cwd}"): modify_sys_path() assert sys.path == paths[1:] sys.path = copy(paths) with test_environ_pythonpath(".:"): modify_sys_path() assert sys.path == paths[1:] sys.path = copy(paths) with test_environ_pythonpath(f"{cwd}:"): modify_sys_path() assert sys.path == paths[1:] paths = [ "", cwd, *default_paths, cwd, ] sys.path = copy(paths) with test_environ_pythonpath(cwd): modify_sys_path() assert sys.path == paths[1:]
def test_modify_sys_path() -> None: @contextlib.contextmanager def test_sys_path() -> Generator[None, None, None]: original_path = sys.path try: yield finally: sys.path = original_path with test_sys_path(), patch("os.getcwd") as mock_getcwd: cwd = "/tmp/pytest-of-root/pytest-0/test_do_not_import_files_from_0" mock_getcwd.return_value = cwd paths = [ cwd, cwd, "/usr/local/lib/python39.zip", "/usr/local/lib/python3.9", "/usr/local/lib/python3.9/lib-dynload", "/usr/local/lib/python3.9/site-packages", ] sys.path = copy(paths) modify_sys_path() assert sys.path == paths[2:] paths = [ cwd, "/custom_pythonpath", cwd, "/usr/local/lib/python39.zip", "/usr/local/lib/python3.9", "/usr/local/lib/python3.9/lib-dynload", "/usr/local/lib/python3.9/site-packages", ] sys.path = copy(paths) modify_sys_path() assert sys.path == [paths[1]] + paths[3:] paths = [ "", cwd, "/custom_pythonpath", "/usr/local/lib/python39.zip", "/usr/local/lib/python3.9", "/usr/local/lib/python3.9/lib-dynload", "/usr/local/lib/python3.9/site-packages", ] sys.path = copy(paths) modify_sys_path() assert sys.path == paths[2:] paths = [ "", cwd, "/usr/local/lib/python39.zip", "/usr/local/lib/python3.9", "/usr/local/lib/python3.9/lib-dynload", "/usr/local/lib/python3.9/site-packages", cwd, ] sys.path = copy(paths) modify_sys_path() assert sys.path == paths[2:]