From 8843ac8c54773c6a146e91a0b4f36483b1024c32 Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Sun, 17 Jan 2016 16:33:47 +0800 Subject: [PATCH] [test_i18n] Add a test case for I18N --- test/test_i18n.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 test/test_i18n.py diff --git a/test/test_i18n.py b/test/test_i18n.py new file mode 100644 index 000000000..a5b5bd988 --- /dev/null +++ b/test/test_i18n.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# coding: utf-8 + +from __future__ import unicode_literals + +import contextlib +import os +import shutil +import subprocess +import sys + +try: + import unittest + unittest.TestCase.setUpClass +except AttributeError: + import unittest2 as unittest + +# Allow direct execution +rootDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, rootDir) + +from youtube_dl.version import __version__ +from youtube_dl.compat import subprocess_check_output +from youtube_dl.utils import decodeFilename + + +@contextlib.contextmanager +def chdir_to(path): + oldpwd = os.getcwd() + os.chdir(os.path.join(decodeFilename(rootDir), path)) + yield + os.chdir(oldpwd) + + +class I18NTestCase(object): + @classmethod + def setUpClass(cls): + with chdir_to('.'): + cls.make('update-gmo') + + cls.install() + + # avoid false positive + with chdir_to('.'): + shutil.rmtree(os.path.join('share', 'locale')) + + @classmethod + def tearDownClass(cls): + cls.uninstall() + + @classmethod + def make(cls, target): + with chdir_to('.'): + subprocess.check_call([ + 'make', target], env=dict(os.environ, PYTHON=sys.executable)) + + def test_lang_opt(self): + def output_with_lang_opt(lang): + return subprocess_check_output([ + sys.executable, self.PROGRAM, 'i18n:test', '--lang', lang + ]).decode('utf-8').strip() + + self.assertEqual(output_with_lang_opt('en_US.UTF-8'), 'I18N test message') + self.assertEqual(output_with_lang_opt('zh_TW.UTF-8'), 'I18N測試訊息') + + def test_lc_all(self): + def output_with_lc_all(lang): + return subprocess_check_output([ + sys.executable, self.PROGRAM, 'i18n:test' + ], env=dict(os.environ, LC_ALL=lang)).decode('utf-8').strip() + + self.assertEqual(output_with_lc_all('en_US.UTF-8'), 'I18N test message') + self.assertEqual(output_with_lc_all('zh_TW.UTF-8'), 'I18N測試訊息') + + +@unittest.skipUnless(os.environ.get('VIRTUAL_ENV'), 'Requires virtualenv because of call to pip install') +class TestPipInstall(I18NTestCase, unittest.TestCase): + PROGRAM = os.path.join(os.environ['VIRTUAL_ENV'], 'bin', 'youtube-dl') + + @classmethod + def install(cls): + with chdir_to('.'): + subprocess.check_call([sys.executable, 'setup.py', '--quiet', 'sdist']) + + with chdir_to('test'): + subprocess.check_call([ + 'pip', 'install', '--quiet', + os.path.join('..', 'dist', 'youtube_dl-%s.tar.gz' % __version__)]) + + @classmethod + def uninstall(cls): + with chdir_to('test'): + subprocess.check_call(['pip', 'uninstall', '--yes', 'youtube_dl']) + + +class TestDirectInstall(I18NTestCase, unittest.TestCase): + PROGRAM = os.path.join(os.environ['VIRTUAL_ENV'], 'bin', 'youtube-dl') + + @classmethod + def install(cls): + with chdir_to('.'): + subprocess.check_call([sys.executable, 'setup.py', '--quiet', 'install']) + + @classmethod + def uninstall(cls): + with chdir_to('test'): + subprocess.check_call(['pip', 'uninstall', '--yes', 'youtube_dl']) + + +if __name__ == '__main__': + unittest.main()