Files
kevinveenbirkenbachandClaude Opus 5.5 326f063bd3 fix(codescan): clear the mechanical CodeQL notes and lint them away (WIP)
Work-in-progress commit on the collective code-scanning sidetree, so main
can be merged in; the single quality-high gate runs on the integration
branch before the pull request.

Fixes the CodeQL notes py/import-and-import-from, py/unused-import,
py/unused-global-variable, py/empty-except, py/mixed-tuple-returns and
py/unused-local-variable across 194 files, and adds one lint per rule under
tests/lint/repository/python/ so none of them can come back:
test_import_and_import_from, test_unused_imports_hidden_from_ruff,
test_unused_global_variables, test_empty_except, test_mixed_tuple_returns,
test_unused_local_variables, plus test_modules_import.

Accepted security finding: MEDUSA-MP-SCAN-2056 (low) on
utils/i18n/client.py:21, an import line matched by a keyword heuristic;
operator-confirmed false positive.

🎯 Fix: 85 %
💥 Break: 8 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 78
🚦 Gate: ⛔ fix 85 % < 90, break 8 % > 5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 18:44:29 +00:00

104 lines
3.4 KiB
Python

from __future__ import annotations
import sys
import types
import unittest
import unittest.mock
from jinja2 import Environment, StrictUndefined, select_autoescape
from utils.cache.files import PROJECT_ROOT, read_text
TEMPLATE = (
PROJECT_ROOT
/ "roles"
/ "web-app-taiga"
/ "templates"
/ "settings"
/ "config.append.py.j2"
)
CLEAR = "taiga.kanban.example.test"
JINJA = "django_jinja.backend.Jinja2"
DJANGO = "django.template.backends.django.DjangoTemplates"
def settle(subpath: str = "", jinja_backend: str = JINJA) -> dict:
"""Run the rendered append on top of taiga-back's image settings.
Args:
subpath: ``FORCE_SCRIPT_NAME`` as the image derives it from ``TAIGA_SUBPATH``.
jinja_backend: ``BACKEND`` of the first engine in ``TEMPLATES``.
Returns:
The settings namespace after the append ran.
"""
environment = Environment(
undefined=StrictUndefined,
autoescape=select_autoescape(default_for_string=False),
)
environment.filters["bool"] = bool
rendered = environment.from_string(read_text(str(TEMPLATE))).render(
TAIGA_CONFIG_APPEND_MARKER="taiga-config-append",
TAIGA_OIDC_ENABLED=False,
TAIGA_LDAP_ENABLED=False,
application_id="web-app-taiga",
lookup=lambda *_terms: False,
)
namespace = {
"TAIGA_SITES_SCHEME": "https",
"TAIGA_SITES_DOMAIN": CLEAR,
"FORCE_SCRIPT_NAME": subpath,
"STATIC_URL": f"https://{CLEAR}{subpath}/static/",
"TEMPLATES": [
{"BACKEND": jinja_backend, "OPTIONS": {"match_extension": ".jinja"}},
{"BACKEND": DJANGO, "OPTIONS": {}},
],
}
exec(compile(rendered, str(TEMPLATE), "exec"), namespace)
return namespace
def mail_static(namespace: dict, path: str) -> str:
static = types.ModuleType("django.templatetags.static")
static.static = lambda name: namespace["STATIC_URL"] + name
modules = {
"django": types.ModuleType("django"),
"django.templatetags": types.ModuleType("django.templatetags"),
"django.templatetags.static": static,
}
with unittest.mock.patch.dict(sys.modules, modules):
return namespace["TEMPLATES"][0]["OPTIONS"]["globals"]["static"](path)
class TestBrowserAssets(unittest.TestCase):
def test_the_admin_loads_static_files_from_the_page_host(self) -> None:
self.assertEqual(settle()["STATIC_URL"], "/static/")
def test_a_subpath_keeps_its_prefix(self) -> None:
self.assertEqual(settle(subpath="/taiga")["STATIC_URL"], "/taiga/static/")
def test_the_django_engine_keeps_its_static_tag(self) -> None:
self.assertNotIn("globals", settle()["TEMPLATES"][1]["OPTIONS"])
class TestMailAssets(unittest.TestCase):
def test_mail_templates_keep_the_canonical_absolute_url(self) -> None:
self.assertEqual(
mail_static(settle(), "emails/logo-web.png"),
f"https://{CLEAR}/static/emails/logo-web.png",
)
def test_mail_templates_keep_the_subpath_once(self) -> None:
self.assertEqual(
mail_static(settle(subpath="/taiga"), "emails/logo-web.png"),
f"https://{CLEAR}/taiga/static/emails/logo-web.png",
)
def test_a_renamed_jinja_backend_fails_loudly(self) -> None:
with self.assertRaises(StopIteration):
settle(jinja_backend="django_jinja.Jinja2")
if __name__ == "__main__":
unittest.main()