def test_mirror_to(self): """ Not need in travis. """ p = Path(__file__).change(new_basename="app") dst = Path(__file__).change(new_basename="mirror") p.mirror_to(dst.abspath)
class TestRemoveFileOrDir(object): dir_here = Path(__file__).parent path_to_move_file = Path(dir_here, "to_move_file.txt") path_to_move_dir = Path(dir_here, "to_move_dir") path_to_move_dir_file = Path(dir_here, "to_move_dir", "to_move_file.txt") def setup_method(self, method): self.path_to_move_file.write_text("test") self.path_to_move_dir.mkdir_if_not_exists() self.path_to_move_dir_file.write_text("test") # def teardown_method(self, method): def test_remove(self): self.path_to_move_file.remove() assert self.path_to_move_file.exists() is False with pytest.raises(Exception): self.path_to_move_file.remove() def test_remove_if_exists(self): # remove a file self.path_to_move_file.remove_if_exists() assert self.path_to_move_file.exists() is False self.path_to_move_file.remove_if_exists() # remove a dir self.path_to_move_dir.remove_if_exists() assert self.path_to_move_dir.exists() is False self.path_to_move_dir.remove_if_exists() def test_dir_here(self): dir_here = Path.dir_here(__file__) assert dir_here.basename == "tests"
def test_select_by_pattern_in_fname(self): path = Path(__file__).absolute().parent # pathlibm_mate-project/tests for p in path.select_by_pattern_in_fname("test", case_sensitive=True): assert "test" in p.fname for p in path.select_by_pattern_in_fname("TEST", case_sensitive=False): assert "test" in p.fname.lower()
def test(self): p = Path(__file__) assert len({ p.md5, p.get_partial_md5(nbytes=1 << 20), p.sha256, p.get_partial_sha256(nbytes=1 << 20), p.sha512, p.get_partial_sha512(nbytes=1 << 20), }) == 3
def test_select(self): def filters(p): if p.fname.startswith("f"): return True else: return False path = Path(__file__).absolute().parent.parent # pathlibm_mate-project for p in path.select(filters): assert p.fname.startswith("f")
def test_select_by_time(self): path = Path(__file__).absolute().parent.parent # pathlibm_mate-project for p in path.select_by_atime(min_time=0): p.atime >= 0 for p in path.select_by_ctime(min_time=0): p.ctime >= 0 for p in path.select_by_mtime(min_time=0): p.mtime >= 0
def test_file_stat(self): """ Not need in travis. """ p = Path(__file__).parent stat = p.file_stat() assert stat["file"] >= 14 assert stat["dir"] >= 2 assert stat["size"] >= 32000 all_stat = p.file_stat_for_all() assert all_stat[p.abspath] == stat
def teardown_module(module): """ Remove temp file and dir for test. """ p_this = Path(__file__) to_remove_list = [ p_this.change(new_basename="mirror"), ] for p in p_this.parent.select_by_ext(".zip"): to_remove_list.append(p) for p in to_remove_list: if p.is_file(): p.remove() elif p.is_dir(): shutil.rmtree(p.abspath)
def test_print_big_file(self): """ Not need in travis. """ path = Path(__file__).absolute().parent.parent # pathlibm_mate-project path.print_big_file() path.print_big_dir()
def test_backup(self): """ Not need in travis. """ import random p = Path(__file__).change(new_basename="app") dst = Path(__file__) \ .change(new_basename="app-backup-%s.zip" % random.randint(1, 9999)) assert dst.exists() is False p.backup(dst.abspath, ignore_size_larger_than=1000, case_sensitive=False) assert dst.exists() is True
def test_sort_by(self): path = Path(__file__).absolute().parent.parent # pathlibm_mate-project p_list = Path.sort_by_size(path.select_file()) assert is_increasing([p.size for p in p_list]) p_list = Path.sort_by_size(path.select_file(), reverse=True) assert is_decreasing([p.size for p in p_list])
def test_auto_complete_choices(self): p = Path(__file__).change(new_basename="te") for p in p.auto_complete_choices(): assert p.basename.lower().startswith("te") p = Path(__file__).parent for p1 in p.auto_complete_choices(): assert p1 in p
def teardown_module(module): """ Remove temp file and dir for test. """ p_this = Path(__file__) to_remove_list = [ p_this.change(new_basename="file_to_copy.txt"), p_this.change(new_basename="file_to_copy.rst"), p_this.change(new_basename="file_to_move.txt"), p_this.change(new_basename="NOT-EXIST-FOLDER-MOVETO"), p_this.change(new_basename="NOT-EXIST-FOLDER-COPYTO"), p_this.change(new_basename="wow"), p_this.change(new_basename="wow1"), ] for p in to_remove_list: if p.is_file(): p.remove() elif p.is_dir(): shutil.rmtree(p.abspath)
def setup_module(module): """ Create temp file and dir for test. - create a new folder ``/wow`` - create two file `/`wow/file_to_move.txt``, ``wow/file_to_copy.txt`` """ p = Path(__file__).change(new_basename="app") try: shutil.copytree(p.abspath, p.change(new_basename="wow").abspath) except Exception as e: pass p = Path(__file__).change(new_basename="file_to_move.txt") with open(p.abspath, "wb") as f: f.write("test file".encode("utf-8")) p = Path(__file__).change(new_basename="file_to_copy.txt") with open(p.abspath, "wb") as f: f.write("test file".encode("utf-8"))
def test(self): HEXSTR_CHARSET = set("0123456789abcdef") def assert_hexstr(text): assert len(HEXSTR_CHARSET.union(set(text))) <= 16 p = Path(__file__).absolute() assert isinstance(p.abspath, six.string_types) assert isinstance(p.dirpath, six.string_types) assert p.abspath == __file__ assert p.dirpath == os.path.dirname(__file__) assert p.dirname == os.path.basename(os.path.dirname(__file__)) assert p.basename == os.path.basename(__file__) assert p.fname == os.path.splitext(os.path.basename(__file__))[0] assert p.ext == os.path.splitext(__file__)[1] assert_hexstr(p.abspath_hexstr) assert_hexstr(p.dirpath_hexstr) assert_hexstr(p.dirname_hexstr) assert_hexstr(p.basename_hexstr) assert_hexstr(p.fname_hexstr) assert len(p.md5) == 32 assert len(p.get_partial_md5(1)) == 32 assert p.size >= 1024 ts_2016_1_1 = (datetime(2016, 1, 1) - datetime(1970, 1, 1)).total_seconds() assert p.ctime >= ts_2016_1_1 assert p.mtime >= ts_2016_1_1 assert p.atime >= ts_2016_1_1 assert p.modify_datetime >= datetime(2016, 1, 1) assert p.access_datetime >= datetime(2016, 1, 1) assert p.create_datetime >= datetime(2016, 1, 1) assert "KB" in p.size_in_text assert p.get_partial_md5( nbytes=10) == "52ee8aa6c482035e08afabda0f0f8dd8" with raises(ValueError): p.get_partial_md5(-1)
def test_change(self): p = Path(__file__) p1 = p.change(new_ext=".txt") assert p1.ext == ".txt" assert p1.fname == p.fname assert p1.dirname == p.dirname assert p1.dirpath == p.dirpath p1 = p.change(new_fname="hello") assert p1.ext == p.ext assert p1.fname == "hello" assert p1.dirname == p.dirname assert p1.dirpath == p.dirpath p1 = p.change(new_basename="hello.txt") assert p1.ext == ".txt" assert p1.fname == "hello" assert p1.dirname == p.dirname assert p1.dirpath == p.dirpath p1 = p.change(new_dirname="folder") assert p1.ext == p.ext assert p1.fname == p.fname assert p1.dirname == "folder" assert p1.dirpath.endswith("folder") with raises(ValueError): p.change(new_dirpath="new_dirpath", new_dirname="folder") with raises(ValueError): p.change(new_basename="hello.txt", new_fname="hello") with raises(ValueError): p.change(new_basename="hello.txt", new_ext="hello") # because __file__ is OS dependent, so don't test this. system_name = platform.system() if system_name == "Windows": p1 = p.change(new_dirpath="C:\\User") assert p1.ext == p.ext assert p1.fname == p.fname assert p1.dirname == "User" assert p1.dirpath == "C:\\User" elif system_name in ["Darwin", "Linux"]: p1 = p.change(new_dirpath="/Users") assert p1.ext == p.ext assert p1.fname == p.fname assert p1.dirname == "Users" assert p1.dirpath == "/Users"
def test_append_parts(self): p = Path(__file__).parent p1 = p.append_parts("a") assert len(p.parts) == len(p1.parts) - 1 p1 = p.append_parts("a", "b") assert len(p.parts) == len(p1.parts) - 2
def test_drop_parts(self): p = Path(__file__) p1 = p.drop_parts(1) assert len(p.parts) == len(p1.parts) + 1 p1 = p.drop_parts(2) assert len(p.parts) == len(p1.parts) + 2
def test_dir_here(self): dir_here = Path.dir_here(__file__) assert dir_here.basename == "tests"
def test_select_by_ext(self): path = Path(__file__).absolute().parent.parent # pathlibm_mate-project for p in path.select_by_ext(".Bat"): assert p.ext.lower() == ".bat"
def test_copyto(self): # copy file p_file = Path(__file__).change(new_basename="file_to_copy.txt") p_file_new = p_file.change(new_ext=".rst") assert p_file_new.exists() is False p_file_new = p_file.copyto(new_ext=".rst") assert p_file.exists() is True assert p_file_new.exists() is True # copy file into not existing folder with pytest.raises(Exception): p_file.copyto(new_dirname="NOT-EXIST-FOLDER-COPYTO") # copy file into not existing folder, and create the folder p_file_new = p_file.change( new_abspath=Path(__file__).parent.append_parts( "NOT-EXIST-FOLDER-COPYTO", "file_to_copy.txt"), ) assert p_file_new.exists() is False assert p_file_new.parent.exists() is False p_file_new = p_file.copyto( new_abspath=Path(__file__).parent.append_parts( "NOT-EXIST-FOLDER-COPYTO", "file_to_copy.txt"), makedirs=True, ) assert p_file_new.exists() is True # copy directory p_dir = Path(__file__).change(new_basename="wow") n_files = p_dir.n_file p_dir_new = p_dir.moveto(new_basename="wow1") assert n_files == p_dir_new.n_file
def test_moveto(self): # move file p_file = Path(__file__).change(new_basename="file_to_move.txt") p_file_new = p_file.moveto(new_ext=".rst") # change extension assert p_file.exists() is False assert p_file_new.exists() is True p_file = p_file_new.moveto(new_ext=".txt") # move back # move file into not existing folder with pytest.raises(EnvironmentError): p_file.moveto(new_dirname="NOT-EXIST-FOLDER-MOVETO") # move file into not existsing folder, and create the folder p_file_new = p_file.moveto( new_abspath=Path(__file__).parent.append_parts( "NOT-EXIST-FOLDER-MOVETO", "file_to_move.txt"), makedirs=True, ) p_file = p_file_new.moveto(new_abspath=p_file.abspath) # move directory p_dir = Path(__file__).change(new_basename="wow") n_files = p_dir.n_file n_dir = p_dir.n_dir p_dir_new = p_dir.moveto(new_basename="wow1") assert n_files == p_dir_new.n_file assert n_dir == p_dir_new.n_dir p_dir = p_dir_new.moveto(new_basename="wow")
def test_dirsize(self): p = Path(__file__).parent assert p.parent.dirsize >= 32768
# -*- coding: utf-8 -*- import pytest from pathlib_mate.pathlib2 import Path dir_tests = Path.dir_here(__file__) dir_project_root = dir_tests.parent dir_pathlib_mate = Path(dir_project_root, "pathlib_mate") dir_app = Path(dir_tests, "app") p_this = Path(__file__).absolute() def clear_all_zip_file_in_tests_dir(): for p in dir_tests.select_by_ext(".zip"): p.remove_if_exists() def setup_module(module): clear_all_zip_file_in_tests_dir() def teardown_module(module): clear_all_zip_file_in_tests_dir() class TestToolBoxZip(object): def test_auto_zip_archive_dst(self): p = dir_pathlib_mate._default_zip_dst() assert p.dirpath == dir_pathlib_mate.dirpath p = p_this._default_zip_dst()
def test_assert_is_dir_and_exists(self): Path(__file__).parent.assert_is_dir_and_exists() with raises(Exception): Path(__file__).assert_is_dir_and_exists() with raises(Exception): Path("THIS-FILE-NOT-EXIST").assert_is_dir_and_exists()
def test_assert_exists(self): with raises(Exception): Path("THIS-FILE-NOT-EXIST").assert_exists()
def test_contains(self): p = Path(__file__).absolute() assert p in p.parent assert p.abspath in p.parent # string also works assert p.parent not in p
def test_select_image(self): path = Path(__file__).absolute().parent.parent # pathlibm_mate-project for p in path.select_image(): assert p.ext in [".jpg", ".png", ".gif", ".svg"]
def test_iter(self): p = Path(__file__).absolute() for ancestor in p: assert p in ancestor
def test_select_dir(self): path = Path(__file__).absolute().parent.parent # pathlibm_mate-project for p in path.select_dir(): assert p.is_dir()