def test_multipleDefaultFolders(self): '''SetupMultiple default folders, and see that they are cleaned up with cleanupForExit''' # create the first target firstTarget = tempfile.mkdtemp(dir='/tmp') firstResult = tempFolderManager.setDefaultFolder(firstTarget) self.assertTrue( os.path.samefile(firstTarget, firstResult), 'When setDefaultFolder was given a target location of "%s" it returned: %s' % (firstTarget, firstResult)) generateSomeContent(firstTarget) # create the second target secondTarget = tempfile.mkdtemp(dir='/tmp') self.assertNotEqual( firstTarget, secondTarget, 'Error in tempfile module: two calls to mkdtemp returned the same result' ) secondResult = tempFolderManager.setDefaultFolder(secondTarget) self.assertTrue( os.path.samefile(secondTarget, secondResult), 'When setDefaultFolder was given a target location of "%s" it returned: %s' % (firstTarget, firstResult)) generateSomeContent(secondTarget) # clean up everything tempFolderManager.cleanupForExit() # verify that the targets went away self.assertFalse( os.path.exists(firstTarget) or os.path.exists(secondTarget), 'Both folders were not deleted when cleanupForExit was called: %s %s' % (firstTarget, secondTarget))
def test_cleanupForExit(self): '''Confirm that cleanupForExit works as expected''' # create a few items, so we know they are there mainTempFolder = tempFolderManager.getDefaultFolder() generateSomeContent(mainTempFolder) self.assertTrue( len(os.listdir(mainTempFolder)) > 0, 'generateSomeContent failed to generate any test content') # copy the list of managed paths cachedItemPaths = list(tempFolderManager.managedItems) # call cleanupAtExit tempFolderManager.cleanupForExit() # confirm that everything has been deleted, and the variables cleaned up for thisItem in cachedItemPaths: self.assertFalse( os.path.exists(thisItem), 'cleanupAtExit left an item un-deleted: ' + str(thisItem)) self.assertEquals( tempFolderManager.managedItems, [], 'cleanupAtExit left the managedItems variable with a value: ' + str(tempFolderManager.managedItems)) self.assertTrue( tempFolderManager.defaultFolder is None, 'cleanupAtExit left the defaultFolder variable with a value: ' + str(tempFolderManager.defaultFolder))
def test_badDefaultFolders(self): '''These tests should all fail, and are designed to make sure that we can't wrongly delete things''' self.assertRaises(ValueError, tempFolderManager.setDefaultFolder, '/tmp/this-should-not-exist/inner') # parent folder does not exist # create a folder with contents existingFolder = tempfile.mkdtemp(dir='/tmp') generateSomeContent(existingFolder, maxFilesInFolders=1, maxSubFolders=0) self.assertRaises(ValueError, tempFolderManager.setDefaultFolder, existingFolder) # folder already exists shutil.rmtree(existingFolder)
def test_badDefaultFolders(self): '''These tests should all fail, and are designed to make sure that we can't wrongly delete things''' self.assertRaises( ValueError, tempFolderManager.setDefaultFolder, '/tmp/this-should-not-exist/inner') # parent folder does not exist # create a folder with contents existingFolder = tempfile.mkdtemp(dir='/tmp') generateSomeContent(existingFolder, maxFilesInFolders=1, maxSubFolders=0) self.assertRaises(ValueError, tempFolderManager.setDefaultFolder, existingFolder) # folder already exists shutil.rmtree(existingFolder)
def test_cleanupForExit(self): '''Confirm that cleanupForExit works as expected''' # create a few items, so we know they are there mainTempFolder = tempFolderManager.getDefaultFolder() generateSomeContent(mainTempFolder) self.assertTrue(len(os.listdir(mainTempFolder)) > 0, 'generateSomeContent failed to generate any test content') # copy the list of managed paths cachedItemPaths = list(tempFolderManager.managedItems) # call cleanupAtExit tempFolderManager.cleanupForExit() # confirm that everything has been deleted, and the variables cleaned up for thisItem in cachedItemPaths: self.assertFalse(os.path.exists(thisItem), 'cleanupAtExit left an item un-deleted: ' + str(thisItem)) self.assertEquals(tempFolderManager.managedItems, [], 'cleanupAtExit left the managedItems variable with a value: ' + str(tempFolderManager.managedItems)) self.assertTrue(tempFolderManager.defaultFolder is None, 'cleanupAtExit left the defaultFolder variable with a value: ' + str(tempFolderManager.defaultFolder))
def test_multipleDefaultFolders(self): '''SetupMultiple default folders, and see that they are cleaned up with cleanupForExit''' # create the first target firstTarget = tempfile.mkdtemp(dir='/tmp') firstResult = tempFolderManager.setDefaultFolder(firstTarget) self.assertTrue(os.path.samefile(firstTarget, firstResult), 'When setDefaultFolder was given a target location of "%s" it returned: %s' % (firstTarget, firstResult)) generateSomeContent(firstTarget) # create the second target secondTarget = tempfile.mkdtemp(dir='/tmp') self.assertNotEqual(firstTarget, secondTarget, 'Error in tempfile module: two calls to mkdtemp returned the same result') secondResult = tempFolderManager.setDefaultFolder(secondTarget) self.assertTrue(os.path.samefile(secondTarget, secondResult), 'When setDefaultFolder was given a target location of "%s" it returned: %s' % (firstTarget, firstResult)) generateSomeContent(secondTarget) # clean up everything tempFolderManager.cleanupForExit() # verify that the targets went away self.assertFalse(os.path.exists(firstTarget) or os.path.exists(secondTarget), 'Both folders were not deleted when cleanupForExit was called: %s %s' % (firstTarget, secondTarget))
def test_withStatementFunction(self): '''Test the use of items with the "with" statement''' location = None with tempFolderManager() as thisTempFolder: self.assertTrue(isinstance(thisTempFolder, tempFolderManager), 'While using a with statement a tempFolderManager item was not created correctly') location = thisTempFolder.getPath() self.assertTrue(location is not None, 'When using a with statement getPath method returned None') self.assertTrue(os.path.isdir(location), 'When using a with statement getPath method returned a path that was not an existing directory') self.assertTrue(tempFolderManager.getManagedPathForPath(location) is not None, 'When using a with statement getPath returned a path that was not in a managed item') # create some contents to make it interesting generateSomeContent(location) # outside the with statement the item should have auto-cleaned iteself self.assertFalse(os.path.exists(location), 'After exiting a with statement the item was not properly cleaned up') # repeat the same exercise with a preset location location = tempfile.mkdtemp(dir='/tmp') with tempFolderManager(location) as thisTempFolder: self.assertTrue(isinstance(thisTempFolder, tempFolderManager), 'While using a with statement and a preset location a tempFolderManager item was not created correctly') returnedLocation = thisTempFolder.getPath() self.assertTrue(returnedLocation is not None, 'When using a with statement and a preset location getPath method returned None') self.assertTrue(os.path.samefile(returnedLocation, location), 'When using a with statement and a preset location getPath did not return the expected location: "%s" vs. "%s"' % (location, returnedLocation)) self.assertTrue(os.path.isdir(location), 'When using a with statement and a preset location getPath method returned a path that was not an existing directory') self.assertTrue(tempFolderManager.getManagedPathForPath(location) is not None, 'When using a with statement and a preset location getPath returned a path that was not in a managed item') # create some contents to make it interesting generateSomeContent(location) # outside the with statement the item should have auto-cleaned iteself self.assertFalse(os.path.exists(location), 'After exiting a with statement the item was not properly cleaned up')
def test_withStatementFunction(self): '''Test the use of items with the "with" statement''' location = None with tempFolderManager() as thisTempFolder: self.assertTrue( isinstance(thisTempFolder, tempFolderManager), 'While using a with statement a tempFolderManager item was not created correctly' ) location = thisTempFolder.getPath() self.assertTrue( location is not None, 'When using a with statement getPath method returned None') self.assertTrue( os.path.isdir(location), 'When using a with statement getPath method returned a path that was not an existing directory' ) self.assertTrue( tempFolderManager.getManagedPathForPath(location) is not None, 'When using a with statement getPath returned a path that was not in a managed item' ) # create some contents to make it interesting generateSomeContent(location) # outside the with statement the item should have auto-cleaned iteself self.assertFalse( os.path.exists(location), 'After exiting a with statement the item was not properly cleaned up' ) # repeat the same exercise with a preset location location = tempfile.mkdtemp(dir='/tmp') with tempFolderManager(location) as thisTempFolder: self.assertTrue( isinstance(thisTempFolder, tempFolderManager), 'While using a with statement and a preset location a tempFolderManager item was not created correctly' ) returnedLocation = thisTempFolder.getPath() self.assertTrue( returnedLocation is not None, 'When using a with statement and a preset location getPath method returned None' ) self.assertTrue( os.path.samefile(returnedLocation, location), 'When using a with statement and a preset location getPath did not return the expected location: "%s" vs. "%s"' % (location, returnedLocation)) self.assertTrue( os.path.isdir(location), 'When using a with statement and a preset location getPath method returned a path that was not an existing directory' ) self.assertTrue( tempFolderManager.getManagedPathForPath(location) is not None, 'When using a with statement and a preset location getPath returned a path that was not in a managed item' ) # create some contents to make it interesting generateSomeContent(location) # outside the with statement the item should have auto-cleaned iteself self.assertFalse( os.path.exists(location), 'After exiting a with statement the item was not properly cleaned up' )