Beispiel #1
0
 def test_create_file(self):
     path = self.temp_path("a/b/c.txt")
     self.assertFalse(fsutil.exists(path))
     fsutil.create_file(path, content="hello world")
     self.assertTrue(fsutil.exists(path))
     self.assertTrue(fsutil.is_file(path))
     self.assertEqual(fsutil.read_file(path), "hello world")
Beispiel #2
0
 def test_rename_file(self):
     path = self.temp_path('a/b/c.txt')
     fsutil.create_file(path)
     fsutil.rename_file(path, 'd.txt.backup')
     self.assertFalse(fsutil.exists(path))
     path = self.temp_path('a/b/d.txt.backup')
     self.assertTrue(fsutil.exists(path))
Beispiel #3
0
 def test_rename_file_basename(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path)
     fsutil.rename_file_basename(path, "d")
     self.assertFalse(fsutil.exists(path))
     path = self.temp_path("a/b/d.txt")
     self.assertTrue(fsutil.exists(path))
Beispiel #4
0
 def test_rename_file_extension(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path)
     fsutil.rename_file_extension(path, "json")
     self.assertFalse(fsutil.exists(path))
     path = self.temp_path("a/b/c.json")
     self.assertTrue(fsutil.exists(path))
Beispiel #5
0
 def test_is_empty_file(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path)
     self.assertTrue(fsutil.is_empty_file(path))
     path = self.temp_path("a/b/d.txt")
     fsutil.create_file(path, content="hello world")
     self.assertFalse(fsutil.is_empty_file(path))
Beispiel #6
0
 def test_make_dirs_for_file_with_existing_file(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path)
     fsutil.make_dirs_for_file(path)
     self.assertTrue(fsutil.is_dir(self.temp_path("a/b/")))
     self.assertFalse(fsutil.is_dir(path))
     self.assertTrue(fsutil.is_file(path))
Beispiel #7
0
 def test_get_file_creation_date_formatted(self):
     path = self.temp_path('a/b/c.txt')
     fsutil.create_file(path, content='Hello World')
     creation_date_str = fsutil.get_file_creation_date_formatted(
         path, format='%Y/%m/%d')
     creation_date_re = re.compile(r'^[\d]{4}\/[\d]{2}\/[\d]{2}$')
     self.assertTrue(creation_date_re.match(creation_date_str))
Beispiel #8
0
 def test_get_file_last_modified_date_formatted(self):
     path = self.temp_path('a/b/c.txt')
     fsutil.create_file(path, content='Hello World')
     lastmod_date_str = fsutil.get_file_last_modified_date_formatted(path)
     lastmod_date_re = re.compile(
         r'^[\d]{4}\-[\d]{2}\-[\d]{2}[\s]{1}[\d]{2}\:[\d]{2}\:[\d]{2}$')
     self.assertTrue(lastmod_date_re.match(lastmod_date_str))
Beispiel #9
0
 def test_rename_file_with_existing_name(self):
     path = self.temp_path("a/b/c")
     fsutil.create_file(path)
     path = self.temp_path("a/b/d")
     fsutil.create_file(path)
     with self.assertRaises(OSError):
         fsutil.rename_file(path, "c")
Beispiel #10
0
 def test_move_file(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path, content="Hello World")
     dest = self.temp_path("a")
     fsutil.move_file(path, dest)
     self.assertFalse(fsutil.exists(path))
     self.assertTrue(fsutil.is_file(self.temp_path("a/c.txt")))
Beispiel #11
0
 def test_is_empty(self):
     fsutil.create_file(self.temp_path("a/b/c.txt"))
     fsutil.create_file(self.temp_path("a/b/d.txt"), content="1")
     fsutil.create_dir(self.temp_path("a/b/e"))
     self.assertTrue(fsutil.is_empty(self.temp_path("a/b/c.txt")))
     self.assertFalse(fsutil.is_empty(self.temp_path("a/b/d.txt")))
     self.assertTrue(fsutil.is_empty(self.temp_path("a/b/e")))
     self.assertFalse(fsutil.is_empty(self.temp_path("a/b")))
Beispiel #12
0
 def test_is_empty(self):
     fsutil.create_file(self.temp_path('a/b/c.txt'))
     fsutil.create_file(self.temp_path('a/b/d.txt'), content='1')
     fsutil.create_dir(self.temp_path('a/b/e'))
     self.assertTrue(fsutil.is_empty(self.temp_path('a/b/c.txt')))
     self.assertFalse(fsutil.is_empty(self.temp_path('a/b/d.txt')))
     self.assertTrue(fsutil.is_empty(self.temp_path('a/b/e')))
     self.assertFalse(fsutil.is_empty(self.temp_path('a/b')))
