Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import errno
import os
import shutil
import stat
from pathlib import Path
from typing import BinaryIO
import pytest
from invokeai.app.util import no_replace
from invokeai.app.util.no_replace import file_signature, publish_noreplace, rename_noreplace
def _refuse_link(src: Path, dst: Path) -> None:
"""Refuse hard links the way exFAT does on Linux."""
raise PermissionError(errno.EPERM, "Operation not permitted", str(dst))
@pytest.fixture
def without_hard_links(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(no_replace.os, "link", _refuse_link)
def _refuse_no_replace_rename(src: Path, dst: Path) -> None:
"""Refuse an atomic no-replace rename the way macOS exFAT, NFS and many FUSE mounts do."""
raise OSError(errno.ENOTSUP, "Operation not supported", str(dst))
def _limit_mount(monkeypatch: pytest.MonkeyPatch, mount: str) -> None:
if mount in ("no-hard-links", "copy-only"):
monkeypatch.setattr(no_replace.os, "link", _refuse_link)
if mount == "copy-only":
monkeypatch.setattr(no_replace, "rename_noreplace", _refuse_no_replace_rename)
def test_publish_gives_the_file_its_final_name_and_keeps_the_source_name(tmp_path: Path) -> None:
src = tmp_path / "work.tmp"
src.write_bytes(b"complete payload")
dst = tmp_path / "published"
publish_noreplace(src, dst)
assert dst.read_bytes() == b"complete payload"
assert src.exists()
def test_publish_without_hard_links_moves_the_file(tmp_path: Path, without_hard_links: None) -> None:
src = tmp_path / "work.tmp"
src.write_bytes(b"complete payload")
dst = tmp_path / "published"
publish_noreplace(src, dst)
assert dst.read_bytes() == b"complete payload"
assert not src.exists()
def test_publish_without_link_or_no_replace_rename_copies_and_keeps_the_source(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
_limit_mount(monkeypatch, "copy-only")
src = tmp_path / "work.tmp"
src.write_bytes(b"complete payload")
dst = tmp_path / "published"
publish_noreplace(src, dst)
assert dst.read_bytes() == b"complete payload"
assert src.read_bytes() == b"complete payload"
@pytest.mark.parametrize("mount", ["hard-links", "no-hard-links", "copy-only"])
def test_publish_refuses_an_existing_destination(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mount: str) -> None:
_limit_mount(monkeypatch, mount)
src = tmp_path / "work.tmp"
src.write_bytes(b"new payload")
dst = tmp_path / "published"
dst.write_bytes(b"other owner")
with pytest.raises(FileExistsError):
publish_noreplace(src, dst)
assert dst.read_bytes() == b"other owner"
assert src.read_bytes() == b"new payload"
@pytest.mark.parametrize("read_only_source", [False, True], ids=["writable-source", "read-only-source"])
def test_a_failed_copy_leaves_no_destination_and_reports_every_attempt(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, read_only_source: bool
) -> None:
_limit_mount(monkeypatch, "copy-only")
src = tmp_path / "work.tmp"
src.write_bytes(b"complete payload")
if read_only_source:
# The copy inherits the read-only mode, which Windows refuses to unlink.
src.chmod(stat.S_IREAD)
dst = tmp_path / "published"
def fail_midway(source: BinaryIO, target: BinaryIO) -> None:
target.write(source.read(4))
raise OSError(errno.ENOSPC, "No space left on device")
monkeypatch.setattr(no_replace.shutil, "copyfileobj", fail_midway)
with pytest.raises(OSError) as exc_info:
publish_noreplace(src, dst)
assert exc_info.value.errno == errno.ENOSPC
assert not dst.exists()
assert src.read_bytes() == b"complete payload"
rename_error = exc_info.value.__cause__
assert isinstance(rename_error, OSError) and rename_error.errno == errno.ENOTSUP
assert isinstance(rename_error.__cause__, PermissionError)
@pytest.mark.skipif(os.name == "nt", reason="Windows cannot replace a file that is open")
def test_a_failed_copy_keeps_a_destination_another_writer_put_in_its_place(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
_limit_mount(monkeypatch, "copy-only")
src = tmp_path / "work.tmp"
src.write_bytes(b"complete payload")
dst = tmp_path / "published"
def replace_destination_then_fail(source: BinaryIO, target: BinaryIO) -> None:
target.write(source.read(4))
replacement = tmp_path / "replacement"
replacement.write_bytes(b"other owner")
os.replace(replacement, dst)
raise OSError(errno.ENOSPC, "No space left on device")
monkeypatch.setattr(no_replace.shutil, "copyfileobj", replace_destination_then_fail)
with pytest.raises(OSError) as exc_info:
publish_noreplace(src, dst)
assert exc_info.value.errno == errno.ENOSPC
assert dst.read_bytes() == b"other owner"
assert src.read_bytes() == b"complete payload"
@pytest.mark.skipif(os.name == "nt", reason="Windows has no POSIX permission bits")
def test_a_copy_is_never_more_accessible_than_its_source(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
_limit_mount(monkeypatch, "copy-only")
src = tmp_path / "work.tmp"
src.write_bytes(b"private payload")
src.chmod(0o600)
dst = tmp_path / "published"
real_copyfileobj = shutil.copyfileobj
modes_while_copying: list[int] = []
def copy_and_record_mode(source: BinaryIO, target: BinaryIO) -> None:
modes_while_copying.append(stat.S_IMODE(os.fstat(target.fileno()).st_mode))
real_copyfileobj(source, target)
monkeypatch.setattr(no_replace.shutil, "copyfileobj", copy_and_record_mode)
previous_umask = os.umask(0o022)
try:
publish_noreplace(src, dst)
finally:
os.umask(previous_umask)
assert modes_while_copying == [0o600]
assert stat.S_IMODE(dst.stat().st_mode) == 0o600
assert dst.read_bytes() == b"private payload"
@pytest.mark.parametrize("mount", ["hard-links", "no-hard-links", "copy-only"])
def test_publish_returns_the_signature_of_the_file_it_published(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mount: str
) -> None:
_limit_mount(monkeypatch, mount)
src = tmp_path / "work.tmp"
src.write_bytes(b"complete payload")
dst = tmp_path / "published"
signature = publish_noreplace(src, dst)
assert signature == file_signature(dst.lstat())
def test_rename_refuses_an_existing_destination(tmp_path: Path) -> None:
src = tmp_path / "src"
src.write_bytes(b"source")
dst = tmp_path / "dst"
dst.write_bytes(b"destination")
with pytest.raises(FileExistsError):
rename_noreplace(src, dst)
assert src.read_bytes() == b"source"
assert dst.read_bytes() == b"destination"
def test_rename_moves_a_directory_with_its_contents(tmp_path: Path) -> None:
src = tmp_path / "src"
src.mkdir()
(src / "weights.safetensors").write_bytes(b"weights")
dst = tmp_path / "dst"
rename_noreplace(src, dst)
assert not src.exists()
assert (dst / "weights.safetensors").read_bytes() == b"weights"