diff --git a/requirements-test.txt b/requirements-test.txt --- a/requirements-test.txt +++ b/requirements-test.txt @@ -3,3 +3,4 @@ swh.core[http] >= 0.0.61 swh.scheduler[testing] >= 0.5.0 swh.storage[testing] +pytest-postgresql >= 2.1.0, < 3.0 diff --git a/swh/loader/mercurial/from_disk.py b/swh/loader/mercurial/from_disk.py --- a/swh/loader/mercurial/from_disk.py +++ b/swh/loader/mercurial/from_disk.py @@ -100,6 +100,11 @@ return default +def in_testing_environment() -> bool: + """Returns whether we are in a `Pytest` session""" + return "PYTEST_CURRENT_TEST" in os.environ + + class HgLoaderFromDisk(BaseLoader): """Load a mercurial repository from a local repository.""" @@ -167,9 +172,16 @@ # If set, will override the default value self._visit_status = None - self.old_environ = os.environ.copy() - os.environ.clear() - os.environ.update(get_minimum_env()) + if not in_testing_environment(): + # Do not execute in tests, since the tests already have a clean environment. + # Loading multiple loaders in a test will cause other tests that rely on + # environment variables to fail. + # Playing with `os.environ` is all kinds of terrible, but unfortunately + # `billiard` (which we use for subprocesses) does not appear to have a way + # of handling this any better. + self.old_environ = os.environ.copy() + os.environ.clear() + os.environ.update(get_minimum_env()) def pre_cleanup(self) -> None: """As a first step, will try and check for dangling data to cleanup. @@ -184,8 +196,10 @@ def cleanup(self) -> None: """Last step executed by the loader.""" - os.environ.clear() - os.environ.update(self.old_environ) + if not in_testing_environment(): + # See comment in `__init__` about this. + os.environ.clear() + os.environ.update(self.old_environ) if self._repo_directory and os.path.exists(self._repo_directory): self.log.debug(f"Cleanup up repository {self._repo_directory}") diff --git a/swh/loader/mercurial/tests/conftest.py b/swh/loader/mercurial/tests/conftest.py --- a/swh/loader/mercurial/tests/conftest.py +++ b/swh/loader/mercurial/tests/conftest.py @@ -47,3 +47,4 @@ """ os.environ["HGPLAIN"] = "" os.environ["HGRCPATH"] = "" + os.environ["HGSKIPREPO"] = ""