gh-21 Initial unit tests for FileSystem.create_directory method
This commit is contained in:
		
							parent
							
								
									5b050fe485
								
							
						
					
					
						commit
						0a72ef481e
					
				@ -21,8 +21,17 @@ class FileSystem:
 | 
				
			|||||||
    @param, directory_name, string, A fully qualified path of the directory to create.
 | 
					    @param, directory_name, string, A fully qualified path of the directory to create.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    def create_directory(self, directory_path):
 | 
					    def create_directory(self, directory_path):
 | 
				
			||||||
        if not os.path.exists(directory_path):
 | 
					        try:
 | 
				
			||||||
            os.makedirs(directory_path)
 | 
					            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.
 | 
					    Delete a directory only if it's empty.
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										51
									
								
								elodie/tests/filesystem.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								elodie/tests/filesystem.py
									
									
									
									
									
										Normal 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))
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user