Skip to content

Commit f1d59dd

Browse files
committed
conftest: empty dir is tmp_path now
1 parent 97e4b02 commit f1d59dd

File tree

4 files changed

+59
-61
lines changed

4 files changed

+59
-61
lines changed

dvc/external_repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _external_repo(url=None, rev=None, cache_dir=None):
6262

6363
# Adjust new clone/copy to fit rev and cache_dir
6464

65-
# Checkout needs to be done first because current branch might not be dvc
65+
# Checkout needs to be done first because current branch might not be
6666
# DVC repository
6767
if rev is not None:
6868
_git_checkout(new_path, rev)

tests/basic_env.py

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,7 @@
2121
logger = logging.getLogger("dvc")
2222

2323

24-
class EmptyDirFixture(object):
25-
def __init__(self):
26-
root_dir = self.mkdtemp()
27-
self._root_dir = os.path.realpath(root_dir)
28-
29-
@property
30-
def root_dir(self):
31-
return self._root_dir
32-
33-
def _pushd(self, d):
34-
if not hasattr(self, "_saved_dir"):
35-
self._saved_dir = os.path.realpath(os.curdir)
36-
os.chdir(d)
37-
38-
def _popd(self):
39-
os.chdir(self._saved_dir)
40-
self._saved_dir = None
41-
42-
@staticmethod
43-
def mkdtemp(base_directory=None):
44-
prefix = "dvc-test.{}.".format(os.getpid())
45-
suffix = ".{}".format(shortuuid.uuid())
46-
return tempfile.mkdtemp(
47-
prefix=prefix, suffix=suffix, dir=base_directory
48-
)
49-
50-
def setUp(self):
51-
self._pushd(self._root_dir)
52-
53-
def tearDown(self):
54-
self._popd()
55-
try:
56-
remove(self._root_dir)
57-
except OSError as exc:
58-
# We ignore this under Windows with a warning because it happened
59-
# to be really hard to trace all not properly closed files.
60-
#
61-
# Best guess so far is that gitpython is the culprit:
62-
# it opens files and uses __del__ to close them, which can happen
63-
# late in current pythons. TestGitFixture and TestDvcFixture try
64-
# to close that and it works on most of the tests, but not all.
65-
# Repos and thus git repos are created all over the dvc ;)
66-
if os.name == "nt" and exc.winerror == 32:
67-
warnings.warn("Failed to remove test dir: " + str(exc))
68-
else:
69-
raise
70-
71-
72-
class TestDirFixture(EmptyDirFixture):
24+
class TestDirFixture(object):
7325
DATA_DIR = "data_dir"
7426
DATA_SUB_DIR = os.path.join(DATA_DIR, "data_sub_dir")
7527
DATA = os.path.join(DATA_DIR, "data")
@@ -97,6 +49,23 @@ class TestDirFixture(EmptyDirFixture):
9749
UNICODE = "тест"
9850
UNICODE_CONTENTS = "проверка"
9951

52+
def __init__(self):
53+
root_dir = self.mkdtemp()
54+
self._root_dir = os.path.realpath(root_dir)
55+
56+
@property
57+
def root_dir(self):
58+
return self._root_dir
59+
60+
def _pushd(self, d):
61+
if not hasattr(self, "_saved_dir"):
62+
self._saved_dir = os.path.realpath(os.curdir)
63+
os.chdir(d)
64+
65+
def _popd(self):
66+
os.chdir(self._saved_dir)
67+
self._saved_dir = None
68+
10069
def create(self, name, contents):
10170
dname = os.path.dirname(name)
10271
if len(dname) > 0 and not os.path.isdir(dname):
@@ -109,8 +78,16 @@ def create(self, name, contents):
10978
else contents.decode("utf-8")
11079
)
11180

81+
@staticmethod
82+
def mkdtemp(base_directory=None):
83+
prefix = "dvc-test.{}.".format(os.getpid())
84+
suffix = ".{}".format(shortuuid.uuid())
85+
return tempfile.mkdtemp(
86+
prefix=prefix, suffix=suffix, dir=base_directory
87+
)
88+
11289
def setUp(self):
113-
super(TestDirFixture, self).setUp()
90+
self._pushd(self._root_dir)
11491
self.create(self.FOO, self.FOO_CONTENTS)
11592
self.create(self.BAR, self.BAR_CONTENTS)
11693
self.create(self.CODE, self.CODE_CONTENTS)
@@ -120,6 +97,24 @@ def setUp(self):
12097
self.create(self.DATA_SUB, self.DATA_SUB_CONTENTS)
12198
self.create(self.UNICODE, self.UNICODE_CONTENTS)
12299

100+
def tearDown(self):
101+
self._popd()
102+
try:
103+
remove(self._root_dir)
104+
except OSError as exc:
105+
# We ignore this under Windows with a warning because it happened
106+
# to be really hard to trace all not properly closed files.
107+
#
108+
# Best guess so far is that gitpython is the culprit:
109+
# it opens files and uses __del__ to close them, which can happen
110+
# late in current pythons. TestGitFixture and TestDvcFixture try
111+
# to close that and it works on most of the tests, but not all.
112+
# Repos and thus git repos are created all over the dvc ;)
113+
if os.name == "nt" and exc.winerror == 32:
114+
warnings.warn("Failed to remove test dir: " + str(exc))
115+
else:
116+
raise
117+
123118

124119
class TestGitFixture(TestDirFixture):
125120
N_RETRIES = 5

tests/conftest.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
from git import Repo
88
from git.exc import GitCommandNotFound
99

10-
from .basic_env import TestDirFixture, EmptyDirFixture
10+
from .basic_env import TestDirFixture
1111
from .basic_env import TestDvcGitFixture
1212
from .basic_env import TestGitFixture
1313
from dvc.remote.config import RemoteConfig
1414
from dvc.remote.ssh.connection import SSHConnection
1515
from dvc.repo import Repo as DvcRepo
16-
from dvc.utils.compat import cast_bytes_py2
16+
from dvc.utils.compat import cast_bytes_py2, fspath
1717

1818
# Prevent updater and analytics from running their processes
1919
os.environ[cast_bytes_py2("DVC_TEST")] = cast_bytes_py2("true")
@@ -193,8 +193,6 @@ def git_erepo():
193193

194194

195195
@pytest.fixture
196-
def empty_dir():
197-
directory = EmptyDirFixture()
198-
directory.setUp()
199-
yield directory
200-
directory.tearDown()
196+
def empty_dir(tmp_path, monkeypatch):
197+
monkeypatch.chdir(tmp_path)
198+
return fspath(tmp_path)

tests/func/test_get.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_get_to_dir(dname, erepo):
9292

9393

9494
def test_get_from_non_dvc_master(empty_dir, git_erepo, caplog):
95-
storage = empty_dir.mkdtemp()
95+
storage = git_erepo.mkdtemp()
9696

9797
dvc_branch = "dvc_test"
9898
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):
111111

112112
git_erepo.git.git.checkout("master")
113113

114-
os.chdir(empty_dir._root_dir)
114+
os.chdir(empty_dir)
115115

116116
caplog.clear()
117117
imported_file = "foo_imported"
118118
with caplog.at_level(logging.INFO, logger="dvc"):
119-
Repo.get(git_erepo._root_dir, "foo", out=imported_file, rev=dvc_branch)
119+
Repo.get(
120+
git_erepo._root_dir,
121+
git_erepo.FOO,
122+
out=imported_file,
123+
rev=dvc_branch,
124+
)
120125

121126
assert caplog.text == ""
122127
assert filecmp.cmp(

0 commit comments

Comments
 (0)