modify file.makedirs logical

This commit is contained in:
Prodesire 2017-12-01 08:09:39 +08:00
parent ffafe04d70
commit 3dcc82f416
2 changed files with 11 additions and 8 deletions

View File

@ -5,12 +5,12 @@ import shutil
# todo tests and docs
def makedirs(path, mode=0o755, ignore_errors=False, exist_ok=True):
if exist_ok and os.path.exists(path):
return
try:
os.makedirs(path, mode)
except Exception:
if not ignore_errors:
if not exist_ok:
raise OSError('{} is exist'.format(path))
raise OSError('Create dir: {} error.')

View File

@ -4,16 +4,19 @@ import pytest
class Testmakedirs():
def test_makedirs(self,tmpdir):
def test_makedirs(self, tmpdir):
path = str(tmpdir.join('test'))
makedirs(path)
assert os.path.exists(path)
def test_makedirs_with_exists_path(self,tmpdir):
def test_makedirs_with_exists_path(self, tmpdir):
path = str(tmpdir.join('test'))
makedirs(path)
with pytest.raises(Exception) as e_info:
makedirs(path, exist_ok=True)
makedirs(path, exist_ok=True)
with pytest.raises(Exception):
makedirs(path, exist_ok=False)
def test_makedirs_with_ignore_error(self, tmpdir):
path = str(tmpdir.join('test'))
@ -23,8 +26,8 @@ class Testmakedirs():
def test_makedirs_without_ignore_error(self, tmpdir):
path = str(tmpdir.join('test'))
makedirs(path)
with pytest.raises(Exception) as e_info:
makedirs(path, ignore_errors=False)
with pytest.raises(Exception):
makedirs(path, ignore_errors=False, exist_ok=False)
def test_makedirs_with_mutl_dirs(self, tmpdir):
path = str(tmpdir.join('test/test'))