From 92bb1e914889ef4d8730c5341a77189cba3345ba Mon Sep 17 00:00:00 2001 From: Guilhem Allaman <40383801+gounux@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:55:34 +0200 Subject: [PATCH] fix header image extension check (#231) Co-authored-by: gounux --- geotribu_cli/content/header_check.py | 11 +-- tests/test_yaml_header_check.py | 102 +++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 4 deletions(-) diff --git a/geotribu_cli/content/header_check.py b/geotribu_cli/content/header_check.py index 750d2d7..1d7af4f 100644 --- a/geotribu_cli/content/header_check.py +++ b/geotribu_cli/content/header_check.py @@ -148,7 +148,7 @@ def check_image_size( def check_image_ratio( - image_url: str, images: dict, min_ratio: int, max_ratio: int + image_url: str, images: dict, min_ratio: float, max_ratio: float ) -> bool: key = image_url.replace(f"{defaults_settings.cdn_base_url}img/", "") if key not in images: @@ -163,7 +163,7 @@ def check_image_extension( allowed_extensions: tuple[str] = defaults_settings.images_header_extensions, ) -> bool: ext = image_url.split(".")[-1] - return ext in allowed_extensions + return f".{ext}" in allowed_extensions def get_existing_tags() -> list[str]: @@ -226,6 +226,9 @@ def run(args: argparse.Namespace) -> None: logger.debug(f"Running {args.command} with {args}") content_paths: list[Path] = args.content_path + # fetch image sizes dict once before processing + image_sizes = download_image_sizes() + for content_path in content_paths: logger.info(f"Checking header of {content_path}") check_path( @@ -249,7 +252,7 @@ def run(args: argparse.Namespace) -> None: # check image max size if not check_image_size( yaml_meta["image"], - download_image_sizes(), + image_sizes, args.max_image_width, args.max_image_height, ): @@ -267,7 +270,7 @@ def run(args: argparse.Namespace) -> None: # check image max ratio if not check_image_ratio( yaml_meta["image"], - download_image_sizes(), + image_sizes, args.min_image_ratio, args.max_image_ratio, ): diff --git a/tests/test_yaml_header_check.py b/tests/test_yaml_header_check.py index 27e0b18..1aced97 100644 --- a/tests/test_yaml_header_check.py +++ b/tests/test_yaml_header_check.py @@ -7,16 +7,24 @@ from geotribu_cli.content.header_check import ( check_author_md, check_existing_tags, + check_image_extension, + check_image_ratio, + check_image_size, check_license, check_missing_mandatory_keys, check_tags_order, + download_image_sizes, ) # -- GLOBALS TEAM_FOLDER = Path("tests/fixtures/team") +URL_TEST_VERTICAL_IMAGE = "https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/mise_en_place_qfieldcloud_custom/screenshot_qfield_qfc_project.webp" +URL_TEST_HORIZONTAL_IMAGE = "https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/carte_trains_europe.png" class TestYamlHeaderCheck(unittest.TestCase): + image_sizes = download_image_sizes() + def setUp(self): with open("tests/fixtures/content/2012-12-21_article_passe.md") as past_file: past_content = past_file.read() @@ -84,3 +92,97 @@ def test_license_ok(self): def test_license_nok(self): self.assertFalse(check_license(self.future_yaml_meta["license"])) + + def test_image_extension_ok(self): + self.assertTrue(check_image_extension("https://mon.image.png")) + self.assertTrue(check_image_extension("https://mon.image.jpg")) + self.assertTrue(check_image_extension("https://mon.image.jpeg")) + + def test_image_extension_nok(self): + self.assertFalse(check_image_extension("https://mon.image.webp")) + self.assertFalse(check_image_extension("https://mon.image.gif")) + self.assertFalse(check_image_extension("https://mon.image.tiff")) + + def test_image_size_ok(self): + self.assertTrue( + check_image_size( + URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=800, max_height=800 + ) + ) + self.assertTrue( + check_image_size( + URL_TEST_HORIZONTAL_IMAGE, + self.image_sizes, + max_width=800, + max_height=800, + ) + ) + + def test_image_size_nok(self): + self.assertFalse( + check_image_size( + URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=380, max_height=800 + ) + ) + self.assertFalse( + check_image_size( + URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=800, max_height=799 + ) + ) + self.assertFalse( + check_image_size( + URL_TEST_HORIZONTAL_IMAGE, + self.image_sizes, + max_width=799, + max_height=800, + ) + ) + self.assertFalse( + check_image_size( + URL_TEST_HORIZONTAL_IMAGE, + self.image_sizes, + max_width=800, + max_height=532, + ) + ) + + def test_image_ratio_ok(self): + self.assertTrue( + check_image_ratio( + URL_TEST_VERTICAL_IMAGE, self.image_sizes, min_ratio=0.45, max_ratio=0.5 + ) + ) + self.assertTrue( + check_image_ratio( + URL_TEST_HORIZONTAL_IMAGE, + self.image_sizes, + min_ratio=1.45, + max_ratio=1.55, + ) + ) + self.assertTrue( + check_image_ratio( + URL_TEST_HORIZONTAL_IMAGE, + self.image_sizes, + min_ratio=1.49, + max_ratio=1.51, + ) + ) + + def test_image_ratio_nok(self): + self.assertFalse( + check_image_ratio( + URL_TEST_VERTICAL_IMAGE, + self.image_sizes, + min_ratio=1.45, + max_ratio=1.55, + ) + ) + self.assertFalse( + check_image_ratio( + URL_TEST_HORIZONTAL_IMAGE, + self.image_sizes, + min_ratio=1.2, + max_ratio=1.3, + ) + )