This repository was archived by the owner on May 6, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
fix: skip missing media files instead of crashing #113
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -96,12 +96,17 @@ def test_write_new_apkg_with_media(self, mock_replace, mock_package): | |||||||||||
| "desc": "Test Description", | ||||||||||||
| "notes": [note] | ||||||||||||
| } | ||||||||||||
| media_files = ['test.jpg', 'audio.mp3'] | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| with tempfile.TemporaryDirectory() as tmpdir: | ||||||||||||
| img_path = os.path.join(tmpdir, 'test.jpg') | ||||||||||||
| audio_path = os.path.join(tmpdir, 'audio.mp3') | ||||||||||||
| open(img_path, 'w').close() | ||||||||||||
| open(audio_path, 'w').close() | ||||||||||||
| media_files = [img_path, audio_path] | ||||||||||||
|
|
||||||||||||
| with mock.patch('os.getcwd', return_value=tmpdir): | ||||||||||||
| _write_new_apkg([deck_payload], media_files) | ||||||||||||
|
|
||||||||||||
| mock_package.assert_called_once() | ||||||||||||
| package_instance = mock_package.return_value | ||||||||||||
| self.assertEqual(package_instance.media_files, media_files) | ||||||||||||
|
|
@@ -110,6 +115,28 @@ def test_write_new_apkg_with_media(self, mock_replace, mock_package): | |||||||||||
| mock_package.return_value.write_to_file.assert_called_once() | ||||||||||||
| mock_replace.assert_called_once() | ||||||||||||
|
|
||||||||||||
| @mock.patch('helpers.write_apkg.Package') | ||||||||||||
| @mock.patch('helpers.write_apkg.os.replace') | ||||||||||||
| def test_write_new_apkg_skips_missing_media(self, mock_replace, mock_package): | ||||||||||||
| note = Note(model=self.test_model, fields=['Q1', 'A1']) | ||||||||||||
| deck_payload = { | ||||||||||||
| "id": 1234567890, | ||||||||||||
| "name": "Test Deck", | ||||||||||||
| "desc": "Test Description", | ||||||||||||
| "notes": [note] | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| with tempfile.TemporaryDirectory() as tmpdir: | ||||||||||||
| existing_path = os.path.join(tmpdir, 'exists.jpg') | ||||||||||||
| open(existing_path, 'w').close() | ||||||||||||
| media_files = [existing_path, 'image.png'] | ||||||||||||
|
Comment on lines
+131
to
+132
|
||||||||||||
| open(existing_path, 'w').close() | |
| media_files = [existing_path, 'image.png'] | |
| missing_path = os.path.join(tmpdir, 'image.png') | |
| open(existing_path, 'w').close() | |
| media_files = [existing_path, missing_path] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missingis computed viaset(media_files) - set(existing_media), which collapses duplicates and produces a non-deterministic ordering. Sincemedia_filescan contain duplicates (e.g., same image referenced by multiple cards), the reported count/list can be inaccurate and confusing. Consider computingmissingas an ordered list (e.g.,[f for f in media_files if not os.path.exists(f)]) and using that for both the count and the message.