From 834c2c472ffb722ffe334258ff490df0cb79bee0 Mon Sep 17 00:00:00 2001 From: "Dave St.Germain" Date: Tue, 10 Jun 2014 11:50:59 -0400 Subject: [PATCH] Moved test_generate from i18n-tools --- common/lib/test_generate.py | 98 +++++++++++++++++++++++++ pavelib/tests.py | 16 +--- pavelib/utils/test/suites/__init__.py | 1 - pavelib/utils/test/suites/i18n_suite.py | 42 ----------- rakelib/tests_deprecated.rake | 3 - 5 files changed, 99 insertions(+), 61 deletions(-) create mode 100644 common/lib/test_generate.py delete mode 100644 pavelib/utils/test/suites/i18n_suite.py diff --git a/common/lib/test_generate.py b/common/lib/test_generate.py new file mode 100644 index 0000000000..8cf0a41250 --- /dev/null +++ b/common/lib/test_generate.py @@ -0,0 +1,98 @@ +from datetime import datetime, timedelta +import os +import sys +import string +import random +import re + +from unittest import TestCase +from mock import patch +from polib import pofile +from pytz import UTC + +from i18n import config +from i18n import extract +from i18n import generate +from i18n import dummy + + +class TestGenerate(TestCase): + """ + Tests functionality of i18n/generate.py + """ + generated_files = ('django-partial.po', 'djangojs-partial.po', 'mako.po') + + @classmethod + def setUpClass(cls): + cfg = os.path.join('conf', 'locale', 'config.yaml') + config.CONFIGURATION = config.Configuration(cfg) + sys.stderr.write( + "\nExtracting i18n strings and generating dummy translations; " + "this may take a few minutes\n" + ) + sys.stderr.flush() + extract.main(verbosity=0, config=cfg) + dummy.main(verbosity=0, config=cfg) + + def setUp(self): + # Subtract 1 second to help comparisons with file-modify time succeed, + # since os.path.getmtime() is not millisecond-accurate + self.start_time = datetime.now(UTC) - timedelta(seconds=1) + + def test_merge(self): + """ + Tests merge script on English source files. + """ + filename = os.path.join(config.CONFIGURATION.source_messages_dir, random_name()) + generate.merge(config.CONFIGURATION.source_locale, target=filename) + self.assertTrue(os.path.exists(filename)) + os.remove(filename) + + # Patch dummy_locales to not have esperanto present + @patch.object(config.CONFIGURATION, 'dummy_locales', ['fake2']) + def test_main(self): + """ + Runs generate.main() which should merge source files, + then compile all sources in all configured languages. + Validates output by checking all .mo files in all configured languages. + .mo files should exist, and be recently created (modified + after start of test suite) + """ + generate.main(verbosity=0, strict=False) + for locale in config.CONFIGURATION.translated_locales: + for filename in ('django', 'djangojs'): + mofile = filename+'.mo' + path = os.path.join(config.CONFIGURATION.get_messages_dir(locale), mofile) + exists = os.path.exists(path) + self.assertTrue(exists, msg='Missing file in locale %s: %s' % (locale, mofile)) + self.assertTrue(datetime.fromtimestamp(os.path.getmtime(path), UTC) >= self.start_time, + msg='File not recently modified: %s' % path) + # Segmenting means that the merge headers don't work they way they + # used to, so don't make this check for now. I'm not sure if we'll + # get the merge header back eventually, or delete this code eventually. + # self.assert_merge_headers(locale) + + def assert_merge_headers(self, locale): + """ + This is invoked by test_main to ensure that it runs after + calling generate.main(). + + There should be exactly three merge comment headers + in our merged .po file. This counts them to be sure. + A merge comment looks like this: + # #-#-#-#-# django-partial.po (0.1a) #-#-#-#-# + + """ + path = os.path.join(config.CONFIGURATION.get_messages_dir(locale), 'django.po') + po = pofile(path) + pattern = re.compile('^#-#-#-#-#', re.M) + match = pattern.findall(po.header) + self.assertEqual(len(match), 3, + msg="Found %s (should be 3) merge comments in the header for %s" % \ + (len(match), path)) + + +def random_name(size=6): + """Returns random filename as string, like test-4BZ81W""" + chars = string.ascii_uppercase + string.digits + return 'test-' + ''.join(random.choice(chars) for x in range(size)) diff --git a/pavelib/tests.py b/pavelib/tests.py index 4ec4f64536..b6e2a7bccd 100644 --- a/pavelib/tests.py +++ b/pavelib/tests.py @@ -125,19 +125,6 @@ def test_python(options): python_suite.run() -@task -@needs( - 'pavelib.prereqs.install_python_prereqs', - 'pavelib.utils.test.utils.clean_reports_dir', -) -def test_i18n(): - """ - Run all i18n tests - """ - i18n_suite = suites.I18nTestSuite('i18n') - i18n_suite.run() - - @task @needs( 'pavelib.prereqs.install_prereqs', @@ -157,11 +144,10 @@ def test(options): } # Subsuites to be added to the main suite python_suite = suites.PythonTestSuite('Python Tests', **opts) - i18n_suite = suites.I18nTestSuite('i18n', **opts) js_suite = suites.JsTestSuite('JS Tests', mode='run', with_coverage=True) # Main suite to be run - all_unittests_suite = suites.TestSuite('All Tests', subsuites=[i18n_suite, js_suite, python_suite]) + all_unittests_suite = suites.TestSuite('All Tests', subsuites=[js_suite, python_suite]) all_unittests_suite.run() diff --git a/pavelib/utils/test/suites/__init__.py b/pavelib/utils/test/suites/__init__.py index 3aa94bbbfd..737e7994af 100644 --- a/pavelib/utils/test/suites/__init__.py +++ b/pavelib/utils/test/suites/__init__.py @@ -5,4 +5,3 @@ from .suite import TestSuite from .nose_suite import NoseTestSuite, SystemTestSuite, LibTestSuite from .python_suite import PythonTestSuite from .js_suite import JsTestSuite -from .i18n_suite import I18nTestSuite diff --git a/pavelib/utils/test/suites/i18n_suite.py b/pavelib/utils/test/suites/i18n_suite.py deleted file mode 100644 index 4d11c494f8..0000000000 --- a/pavelib/utils/test/suites/i18n_suite.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -Classes used for defining and running i18n test suites -""" -from pavelib.utils.test.suites import TestSuite -from pavelib.utils.envs import Env - -__test__ = False # do not collect - - -class I18nTestSuite(TestSuite): - """ - Run tests for the internationalization library - """ - def __init__(self, *args, **kwargs): - super(I18nTestSuite, self).__init__(*args, **kwargs) - self.report_dir = Env.I18N_REPORT_DIR - self.xunit_report = self.report_dir / 'nosetests.xml' - - def __enter__(self): - super(I18nTestSuite, self).__enter__() - self.report_dir.makedirs_p() - - @property - def cmd(self): - pythonpath_prefix = ( - "PYTHONPATH={repo_root}/i18n:$PYTHONPATH".format( - repo_root=Env.REPO_ROOT - ) - ) - - cmd = ( - "{pythonpath_prefix} nosetests {repo_root}/i18n/tests " - "--with-xunit --xunit-file={xunit_report} " - "--verbosity={verbosity}".format( - pythonpath_prefix=pythonpath_prefix, - repo_root=Env.REPO_ROOT, - xunit_report=self.xunit_report, - verbosity=self.verbosity, - ) - ) - - return cmd diff --git a/rakelib/tests_deprecated.rake b/rakelib/tests_deprecated.rake index 36ee24ab7d..ada6167036 100644 --- a/rakelib/tests_deprecated.rake +++ b/rakelib/tests_deprecated.rake @@ -37,9 +37,6 @@ end deprecated("coverage", "paver coverage", false) -# deprecates i18n:test from i18n.rake -deprecated("i18n:test", 'paver test_i18n', false) - deprecated("clean_reports_dir", "paver clean_reports_dir", false) deprecated("clean_test_files", "paver clean_test_files", false) deprecated("test:clean_mongo", "paver clean_mongo", false)