Skip to content

Commit b613ca5

Browse files
committed
implement: Export single questions without wiping output (t32)
1 parent eeaa782 commit b613ca5

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

in2lambda/json_convert/json_convert.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ def _zip(files: list[Path], root: Path, zip_path: str) -> None:
4747
root: The folder the archive names are relative to.
4848
zip_path: The path where the zip file will be created.
4949
"""
50-
# Sort by archive name for deterministic, alphabetical order
51-
names = sorted((str(file.relative_to(root)), file) for file in files)
50+
# Sort by archive name for deterministic, alphabetical order. A file can be
51+
# written more than once — an image used by both a question and its worked
52+
# solution — and is still one file on disk, so name it once here too.
53+
names = sorted({str(file.relative_to(root)): file for file in files}.items())
5254
with zipfile.ZipFile(zip_path, "w") as zf:
5355
for name, file in names:
5456
zf.write(file, arcname=name)

tests/test_exports.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ def test_writing_leaves_other_files(tmp_path: Path) -> None:
199199
]
200200

201201

202+
def test_repeated_image_zipped_once(tmp_path: Path) -> None:
203+
"""An image listed twice, as one used in both a question and its solution, is one file."""
204+
image = tmp_path / "diagram.png"
205+
image.write_bytes(b"not really a png")
206+
question_set = Set(questions=[Question(title="Q", images=[str(image), str(image)])])
207+
208+
written = _write_back(question_set, tmp_path)
209+
210+
assert _relative_files(written / "media") == ["diagram.png"]
211+
with zipfile.ZipFile(f"{written}.zip") as zf:
212+
assert [name for name in zf.namelist() if name.startswith("media/")] == [
213+
"media/diagram.png"
214+
]
215+
216+
202217
def _area_shape(area: dict) -> frozenset[str]:
203218
# Without indices, an area's shape is the keys it has, not how many tests, cases
204219
# or symbols it lists.

0 commit comments

Comments
 (0)