43
47
m_patch = '{}.open'.format(mod_name)
44
48
with mock.patch(m_patch, m_open, create=True):
52
class CiTestCase(TestCase):
53
"""Common testing class which all curtin unit tests subclass."""
55
def add_patch(self, target, attr, **kwargs):
56
"""Patches specified target object and sets it as attr on test
57
instance also schedules cleanup"""
58
if 'autospec' not in kwargs:
59
kwargs['autospec'] = True
60
m = mock.patch(target, **kwargs)
62
self.addCleanup(m.stop)
63
setattr(self, attr, p)
65
def tmp_dir(self, dir=None, cleanup=True):
66
"""Return a full path to a temporary directory for the test run."""
68
tmpd = tempfile.mkdtemp(
69
prefix="curtin-ci-%s." % self.__class__.__name__)
71
tmpd = tempfile.mkdtemp(dir=dir)
72
self.addCleanup(shutil.rmtree, tmpd)
75
def tmp_path(self, path, _dir=None):
76
# return an absolute path to 'path' under dir.
77
# if dir is None, one will be created with tmp_dir()
78
# the file is not created or modified.
81
return os.path.normpath(os.path.abspath(os.path.join(_dir, path)))