mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
Refs #26677 -- Simplified i18n test cleanups.
The fact that we aren't dealing with the Django source tree anymore allows us to drop several tearDown()/addCleanup() calls that were concerned with removing apiece files/dirs/symlinks created by test cases, as we are covered by the removal of the parent temporary tree anyways. Thanks Tim Graham for advice and review.
This commit is contained in:
parent
0451dcc2eb
commit
bb7bb379e8
4 changed files with 77 additions and 161 deletions
67
tests/i18n/utils.py
Normal file
67
tests/i18n/utils.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
import os
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
from django.utils._os import upath
|
||||
|
||||
|
||||
source_code_dir = os.path.dirname(upath(__file__))
|
||||
|
||||
|
||||
def copytree(src, dst):
|
||||
shutil.copytree(src, dst, ignore=shutil.ignore_patterns('*.pyc', '__pycache__'))
|
||||
|
||||
|
||||
class POFileAssertionMixin(object):
|
||||
|
||||
def _assertPoKeyword(self, keyword, expected_value, haystack, use_quotes=True):
|
||||
q = '"'
|
||||
if use_quotes:
|
||||
expected_value = '"%s"' % expected_value
|
||||
q = "'"
|
||||
needle = '%s %s' % (keyword, expected_value)
|
||||
expected_value = re.escape(expected_value)
|
||||
return self.assertTrue(
|
||||
re.search('^%s %s' % (keyword, expected_value), haystack, re.MULTILINE),
|
||||
'Could not find %(q)s%(n)s%(q)s in generated PO file' % {'n': needle, 'q': q}
|
||||
)
|
||||
|
||||
def assertMsgId(self, msgid, haystack, use_quotes=True):
|
||||
return self._assertPoKeyword('msgid', msgid, haystack, use_quotes=use_quotes)
|
||||
|
||||
|
||||
class RunInTmpDirMixin(object):
|
||||
"""
|
||||
Allow i18n tests that need to generate .po/.mo files to run in an isolated
|
||||
temporary filesystem tree created by tempfile.mkdtemp() that contains a
|
||||
clean copy of the relevant test code.
|
||||
|
||||
Test classes using this mixin need to define a `work_subdir` attribute
|
||||
which designates the subdir under `tests/i18n/` that will be copied to the
|
||||
temporary tree from which its test cases will run.
|
||||
|
||||
The setUp() method sets the current working dir to the temporary tree.
|
||||
It'll be removed when cleaning up.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self._cwd = os.getcwd()
|
||||
self.work_dir = tempfile.mkdtemp(prefix='i18n_')
|
||||
self.test_dir = os.path.abspath(os.path.join(self.work_dir, self.work_subdir))
|
||||
copytree(os.path.join(source_code_dir, self.work_subdir), self.test_dir)
|
||||
# Step out of the temporary working tree before removing it to avoid
|
||||
# deletion problems on Windows. Cleanup actions registered with
|
||||
# addCleanup() are called in reverse so preserve this ordering.
|
||||
self.addCleanup(self._rmrf, self.test_dir)
|
||||
self.addCleanup(os.chdir, self._cwd)
|
||||
os.chdir(self.test_dir)
|
||||
|
||||
def _rmrf(self, dname):
|
||||
if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
|
||||
return
|
||||
shutil.rmtree(dname)
|
||||
|
||||
def rmfile(self, filepath):
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
Loading…
Add table
Add a link
Reference in a new issue