コード例 #1
0
 def test_replace_existing_file_on_rename(self):
     # The temporary files need to be closed, but not deleted when .close()
     # is called. Otherwise, Windows will raise a PermissionError.
     tf1 = tempfile.NamedTemporaryFile(delete=False)
     tf1_file_name = File(tf1.name).name
     tf2 = tempfile.NamedTemporaryFile(delete=False)
     
     tf1.close()
     tf2.close()
     
     # Try to rename the second temporary file to that of the first
     with TemporaryFileHandler(tf1):
         with TemporaryFileHandler(tf2):
             f = File(tf2.name)
             try:
                 f.rename(tf1_file_name, replace_existing_file=True)
             except FileExistsError:
                 self.fail(
                     "The file should have been replaced, but a "
                     "FileExistsError was raised instead."
                 )
             else:
                 self.assertFalse(os.path.exists(tf2.name))
                 self.assertTrue(os.path.exists(tf1.name))
     
     return
コード例 #2
0
 def test_raise_exception_on_rename_to_existing_path(self):
     # The Existing Path Refers to an Existing File
     #
     # Create two temporary files
     tf1 = tempfile.NamedTemporaryFile()
     tf1_file_name = File(tf1.name).name
     tf2 = tempfile.NamedTemporaryFile()
     # Try to rename the second temporary file to that of the first
     with TemporaryFileHandler(tf1):
         with TemporaryFileHandler(tf2):
             f = File(tf2.name)
             with self.assertRaises(FileExistsError, msg="Existing file assert failed"):
                 f.rename(tf1_file_name)
     
     # The Existing Path Refers to an Existing Directory
     tf = tempfile.NamedTemporaryFile()
     td = tempfile.TemporaryDirectory()
     td_name = os.path.split(td.name)[1]
     # Try to rename the temporary file to that of the temporary directory
     with TemporaryDirectoryHandler(td):
         with TemporaryFileHandler(tf):
             f = File(tf.name)
             with self.assertRaises(IsADirectoryError, msg="Existing directory assert failed"):
                 f.rename(td_name)                
     
     return
コード例 #3
0
 def test_rename_file(self):
     tf = tempfile.NamedTemporaryFile()
     with TemporaryFileHandler(tf):
         # The base directory will be the same for both the original and 
         # renamed file.
         BASE_DIRECTORY = str(pathlib.Path(tf.name).parent)
         
         # Rename the File
         renamed_file_name = utils.get_random_file_name(BASE_DIRECTORY)
         expected_new_file_path = os.path.join(BASE_DIRECTORY, 
                                               renamed_file_name)
         
         # Check if the file already exists. It should only exist if this 
         # file's tests were run previously. In such a case, it is fine to
         # just delete the file.
         if os.path.exists(expected_new_file_path):
             os.remove(expected_new_file_path)
         
         self.assertFalse(
             os.path.exists(expected_new_file_path), 
             msg="The file can't be renamed because the path already exists"
         )
         
         f = File(tf.name)
         f.rename(renamed_file_name)
         self.assertTrue(
             os.path.exists(expected_new_file_path), 
             msg="The file was not renamed"
         )
         
     return
コード例 #4
0
 def test_rename_updates_path(self):
     """If a file object is renamed, then its path should be updated"""
     tf = tempfile.NamedTemporaryFile()
     with TemporaryFileHandler(tf):
         BASE_DIRECTORY = str(pathlib.Path(tf.name).parent)
         
         f = File(tf.name)
         new_file_name = utils.get_random_file_name(BASE_DIRECTORY)
         expected_path = os.path.join(BASE_DIRECTORY, new_file_name)            
         
         f.rename(new_file_name)
         self.assertEqual(f.path, expected_path)
             
     return