Ticket #27526: patch

File patch, 4.1 KB (added by Raphaël Hertzog, 8 years ago)
  • tests/i18n/test_compilation.py

    diff --git a/tests/i18n/test_compilation.py b/tests/i18n/test_compilation.py
    index a65fcac..8bc3f51 100644
    a b import gettext as gettext_module  
    55import os
    66import shutil
    77import stat
     8import tempfile
    89import unittest
    910from subprocess import Popen
    1011
    class MessageCompilationTests(SimpleTestCase):  
    3132    test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'commands'))
    3233
    3334    def setUp(self):
     35        # Update test_dir to point to a temporary directory and work from there
     36        self._test_dir = self.test_dir
    3437        self._cwd = os.getcwd()
    35         self.addCleanup(os.chdir, self._cwd)
     38        self._tempdir = tempfile.mkdtemp()
     39        self.test_dir = os.path.join(self._tempdir,
     40                                     os.path.basename(self._test_dir))
     41        shutil.copytree(self._test_dir, self.test_dir)
    3642        os.chdir(self.test_dir)
    3743
    38     def _rmrf(self, dname):
    39         if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
    40             return
    41         shutil.rmtree(dname)
    42 
    43     def rmfile(self, filepath):
    44         if os.path.exists(filepath):
    45             os.remove(filepath)
     44    def tearDown(self):
     45        shutil.rmtree(self._tempdir)
     46        self.test_dir = self._test_dir
     47        os.chdir(self._cwd)
    4648
    4749
    4850class PoFileTests(MessageCompilationTests):
    class PoFileContentsTests(MessageCompilationTests):  
    7678    LOCALE = 'fr'
    7779    MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
    7880
    79     def setUp(self):
    80         super(PoFileContentsTests, self).setUp()
    81         self.addCleanup(os.unlink, os.path.join(self.test_dir, self.MO_FILE))
    82 
    8381    def test_percent_symbol_in_po_file(self):
    8482        call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
    8583        self.assertTrue(os.path.exists(self.MO_FILE))
    class MultipleLocaleCompilationTests(MessageCompilationTests):  
    9593        localedir = os.path.join(self.test_dir, 'locale')
    9694        self.MO_FILE_HR = os.path.join(localedir, 'hr/LC_MESSAGES/django.mo')
    9795        self.MO_FILE_FR = os.path.join(localedir, 'fr/LC_MESSAGES/django.mo')
    98         self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_HR))
    99         self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_FR))
    10096
    10197    def test_one_locale(self):
    10298        with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, 'locale')]):
    class ExcludedLocaleCompilationTests(MessageCompilationTests):  
    122118        super(ExcludedLocaleCompilationTests, self).setUp()
    123119
    124120        shutil.copytree('canned_locale', 'locale')
    125         self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale'))
    126121
    127122    def test_command_help(self):
    128123        with captured_stdout(), captured_stderr():
    class CompilationErrorHandling(MessageCompilationTests):  
    161156    def test_error_reported_by_msgfmt(self):
    162157        # po file contains wrong po formatting.
    163158        mo_file = 'locale/ja/LC_MESSAGES/django.mo'
    164         self.addCleanup(self.rmfile, os.path.join(self.test_dir, mo_file))
    165159        with self.assertRaises(CommandError):
    166160            call_command('compilemessages', locale=['ja'], verbosity=0)
    167161
    168162    def test_msgfmt_error_including_non_ascii(self):
    169163        # po file contains invalid msgstr content (triggers non-ascii error content).
    170164        mo_file = 'locale/ko/LC_MESSAGES/django.mo'
    171         self.addCleanup(self.rmfile, os.path.join(self.test_dir, mo_file))
    172165        # Make sure the output of msgfmt is unaffected by the current locale.
    173166        env = os.environ.copy()
    174167        env.update({str('LANG'): str('C')})
    class ProjectAndAppTests(MessageCompilationTests):  
    192185    PROJECT_MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
    193186    APP_MO_FILE = 'app_with_locale/locale/%s/LC_MESSAGES/django.mo' % LOCALE
    194187
    195     def setUp(self):
    196         super(ProjectAndAppTests, self).setUp()
    197         self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.PROJECT_MO_FILE))
    198         self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.APP_MO_FILE))
    199 
    200188
    201189class FuzzyTranslationTest(ProjectAndAppTests):
    202190
Back to Top