Skip to content

Commit

Permalink
tests: isolate dvc config and directories (iterative#9450)
Browse files Browse the repository at this point in the history
* tests: isolate dvc config and directories

by introducing envvars to change default values. This lets pytest
handle the deletion of tmpdirs, instead of filling /var/tmp with
hundreds of thousands of directories. Also isolates test run from
faulty global/system config files.

* move to dvc/env and sort
  • Loading branch information
skshetry authored May 15, 2023
1 parent 4518150 commit 004f497
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
9 changes: 3 additions & 6 deletions dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ class Config(dict):
ConfigError: thrown if config has an invalid format.
"""

APPNAME = "dvc"
APPAUTHOR = "iterative"

SYSTEM_LEVELS = ("system", "global")
REPO_LEVELS = ("repo", "local")
# In the order they shadow each other
Expand Down Expand Up @@ -121,14 +118,14 @@ def from_cwd(cls, fs: Optional["FileSystem"] = None, **kwargs):

@classmethod
def get_dir(cls, level):
from platformdirs import site_config_dir, user_config_dir
from dvc.dirs import global_config_dir, system_config_dir

assert level in ("global", "system")

if level == "global":
return user_config_dir(cls.APPNAME, cls.APPAUTHOR)
return global_config_dir()
if level == "system":
return site_config_dir(cls.APPNAME, cls.APPAUTHOR)
return system_config_dir()

@cached_property
def files(self) -> Dict[str, str]:
Expand Down
26 changes: 26 additions & 0 deletions dvc/dirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

import platformdirs

from . import env

APPNAME = "dvc"
APPAUTHOR = "iterative"


def system_config_dir():
return os.getenv(env.DVC_SYSTEM_CONFIG_DIR) or platformdirs.site_config_dir(
APPNAME, APPAUTHOR
)


def global_config_dir():
return os.getenv(env.DVC_SYSTEM_CONFIG_DIR) or platformdirs.user_config_dir(
APPNAME, APPAUTHOR
)


def site_cache_dir():
return os.getenv(env.DVC_SITE_CACHE_DIR) or platformdirs.site_cache_dir(
APPNAME, APPAUTHOR, opinion=True
)
15 changes: 9 additions & 6 deletions dvc/env.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
DVCLIVE_RESUME = "DVCLIVE_RESUME"
DVC_CHECKPOINT = "DVC_CHECKPOINT"
DVC_DAEMON = "DVC_DAEMON"
DVC_PAGER = "DVC_PAGER"
DVC_ROOT = "DVC_ROOT"
DVCLIVE_RESUME = "DVCLIVE_RESUME"
DVC_IGNORE_ISATTY = "DVC_IGNORE_ISATTY"
DVC_EXP_AUTO_PUSH = "DVC_EXP_AUTO_PUSH"
DVC_EXP_BASELINE_REV = "DVC_EXP_BASELINE_REV"
DVC_EXP_NAME = "DVC_EXP_NAME"
DVC_EXP_GIT_REMOTE = "DVC_EXP_GIT_REMOTE"
DVC_EXP_AUTO_PUSH = "DVC_EXP_AUTO_PUSH"
DVC_EXP_NAME = "DVC_EXP_NAME"
DVC_GLOBAL_CONFIG_DIR = "DVC_GLOBAL_CONFIG_DIR"
DVC_IGNORE_ISATTY = "DVC_IGNORE_ISATTY"
DVC_NO_ANALYTICS = "DVC_NO_ANALYTICS"
DVC_PAGER = "DVC_PAGER"
DVC_ROOT = "DVC_ROOT"
DVC_SHOW_TRACEBACK = "DVC_SHOW_TRACEBACK"
DVC_SITE_CACHE_DIR = "DVC_SITE_CACHE_DIR"
DVC_STUDIO_TOKEN = "DVC_STUDIO_TOKEN" # noqa: S105 # nosec B105
DVC_STUDIO_URL = "DVC_STUDIO_URL"
DVC_SYSTEM_CONFIG_DIR = "DVC_SYSTEM_CONFIG_DIR"
6 changes: 2 additions & 4 deletions dvc/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,10 @@ def site_cache_dir(self) -> str:
import getpass
import hashlib

import platformdirs

from dvc.dirs import site_cache_dir
from dvc.fs import GitFileSystem

default = platformdirs.site_cache_dir("dvc", "iterative", opinion=True)
cache_dir = self.config["core"].get("site_cache_dir") or default
cache_dir = self.config["core"].get("site_cache_dir") or site_cache_dir()

if isinstance(self.fs, GitFileSystem):
relparts = ()
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from dvc import env
from dvc.testing.fixtures import * # noqa, pylint: disable=wildcard-import

from .dir_helpers import * # noqa, pylint: disable=wildcard-import
Expand Down Expand Up @@ -206,7 +207,13 @@ def isolate(tmp_path_factory):
import pygit2

pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = str(home_dir)

monkeypatch.setenv(env.DVC_SYSTEM_CONFIG_DIR, os.fspath(path / "system"))
monkeypatch.setenv(env.DVC_GLOBAL_CONFIG_DIR, os.fspath(path / "global"))
monkeypatch.setenv(env.DVC_SITE_CACHE_DIR, os.fspath(path / "site_cache_dir"))

yield

monkeypatch.undo()


Expand Down

0 comments on commit 004f497

Please sign in to comment.