mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-10 15:28:34 +01:00
conftest: empty dir is tmp_path now
This commit is contained in:
@@ -62,7 +62,7 @@ def _external_repo(url=None, rev=None, cache_dir=None):
|
||||
|
||||
# Adjust new clone/copy to fit rev and cache_dir
|
||||
|
||||
# Checkout needs to be done first because current branch might not be dvc
|
||||
# Checkout needs to be done first because current branch might not be
|
||||
# DVC repository
|
||||
if rev is not None:
|
||||
_git_checkout(new_path, rev)
|
||||
|
||||
+45
-50
@@ -21,55 +21,7 @@ from dvc.utils.fs import remove
|
||||
logger = logging.getLogger("dvc")
|
||||
|
||||
|
||||
class EmptyDirFixture(object):
|
||||
def __init__(self):
|
||||
root_dir = self.mkdtemp()
|
||||
self._root_dir = os.path.realpath(root_dir)
|
||||
|
||||
@property
|
||||
def root_dir(self):
|
||||
return self._root_dir
|
||||
|
||||
def _pushd(self, d):
|
||||
if not hasattr(self, "_saved_dir"):
|
||||
self._saved_dir = os.path.realpath(os.curdir)
|
||||
os.chdir(d)
|
||||
|
||||
def _popd(self):
|
||||
os.chdir(self._saved_dir)
|
||||
self._saved_dir = None
|
||||
|
||||
@staticmethod
|
||||
def mkdtemp(base_directory=None):
|
||||
prefix = "dvc-test.{}.".format(os.getpid())
|
||||
suffix = ".{}".format(shortuuid.uuid())
|
||||
return tempfile.mkdtemp(
|
||||
prefix=prefix, suffix=suffix, dir=base_directory
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self._pushd(self._root_dir)
|
||||
|
||||
def tearDown(self):
|
||||
self._popd()
|
||||
try:
|
||||
remove(self._root_dir)
|
||||
except OSError as exc:
|
||||
# We ignore this under Windows with a warning because it happened
|
||||
# to be really hard to trace all not properly closed files.
|
||||
#
|
||||
# Best guess so far is that gitpython is the culprit:
|
||||
# it opens files and uses __del__ to close them, which can happen
|
||||
# late in current pythons. TestGitFixture and TestDvcFixture try
|
||||
# to close that and it works on most of the tests, but not all.
|
||||
# Repos and thus git repos are created all over the dvc ;)
|
||||
if os.name == "nt" and exc.winerror == 32:
|
||||
warnings.warn("Failed to remove test dir: " + str(exc))
|
||||
else:
|
||||
raise
|
||||
|
||||
|
||||
class TestDirFixture(EmptyDirFixture):
|
||||
class TestDirFixture(object):
|
||||
DATA_DIR = "data_dir"
|
||||
DATA_SUB_DIR = os.path.join(DATA_DIR, "data_sub_dir")
|
||||
DATA = os.path.join(DATA_DIR, "data")
|
||||
@@ -97,6 +49,23 @@ class TestDirFixture(EmptyDirFixture):
|
||||
UNICODE = "тест"
|
||||
UNICODE_CONTENTS = "проверка"
|
||||
|
||||
def __init__(self):
|
||||
root_dir = self.mkdtemp()
|
||||
self._root_dir = os.path.realpath(root_dir)
|
||||
|
||||
@property
|
||||
def root_dir(self):
|
||||
return self._root_dir
|
||||
|
||||
def _pushd(self, d):
|
||||
if not hasattr(self, "_saved_dir"):
|
||||
self._saved_dir = os.path.realpath(os.curdir)
|
||||
os.chdir(d)
|
||||
|
||||
def _popd(self):
|
||||
os.chdir(self._saved_dir)
|
||||
self._saved_dir = None
|
||||
|
||||
def create(self, name, contents):
|
||||
dname = os.path.dirname(name)
|
||||
if len(dname) > 0 and not os.path.isdir(dname):
|
||||
@@ -109,8 +78,16 @@ class TestDirFixture(EmptyDirFixture):
|
||||
else contents.decode("utf-8")
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def mkdtemp(base_directory=None):
|
||||
prefix = "dvc-test.{}.".format(os.getpid())
|
||||
suffix = ".{}".format(shortuuid.uuid())
|
||||
return tempfile.mkdtemp(
|
||||
prefix=prefix, suffix=suffix, dir=base_directory
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
super(TestDirFixture, self).setUp()
|
||||
self._pushd(self._root_dir)
|
||||
self.create(self.FOO, self.FOO_CONTENTS)
|
||||
self.create(self.BAR, self.BAR_CONTENTS)
|
||||
self.create(self.CODE, self.CODE_CONTENTS)
|
||||
@@ -120,6 +97,24 @@ class TestDirFixture(EmptyDirFixture):
|
||||
self.create(self.DATA_SUB, self.DATA_SUB_CONTENTS)
|
||||
self.create(self.UNICODE, self.UNICODE_CONTENTS)
|
||||
|
||||
def tearDown(self):
|
||||
self._popd()
|
||||
try:
|
||||
remove(self._root_dir)
|
||||
except OSError as exc:
|
||||
# We ignore this under Windows with a warning because it happened
|
||||
# to be really hard to trace all not properly closed files.
|
||||
#
|
||||
# Best guess so far is that gitpython is the culprit:
|
||||
# it opens files and uses __del__ to close them, which can happen
|
||||
# late in current pythons. TestGitFixture and TestDvcFixture try
|
||||
# to close that and it works on most of the tests, but not all.
|
||||
# Repos and thus git repos are created all over the dvc ;)
|
||||
if os.name == "nt" and exc.winerror == 32:
|
||||
warnings.warn("Failed to remove test dir: " + str(exc))
|
||||
else:
|
||||
raise
|
||||
|
||||
|
||||
class TestGitFixture(TestDirFixture):
|
||||
N_RETRIES = 5
|
||||
|
||||
+5
-7
@@ -7,13 +7,13 @@ import pytest
|
||||
from git import Repo
|
||||
from git.exc import GitCommandNotFound
|
||||
|
||||
from .basic_env import TestDirFixture, EmptyDirFixture
|
||||
from .basic_env import TestDirFixture
|
||||
from .basic_env import TestDvcGitFixture
|
||||
from .basic_env import TestGitFixture
|
||||
from dvc.remote.config import RemoteConfig
|
||||
from dvc.remote.ssh.connection import SSHConnection
|
||||
from dvc.repo import Repo as DvcRepo
|
||||
from dvc.utils.compat import cast_bytes_py2
|
||||
from dvc.utils.compat import cast_bytes_py2, fspath
|
||||
|
||||
# Prevent updater and analytics from running their processes
|
||||
os.environ[cast_bytes_py2("DVC_TEST")] = cast_bytes_py2("true")
|
||||
@@ -193,8 +193,6 @@ def git_erepo():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def empty_dir():
|
||||
directory = EmptyDirFixture()
|
||||
directory.setUp()
|
||||
yield directory
|
||||
directory.tearDown()
|
||||
def empty_dir(tmp_path, monkeypatch):
|
||||
monkeypatch.chdir(tmp_path)
|
||||
return fspath(tmp_path)
|
||||
|
||||
@@ -92,7 +92,7 @@ def test_get_to_dir(dname, erepo):
|
||||
|
||||
|
||||
def test_get_from_non_dvc_master(empty_dir, git_erepo, caplog):
|
||||
storage = empty_dir.mkdtemp()
|
||||
storage = git_erepo.mkdtemp()
|
||||
|
||||
dvc_branch = "dvc_test"
|
||||
git_erepo.git.git.checkout("master", b=dvc_branch)
|
||||
@@ -111,12 +111,17 @@ def test_get_from_non_dvc_master(empty_dir, git_erepo, caplog):
|
||||
|
||||
git_erepo.git.git.checkout("master")
|
||||
|
||||
os.chdir(empty_dir._root_dir)
|
||||
os.chdir(empty_dir)
|
||||
|
||||
caplog.clear()
|
||||
imported_file = "foo_imported"
|
||||
with caplog.at_level(logging.INFO, logger="dvc"):
|
||||
Repo.get(git_erepo._root_dir, "foo", out=imported_file, rev=dvc_branch)
|
||||
Repo.get(
|
||||
git_erepo._root_dir,
|
||||
git_erepo.FOO,
|
||||
out=imported_file,
|
||||
rev=dvc_branch,
|
||||
)
|
||||
|
||||
assert caplog.text == ""
|
||||
assert filecmp.cmp(
|
||||
|
||||
Reference in New Issue
Block a user