Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ jobs:
run: |
pip install --upgrade pip setuptools wheel
pip install -e ".[dev]"
- name: setup git
run: |
git config --global user.email "dvctester@example.com"
git config --global user.name "DVC Tester"
- name: run tests
timeout-minutes: 40
run: >-
Expand Down
29 changes: 7 additions & 22 deletions dvc/testing/tmp_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
from contextlib import contextmanager
from functools import partialmethod

from funcy import lmap, retry

from dvc.utils import serialize
from dvc.utils.fs import makedirs

Expand All @@ -62,7 +60,7 @@ class TmpDir(pathlib.Path):

@property
def fs_path(self):
return str(self)
return os.fspath(self)

@property
def url(self):
Expand Down Expand Up @@ -98,18 +96,18 @@ def init(self, *, scm=False, dvc=False, subdir=False):
assert not scm or not hasattr(self, "scm")
assert not dvc or not hasattr(self, "dvc")

str_path = os.fspath(self)

if scm:
git_init(str_path)
Git.init(self.fs_path).close()
if dvc:
self.dvc = Repo.init(
str_path,
self.fs_path,
no_scm=not scm and not hasattr(self, "scm"),
subdir=subdir,
)
if scm:
self.scm = self.dvc.scm if hasattr(self, "dvc") else Git(str_path)
self.scm = (
self.dvc.scm if hasattr(self, "dvc") else Git(self.fs_path)
)
if dvc and hasattr(self, "scm"):
self.scm.commit("init dvc")

Expand Down Expand Up @@ -264,7 +262,7 @@ def modify(self, *args, **kwargs):
def _coerce_filenames(filenames):
if isinstance(filenames, (str, bytes, pathlib.PurePath)):
filenames = [filenames]
return lmap(os.fspath, filenames)
return list(map(os.fspath, filenames))


class WindowsTmpDir(TmpDir, pathlib.PureWindowsPath):
Expand All @@ -273,16 +271,3 @@ class WindowsTmpDir(TmpDir, pathlib.PureWindowsPath):

class PosixTmpDir(TmpDir, pathlib.PurePosixPath):
pass


def git_init(path):
from git import Repo
from git.exc import GitCommandNotFound

# NOTE: handles EAGAIN error on BSD systems (osx in our case).
# Otherwise when running tests you might get this exception:
#
# GitCommandNotFound: Cmd('git') not found due to:
# OSError('[Errno 35] Resource temporarily unavailable')
git = retry(5, GitCommandNotFound)(Repo.init)(path)
git.close()
23 changes: 4 additions & 19 deletions tests/basic_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import pytest
import shortuuid
from git import Repo
from git.exc import GitCommandNotFound

from dvc.repo import Repo as DvcRepo
from dvc.utils.fs import remove
Expand Down Expand Up @@ -116,24 +114,11 @@ class TestGitFixture(TestDirFixture):
N_RETRIES = 5

def setUp(self):
from scmrepo.git import Git

super().setUp()
# NOTE: handles EAGAIN error on BSD systems (osx in our case).
# Otherwise when running tests you might get this exception:
#
# GitCommandNotFound: Cmd('git') not found due to:
# OSError('[Errno 35] Resource temporarily unavailable')
retries = self.N_RETRIES
while True:
try:
self.git = Repo.init()
break
except GitCommandNotFound:
retries -= 1
if not retries:
raise

self.git.index.add([self.CODE])
self.git.index.commit("add code")
self.git = Git.init(".")
self.git.add_commit(self.CODE, message="add code")

def tearDown(self):
self.git.close()
Expand Down
40 changes: 39 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from contextlib import suppress

import pytest
Expand Down Expand Up @@ -161,7 +162,7 @@ def pytest_configure(config):
enabled_remotes.add(remote_name)


@pytest.fixture()
@pytest.fixture
def custom_template(tmp_dir, dvc):
try:
import importlib_resources
Expand All @@ -185,3 +186,40 @@ def custom_template(tmp_dir, dvc):
@pytest.fixture(autouse=True)
def mocked_webbrowser_open(mocker):
mocker.patch("webbrowser.open")


