Exemple #1
0
def test_atomicfile__fails_if_path_is_dir(tmp_path: Path):
    already_exists_dir = tmp_path
    with pytest.raises(IsADirectoryError):
        with sh.atomicfile(already_exists_dir):
            pass

    will_exist_dir = tmp_path / "test"
    with pytest.raises(IsADirectoryError):
        with sh.atomicfile(will_exist_dir) as fp:
            will_exist_dir.mkdir()
            fp.write("test")
Exemple #2
0
def test_atomicfile__does_not_overwrite_when_disabled(tmp_path: Path):
    file = tmp_path / "test.txt"
    file.write_text("")

    with pytest.raises(FileExistsError):
        with sh.atomicfile(file, overwrite=False):
            pass
Exemple #3
0
def test_atomicfile__skips_sync_when_disabled(tmp_path: Path):
    file = tmp_path / "test.txt"

    with patch_os_fsync() as mocked_os_fsync:
        with sh.atomicfile(file, skip_sync=True) as fp:
            fp.write("test")

    assert not mocked_os_fsync.called
Exemple #4
0
def test_atomicfile__syncs_new_file_and_dir(tmp_path: Path):
    file = tmp_path / "test.txt"

    with patch_os_fsync() as mocked_os_fsync:
        with sh.atomicfile(file) as fp:
            fp.write("test")

    assert mocked_os_fsync.called
    assert mocked_os_fsync.call_count == 2
Exemple #5
0
def test_atomicfile(tmp_path: Path, opts: t.Dict[str, t.Any]):
    file = tmp_path / "test.txt"
    text = "test"

    with sh.atomicfile(file, **opts) as fp:
        assert not file.exists()
        fp.write(text)
        assert not file.exists()

    assert file.exists()
    assert file.read_text() == text
Exemple #6
0
def test_atomicfile__raises_when_mode_invalid(tmp_path: Path, mode: t.Any):
    with pytest.raises(ValueError):
        with sh.atomicfile(tmp_path / "test.txt", mode):
            pass