test: clean up test dirs (#1904)

* test: clean up test dirs

Closes #1770. Partially does #1888.

* dvc: close anything opened

* test: finalize git repo explicitly to remove test dir later

This was previously an issue on Windows.
This commit is contained in:
Alexander Schepanovski
2019-04-25 14:55:39 +03:00
committed by Ruslan Kuprieiev
parent c0104e2e61
commit 7478e7d4b0
9 changed files with 91 additions and 69 deletions
+27 -1
View File
@@ -7,6 +7,7 @@ import shutil
import uuid
import tempfile
import logging
import warnings
from git import Repo
from git.exc import GitCommandNotFound
@@ -60,7 +61,8 @@ class TestDirFixture(object):
self._root_dir = os.path.realpath(root_dir)
def _pushd(self, d):
self._saved_dir = os.path.realpath(os.curdir)
if not hasattr(self, "_saved_dir"):
self._saved_dir = os.path.realpath(os.curdir)
os.chdir(d)
def _popd(self):
@@ -98,6 +100,21 @@ class TestDirFixture(object):
def tearDown(self):
self._popd()
try:
shutil.rmtree(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):
@@ -122,6 +139,9 @@ class TestGitFixture(TestDirFixture):
self.git.index.add([self.CODE])
self.git.index.commit("add code")
def tearDown(self):
self.git.close()
class TestGitSubmoduleFixture(TestGitFixture):
def __init__(self, root_dir=None):
@@ -145,6 +165,9 @@ class TestDvcFixture(TestGitFixture):
self.dvc.scm.commit("init dvc")
logger.setLevel("DEBUG")
def tearDown(self):
self.dvc.scm.git.close()
class TestDvcGitInitializedFixture(TestDvcFixture):
def __init__(self, root_dir=None):
@@ -196,6 +219,9 @@ class TestDvcDataFileFixture(TestDvcGitInitializedFixture):
)
self.git.index.commit("Hello world commit")
# Return to the dir we started to not confuse parent fixture
os.chdir(self._saved_dir)
# NOTE: Inheritance order in the classes below is important.