diff --git a/dvc/cli/parser.py b/dvc/cli/parser.py index c4f527f5d..06981133f 100644 --- a/dvc/cli/parser.py +++ b/dvc/cli/parser.py @@ -2,6 +2,7 @@ import argparse import logging import os +from functools import lru_cache from dvc import __version__ from dvc.commands import ( @@ -150,6 +151,7 @@ def get_parent_parser(): return parent_parser +@lru_cache(maxsize=1) def get_main_parser(): parent_parser = get_parent_parser() diff --git a/tests/conftest.py b/tests/conftest.py index e9492c94c..10d93bc67 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -174,22 +174,20 @@ def mocked_webbrowser_open(mocker): mocker.patch("webbrowser.open") -@pytest.fixture(autouse=True) -def isolate(tmp_path_factory, monkeypatch) -> None: +@pytest.fixture(scope="session", autouse=True) +def isolate(tmp_path_factory): path = tmp_path_factory.mktemp("mock") home_dir = path / "home" home_dir.mkdir() + monkeypatch = pytest.MonkeyPatch() if sys.platform == "win32": home_drive, home_path = os.path.splitdrive(home_dir) monkeypatch.setenv("USERPROFILE", str(home_dir)) monkeypatch.setenv("HOMEDRIVE", home_drive) monkeypatch.setenv("HOMEPATH", home_path) - for env_var, sub_path in ( - ("APPDATA", "Roaming"), - ("LOCALAPPDATA", "Local"), - ): + for env_var, sub_path in (("APPDATA", "Roaming"), ("LOCALAPPDATA", "Local")): path = home_dir / "AppData" / sub_path path.mkdir(parents=True) monkeypatch.setenv(env_var, os.fspath(path)) @@ -209,6 +207,8 @@ defaultBranch=master import pygit2 pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = str(home_dir) + yield + monkeypatch.undo() @pytest.fixture @@ -247,10 +247,7 @@ def run_copy_metrics(tmp_dir, copy_script): @pytest.fixture(autouse=True) -def gc_collect_on_dvc_close_on_win_311(mocker): - if sys.version_info < (3, 11) and os.name != "nt": - return - +def gc_collect_on_dvc_close_on_win(mocker): from dvc.repo import Repo close = Repo.close @@ -259,4 +256,5 @@ def gc_collect_on_dvc_close_on_win_311(mocker): close(repo) gc.collect() - mocker.patch("dvc.repo.Repo.close", wrapped) + if os.name == "nt": + mocker.patch("dvc.repo.Repo.close", wrapped)