Skip to content

Commit

Permalink
Test if icons from css urls is a subset of filenames in icon dir
Browse files Browse the repository at this point in the history
  • Loading branch information
UlrichB22 committed Jul 9, 2024
1 parent a77f2bf commit 7f334d4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/moin/macros/_tests/test_ShowIcons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
Test for macros.ShowIcons
"""

import re
import os
from moin.macros.ShowIcons import Macro

my_dir = os.path.abspath(os.path.dirname(__file__))


def test_ShowIconsMacro():
"""Call ShowIcons macro and test output"""
Expand All @@ -30,3 +34,35 @@ def test_ShowIconsMacro():
assert expected_tags.issubset(result_tags)
assert expected_texts.issubset(result_texts)
assert expected_paths.issubset(result_paths)


def get_css_icon_names():
"""Read css file and search for icon urls"""
css_file = os.path.join(os.path.split(my_dir)[0], "..", "static", "css", "common.css")
with open(css_file, encoding="utf-8") as f:
common_css = f.readlines()
icon_list = set()
for line in common_css:
link = re.search(r"url\([\'\"]?([(.)/].*?)[\'\"]?\)", line)
if link and link.group(0)[5:].startswith("../img/icons"):
icon_list.add(link.group(0).split("/", 3)[3][:-2])
return icon_list


def get_icon_filenames():
"""Scan img/icons dir for filenames"""
icon_dir = os.path.join(os.path.split(my_dir)[0], "..", "static", "img", "icons")
filenames = set()
with os.scandir(icon_dir) as files:
for file in files:
if not file.name.startswith(".") and file.is_file():
filenames.add(file.name)
return filenames


def test_all_icon_files_exist():
"""Test if icons from css urls is a subset of filenames in icon dir"""
icons = get_css_icon_names()
files = get_icon_filenames()
missing_icons = list(icons - files)
assert not missing_icons

0 comments on commit 7f334d4

Please sign in to comment.