Skip to content

Commit f01baa3

Browse files
authored
Merge pull request #1709 from UlrichB22/test_icons
Test if icons from css urls is a subset of filenames in icon dir
2 parents a77f2bf + 7f334d4 commit f01baa3

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/moin/macros/_tests/test_ShowIcons.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
Test for macros.ShowIcons
66
"""
77

8+
import re
9+
import os
810
from moin.macros.ShowIcons import Macro
911

12+
my_dir = os.path.abspath(os.path.dirname(__file__))
13+
1014

1115
def test_ShowIconsMacro():
1216
"""Call ShowIcons macro and test output"""
@@ -30,3 +34,35 @@ def test_ShowIconsMacro():
3034
assert expected_tags.issubset(result_tags)
3135
assert expected_texts.issubset(result_texts)
3236
assert expected_paths.issubset(result_paths)
37+
38+
39+
def get_css_icon_names():
40+
"""Read css file and search for icon urls"""
41+
css_file = os.path.join(os.path.split(my_dir)[0], "..", "static", "css", "common.css")
42+
with open(css_file, encoding="utf-8") as f:
43+
common_css = f.readlines()
44+
icon_list = set()
45+
for line in common_css:
46+
link = re.search(r"url\([\'\"]?([(.)/].*?)[\'\"]?\)", line)
47+
if link and link.group(0)[5:].startswith("../img/icons"):
48+
icon_list.add(link.group(0).split("/", 3)[3][:-2])
49+
return icon_list
50+
51+
52+
def get_icon_filenames():
53+
"""Scan img/icons dir for filenames"""
54+
icon_dir = os.path.join(os.path.split(my_dir)[0], "..", "static", "img", "icons")
55+
filenames = set()
56+
with os.scandir(icon_dir) as files:
57+
for file in files:
58+
if not file.name.startswith(".") and file.is_file():
59+
filenames.add(file.name)
60+
return filenames
61+
62+
63+
def test_all_icon_files_exist():
64+
"""Test if icons from css urls is a subset of filenames in icon dir"""
65+
icons = get_css_icon_names()
66+
files = get_icon_filenames()
67+
missing_icons = list(icons - files)
68+
assert not missing_icons

0 commit comments

Comments
 (0)