gh-21 Initial unit tests for FileSystem.create_directory method

This commit is contained in:
Jaisen Mathai 2015-12-07 21:33:59 -08:00
parent 5b050fe485
commit 0a72ef481e
2 changed files with 62 additions and 2 deletions

View File

@ -21,8 +21,17 @@ class FileSystem:
@param, directory_name, string, A fully qualified path of the directory to create.
"""
def create_directory(self, directory_path):
if not os.path.exists(directory_path):
os.makedirs(directory_path)
try:
if os.path.exists(directory_path):
return True
else:
os.makedirs(directory_path)
return True
except OSError:
# OSError is thrown for cases like no permission
pass
return False
"""
Delete a directory only if it's empty.

View File

@ -0,0 +1,51 @@
# Project imports
import os
import sys
import random
import shutil
import string
import tempfile
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
from elodie import filesystem
filesystem = filesystem.FileSystem()
def test_create_directory_success():
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10))
status = filesystem.create_directory(folder)
# Needs to be a subdirectory
assert tempfile.gettempdir() != folder
assert status == True
assert os.path.isdir(folder) == True
assert os.path.exists(folder) == True
# Clean up
shutil.rmtree(folder)
def test_create_directory_recursive_success():
folder = '%s/%s/%s' % (tempfile.gettempdir(), random_string(10), random_string(10))
status = filesystem.create_directory(folder)
# Needs to be a subdirectory
assert tempfile.gettempdir() != folder
assert status == True
assert os.path.isdir(folder) == True
assert os.path.exists(folder) == True
shutil.rmtree(folder)
def test_create_directory_invalid_permissions():
status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist')
assert status == False
def random_string(length):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length))