1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/webvirtcloud/test_icon_cache.py
catborise 2295673203 fix: every icon of the templates comes from the local icon cache
django-bootstrap-icons serves icons from static/icon_cache and fetches a
missing one from the CDN while the page renders; a server without
internet access shows an error text instead of the icon.

- copy (Clone on the instance lists), envelope-open ("Lost OTP?" page)
  and save (VM settings, network page) were not cached; they are now,
  fetched with django-bootstrap-icons itself.
- The user and group lists asked for 'exclamation-triangle ' with a
  trailing space, an icon that does not exist; the warning icon never
  showed.

A test checks that every {% bs_icon %} in the templates has its cache
file; it fails on the previous files.
2026-10-05 20:20:57 +03:00

32 lines
1.4 KiB
Python

"""Every {% bs_icon %} of the templates is in the committed icon cache
(BS_ICONS_CACHE). A missing one is fetched from the CDN when a page is
rendered, or shown as an error where the server has no internet access."""
import re
import unittest
from pathlib import Path
from django.conf import settings
BASE = Path(settings.BASE_DIR)
CALL = re.compile(r"\{%\s*bs_icon\s+(.*?)\s*%\}")
NAME = re.compile(r"""^['"]([^'"]+)['"]""")
KWARG = re.compile(r"""(\w+)=['"]([^'"]*)['"]""")
class IconCacheTestCase(unittest.TestCase):
def test_every_template_icon_is_cached(self):
cache = Path(settings.BS_ICONS_CACHE)
missing = []
templates = [*BASE.glob("templates/**/*.html"), *BASE.glob("*/templates/**/*.html")]
self.assertGreater(len(templates), 50) # the globs still find the templates
for template in templates:
for args in CALL.findall(template.read_text(encoding="utf-8")):
name = NAME.match(args)
self.assertIsNotNone(name, f"{template}: icon name is not a literal: {args}")
kw = dict(KWARG.findall(args))
# the file name django-bootstrap-icons uses for its cache
cached = f"{name.group(1)}_{kw.get('size')}_{kw.get('color')}_{kw.get('extra_classes')}.svg".replace(" ", "_")
if not (cache / cached).exists():
missing.append(f"{template.relative_to(BASE)}: {cached}")
self.assertEqual(missing, [])