@pytest.fixture(autouse=True)
def isolate(tmp_path_factory, monkeypatch) -> None:
path = tmp_path_factory.mktemp("mock")
home_dir = path / "home"
home_dir.mkdir()

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"),
):
path = home_dir / "AppData" / sub_path
path.mkdir(parents=True)
monkeypatch.setenv(env_var, os.fspath(path))
else:
monkeypatch.setenv("HOME", str(home_dir))

monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
contents = b"""
[user]
name=DVC Tester
email=dvctester@example.com
[init]
defaultBranch=master
Comment thread
dtrifiro marked this conversation as resolved.
"""
(home_dir / ".gitconfig").write_bytes(contents)

import pygit2

pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = str(home_dir)
3 changes: 0 additions & 3 deletions tests/dir_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,16 @@
import pytest

from dvc.logger import disable_other_loggers
from dvc.testing.tmp_dir import git_init

__all__ = [
"run_copy",
"run_head",
"erepo_dir",
"git_dir",
"git_init",
"git_upstream",
"git_downstream",
]


# see https://github.com/iterative/dvc/issues/3167
disable_other_loggers()

Expand Down
5 changes: 2 additions & 3 deletions tests/func/metrics/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ def test_no_commits(tmp_dir):
from scmrepo.git import Git

from dvc.repo import Repo
from tests.dir_helpers import git_init

git_init(".")
assert Git().no_commits
git = Git.init(tmp_dir.fs_path)
assert git.no_commits

assert Repo.init().metrics.diff() == {}

Expand Down
5 changes: 2 additions & 3 deletions tests/func/params/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ def test_no_commits(tmp_dir):
from scmrepo.git import Git

from dvc.repo import Repo
from tests.dir_helpers import git_init

git_init(".")
assert Git().no_commits
git = Git.init(tmp_dir.fs_path)
assert git.no_commits

assert Repo.init().params.diff() == {}

Expand Down
5 changes: 2 additions & 3 deletions tests/func/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,9 @@ def test_no_commits(tmp_dir):
from scmrepo.git import Git

from dvc.repo import Repo
from tests.dir_helpers import git_init

git_init(".")
assert Git().no_commits
git = Git.init(tmp_dir.fs_path)
assert git.no_commits

assert Repo.init().diff() == {}

Expand Down
9 changes: 3 additions & 6 deletions tests/func/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
from funcy import first
from scmrepo.git import Git

from dvc.config import NoRemoteError
from dvc.dvcfile import Dvcfile
Expand Down Expand Up @@ -268,9 +269,7 @@ def test_pull_wildcard_imported_directory_stage(tmp_dir, dvc, erepo_dir):
def test_push_wildcard_from_bare_git_repo(
tmp_dir, make_tmp_dir, erepo_dir, local_cloud
):
import git

git.Repo.init(os.fspath(tmp_dir), bare=True)
Git.init(tmp_dir.fs_path, bare=True).close()

erepo_dir.add_remote(config=local_cloud.config)
with erepo_dir.chdir():
Expand Down Expand Up @@ -381,9 +380,7 @@ def test_pull_no_rev_lock(erepo_dir, tmp_dir, dvc):
def test_import_from_bare_git_repo(
tmp_dir, make_tmp_dir, erepo_dir, local_cloud
):
import git

git.Repo.init(os.fspath(tmp_dir), bare=True)
Git.init(tmp_dir.fs_path, bare=True).close()

erepo_dir.add_remote(config=local_cloud.config)
with erepo_dir.chdir():
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/fs/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_ssh_port(mock_file, config, expected_port):
),
(
{"host": "example.com"},
[os.path.expanduser("~/.ssh/not_default.key")],
["~/.ssh/not_default.key"],
),
(
{
Expand All @@ -141,7 +141,12 @@ def test_ssh_port(mock_file, config, expected_port):
)
def test_ssh_keyfile(mock_file, config, expected_keyfile):
fs = SSHFileSystem(**config)
assert fs.fs_args.get("client_keys") == expected_keyfile
expected_keyfiles = (
[os.path.expanduser(path) for path in expected_keyfile]
if expected_keyfile
else expected_keyfile
)
assert fs.fs_args.get("client_keys") == expected_keyfiles


mock_ssh_multi_key_config = """
Expand Down