utils.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. import re
  3. import shutil
  4. import tempfile
  5. source_code_dir = os.path.dirname(__file__)
  6. def copytree(src, dst):
  7. shutil.copytree(src, dst, ignore=shutil.ignore_patterns('__pycache__'))
  8. class POFileAssertionMixin:
  9. def _assertPoKeyword(self, keyword, expected_value, haystack, use_quotes=True):
  10. q = '"'
  11. if use_quotes:
  12. expected_value = '"%s"' % expected_value
  13. q = "'"
  14. needle = '%s %s' % (keyword, expected_value)
  15. expected_value = re.escape(expected_value)
  16. return self.assertTrue(
  17. re.search('^%s %s' % (keyword, expected_value), haystack, re.MULTILINE),
  18. 'Could not find %(q)s%(n)s%(q)s in generated PO file' % {'n': needle, 'q': q}
  19. )
  20. def assertMsgId(self, msgid, haystack, use_quotes=True):
  21. return self._assertPoKeyword('msgid', msgid, haystack, use_quotes=use_quotes)
  22. class RunInTmpDirMixin:
  23. """
  24. Allow i18n tests that need to generate .po/.mo files to run in an isolated
  25. temporary filesystem tree created by tempfile.mkdtemp() that contains a
  26. clean copy of the relevant test code.
  27. Test classes using this mixin need to define a `work_subdir` attribute
  28. which designates the subdir under `tests/i18n/` that will be copied to the
  29. temporary tree from which its test cases will run.
  30. The setUp() method sets the current working dir to the temporary tree.
  31. It'll be removed when cleaning up.
  32. """
  33. def setUp(self):
  34. self._cwd = os.getcwd()
  35. self.work_dir = tempfile.mkdtemp(prefix='i18n_')
  36. # Resolve symlinks, if any, in test directory paths.
  37. self.test_dir = os.path.realpath(os.path.join(self.work_dir, self.work_subdir))
  38. copytree(os.path.join(source_code_dir, self.work_subdir), self.test_dir)
  39. # Step out of the temporary working tree before removing it to avoid
  40. # deletion problems on Windows. Cleanup actions registered with
  41. # addCleanup() are called in reverse so preserve this ordering.
  42. self.addCleanup(self._rmrf, self.test_dir)
  43. self.addCleanup(os.chdir, self._cwd)
  44. os.chdir(self.test_dir)
  45. def _rmrf(self, dname):
  46. if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
  47. return
  48. shutil.rmtree(dname)
  49. def rmfile(self, filepath):
  50. if os.path.exists(filepath):
  51. os.remove(filepath)