Coverage for sm / test_templates.py: 93%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-24 12:43 +0000

1import os 

2import re 

3from django.test import TestCase 

4from django.conf import settings 

5 

6 

7class TemplateIntegrityTest(TestCase): 

8 def test_no_scripts_block_override(self): 

9 """ 

10 Verify that no template overrides the 'scripts' block without calling 

11 {{ block.super }}. Overriding 'scripts' without super() blocks jQuery 

12 and causes JS errors. 

13 """ 

14 template_dirs = [os.path.join(settings.BASE_DIR, "sm", "templates")] 

15 # Also check app templates 

16 for app in settings.INSTALLED_APPS: 

17 if not app.startswith("django."): 

18 app_path = os.path.join( 

19 settings.BASE_DIR, app.split(".")[0], "templates" 

20 ) 

21 if os.path.exists(app_path): 

22 template_dirs.append(app_path) 

23 

24 scripts_block_re = re.compile(r"{%\s*block\s+scripts\s*%}") 

25 block_super_re = re.compile(r"{{\s*block\.super\s*}}") 

26 

27 errors = [] 

28 for t_dir in template_dirs: 

29 for root, dirs, files in os.walk(t_dir): 

30 for file in files: 

31 if file.endswith(".html"): 

32 path = os.path.join(root, file) 

33 # Skip base template itself 

34 if "theme_bootstrap/base.html" in path: 

35 continue 

36 

37 with open(path) as f: 

38 content = f.read() 

39 if scripts_block_re.search(content): 

40 if not block_super_re.search(content): 

41 rel_path = os.path.relpath(path, settings.BASE_DIR) 

42 errors.append( 

43 f"{rel_path} overrides 'scripts' block" 

44 " without calling" 

45 " {{ block.super }}" 

46 ) 

47 

48 self.assertEqual(errors, [], "\n".join(errors))