Beispiel #13
0
 def test_get_file_creation_date_formatted(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path, content="Hello World")
     creation_date_str = fsutil.get_file_creation_date_formatted(
         path, format="%Y/%m/%d"
     )
     creation_date_re = re.compile(r"^[\d]{4}\/[\d]{2}\/[\d]{2}$")
     self.assertTrue(creation_date_re.match(creation_date_str))
Beispiel #14
0
 def test_copy_file(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path, content="hello world")
     dest = self.temp_path("x/y/z.txt")
     fsutil.copy_file(path, dest)
     self.assertTrue(fsutil.is_file(path))
     self.assertTrue(fsutil.is_file(dest))
     self.assertEqual(fsutil.get_file_hash(path), fsutil.get_file_hash(dest))
Beispiel #15
0
 def test_is_empty_dir(self):
     path = self.temp_path("a/b/")
     fsutil.create_dir(path)
     self.assertTrue(fsutil.is_empty_dir(path))
     filepath = self.temp_path("a/b/c.txt")
     fsutil.create_file(filepath)
     self.assertTrue(fsutil.is_file(filepath))
     self.assertFalse(fsutil.is_empty_dir(path))
Beispiel #16
0
 def test_list_dirs(self):
     for i in range(0, 5):
         fsutil.create_dir(self.temp_path('a/b/c/d-{}'.format(i)))
         fsutil.create_file(self.temp_path('a/b/c/f-{}'.format(i)),
                            content='{}'.format(i))
     dirpaths = fsutil.list_dirs(self.temp_path('a/b/c'))
     dirnames = [fsutil.split_path(dirpath)[-1] for dirpath in dirpaths]
     self.assertEqual(len(dirpaths), 5)
     self.assertEqual(dirnames, ['d-0', 'd-1', 'd-2', 'd-3', 'd-4'])
Beispiel #17
0
 def test_is_dir(self):
     path = self.temp_path('a/b/')
     self.assertFalse(fsutil.is_dir(path))
     fsutil.create_dir(path)
     self.assertTrue(fsutil.is_dir(path))
     path = self.temp_path('a/b/c.txt')
     self.assertFalse(fsutil.is_dir(path))
     fsutil.create_file(path)
     self.assertFalse(fsutil.is_dir(path))
Beispiel #18
0
 def test_exists(self):
     path = self.temp_path("a/b/")
     self.assertFalse(fsutil.exists(path))
     fsutil.create_dir(path)
     self.assertTrue(fsutil.exists(path))
     path = self.temp_path("a/b/c.txt")
     self.assertFalse(fsutil.exists(path))
     fsutil.create_file(path)
     self.assertTrue(fsutil.exists(path))
