From 3297ce5e674977cd4e41dd0b286a9a2f3a3b6d00 Mon Sep 17 00:00:00 2001 From: Yax Date: Wed, 7 Jan 2026 14:10:32 +0100 Subject: [PATCH] Improve slugify function, add tests, and enhance Makefile Applied improvements from feature-code-quality branch: - Refactored slugify() for better readability with inline comments - Renamed log() parameter *args to *log_args (avoid shadowing built-in) - Added comprehensive unit tests for slugify function - Enhanced Makefile with typecheck and test targets - Updated build target to run type checks and tests before Docker build All tests pass and type checking succeeds. --- Makefile | 23 ++++++++++++++++++----- makesite.py | 15 ++++++--------- test/__init__.py | 1 + test/test_slugify.py | 13 +++++++++++++ 4 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 test/__init__.py create mode 100644 test/test_slugify.py diff --git a/Makefile b/Makefile index 0ba09c6..99068b9 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,28 @@ -# Makefile +# Makefile -# build target should be built even if no files depend on it -.PHONY: build +# declare phony targets +.PHONY: build test typecheck # run locally -site: +site: uv run python makesite.py --params params-local.json cd _site && python -m SimpleHTTPServer 2> /dev/null || python3 -m http.server +# run type checks +typecheck: + uv run mypy makesite.py monitor.py + +# run unit tests +test: + uv run python -m unittest discover test -v + # docker build image -build: +build: + @echo "Running type checks..." + uv run mypy makesite.py monitor.py + @echo "Running tests..." + uv run python -m unittest discover test -v + @echo "Building Docker image..." docker build -t source.madyanne.fr/yax/blog . # docker publish image diff --git a/makesite.py b/makesite.py index 3a61b10..469b90b 100755 --- a/makesite.py +++ b/makesite.py @@ -66,9 +66,9 @@ def fwrite(filename, text): f.write(text) -def log(msg, *args): +def log(msg, *log_args): """Log message with specified arguments.""" - sys.stderr.write(msg.format(*args) + "\n") + sys.stderr.write(msg.format(*log_args) + "\n") def _strip_tags_and_truncate(text, words=25): @@ -98,13 +98,10 @@ def slugify(value): underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace. """ - value = ( - unicodedata.normalize("NFKD", value) - .encode("ascii", "ignore") - .decode("ascii") - ) - value = re.sub(r"[^\w\s-]", "", value).strip().lower() - return re.sub(r"[-\s]+", "-", value) + value = unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii") + value = re.sub(r"[^\w\s-]", "", value) # Remove non-word characters and spaces + value = re.sub(r"\s+", "-", value) # Replace multiple spaces with a single hyphen + return value.lower() # Convert to lowercase def parse_post_file(filename, params): diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..66173ae --- /dev/null +++ b/test/__init__.py @@ -0,0 +1 @@ +# Test package diff --git a/test/test_slugify.py b/test/test_slugify.py new file mode 100644 index 0000000..da04846 --- /dev/null +++ b/test/test_slugify.py @@ -0,0 +1,13 @@ +import unittest +import makesite + + +class SlugifyTest(unittest.TestCase): + + def test_slugify(self): + self.assertEqual(makesite.slugify('NginX est brillant'), 'nginx-est-brillant') + self.assertEqual(makesite.slugify('Bilan hébergement 2023'), 'bilan-hebergement-2023') + self.assertEqual(makesite.slugify('Sécurisation Docker : des pistes'), 'securisation-docker-des-pistes') + self.assertEqual(makesite.slugify('Il court, il court, le furet'), 'il-court-il-court-le-furet') + self.assertEqual(makesite.slugify('De GNU/Linux à gnuSystemlinuxdGnomeOs'), 'de-gnulinux-a-gnusystemlinuxdgnomeos') + self.assertEqual(makesite.slugify('Au fait... mon téléphone'), 'au-fait-mon-telephone')