def test_delete_folder_contents_case_5(self): """Test delete_folder_contents() when a folder path doesn't exist. Case 5 consists in testing that :meth:`~pyutils.genutils.delete_folder_contents` raises an :exc:`OSError` exception when the folder to be cleared doesn't exist. See Also -------- populate_folder : populates a folder with text files and subdirectories. """ self.logger.warning("\n\n<color>test_delete_folder_contents_case_5()" "</color>") self.logger.info("Testing <color>case 5 of delete_folder_contents()" "</color> when a folder path doesn't exist...") try: # Delete everything in the directory that doesn't exist delete_folder_contents(folderpath=os.path.join( self.sandbox_tmpdir, "fakedir"), remove_subdirs=True, delete_recursively=False) except OSError as e: self.logger.info("Raised an OSError exception as expected: " "{}".format(get_error_msg(e))) else: self.fail("An OSError exception was not raised as expected")
def test_delete_folder_contents_case_4(self): """Test that delete_folder_contents() removes everything in a folder with `delete_recursively` set to True. Case 4 consists in testing that :meth:`~pyutils.genutils.delete_folder_contents` removes everything in a folder with the flag `delete_recursively` set to False. Thus, at the end, only the empty root directory should be left. See Also -------- populate_folder : populates a folder with text files and subdirectories. Notes ----- Case 4 sets `remove_subdirs` to True and `delete_recursively` to True. Case 4 is similar to case 1 where everything is deleted but with `remove_subdirs` set to True and `delete_recursively` to False. """ self.logger.warning("\n\n<color>test_delete_folder_contents_case_4()" "</color>") self.logger.info("Testing <color>case 4 of delete_folder_contents()" "</color> where delete_recursively is True ...") # Create the main test directory along with subdirectories and files dirpath = self.populate_folder() # Delete everything recursively in the main test directory delete_folder_contents(folderpath=dirpath, remove_subdirs=True, delete_recursively=True) # Test that the main test directory is empty msg = "The folder {} couldn't be cleared".format(dirpath) self.assertTrue(len(os.listdir(dirpath)) == 0, msg) self.logger.info("The folder {} is empty".format(dirpath))
def test_delete_folder_contents_case_1(self): """Test that delete_folder_contents() removes everything in a folder. Case 1 consists in testing that :meth:`~pyutils.genutils.delete_folder_contents` removes everything in a folder, including the files and subdirectories at the root of the given folder. See Also -------- populate_folder : populates a folder with text files and subdirectories. Notes ----- Case 1 sets `remove_subdirs` to True and `delete_recursively` to False. """ self.logger.warning("\n\n<color>test_delete_folder_contents_case_1()" "</color>") self.logger.info("Testing <color>case 1 of delete_folder_contents()" "</color> where everything in a folder must be " "removed...") # Create the main test directory along with subdirectories and files dirpath = self.populate_folder() # Delete the whole content of the main test directory delete_folder_contents(dirpath) # Test that the main test directory is empty msg = "The folder {} couldn't be cleared".format(dirpath) self.assertTrue(len(os.listdir(dirpath)) == 0, msg) self.logger.info("The folder {} is empty".format(dirpath))
def tearDown(cls): """TODO """ # Cleanup temporary directories # TODO: explain if condition if cls.sandbox_tmpdir and __package__ != pyutils.__package__: logger.info("\nCleanup...") delete_folder_contents(cls.sandbox_tmpdir)
def tearDown(cls): """TODO """ cls.logger.info("Cleanup...") # Cleanup temporary directories # TODO: explain the if... if cls.sandbox_tmpdir: delete_folder_contents(cls.sandbox_tmpdir)
def test_delete_folder_contents_case_2(self): """Test that delete_folder_contents() removes everything at the root of a directory except subdirectories and their contents. Case 2 consists in testing that :meth:`~pyutils.genutils.delete_folder_contents` removes everything in a folder, except the subdirectories and their contents at the root of the given folder. See Also -------- populate_folder : populates a folder with text files and subdirectories. Notes ----- Case 2 sets `remove_subdirs` to False and `delete_recursively` to False. """ self.logger.warning("\n\n<color>test_delete_folder_contents_case_2()" "</color>") self.logger.info("Testing <color>case 2 of delete_folder_contents()" "</color> where everything in a folder must be " "removed except the subdirectories and their " "contents...") # Create the main test directory along with subdirectories and files dirpath = self.populate_folder() # Delete everything in the main test directory, except subdirectories # and their contents delete_folder_contents(dirpath, remove_subdirs=False) # Test that the folder only has subdirectories but no files at its root for root, dirs, files in os.walk(dirpath): if root == dirpath: msg = "There is a file at the top of the directory" self.assertTrue(len(files) == 0, msg) else: msg = "There is a subdirectory that is empty" self.assertTrue(len(files) > 0, msg) self.logger.info( "No files found at the top and the subdirectories are " "not empty".format(dirpath))
def test_delete_folder_contents_case_3(self): """Test that delete_folder_contents() removes all text files recursively, except subdirectories. Case 3 consists in testing that :meth:`~pyutils.genutils.delete_folder_contents` removes all text files recursively, except subdirectories. Thus, at the end, anything left should be empty subdirectories and the root directory. See Also -------- populate_folder : populates a folder with text files and subdirectories. Notes ----- Case 3 sets `remove_subdirs` to False and `delete_recursively` to True. """ self.logger.warning("\n\n<color>test_delete_folder_contents_case_3()" "</color>") self.logger.info("Testing <color>case 3 of delete_folder_contents()" "</color> where every text files are removed even in " "the subdirectories...") # Create the main test directory along with subdirectories and files dirpath = self.populate_folder() # Delete all text files recursively in the main test directory, except # subdirectories delete_folder_contents(folderpath=dirpath, remove_subdirs=False, delete_recursively=True) # Test that only the subdirectories are left for root, dirs, files in os.walk(dirpath): msg = "There is still a file in the main test directory" self.assertTrue(len(files) == 0, msg) self.logger.info("All subdirectories are empty including the main " "directory".format(dirpath))