def test_symlink_backup(tmp_path): assert list(tmp_path.iterdir()) == [] realdir = tmp_path / "real" realdir.mkdir() real = realdir / "realfile.txt" real.write_text(TEXT) linkdir = tmp_path / "link" linkdir.mkdir() link = linkdir / "linkfile.txt" target = relpath(real, linkdir) link.symlink_to(target) bkp = tmp_path / "backup.txt" with InPlace(str(link), backup=str(bkp)) as fp: for line in fp: fp.write(line.swapcase()) assert sorted(tmp_path.iterdir(), key=attrgetter("name")) == [ bkp, linkdir, realdir, ] assert list(realdir.iterdir()) == [real] assert list(linkdir.iterdir()) == [link] assert link.is_symlink() assert os.readlink(str(link)) == target assert bkp.read_text() == TEXT assert link.read_text() == TEXT.swapcase() assert real.read_text() == TEXT.swapcase()
def test_readlines_nobackup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p)) as fp: assert fp.readlines() == TEXT.splitlines(True) assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == ""
def test_readline_nobackup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p)) as fp: for line in iter(fp.readline, ""): fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_move_first_early_close_nobackup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p), move_first=True) as fp: for line in fp: fp.write(line.swapcase()) fp.close() assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_writelines_backup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write("") bkp = tmpdir.join("backup.txt") with InPlace(str(p), backup=str(bkp)) as fp: fp.writelines(TEXT.splitlines(True)) assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == "" assert p.read() == TEXT
def test_move_first_backup_ext(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p), backup_ext="~", move_first=True) as fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["file.txt", "file.txt~"] assert p.new(ext="txt~").read() == TEXT assert p.read() == TEXT.swapcase()
def test_delete_nobackup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p)) as fp: for i, line in enumerate(fp): fp.write(line.swapcase()) if i == 5: p.remove() assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_move_first_prechdir_backup(tmpdir, monkeypatch): assert pylistdir(tmpdir) == [] monkeypatch.chdir(tmpdir) p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p), backup="backup.txt", move_first=True) as fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert tmpdir.join("backup.txt").read() == TEXT assert p.read() == TEXT.swapcase()
def test_symlink_nobackup(tmp_path): assert list(tmp_path.iterdir()) == [] realdir = tmp_path / "real" realdir.mkdir() real = realdir / "realfile.txt" real.write_text(TEXT) linkdir = tmp_path / "link" linkdir.mkdir() link = linkdir / "linkfile.txt" target = relpath(real, linkdir) link.symlink_to(target) with InPlace(str(link)) as fp: for line in fp: fp.write(line.swapcase()) assert list(realdir.iterdir()) == [real] assert list(linkdir.iterdir()) == [link] assert link.is_symlink() assert os.readlink(str(link)) == target assert link.read_text() == TEXT.swapcase() assert real.read_text() == TEXT.swapcase()
def test_print_backup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) bkp = tmpdir.join("backup.txt") with InPlace(str(p), backup=str(bkp)) as fp: for line in fp: print(line.swapcase(), end="", file=fp) assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == TEXT assert p.read() == TEXT.swapcase()
def test_use_and_reenter_backup_ext(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p), backup_ext="~") as fp: fp.write(fp.readline().swapcase()) with fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["file.txt", "file.txt~"] assert p.new(ext="txt~").read() == TEXT assert p.read() == TEXT.swapcase()
def test_pathlike_bytes_backup_ext(tmpdir): from os import fsencode assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(PathLike(fsencode(str(p))), backup_ext="~") as fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["file.txt", "file.txt~"] assert p.new(ext="txt~").read() == TEXT assert p.read() == TEXT.swapcase()
def test_move_first_rollback_too_late(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p), backup_ext="~", move_first=True) as fp: for line in fp: fp.write(line.swapcase()) with pytest.raises(ValueError): fp.rollback() assert pylistdir(tmpdir) == ["file.txt", "file.txt~"] assert p.new(ext="txt~").read() == TEXT assert p.read() == TEXT.swapcase()
def test_early_close_backup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) bkp = tmpdir.join("backup.txt") with InPlace(str(p), backup=str(bkp)) as fp: for line in fp: fp.write(line.swapcase()) fp.close() assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == TEXT assert p.read() == TEXT.swapcase()
def test_move_first_overwrite_backup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) bkp = tmpdir.join("backup.txt") bkp.write("This is not the file you are looking for.\n") with InPlace(str(p), backup=str(bkp), move_first=True) as fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == TEXT assert p.read() == TEXT.swapcase()
def test_move_first_early_close_and_write_nobackup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with pytest.raises(ValueError): with InPlace(str(p), move_first=True) as fp: for line in fp: fp.write(line.swapcase()) fp.close() fp.write("And another thing...\n") assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_move_first_nobackup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p), move_first=True) as fp: assert not fp.closed for line in fp: assert isinstance(line, str) fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_pathlike(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(PathLike(str(p))) as fp: assert not fp.closed for line in fp: assert isinstance(line, str) fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_move_first_backup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) bkp = tmpdir.join("backup.txt") with InPlace(str(p), backup=str(bkp), move_first=True) as fp: assert not fp.closed for line in fp: fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == TEXT assert p.read() == TEXT.swapcase()
def test_early_close_and_write_backup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) bkp = tmpdir.join("backup.txt") with pytest.raises(ValueError): with InPlace(str(p), backup=str(bkp)) as fp: for line in fp: fp.write(line.swapcase()) fp.close() fp.write("And another thing...\n") assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == TEXT assert p.read() == TEXT.swapcase()
def test_move_first_double_open_nobackup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(str(p), move_first=True) as fp: with pytest.raises(ValueError): fp.open() assert not fp.closed for line in fp: fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_pathlike_backup(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) bkp = tmpdir.join("backup.txt") with InPlace(PathLike(str(p)), backup=PathLike(str(bkp))) as fp: assert not fp.closed for line in fp: fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == TEXT assert p.read() == TEXT.swapcase()
def test_different_dir_backup(tmpdir, monkeypatch): monkeypatch.chdir(tmpdir) filedir = tmpdir.mkdir("filedir") bkpdir = tmpdir.mkdir("bkpdir") p = filedir.join("file.txt") p.write(TEXT) with InPlace( os.path.join("filedir", "file.txt"), backup=os.path.join("bkpdir", "backup.txt") ) as fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(filedir) == ["file.txt"] assert pylistdir(bkpdir) == ["backup.txt"] assert bkpdir.join("backup.txt").read() == TEXT assert p.read() == TEXT.swapcase()
def test_bytes(tmpdir): from os import fsencode assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) with InPlace(fsencode(str(p))) as fp: assert not fp.closed for line in fp: assert isinstance(line, str) fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT.swapcase()
def test_symlink_backup_ext(tmp_path): assert list(tmp_path.iterdir()) == [] realdir = tmp_path / "real" realdir.mkdir() real = realdir / "realfile.txt" real.write_text(TEXT) linkdir = tmp_path / "link" linkdir.mkdir() link = linkdir / "linkfile.txt" target = relpath(real, linkdir) link.symlink_to(target) with InPlace(str(link), backup_ext="~") as fp: for line in fp: fp.write(line.swapcase()) assert list(realdir.iterdir()) == [real] assert sorted(linkdir.iterdir(), key=attrgetter("name")) == [ link, link.with_suffix(".txt~"), ] assert link.is_symlink() assert os.readlink(str(link)) == target assert link.with_suffix(".txt~").read_text() == TEXT assert link.read_text() == TEXT.swapcase() assert real.read_text() == TEXT.swapcase()
def test_move_first_postchdir_backup(tmpdir, monkeypatch): """Assert that changing directory after opening an InPlace object works""" filedir = tmpdir.mkdir("filedir") wrongdir = tmpdir.mkdir("wrongdir") p = filedir.join("file.txt") p.write(TEXT) monkeypatch.chdir(filedir) with InPlace("file.txt", backup="backup.txt", move_first=True) as fp: monkeypatch.chdir(wrongdir) for line in fp: fp.write(line.swapcase()) assert os.getcwd() == str(wrongdir) assert pylistdir(wrongdir) == [] assert pylistdir(filedir) == ["backup.txt", "file.txt"] assert filedir.join("backup.txt").read() == TEXT assert p.read() == TEXT.swapcase()
def test_bytes_backup(tmpdir): from os import fsencode assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) bkp = tmpdir.join("backup.txt") with InPlace(fsencode(str(p)), backup=fsencode(str(bkp))) as fp: assert not fp.closed for line in fp: fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert pylistdir(tmpdir) == ["backup.txt", "file.txt"] assert bkp.read() == TEXT assert p.read() == TEXT.swapcase()
def test_move_first_rollback_too_early(tmpdir): assert pylistdir(tmpdir) == [] p = tmpdir.join("file.txt") p.write(TEXT) fp = InPlace(str(p), backup_ext="~", delay_open=True, move_first=True) with pytest.raises(ValueError): fp.rollback() assert fp.closed assert pylistdir(tmpdir) == ["file.txt"] assert p.read() == TEXT with fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["file.txt", "file.txt~"] assert p.new(ext="txt~").read() == TEXT assert p.read() == TEXT.swapcase()
def test_different_dir_file_backup(tmpdir, monkeypatch): """ Assert that if the input filepath contains a directory component and the backup path does not, the backup file will be created in the current directory """ monkeypatch.chdir(tmpdir) filedir = tmpdir.mkdir("filedir") p = filedir.join("file.txt") p.write(TEXT) with InPlace( os.path.join("filedir", "file.txt"), backup="backup.txt", ) as fp: for line in fp: fp.write(line.swapcase()) assert pylistdir(tmpdir) == ["backup.txt", "filedir"] assert pylistdir(filedir) == ["file.txt"] assert tmpdir.join("backup.txt").read() == TEXT assert p.read() == TEXT.swapcase()
def test_move_first_midchdir_backup(tmpdir, monkeypatch): """ Assert that changing directory between creating an InPlace object and opening it works """ filedir = tmpdir.mkdir("filedir") wrongdir = tmpdir.mkdir("wrongdir") p = filedir.join("file.txt") p.write(TEXT) monkeypatch.chdir(filedir) fp = InPlace("file.txt", backup="backup.txt", delay_open=True, move_first=True) monkeypatch.chdir(wrongdir) assert fp.closed with fp: assert not fp.closed for line in fp: fp.write(line.swapcase()) assert not fp.closed assert fp.closed assert os.getcwd() == str(wrongdir) assert pylistdir(wrongdir) == [] assert pylistdir(filedir) == ["backup.txt", "file.txt"] assert filedir.join("backup.txt").read() == TEXT assert p.read() == TEXT.swapcase()