import copy import os import pytest from viano_bug_analysis.package import PackageFactory, PackageReader, PackageWriteError def test_generic_factory(manifest, tmp_path): original = copy.deepcopy(manifest) path = tmp_path / "first.package" assert PackageFactory().create(path, manifest, {"unknown": b"opaque"}).valid assert manifest == original with PackageReader(path) as reader: assert reader.manifest["package_generation"] == 0 assert reader.read_bytes("unknown") == b"opaque" @pytest.mark.parametrize("artifacts", [{"../escape": b"x"}, {"manifest.json": b"x"}, {"a": b"x", "A": b"y"}, {"a": b"x", "a/b": b"y"}, {"a": "not bytes"}]) def test_invalid_creation_keeps_target_absent(manifest, tmp_path, artifacts): target = tmp_path / "out" with pytest.raises(PackageWriteError): PackageFactory().create(target, manifest, artifacts) assert not target.exists() assert not list(tmp_path.glob(".vbcase-*.tmp")) def test_factory_failure_preserves_existing_target(manifest, tmp_path): target = tmp_path / "out" target.write_bytes(b"previous") with pytest.raises(PackageWriteError): PackageFactory().create(target, manifest, {}) assert target.read_bytes() == b"previous" def test_factory_fsync_failure(manifest, tmp_path, monkeypatch): target = tmp_path / "out" def fail(*args): raise OSError("disk error") monkeypatch.setattr(os, "fsync", fail) with pytest.raises(PackageWriteError): PackageFactory().create(target, manifest, {}) assert not target.exists() assert not list(tmp_path.glob(".vbcase-*.tmp"))