def test_backup__can_overwrite_destination(src_file: Path): dst = src_file.parent / f"{src_file.name}~" dst.touch() backup_file = sh.backup(src_file, suffix="~", timestamp=None, overwrite=True) assert backup_file == dst assert is_same_file(src_file, backup_file)
def test_backup__customizes_backup_parent_directory(tmp_path: Path, src_file: Path): dir = tmp_path / "a" / "b" / "c" dir.mkdir(parents=True) backup_file = sh.backup(src_file, dir=dir) assert backup_file.parent == dir assert is_same_file(src_file, backup_file)
def test_backup__customizes_filename(write_file: T_WRITE_FILE, filename: str, args: t.Dict[str, t.Any], pattern: t.Pattern): src_file = write_file(filename, "test") backup_file = sh.backup(src_file, **args) assert pattern.fullmatch(backup_file.name), ( f"Backup of {src_file.name!r} with name {backup_file.name!r}" f" did not match pattern {pattern!r}")
def test_backup__backs_up_directory(tmp_path: Path): src_dir = Dir( tmp_path / "src", File("1.txt", text="1"), File("2.txt", text="2"), File("a/a1.txt", text="a1"), File("a/a2.txt", text="a2"), ) src_dir.mkdir() backup_dir = sh.backup(src_dir.path) assert is_same_dir(src_dir.path, backup_dir)
def test_backup__backs_up_directory_as_archive(tmp_path: Path, arc_ext: str): src_dir = Dir( tmp_path / "src", File("1.txt", text="1"), File("2.txt", text="2"), File("a/a1.txt", text="a1"), File("a/a2.txt", text="a2"), ) src_dir.mkdir() backup_archive = sh.backup(src_dir.path, ext=arc_ext) assert backup_archive.is_file() dst_path = tmp_path / "dst" sh.unarchive(backup_archive, dst_path) assert is_same_dir(src_dir.path, dst_path / "src")
def test_backup__backs_up_file(src_file: Path): backup_file = sh.backup(src_file) assert backup_file != src_file assert backup_file.parent == src_file.parent assert is_same_file(src_file, backup_file)
def test_backup__raises_when_destination_exists(src_file): dst = src_file.parent / f"{src_file.name}~" dst.touch() with pytest.raises(FileExistsError): sh.backup(src_file, suffix="~", timestamp=None)
def test_backup__raises_when_destination_is_same_as_source(src_file): with pytest.raises(FileExistsError): sh.backup(src_file, prefix="", suffix="", timestamp=None)
def test_backup__raises_when_timestamp_is_invalid(src_file: Path, timestamp: t.Any): with pytest.raises(ValueError): sh.backup(src_file, timestamp=timestamp)
def test_backup__appends_utc_timestamp_using_strftime(freezer, src_file: Path): utcnow = datetime.now(timezone.utc) expected_name = f"{src_file.name}.{utcnow.strftime(DEFAULT_TS_FORMAT)}~" backup_file = sh.backup(src_file, utc=True) assert backup_file.name == expected_name
def test_backup__appends_local_timestamp_using_strftime(src_file: Path): now = datetime.now() expected_name = f"{src_file.name}.{now.strftime(DEFAULT_TS_FORMAT)}~" backup_file = sh.backup(src_file) assert backup_file.name == expected_name