def tearDown(self): '''Clean up the items we created for tests''' # cache folder if cacheController.writeableCacheFolder == self.cacheFolderPath: cacheController.removeCacheFolder() tempFolderManager.cleanupItem(self.cacheFolderPath) # source folders if self.firstSourceFolderPath in cacheController.sourceFolders: cacheController.removeSourceFolders(self.firstSourceFolderPath) tempFolderManager.cleanupItem(self.firstSourceFolderPath) if self.secondSourceFolderPath in cacheController.sourceFolders: cacheController.removeSourceFolders(self.secondSourceFolderPath) tempFolderManager.cleanupItem(self.secondSourceFolderPath) # verified files cacheController.verifiedFiles = {} self.cacheFolderPath = None self.firstSourceFolderPath = None self.secondSourceFolderPath = None self.testMaterials = None tempFolderManager.cleanupForExit()
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 tearDown(self): '''Clean up the items we created for tests''' # cache folder if cacheController.writeableCacheFolder == self.cacheFolderPath: cacheController.removeCacheFolder() tempFolderManager.cleanupItem(self.cacheFolderPath) # source folders if self.firstSourceFolderPath in cacheController.sourceFolders: cacheController.removeSourceFolders(self.firstSourceFolderPath) tempFolderManager.cleanupItem(self.firstSourceFolderPath) if self.secondSourceFolderPath in cacheController.sourceFolders: cacheController.removeSourceFolders(self.secondSourceFolderPath) tempFolderManager.cleanupItem(self.secondSourceFolderPath) # verified files cacheController.verifiedFiles = {} self.cacheFolderPath = None self.firstSourceFolderPath = None self.secondSourceFolderPath = None self.testMaterials = None tempFolderManager.cleanupForExit()
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_getNewTempFolder(self): '''Test the getNewTempFolder method''' # test without any input firstFolderPath = tempFolderManager.getNewTempFolder() self.assertTrue(firstFolderPath is not None, 'Called with no options getNewTempFolder gave back None') self.assertTrue(os.path.isdir(firstFolderPath), 'Called with no options getNewTempFolder returned a string that was not a path to an existing folder: ' + str(firstFolderPath)) self.assertTrue(tempFolderManager.getManagedPathForPath(firstFolderPath) is not None, 'Called with no options getNewTempFolder returned a path that was not in any managed path (according to getManagedPathForPath): ' + firstFolderPath) # test with the parent folder option secondParentFolder = tempfile.mkdtemp(dir='/tmp') secondFolderPath = tempFolderManager.getNewTempFolder(parentFolder=secondParentFolder) self.assertTrue(secondFolderPath is not None, 'Called with a parent folder getNewTempFolder gave back None') self.assertTrue(os.path.isdir(secondFolderPath), 'Called with a parent folder getNewTempFolder returned a string that was not a path to an existing folder: ' + str(secondFolderPath)) self.assertTrue(secondFolderPath.startswith(pathHelpers.normalizePath(secondParentFolder, followSymlink=True)), 'Called with a parent folder (%s) getNewTempFolder returned a path not in the parent folder: %s' % (secondParentFolder, secondFolderPath)) self.assertTrue(tempFolderManager.getManagedPathForPath(secondFolderPath) is not None, 'Called with a parent folder getNewTempFolder returned a path that was not in any managed path (according to getManagedPathForPath): ' + secondFolderPath) # test with the prefix option prefixOption = "thisIsATest" thirdFolderPath = tempFolderManager.getNewTempFolder(prefix=prefixOption) self.assertTrue(thirdFolderPath is not None, 'Called with the prefix option (%s) getNewTempFolder gave back None' % prefixOption) self.assertTrue(os.path.isdir(thirdFolderPath), 'Called with the prefix option (%s) getNewTempFolder returned a string that was not a path to an existing folder: %s' % (prefixOption, str(thirdFolderPath))) self.assertTrue(os.path.basename(thirdFolderPath).startswith(prefixOption), 'Called with the prefix option (%s) getNewTempFolder returned a path not in the parent folder: %s' % (prefixOption, thirdFolderPath)) self.assertTrue(tempFolderManager.getManagedPathForPath(secondFolderPath) is not None, 'Called with the prefix option (%s) getNewTempFolder returned a path that was not in any managed path (according to getManagedPathForPath): %s' % (prefixOption, thirdFolderPath)) # call cleanupAtExit to clear everything tempFolderManager.cleanupForExit() # verify that the folders dissapeared self.assertFalse(os.path.exists(firstFolderPath), 'After being created with getNewTempFolder using no options the folder path was not cleaned properly by cleanupForExit: ' + firstFolderPath) self.assertFalse(os.path.exists(secondFolderPath), 'After being created with getNewTempFolder using the parent folder the folder optionthe path was not cleaned properly by cleanupForExit: ' + secondFolderPath) self.assertFalse(os.path.exists(thirdFolderPath), 'After being created with getNewTempFolder using the prefix option (%s) the folder path was not cleaned properly by cleanupForExit: %s' % (prefixOption, thirdFolderPath)) # remove the tempdir we made for the parent folder test shutil.rmtree(secondParentFolder)
def test_plainFiles(self): '''Test that a random file will be deleted''' (fileHandle, filePath) = tempfile.mkstemp() os.close(fileHandle) # we don't need to write anything to this filePath = pathHelpers.normalizePath(filePath, followSymlink=True) # add it, and confirm that this is managed tempFolderManager.addManagedItem(filePath) self.assertTrue(filePath in tempFolderManager.managedItems, 'Adding a file using addManagedItem did not result in it being put in managedItems') self.assertTrue(tempFolderManager.getManagedPathForPath(filePath) is not None, 'Adding a file using addManagedItem did not make it managed (according to getManagedPathForPath)') # wipe this out using cleanupItem tempFolderManager.cleanupItem(filePath) self.assertFalse(os.path.exists(filePath), 'Removing a file added with addManagedItem with cleanupItem did not get rid of the file') # repeat the exercise for cleanupForExit (fileHandle, filePath) = tempfile.mkstemp() os.close(fileHandle) # we don't need to write anything to this filePath = pathHelpers.normalizePath(filePath, followSymlink=True) # add it, and confirm that this is managed tempFolderManager.addManagedItem(filePath) self.assertTrue(filePath in tempFolderManager.managedItems, 'Adding a file using addManagedItem did not result in it being put in managedItems') self.assertTrue(tempFolderManager.getManagedPathForPath(filePath) is not None, 'Adding a file using addManagedItem did not make it managed (according to getManagedPathForPath)') # wipe this out using cleanupItem tempFolderManager.cleanupForExit() self.assertFalse(os.path.exists(filePath), 'Removing a file added with addManagedItem with cleanupForExit did not get rid of the file')
def test_plainFiles(self): '''Test that a random file will be deleted''' (fileHandle, filePath) = tempfile.mkstemp() os.close(fileHandle) # we don't need to write anything to this filePath = pathHelpers.normalizePath(filePath, followSymlink=True) # add it, and confirm that this is managed tempFolderManager.addManagedItem(filePath) self.assertTrue( filePath in tempFolderManager.managedItems, 'Adding a file using addManagedItem did not result in it being put in managedItems' ) self.assertTrue( tempFolderManager.getManagedPathForPath(filePath) is not None, 'Adding a file using addManagedItem did not make it managed (according to getManagedPathForPath)' ) # wipe this out using cleanupItem tempFolderManager.cleanupItem(filePath) self.assertFalse( os.path.exists(filePath), 'Removing a file added with addManagedItem with cleanupItem did not get rid of the file' ) # repeat the exercise for cleanupForExit (fileHandle, filePath) = tempfile.mkstemp() os.close(fileHandle) # we don't need to write anything to this filePath = pathHelpers.normalizePath(filePath, followSymlink=True) # add it, and confirm that this is managed tempFolderManager.addManagedItem(filePath) self.assertTrue( filePath in tempFolderManager.managedItems, 'Adding a file using addManagedItem did not result in it being put in managedItems' ) self.assertTrue( tempFolderManager.getManagedPathForPath(filePath) is not None, 'Adding a file using addManagedItem did not make it managed (according to getManagedPathForPath)' ) # wipe this out using cleanupItem tempFolderManager.cleanupForExit() self.assertFalse( os.path.exists(filePath), 'Removing a file added with addManagedItem with cleanupForExit did not get rid of the file' )
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 tearDown(self): tempFolderManager.cleanupForExit()
def tearDown(self): tempFolderManager.cleanupForExit()
def test_getNewTempFolder(self): '''Test the getNewTempFolder method''' # test without any input firstFolderPath = tempFolderManager.getNewTempFolder() self.assertTrue( firstFolderPath is not None, 'Called with no options getNewTempFolder gave back None') self.assertTrue( os.path.isdir(firstFolderPath), 'Called with no options getNewTempFolder returned a string that was not a path to an existing folder: ' + str(firstFolderPath)) self.assertTrue( tempFolderManager.getManagedPathForPath(firstFolderPath) is not None, 'Called with no options getNewTempFolder returned a path that was not in any managed path (according to getManagedPathForPath): ' + firstFolderPath) # test with the parent folder option secondParentFolder = tempfile.mkdtemp(dir='/tmp') secondFolderPath = tempFolderManager.getNewTempFolder( parentFolder=secondParentFolder) self.assertTrue( secondFolderPath is not None, 'Called with a parent folder getNewTempFolder gave back None') self.assertTrue( os.path.isdir(secondFolderPath), 'Called with a parent folder getNewTempFolder returned a string that was not a path to an existing folder: ' + str(secondFolderPath)) self.assertTrue( secondFolderPath.startswith( pathHelpers.normalizePath(secondParentFolder, followSymlink=True)), 'Called with a parent folder (%s) getNewTempFolder returned a path not in the parent folder: %s' % (secondParentFolder, secondFolderPath)) self.assertTrue( tempFolderManager.getManagedPathForPath(secondFolderPath) is not None, 'Called with a parent folder getNewTempFolder returned a path that was not in any managed path (according to getManagedPathForPath): ' + secondFolderPath) # test with the prefix option prefixOption = "thisIsATest" thirdFolderPath = tempFolderManager.getNewTempFolder( prefix=prefixOption) self.assertTrue( thirdFolderPath is not None, 'Called with the prefix option (%s) getNewTempFolder gave back None' % prefixOption) self.assertTrue( os.path.isdir(thirdFolderPath), 'Called with the prefix option (%s) getNewTempFolder returned a string that was not a path to an existing folder: %s' % (prefixOption, str(thirdFolderPath))) self.assertTrue( os.path.basename(thirdFolderPath).startswith(prefixOption), 'Called with the prefix option (%s) getNewTempFolder returned a path not in the parent folder: %s' % (prefixOption, thirdFolderPath)) self.assertTrue( tempFolderManager.getManagedPathForPath(secondFolderPath) is not None, 'Called with the prefix option (%s) getNewTempFolder returned a path that was not in any managed path (according to getManagedPathForPath): %s' % (prefixOption, thirdFolderPath)) # call cleanupAtExit to clear everything tempFolderManager.cleanupForExit() # verify that the folders dissapeared self.assertFalse( os.path.exists(firstFolderPath), 'After being created with getNewTempFolder using no options the folder path was not cleaned properly by cleanupForExit: ' + firstFolderPath) self.assertFalse( os.path.exists(secondFolderPath), 'After being created with getNewTempFolder using the parent folder the folder optionthe path was not cleaned properly by cleanupForExit: ' + secondFolderPath) self.assertFalse( os.path.exists(thirdFolderPath), 'After being created with getNewTempFolder using the prefix option (%s) the folder path was not cleaned properly by cleanupForExit: %s' % (prefixOption, thirdFolderPath)) # remove the tempdir we made for the parent folder test shutil.rmtree(secondParentFolder)