Beispiel #1
0
def test_mkdir_concurrent_parent_creation(BASE):
    for pattern_num in range(32):
        p = PathPlus(BASE, "dirCPC%d" % pattern_num)
        assert not (p.exists())

        def my_mkdir(path, mode=0o777):
            path = str(path)
            # Emulate another process that would create the directory
            # just before we try to create it ourselves.  We do it
            # in all possible pattern combinations, assuming that this
            # function is called at most 5 times (dirCPC/dir1/dir2,
            # dirCPC/dir1, dirCPC, dirCPC/dir1, dirCPC/dir1/dir2).
            if pattern.pop():
                os.mkdir(path, mode)  # From another process.
                concurrently_created.add(path)
            os.mkdir(path, mode)  # Our real call.

        pattern = [bool(pattern_num & (1 << n)) for n in range(5)]
        concurrently_created: Set = set()
        p12 = p / "dir1" / "dir2"
        try:
            with mock.patch("pathlib._normal_accessor.mkdir", my_mkdir):
                p12.mkdir(parents=True, exist_ok=False)
        except FileExistsError:
            assert (str(p12) in concurrently_created)
        else:
            assert (str(p12) not in concurrently_created)
        assert (p.exists())
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
def test_is_char_device_true():
    # Under Unix, /dev/null should generally be a char device.
    P = PathPlus("/dev/null")
    if not P.exists():
        pytest.skip("/dev/null required")
    assert P.is_char_device()
    assert not P.is_block_device()
    assert not P.is_file()
Beispiel #5
0
def test_with(BASE):
    p = PathPlus(BASE)
    it = p.iterdir()
    it2 = p.iterdir()
    next(it2)
    with p:
        pass

    # Using a path as a context manager is a no-op, thus the following
    # operations should still succeed after the context manage exits.
    next(it)
    next(it2)
    p.exists()
    p.resolve()
    p.absolute()
    with p:
        pass
Beispiel #6
0
def test_mkdir_no_parents_file(BASE):
    p = PathPlus(BASE, "fileA")
    assert p.exists()
    # An exception is raised when the last path component is an existing
    # regular file, regardless of whether exist_ok is true or not.
    with pytest.raises(FileExistsError) as exc_info:
        p.mkdir()
    assert exc_info.value.errno == errno.EEXIST
    with pytest.raises(FileExistsError) as exc_info:
        p.mkdir(exist_ok=True)
    assert exc_info.value.errno == errno.EEXIST
Beispiel #7
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)
def test_tmp_pathplus(tmp_pathplus: PathPlus):
    assert isinstance(tmp_pathplus, PathPlus)
    assert tmp_pathplus.exists()