Repository: ResearchObject/ro-crate-py

Issue:
write_zip should not include the new archive in itself
If one calls `write_zip` with an output path that is within the RO-crate directory, the new zip file ends up inside itself.  As `write_zip` walks the directory tree it'll find the partially written zip file and copy it into the archive.

Discussion:

Patch:
diff --git a/rocrate/rocrate.py b/rocrate/rocrate.py
index 5c3969d..a968f10 100644
--- a/rocrate/rocrate.py
+++ b/rocrate/rocrate.py
@@ -510,6 +510,8 @@ class ROCrate():
                 root = Path(root)
                 for name in files:
                     source = root / name
+                    if source.samefile(out_file_path):
+                        continue
                     dest = source.relative_to(top)
                     if not self.dereference(str(dest)):
                         zf.write(str(source), str(dest))


Test patch:
diff --git a/test/test_write.py b/test/test_write.py
index 63fdc85..c83797c 100644
--- a/test/test_write.py
+++ b/test/test_write.py
@@ -215,3 +215,17 @@ def test_no_parts(tmpdir, helpers):
     json_entities = helpers.read_json_entities(out_path)
     helpers.check_crate(json_entities)
     assert "hasPart" not in json_entities["./"]
+
+
+def test_no_zip_in_zip(test_data_dir, tmpdir):
+    crate_dir = test_data_dir / 'ro-crate-galaxy-sortchangecase'
+    crate = ROCrate(crate_dir)
+
+    zip_name = 'ro_crate_out.crate.zip'
+    zip_path = crate_dir / zip_name  # within the crate dir
+    crate.write_zip(zip_path)
+    out_path = tmpdir / 'ro_crate_out'
+    with zipfile.ZipFile(zip_path, "r") as zf:
+        zf.extractall(out_path)
+
+    assert not (out_path / zip_name).exists()