Skip to content

Commit ced120f

Browse files
feat: improve template coverage cfg (#9230)
* feat: template coverage ignores using path * refactor: reduce repeatitive redundancy * chore: copyrights * refactor: use pathlib to compute BASE_DIR * feat: settings.PROJECT_DIR * fix: glob from PROJECT_DIR not BASE_DIR * chore: remove reference code
1 parent 47afc6e commit ced120f

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

ietf/settings.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright The IETF Trust 2007-2024, All Rights Reserved
1+
# Copyright The IETF Trust 2007-2025, All Rights Reserved
22
# -*- coding: utf-8 -*-
33

44

@@ -9,6 +9,7 @@
99
import os
1010
import sys
1111
import datetime
12+
import pathlib
1213
import warnings
1314
from hashlib import sha384
1415
from typing import Any, Dict, List, Tuple # pyflakes:ignore
@@ -27,8 +28,12 @@
2728
warnings.filterwarnings("ignore", message="Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated", module="bleach")
2829
warnings.filterwarnings("ignore", message="HTTPResponse.getheader\\(\\) is deprecated", module='selenium.webdriver')
2930

30-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
31-
sys.path.append(os.path.abspath(BASE_DIR + "/.."))
31+
base_path = pathlib.Path(__file__).resolve().parent
32+
BASE_DIR = str(base_path)
33+
34+
project_path = base_path.parent
35+
PROJECT_DIR = str(project_path)
36+
sys.path.append(PROJECT_DIR)
3237

3338
from ietf import __version__
3439
import debug
@@ -717,12 +722,13 @@ def skip_unreadable_post(record):
717722
]
718723

719724
# These are filename globs. They are used by test_parse_templates() and
720-
# get_template_paths()
725+
# get_template_paths(). Globs are applied via pathlib.Path().match, using
726+
# the path to the template from the project root.
721727
TEST_TEMPLATE_IGNORE = [
722-
".*", # dot-files
723-
"*~", # tilde temp-files
724-
"#*", # files beginning with a hashmark
725-
"500.html" # isn't loaded by regular loader, but checked by test_500_page()
728+
".*", # dot-files
729+
"*~", # tilde temp-files
730+
"#*", # files beginning with a hashmark
731+
"500.html", # isn't loaded by regular loader, but checked by test_500_page()
726732
]
727733

728734
TEST_COVERAGE_MAIN_FILE = os.path.join(BASE_DIR, "../release-coverage.json")

ietf/utils/test_runner.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright The IETF Trust 2009-2020, All Rights Reserved
1+
# Copyright The IETF Trust 2009-2025, All Rights Reserved
22
# -*- coding: utf-8 -*-
33
#
44
# Portion Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
@@ -54,7 +54,6 @@
5454
import urllib3
5555
import warnings
5656

57-
from fnmatch import fnmatch
5857
from typing import Callable, Optional
5958
from urllib.parse import urlencode
6059

@@ -414,8 +413,9 @@ def do_append(res, p0, p1, item):
414413
res.append((str(item.pattern), item))
415414
return res
416415

416+
417417
_all_templates = None
418-
def get_template_paths(apps=None):
418+
def get_template_paths(apps=None) -> list[str]:
419419
global _all_templates
420420
if not _all_templates:
421421
# TODO: Add app templates to the full list, if we are using
@@ -424,25 +424,26 @@ def get_template_paths(apps=None):
424424
templatepaths = settings.TEMPLATES[0]['DIRS']
425425
for templatepath in templatepaths:
426426
for dirpath, dirs, files in os.walk(templatepath):
427-
if ".svn" in dirs:
428-
dirs.remove(".svn")
429-
relative_path = dirpath[len(templatepath)+1:]
430-
for file in files:
431-
ignore = False
432-
for pattern in settings.TEST_TEMPLATE_IGNORE:
433-
if fnmatch(file, pattern):
434-
ignore = True
435-
break
436-
if ignore:
437-
continue
438-
if relative_path != "":
439-
file = os.path.join(relative_path, file)
440-
templates.add(file)
441-
if apps:
442-
templates = [ t for t in templates if t.split(os.path.sep)[0] in apps ]
443-
_all_templates = templates
427+
# glob against path from PROJECT_DIR
428+
project_path = pathlib.Path(
429+
dirpath.removeprefix(settings.PROJECT_DIR).lstrip("/")
430+
)
431+
# label entries with name relative to templatepath
432+
relative_path = pathlib.Path(
433+
dirpath.removeprefix(templatepath).lstrip("/")
434+
)
435+
if apps and relative_path.parts[0] not in apps:
436+
continue # skip uninteresting apps
437+
for filename in files:
438+
file_path = project_path / filename
439+
if not any(
440+
file_path.match(pat) for pat in settings.TEST_TEMPLATE_IGNORE
441+
):
442+
templates.add(relative_path / filename)
443+
_all_templates = [str(t) for t in templates]
444444
return _all_templates
445445

446+
446447
def save_test_results(failures, test_labels):
447448
# Record the test result in a file, in order to be able to check the
448449
# results and avoid re-running tests if we've already run them with OK

ietf/utils/tests.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from email.mime.image import MIMEImage
2020
from email.mime.multipart import MIMEMultipart
2121
from email.mime.text import MIMEText
22-
from fnmatch import fnmatch
2322
from importlib import import_module
2423
from textwrap import dedent
2524
from tempfile import mkdtemp
@@ -320,7 +319,7 @@ class TemplateChecksTestCase(TestCase):
320319
def setUp(self):
321320
super().setUp()
322321
set_coverage_checking(False)
323-
self.paths = list(get_template_paths())
322+
self.paths = get_template_paths() # already filtered ignores
324323
self.paths.sort()
325324
for path in self.paths:
326325
try:
@@ -335,11 +334,7 @@ def tearDown(self):
335334
def test_parse_templates(self):
336335
errors = []
337336
for path in self.paths:
338-
for pattern in settings.TEST_TEMPLATE_IGNORE:
339-
if fnmatch(path, pattern):
340-
continue
341-
if not path in self.templates:
342-
337+
if path not in self.templates:
343338
try:
344339
get_template(path)
345340
except Exception as e:

0 commit comments

Comments
 (0)