Esempio n. 1
0
def test_select_by_pattern_in_abspath():
    path = Path(__file__).absolute().parent.parent  # pathlibm_mate-project

    for p in path.select_by_pattern_in_abspath("test", case_sensitive=True):
        assert "test" in p.abspath

    for p in path.select_by_pattern_in_abspath("TEST", case_sensitive=False):
        assert "test" in p.abspath
Esempio n. 2
0
def test_select():
    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")
Esempio n. 3
0
def test_select_by_time():
    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
Esempio n. 4
0
def test_file_stat():
    """Not need in travis.
    """
    p = Path(__file__).parent
    stat = p.file_stat()

    assert stat["file"] >= 6
    assert stat["dir"] >= 2
    assert stat["size"] >= 11000

    all_stat = p.file_stat_for_all()
    assert all_stat[p.abspath] == stat
Esempio n. 5
0
def teardown_module(module):
    """
    Remove temp file and dir for test.
    """
    reserved = ["app", "all.py", "test_pathlib_mate.py"]
    for p in Path(__file__).parent.iterdir():
        if p.basename not in reserved:
            if p.is_dir():
                shutil.rmtree(p.abspath)
            else:
                os.remove(p.abspath)
Esempio n. 6
0
def test_sort_by():
    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])
Esempio n. 7
0
def test_backup():
    """Not need in travis.
    """
    import random
    p = Path(__file__).change(new_basename="app")

    dst = Path(__file__).\
        change(new_basename="app-%s.zip" % random.randint(1, 9999))
    p.backup(dst.abspath, ignore_size_larger_than=1000, case_sensitive=False)
    assert len(list(Path(__file__).parent.select_by_ext(".zip"))) == 1
Esempio n. 8
0
def test_change():
    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")

    # because __file__ is OS dependent, so don't test this.
    system_name = platform.system()
    if system_name == "Windows":
        p1 = p.change(new_dirpath=r"C:\User")
        assert p1.ext == p.ext
        assert p1.fname == p.fname
        assert p1.dirname == "User"
        assert p1.dirpath == r"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"
Esempio n. 9
0
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"))
Esempio n. 10
0
def test_attribute():
    p = Path(__file__).absolute()
    assert isinstance(p.abspath, str)
    assert isinstance(p.dirpath, str)
    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 len(p.md5) == 32
    assert len(p.get_partial_md5(1)) == 32
    assert p.size >= 1024
    assert p.parent.dirsize >= 32768
    assert p.ctime >= 1451624400.0
    assert p.mtime >= 1451624400.0
    assert p.atime >= 1451624400.0
    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) == "2030c5e97b2761d126f41dbd610d80a7"
    with raises(ValueError):
        p.get_partial_md5(0)
Esempio n. 11
0
def test_select_by_size():
    path = Path(__file__).absolute().parent.parent  # pathlibm_mate-project

    for p in path.select_by_size(max_size=1000):
        assert p.size <= 1000
Esempio n. 12
0
def test_mirror_to():
    """Not need in travis.
    """
    p = Path(__file__).change(new_basename="app")
    dst = Path(__file__).change(new_basename="mirror")
    p.mirror_to(dst.abspath)
Esempio n. 13
0
def test_print_big_dir_and_big_file():
    """Not need in travis.
    """
    path = Path(__file__).absolute().parent.parent  # pathlibm_mate-project
    path.print_big_dir_and_big_file()
Esempio n. 14
0
def test_assert_is_dir_and_exists():
    Path(__file__).parent.assert_is_dir_and_exists()
Esempio n. 15
0
def test_assert_is_file_and_exists():
    Path(__file__).assert_is_file_and_exists()
Esempio n. 16
0
def test_append_parts():
    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
Esempio n. 17
0
def test_trail_space():
    """Not need in travis.
    """
    p = Path(__file__).change(new_basename="app")
    p.trail_space()
Esempio n. 18
0
def test_copyto():
    # copy file
    p_file = Path(__file__).change(new_basename="file_to_copy.txt")
    p_file_new = p_file.copyto(new_ext=".rst")  # change extension
    assert p_file.exists() is True
    assert p_file_new.exists() is True

    # copy file into not existing folder
    with pytest.raises(EnvironmentError):
        p_file.copyto(new_dirname="NOT EXIST FOLDER")

    # copy file into not existsing folder, and create the folder
    p_file_new = p_file.copyto(
        new_abspath=Path(__file__).parent.
        append_parts("NOT EXIST FOLDER", "file_to_copy.txt"),
        makedirs=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
Esempio n. 19
0
def test_select_by_ext():
    path = Path(__file__).absolute().parent.parent  # pathlibm_mate-project
    for p in path.select_by_ext(".Bat"):
        assert p.ext.lower() == ".bat"
Esempio n. 20
0
def test_select_dir():
    path = Path(__file__).absolute().parent.parent  # pathlibm_mate-project
    for p in path.select_dir():
        assert p.is_dir()
Esempio n. 21
0
def test_autopep8():
    """Not need in travis.
    """
    p = Path(__file__).change(new_basename="app")
    p.autopep8()
Esempio n. 22
0
def test_select_image():
    path = Path(__file__).absolute().parent.parent  # pathlibm_mate-project

    for p in path.select_image():
        assert p.ext in [".jpg", ".png", ".gif", ".svg"]
Esempio n. 23
0
def test_preprocess():
    assert _preprocess("a") == ["a", ]
    assert _preprocess(Path(__file__)) == [str(Path(__file__)), ]

    assert _preprocess(["a", ]) == ["a", ]
    assert _preprocess([Path(__file__), ]) == [str(Path(__file__)), ]
Esempio n. 24
0
def test_is_empty():
    assert Path(__file__).is_empty() is False
    assert Path(__file__).parent.is_empty(strict=True) is False
    assert Path(__file__).parent.is_empty(strict=False) is False
    with raises(EnvironmentError):
        Path("FileNotExist").is_empty()
Esempio n. 25
0
import attr
import pandas as pd

pd.set_option("display.width", 160)

from pandas_mate.util import ascii_table
from sfm.timer import timeit_wrapper
from pathlib_mate.pathlib import Path
from pathlib_mate.mate import repr_data_size
from compress import CompressAlgorithms

from helper import Spider, create_logger

spider = Spider()
logger = create_logger()
pwd = Path(__file__).parent


@attr.s
class Result(object):
    """
    Single test result schema.

    :param file_number: number of files processed.
    :param before_size: total size of files in bytes.
    :param after_size: total size of compressed files in bytes.
    :param compress_time: average total process time for compression.
    :param decompress_time: average total process time for decompression.
    """
    file_number = attr.ib(default=0)
    before_size = attr.ib(default=0)
Esempio n. 26
0
def test_drop_parts():
    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