def test_filters(self): sources = [ 'tests/i18n/code.py', ] kwargs = { 'translations_dir': config.TEMP_DIR, 'mapping': 'tests/i18n/babel.cfg', 'source': sources, 'extract_update': True, 'compile': True, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_messages(args) manage.sh(""" pybabel init -i {d}/messages.pot -d {d} -l en_US pybabel init -i {d}/messages.pot -d {d} -l fr_FR """.format(d=config.TEMP_DIR)) fake_config = self.get_fake_config() fake_config.SUPPORTED_LOCALES = ['en_US', 'fr_FR'] fake_config.TRANSLATION_DIRS = config.TEMP_DIR for app in (journalist_app.create_app(fake_config), source_app.create_app(fake_config)): assert i18n.LOCALES == fake_config.SUPPORTED_LOCALES self.verify_filesizeformat(app) self.verify_rel_datetime_format(app)
def test_filters(self): sources = [ 'tests/i18n/code.py', ] kwargs = { 'translations_dir': config.TEMP_DIR, 'mapping': 'tests/i18n/babel.cfg', 'source': sources, 'extract_update': True, 'compile': True, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_messages(args) manage.sh(""" pybabel init -i {d}/messages.pot -d {d} -l en_US pybabel init -i {d}/messages.pot -d {d} -l fr_FR """.format(d=config.TEMP_DIR)) supported = getattr(config, 'SUPPORTED_LOCALES', None) try: if supported: del config.SUPPORTED_LOCALES for app in (journalist.app, source.app): config.SUPPORTED_LOCALES = ['en_US', 'fr_FR'] app.config['BABEL_TRANSLATION_DIRECTORIES'] = config.TEMP_DIR i18n.setup_app(app) self.verify_filesizeformat(app) self.verify_rel_datetime_format(app) finally: if supported: config.SUPPORTED_LOCALES = supported
def test_sh_fail(self, caplog): with pytest.raises(subprocess.CalledProcessError) as excinfo: manage.sh("/bin/echo -n AB ; /bin/echo C ; exit 111") assert excinfo.value.returncode == 111 for record in caplog.records(): if record.levelname == 'ERROR': assert ('replay full' in record.message or 'ABC\n' == record.message)
def test_sh_progress(self, caplog): manage.sh("echo AB ; sleep 5 ; echo C") records = caplog.records assert ':sh: ' in records[0].message assert records[0].levelname == 'DEBUG' assert 'AB' == records[1].message assert records[1].levelname == 'DEBUG' assert 'C' == records[2].message assert records[2].levelname == 'DEBUG'
def test_i18n(self): sources = [ 'tests/i18n/code.py', 'tests/i18n/template.html', ] kwargs = { 'translations_dir': config.TEMP_DIR, 'mapping': 'tests/i18n/babel.cfg', 'source': sources, 'extract_update': True, 'compile': True, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_messages(args) manage.sh(""" pybabel init -i {d}/messages.pot -d {d} -l en_US pybabel init -i {d}/messages.pot -d {d} -l fr_FR sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code bonjour"/' \ {d}/fr_FR/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l zh_Hans_CN sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code chinese"/' \ {d}/zh_Hans_CN/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l ar sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code arabic"/' \ {d}/ar/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l nb_NO sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code norwegian"/' \ {d}/nb_NO/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l es_ES sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code spanish"/' \ {d}/es_ES/LC_MESSAGES/messages.po """.format(d=config.TEMP_DIR)) manage.translate_messages(args) fake_config = self.get_fake_config() fake_config.SUPPORTED_LOCALES = [ 'en_US', 'fr_FR', 'zh_Hans_CN', 'ar', 'nb_NO' ] fake_config.TRANSLATION_DIRS = config.TEMP_DIR for app in (journalist_app.create_app(fake_config), source_app.create_app(fake_config)): assert i18n.LOCALES == fake_config.SUPPORTED_LOCALES self.verify_i18n(app)
def test_i18n(self): sources = [ 'tests/i18n/code.py', 'tests/i18n/template.html', ] kwargs = { 'translations_dir': config.TEMP_DIR, 'mapping': 'tests/i18n/babel.cfg', 'source': sources, 'extract_update': True, 'compile': True, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_messages(args) manage.sh(""" pybabel init -i {d}/messages.pot -d {d} -l en_US pybabel init -i {d}/messages.pot -d {d} -l fr_FR sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code bonjour"/' \ {d}/fr_FR/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l zh_Hans_CN sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code chinese"/' \ {d}/zh_Hans_CN/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l ar sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code arabic"/' \ {d}/ar/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l nb_NO sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code norwegian"/' \ {d}/nb_NO/LC_MESSAGES/messages.po """.format(d=config.TEMP_DIR)) manage.translate_messages(args) supported = getattr(config, 'SUPPORTED_LOCALES', None) try: if supported: del config.SUPPORTED_LOCALES for app in (journalist.app, source.app): config.SUPPORTED_LOCALES = [ 'en_US', 'fr_FR', 'zh_Hans_CN', 'ar', 'nb_NO' ] i18n.setup_app(app, translation_dirs=config.TEMP_DIR) self.verify_i18n(app) finally: if supported: config.SUPPORTED_LOCALES = supported
def test_i18n(self): sources = [ 'tests/i18n/code.py', 'tests/i18n/template.html', ] kwargs = { 'translations_dir': config.TEMP_DIR, 'mapping': 'tests/i18n/babel.cfg', 'source': sources, 'extract_update': True, 'compile': True, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_messages(args) manage.sh(""" pybabel init -i {d}/messages.pot -d {d} -l en_US pybabel init -i {d}/messages.pot -d {d} -l fr_FR sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code bonjour"/' \ {d}/fr_FR/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l zh_Hans_CN sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code chinese"/' \ {d}/zh_Hans_CN/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l ar sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code arabic"/' \ {d}/ar/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l nb_NO sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code norwegian"/' \ {d}/nb_NO/LC_MESSAGES/messages.po pybabel init -i {d}/messages.pot -d {d} -l es_ES sed -i -e '/code hello i18n/,+1s/msgstr ""/msgstr "code spanish"/' \ {d}/es_ES/LC_MESSAGES/messages.po """.format(d=config.TEMP_DIR)) manage.translate_messages(args) fake_config = self.get_fake_config() fake_config.SUPPORTED_LOCALES = [ 'en_US', 'fr_FR', 'zh_Hans_CN', 'ar', 'nb_NO'] fake_config.TRANSLATION_DIRS = config.TEMP_DIR for app in (journalist_app.create_app(fake_config), source_app.create_app(fake_config)): assert i18n.LOCALES == fake_config.SUPPORTED_LOCALES self.verify_i18n(app)
def test_sh_fail(self, caplog): level = manage.log.getEffectiveLevel() manage.log.setLevel(logging.INFO) assert manage.log.getEffectiveLevel() == logging.INFO with pytest.raises(subprocess.CalledProcessError) as excinfo: manage.sh("echo AB ; echo C ; exit 111") manage.log.setLevel(level) assert excinfo.value.returncode == 111 records = caplog.records assert 'AB' == records[0].message assert records[0].levelname == 'ERROR' assert 'C' == records[1].message assert records[1].levelname == 'ERROR'
def test_translate_messages_l10n(self): source = [ join(self.dir, 'i18n/code.py'), join(self.dir, 'i18n/template.html'), ] kwargs = { 'translations_dir': config.TEMP_DIR, 'mapping': join(self.dir, 'i18n/babel.cfg'), 'source': source, 'extract_update': True, 'compile': True, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_messages(args) messages_file = join(config.TEMP_DIR, 'messages.pot') assert exists(messages_file) pot = open(messages_file).read() assert 'code hello i18n' in pot assert 'template hello i18n' in pot locale = 'en_US' locale_dir = join(config.TEMP_DIR, locale) manage.sh("pybabel init -i {} -d {} -l {}".format( messages_file, config.TEMP_DIR, locale, )) mo_file = join(locale_dir, 'LC_MESSAGES/messages.mo') assert not exists(mo_file) manage.translate_messages(args) assert exists(mo_file) mo = open(mo_file).read() assert 'code hello i18n' in mo assert 'template hello i18n' in mo
def test_sh_input(self, caplog): assert 'abc' == manage.sh("cat", 'abc')
def test_sh(self): assert 'A' == manage.sh("echo -n A") with pytest.raises(Exception) as excinfo: manage.sh("exit 123") assert excinfo.value.returncode == 123
def test_translate_messages_compile_arg(self): source = [ join(self.dir, 'i18n/code.py'), ] kwargs = { 'translations_dir': config.TEMP_DIR, 'mapping': join(self.dir, 'i18n/babel.cfg'), 'source': source, 'extract_update': True, 'compile': False, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_messages(args) messages_file = join(config.TEMP_DIR, 'messages.pot') assert exists(messages_file) pot = open(messages_file).read() assert 'code hello i18n' in pot locale = 'en_US' locale_dir = join(config.TEMP_DIR, locale) po_file = join(locale_dir, 'LC_MESSAGES/messages.po') manage.sh("pybabel init -i {} -d {} -l {}".format( messages_file, config.TEMP_DIR, locale, )) assert exists(po_file) # pretend this happened a few seconds ago few_seconds_ago = time.time() - 60 os.utime(po_file, (few_seconds_ago, few_seconds_ago)) mo_file = join(locale_dir, 'LC_MESSAGES/messages.mo') # # Extract+update but do not compile # old_po_mtime = getmtime(po_file) assert not exists(mo_file) manage.translate_messages(args) assert not exists(mo_file) current_po_mtime = getmtime(po_file) assert old_po_mtime < current_po_mtime # # Compile but do not extract+update # source = [ join(self.dir, 'i18n/code.py'), join(self.dir, 'i18n/template.html'), ] kwargs['extract_update'] = False kwargs['compile'] = True args = argparse.Namespace(**kwargs) old_po_mtime = current_po_mtime manage.translate_messages(args) assert old_po_mtime == getmtime(po_file) mo = open(mo_file).read() assert 'code hello i18n' in mo assert 'template hello i18n' not in mo
def test_translate_desktop_l10n(self): in_files = {} for what in ('source', 'journalist'): in_files[what] = join(config.TEMP_DIR, what + '.desktop.in') shutil.copy(join(self.dir, 'i18n/' + what + '.desktop.in'), in_files[what]) kwargs = { 'translations_dir': config.TEMP_DIR, 'source': [in_files['source']], 'extract_update': True, 'compile': False, 'verbose': logging.DEBUG, 'version': version.__version__, } args = argparse.Namespace(**kwargs) manage.setup_verbosity(args) manage.translate_desktop(args) messages_file = join(config.TEMP_DIR, 'desktop.pot') assert exists(messages_file) pot = open(messages_file).read() assert 'SecureDrop Source Interfaces' in pot # pretend this happened a few seconds ago few_seconds_ago = time.time() - 60 os.utime(messages_file, (few_seconds_ago, few_seconds_ago)) i18n_file = join(config.TEMP_DIR, 'source.desktop') # # Extract+update but do not compile # kwargs['source'] = in_files.values() old_messages_mtime = getmtime(messages_file) assert not exists(i18n_file) manage.translate_desktop(args) assert not exists(i18n_file) current_messages_mtime = getmtime(messages_file) assert old_messages_mtime < current_messages_mtime locale = 'fr_FR' po_file = join(config.TEMP_DIR, locale + ".po") manage.sh(""" msginit --no-translator \ --locale {locale} \ --output {po_file} \ --input {messages_file} sed -i -e '/{source}/,+1s/msgstr ""/msgstr "SOURCE FR"/' \ {po_file} """.format(source='SecureDrop Source Interfaces', messages_file=messages_file, po_file=po_file, locale=locale)) assert exists(po_file) # # Compile but do not extract+update # kwargs['source'] = in_files.values() + ['BOOM'] kwargs['extract_update'] = False kwargs['compile'] = True args = argparse.Namespace(**kwargs) old_messages_mtime = current_messages_mtime manage.translate_desktop(args) assert old_messages_mtime == getmtime(messages_file) po = open(po_file).read() assert 'SecureDrop Source Interfaces' in po assert 'SecureDrop Journalist Interfaces' not in po i18n = open(i18n_file).read() assert 'SOURCE FR' in i18n