Beispiel #19
0
 def test_delete_file(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(self.temp_path("a/b/c.txt"))
     self.assertTrue(fsutil.exists(path))
     deleted = fsutil.delete_file(self.temp_path("a/b/d.txt"))
     self.assertFalse(deleted)
     deleted = fsutil.delete_file(path)
     self.assertTrue(deleted)
     self.assertFalse(fsutil.exists(path))
Beispiel #20
0
 def test_remove_file(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(self.temp_path("a/b/c.txt"))
     self.assertTrue(fsutil.exists(path))
     removed = fsutil.remove_file(self.temp_path("a/b/d.txt"))
     self.assertFalse(removed)
     removed = fsutil.remove_file(path)
     self.assertTrue(removed)
     self.assertFalse(fsutil.exists(path))
Beispiel #21
0
 def test_list_files(self):
     for i in range(0, 5):
         fsutil.create_dir(self.temp_path('a/b/c/d-{}'.format(i)))
         fsutil.create_file(self.temp_path('a/b/c/f-{}.txt'.format(i)),
                            content='{}'.format(i))
     filepaths = fsutil.list_files(self.temp_path('a/b/c'))
     filenames = [fsutil.get_filename(filepath) for filepath in filepaths]
     self.assertEqual(len(filepaths), 5)
     self.assertEqual(
         filenames, ['f-0.txt', 'f-1.txt', 'f-2.txt', 'f-3.txt', 'f-4.txt'])
Beispiel #22
0
 def test_list_dirs(self):
     for i in range(0, 5):
         fsutil.create_dir(self.temp_path("a/b/c/d-{}".format(i)))
         fsutil.create_file(
             self.temp_path("a/b/c/f-{}".format(i)), content="{}".format(i)
         )
     dirpaths = fsutil.list_dirs(self.temp_path("a/b/c"))
     dirnames = [fsutil.split_path(dirpath)[-1] for dirpath in dirpaths]
     self.assertEqual(len(dirpaths), 5)
     self.assertEqual(dirnames, ["d-0", "d-1", "d-2", "d-3", "d-4"])
Beispiel #23
0
 def test_extract_zip_file_with_autodelete(self):
     zip_path = self.temp_path("archive.zip")
     unzip_path = self.temp_path("unarchive/")
     path = self.temp_path("f1.txt")
     fsutil.create_file(path, content="hello world 1")
     fsutil.create_zip_file(zip_path, [path])
     fsutil.extract_zip_file(zip_path, unzip_path, autodelete=True)
     self.assertTrue(fsutil.is_dir(unzip_path))
     self.assertTrue(fsutil.is_file(self.temp_path("unarchive/f1.txt")))
     self.assertFalse(fsutil.is_file(zip_path))
Beispiel #24
0
 def test_get_file_creation_date(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path, content="Hello World")
     creation_date = fsutil.get_file_creation_date(path)
     now = dt.datetime.now()
     self.assertTrue((now - creation_date) < dt.timedelta(seconds=0.1))
     time.sleep(0.2)
     creation_date = fsutil.get_file_creation_date(path)
     now = dt.datetime.now()
     self.assertFalse((now - creation_date) < dt.timedelta(seconds=0.1))
Beispiel #25
0
 def test_get_file_last_modified_date(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path, content="Hello")
     creation_date = fsutil.get_file_creation_date(path)
     time.sleep(0.2)
     fsutil.write_file(path, content="Goodbye", append=True)
     now = dt.datetime.now()
     lastmod_date = fsutil.get_file_last_modified_date(path)
     self.assertTrue((now - lastmod_date) < dt.timedelta(seconds=0.1))
     self.assertTrue((lastmod_date - creation_date) > dt.timedelta(seconds=0.15))
Beispiel #26
0
 def test_get_dir_last_modified_date_formatted(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(path, content="Hello World")
     lastmod_date_str = fsutil.get_dir_last_modified_date_formatted(
         self.temp_path("a")
     )
     lastmod_date_re = re.compile(
         r"^[\d]{4}\-[\d]{2}\-[\d]{2}[\s]{1}[\d]{2}\:[\d]{2}\:[\d]{2}$"
     )
     self.assertTrue(lastmod_date_re.match(lastmod_date_str))
Beispiel #27
0
 def test_extract_zip_file_with_autodelete(self):
     zip_path = self.temp_path('archive.zip')
     unzip_path = self.temp_path('unarchive/')
     path = self.temp_path('f1.txt')
     fsutil.create_file(path, content='hello world 1')
     fsutil.create_zip_file(zip_path, [path])
     fsutil.extract_zip_file(zip_path, unzip_path, autodelete=True)
     self.assertTrue(fsutil.is_dir(unzip_path))
     self.assertTrue(fsutil.is_file(self.temp_path('unarchive/f1.txt')))
     self.assertFalse(fsutil.is_file(zip_path))
Beispiel #28
0
 def test_clean_dir_dirs_and_files(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(self.temp_path("a/b/c/f1.txt"))
     fsutil.create_file(self.temp_path("a/b/c/f2.txt"))
     fsutil.create_file(self.temp_path("a/b/c/f3.txt"))
     fsutil.create_file(self.temp_path("a/b/c/d/f4.txt"))
     fsutil.create_file(self.temp_path("a/b/c/d/f5.txt"))
     fsutil.clean_dir(self.temp_path("a"), dirs=True, files=True)
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c/d/f5.txt")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c/d/f4.txt")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f3.txt")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f2.txt")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f1.txt")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b")))
     self.assertTrue(fsutil.exists(self.temp_path("a")))
Beispiel #29
0
 def test_copy_dir_with_overwrite(self):
     fsutil.create_file(self.temp_path("a/b/f-1.txt"))
     fsutil.create_file(self.temp_path("a/b/f-2.txt"))
     fsutil.create_file(self.temp_path("a/b/f-3.txt"))
     fsutil.create_file(self.temp_path("x/y/z/f-0.txt"))
     fsutil.copy_dir(self.temp_path("a/b"), self.temp_path("x/y/z"), overwrite=False)
     with self.assertRaises(OSError):
         fsutil.copy_dir(
             self.temp_path("a/b"), self.temp_path("x/y/z"), overwrite=False
         )
     fsutil.copy_dir(self.temp_path("a/b"), self.temp_path("x/y/z"), overwrite=True)
Beispiel #30
0
 def test_clean_dir_only_files(self):
     path = self.temp_path("a/b/c.txt")
     fsutil.create_file(self.temp_path("a/b/c/f1.txt"), content="hello world")
     fsutil.create_file(self.temp_path("a/b/c/f2.txt"))
     fsutil.create_file(self.temp_path("a/b/c/f3.txt"), content="hello world")
     fsutil.create_file(self.temp_path("a/b/c/f4.txt"))
     fsutil.create_file(self.temp_path("a/b/c/f5.txt"), content="hello world")
     fsutil.clean_dir(self.temp_path("a"), dirs=False, files=False)
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f1.txt")))
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f2.txt")))
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f3.txt")))
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f4.txt")))
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f5.txt")))
     fsutil.clean_dir(self.temp_path("a"), dirs=False, files=True)
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f1.txt")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f2.txt")))
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f3.txt")))
     self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f4.txt")))
     self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f5.txt")))