From 154b7494d4ff580f5ed5c582afc6c91092606484 Mon Sep 17 00:00:00 2001 From: Uchechukwu Orji Date: Tue, 17 Nov 2026 12:06:48 +0100 Subject: [PATCH] include values that fail constraints in ValueError messages fix typos --- .gitignore | 1 + CHANGELOG.md | 1 + src/zimscraperlib/zim/metadata.py | 10 +++++++--- tests/zim/test_metadata.py | 26 +++++++++++++++++++------- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index fc438594..21422ef8 100644 --- a/.gitignore +++ b/.gitignore @@ -266,3 +266,4 @@ src/zimscraperlib/rewriting/statics/wombatSetup.js # wombat.js is installed from online source src/zimscraperlib/rewriting/statics/wombat.js +.python-version diff --git a/CHANGELOG.md b/CHANGELOG.md index a2090ce3..6b27dea3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ArticleUrlRewriter.normalize()` no longer decodes a percent-encoded `/` (`%2F`) in the path into a literal `/`, fixing a content collision between e.g. `/foo%2Fbar` (one segment, containing a literal `/`) and `/foo/bar` (two segments), which used to normalize to the same ZIM path (#396) - `ArticleUrlRewriter.escape_dot_segments()` now also escapes a literal `_.`/`_..` (and further escaped forms like `__.`/`__..`) segment with one more leading `_`, fixing a content collision with the escaped form of an actual `.`/`..` dot-segment, which used to normalize to the same ZIM path (#396) - `ArticleUrlRewriter.normalize()` now prefixes every ZIM path with a constant `replay://` marker, and `wombatSetup.js`'s `urlRewriteFunction`/`head_insert.html` do the same, fixing wombat.js computing an empty or single-`/` replay prefix ("`wb_rel_prefix`") for a ZIM served with no extra "collection" path segment (e.g. a bare `kiwix.app/`), which corrupted any `innerHTML`/`outerHTML` read on the page by deleting arbitrary `/text/` spans, and caused related, quieter bugs in `rewriteUrl`/`rewriteCookie` (#396, openzim/zimit#589) +- Include values that violate metadata length restriction and names that violate custom metadata name conventions in `ValueError` messages (#405) ### Changed diff --git a/src/zimscraperlib/zim/metadata.py b/src/zimscraperlib/zim/metadata.py index 326ec577..79e13907 100644 --- a/src/zimscraperlib/zim/metadata.py +++ b/src/zimscraperlib/zim/metadata.py @@ -128,7 +128,9 @@ def get_name(self) -> str: and not self.meta_name.startswith("X-") and self.matches_reserved_name(self.meta_name) ): - raise ValueError("Custom metdata name must be X- prefixed") + raise ValueError( + f"Custom metadata name ('{self.meta_name}') must be X- prefixed" + ) if ( self.oz_x_prefixed and APPLY_RECOMMENDATIONS @@ -270,9 +272,11 @@ def get_cleaned_value(self, value: str) -> str: # max-length is openZIM recommendation if getattr(self, "oz_max_length", 0) and APPLY_RECOMMENDATIONS: - if nb_grapheme_for(value) > self.oz_max_length: + nb_graphemes = nb_grapheme_for(value) + if nb_graphemes > self.oz_max_length: raise ValueError( - f"{self.name} value is too long ({self.oz_max_length})" + f"{self.name} value ('{value}') is too long " + f"({nb_graphemes}/{self.oz_max_length})" ) return value diff --git a/tests/zim/test_metadata.py b/tests/zim/test_metadata.py index 44a7af6c..a361b4af 100644 --- a/tests/zim/test_metadata.py +++ b/tests/zim/test_metadata.py @@ -136,8 +136,11 @@ def test_validate_short_grapheme_title_check_enabled(): def test_validate_too_long_title_check_enabled(): - with pytest.raises(ValueError, match="Title value is too long"): - assert metadata.TitleMetadata("T" * 31) + value = "T" * 31 + with pytest.raises( + ValueError, match=rf"Title value \('{value}'\) is too long \(31/30\)" + ): + assert metadata.TitleMetadata(value) def test_validate_too_long_title_check_disabled( @@ -155,8 +158,11 @@ def test_validate_short_grapheme_description_check_enabled(): def test_validate_too_long_description_check_enabled(): - with pytest.raises(ValueError, match="Description value is too long"): - assert metadata.DescriptionMetadata("T" * 81) + value = "T" * 81 + with pytest.raises( + ValueError, match=rf"Description value \('{value}'\) is too long" + ): + assert metadata.DescriptionMetadata(value) def test_validate_too_long_description_check_disabled( @@ -174,8 +180,11 @@ def test_validate_short_grapheme_longdescription_check_enabled(): def test_validate_too_long_longdescription_check_enabled(): - with pytest.raises(ValueError, match="LongDescription value is too long"): - assert metadata.LongDescriptionMetadata("T" * 4001) + value = "T" * 4001 + with pytest.raises( + ValueError, match=rf"LongDescription value \('{value}'\) is too long" + ): + assert metadata.LongDescriptionMetadata(value) def test_validate_too_long_longdescription_check_disabled( @@ -287,7 +296,10 @@ def metadata_init(request: pytest.FixtureRequest) -> MetadataInitConfig: ], ) def test_reserved_names(metadata_name: str, metadata_type: type): - with pytest.raises(ValueError, match="Custom metdata name must be X- prefixed"): + with pytest.raises( + ValueError, + match=rf"Custom metadata name \('{metadata_name}'\) must be X- prefixed", + ): value = "x" if metadata_type == metadata.CustomTextMetadata else b"x" metadata_type(name=metadata_name, value=value)