def test_safe_makedirs(self):
from softwarecenter.utils import safe_makedirs
from tempfile import mkdtemp
tmp = mkdtemp()
# create base dir
target = os.path.join(tmp, "foo", "bar")
safe_makedirs(target)
# we need the patch to ensure that the code is actually executed
with patch("os.path.exists") as mock_:
mock_.return_value = False
self.assertTrue(os.path.isdir(target))
# ensure that creating the base dir again does not crash
safe_makedirs(target)
self.assertTrue(os.path.isdir(target))
# ensure we still get regular errors like permission denied
# (stat.S_IRUSR)
os.chmod(os.path.join(tmp, "foo"), 0400)
self.assertRaises(OSError, safe_makedirs, target)
# set back to stat.(S_IRUSR|S_IWUSR|S_IXUSR) to make rmtree work
os.chmod(os.path.join(tmp, "foo"), 0700)
# cleanup
shutil.rmtree(tmp)
评论列表
文章目录