1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
# -*- coding: utf-8 -*-
import os
import shutil
from io import StringIO
from tempfile import mkdtemp
from django.conf import settings
from django.core.management import CommandError, call_command
from django.test import TestCase
from django.test.utils import override_settings
def test_validate_templates():
out = StringIO()
try:
call_command("validate_templates", verbosity=3, stdout=out, stderr=out)
except CommandError:
print(out.getvalue())
raise
output = out.getvalue()
assert "0 errors found\n" in output
class ValidateTemplatesTests(TestCase):
def setUp(self):
self.tempdir = mkdtemp()
def tearDown(self):
shutil.rmtree(self.tempdir)
def test_should_print_that_there_is_error(self):
with override_settings(
INSTALLED_APPS=settings.INSTALLED_APPS
+ ["tests.testapp_with_template_errors"]
):
with self.assertRaisesRegex(CommandError, "1 errors found"):
call_command("validate_templates", verbosity=3)
def test_should_not_print_any_errors_if_template_in_VALIDATE_TEMPLATES_IGNORES(
self,
):
out = StringIO()
with override_settings(
INSTALLED_APPS=settings.INSTALLED_APPS
+ ["tests.testapp_with_template_errors"],
VALIDATE_TEMPLATES_IGNORES=["template_with_error.html"],
):
call_command("validate_templates", verbosity=3, stdout=out, stderr=out)
output = out.getvalue()
self.assertIn("0 errors found\n", output)
def test_should_not_print_any_errors_if_app_in_VALIDATE_TEMPLATES_IGNORE_APPS(self):
out = StringIO()
with override_settings(
INSTALLED_APPS=settings.INSTALLED_APPS
+ ["tests.testapp_with_template_errors"],
VALIDATE_TEMPLATES_IGNORE_APPS=["tests.testapp_with_template_errors"],
):
call_command("validate_templates", verbosity=3, stdout=out, stderr=out)
output = out.getvalue()
self.assertIn("0 errors found\n", output)
def test_should_break_when_first_error_occur(self):
fn = os.path.join(self.tempdir, "template_with_error.html")
with open(fn, "w") as f:
f.write("""{% invalid_tag %}""")
with self.assertRaisesRegex(CommandError, "Errors found"):
call_command("validate_templates", "-i", self.tempdir, "-b", verbosity=3)
|