Esempio n. 1
0
def test_unlink(BASE):
    p = PathPlus(BASE) / "fileA"
    p.unlink()
    with pytest.raises(FileNotFoundError):
        p.stat()
    with pytest.raises(FileNotFoundError):
        p.unlink()
Esempio n. 2
0
def test_rmdir(BASE):
    p = PathPlus(BASE) / "dirA"
    for q in p.iterdir():
        q.unlink()
    p.rmdir()
    with pytest.raises(FileNotFoundError):
        p.stat()
    with pytest.raises(FileNotFoundError):
        p.unlink()
Esempio n. 3
0
def test_mkdir_exist_ok(BASE):
    p = PathPlus(BASE, "dirB")
    st_ctime_first = p.stat().st_ctime
    assert (p.exists())
    assert (p.is_dir())
    with pytest.raises(FileExistsError) as cm:
        p.mkdir()
    assert (cm.value.errno == errno.EEXIST)
    p.mkdir(exist_ok=True)
    assert (p.exists())
    assert (p.stat().st_ctime == st_ctime_first)
Esempio n. 4
0
def test_chmod(BASE):
    p = PathPlus(BASE) / "fileA"
    mode = p.stat().st_mode
    # Clear writable bit.
    new_mode = mode & ~0o222
    p.chmod(new_mode)
    assert (p.stat().st_mode == new_mode)
    # Set writable bit.
    new_mode = mode | 0o222
    p.chmod(new_mode)
    assert (p.stat().st_mode == new_mode)
Esempio n. 5
0
def test_stat(BASE):
    p = PathPlus(BASE) / "fileA"
    st = p.stat()
    assert (p.stat() == st)
    # Change file mode by flipping write bit.
    p.chmod(st.st_mode ^ 0o222)

    try:
        assert (p.stat() != st)
    finally:
        p.chmod(st.st_mode)
Esempio n. 6
0
def test_mkdir_exist_ok_with_parent(BASE):
    p = PathPlus(BASE, "dirC")
    assert p.exists()
    with pytest.raises(FileExistsError) as cm:
        p.mkdir()
    assert (cm.value.errno == errno.EEXIST)
    p = p / "newdirC"
    p.mkdir(parents=True)
    st_ctime_first = p.stat().st_ctime
    assert p.exists()
    with pytest.raises(FileExistsError) as cm:
        p.mkdir(parents=True)
    assert (cm.value.errno == errno.EEXIST)
    p.mkdir(parents=True, exist_ok=True)
    assert p.exists()
    assert (p.stat().st_ctime == st_ctime_first)
Esempio n. 7
0
def test_group(BASE):
    grp = pytest.importorskip("grp",
                              reason="the grp module is needed for this test")

    p = PathPlus(BASE) / "fileA"
    gid = p.stat().st_gid
    try:
        name = grp.getgrgid(gid).gr_name
    except KeyError:
        pytest.skip(
            f"group {gid:d} doesn't have an entry in the system database")
    assert (name == p.group())
Esempio n. 8
0
def test_owner(BASE):
    pwd = pytest.importorskip("pwd",
                              reason="the pwd module is needed for this test")

    p = PathPlus(BASE) / "fileA"
    uid = p.stat().st_uid
    try:
        name = pwd.getpwuid(uid).pw_name
    except KeyError:
        pytest.skip(
            f"user {uid:d} doesn't have an entry in the system database")
    assert (name == p.owner())
Esempio n. 9
0
def test_mkdir_parents(BASE):
    # Creating a chain of directories.
    p = PathPlus(BASE, "newdirB", "newdirC")
    assert not (p.exists())
    with pytest.raises(OSError) as cm:
        p.mkdir()
    assert (cm.value.errno == errno.ENOENT)
    p.mkdir(parents=True)
    assert (p.exists())
    assert (p.is_dir())
    with pytest.raises(OSError) as cm:
        p.mkdir(parents=True)
    assert (cm.value.errno == errno.EEXIST)
    # Test `mode` arg.
    mode = stat.S_IMODE(p.stat().st_mode)  # Default mode.
    p = PathPlus(BASE, "newdirD", "newdirE")
    p.mkdir(0o555, parents=True)
    assert (p.exists())
    assert (p.is_dir())
    if os.name != "nt":
        # The directory's permissions follow the mode argument.
        assert (stat.S_IMODE(p.stat().st_mode) == 0o7555 & mode)
    # The parent's permissions follow the default process settings.
    assert (stat.S_IMODE(p.parent.stat().st_mode) == mode)
Esempio n. 10
0
def test_pickling_common(BASE):
    p = PathPlus(BASE, "fileA")
    for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
        dumped = pickle.dumps(p, proto)
        pp = pickle.loads(dumped)
        assert pp.stat() == p.stat()
Esempio n. 11
0
def test_lstat_nosymlink(BASE):
    p = PathPlus(BASE) / "fileA"
    st = p.stat()
    assert (st == p.lstat())