def test_auto_remote_project(self, extension_mock, get_details_mock): # change the host to tx open(self.config_file, "w").write( '[main]\nhost = https://www.transifex.com\n' ) expected = ("[main]\nhost = https://www.transifex.com\n\n" "[proj.resource_1]\n" "file_filter = translations/proj.resource_1/<lang>.txt\n" "source_lang = fr\ntype = TXT\n\n[proj.resource_2]\n" "file_filter = translations/proj.resource_2/<lang>.txt\n" "source_lang = fr\ntype = TXT\n\n") extension_mock.return_value = ".txt" get_details_mock.side_effect = [ # project details { 'resources': [ {'slug': 'resource_1', 'name': 'resource 1'}, {'slug': 'resource_2', 'name': 'resource 2'} ] }, # resources details { 'source_language_code': 'fr', 'i18n_type': 'TXT', 'slug': 'resource_1', }, { 'source_language_code': 'fr', 'i18n_type': 'TXT', 'slug': 'resource_2', } ] args = [MAPPINGREMOTE, "https://www.transifex.com/test-org/proj/"] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_invalid_expression(self): args = [MAPPINGBULK, "-p", "test-project", "--source-file-dir", "translations", "--source-language", "en", "-t", "TXT", "--file-extension", ".txt", "--execute", "--expression", "expression/{filename}{extension}"] with self.assertRaises(SystemExit): cmd_config(args, self.path_to_tx)
def test_auto_locale(self): expected = "[main]\nhost = https://foo.var\n" args = [MAPPING, "-r", "project1.resource1", '--source-language', 'en', 'translations/<lang>/test.txt'] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_auto_locale_no_expression(self): error_msg = "You need to specify an expression" with assertRaisesRegex(self, Exception, error_msg): args = [ MAPPING, "-r", "project1.resource1", '--source-language', 'en' ] cmd_config(args, self.path_to_tx)
def test_bare_set_source_no_file(self): with self.assertRaises(SystemExit): args = ["-r", "project1.resource1", '--is-source', '-l', 'en'] cmd_config(args, None) with self.assertRaises(Exception): args = ['-r', 'project1.resource1', '--source', '-l', 'en', 'noexistent-file.txt'] cmd_config(args, self.path_to_tx)
def test_auto_locale(self): expected = "[main]\nhost = https://foo.var\n" args = [ MAPPING, "-r", "project1.resource1", '--source-language', 'en', 'translations/<lang>/test.txt' ] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_invalid_expression(self): args = [ MAPPINGBULK, "-p", "test-project", "--source-file-dir", "translations", "--source-language", "en", "-t", "TXT", "--file-extension", ".txt", "--execute", "--expression", "expression/{filename}{extension}" ] with self.assertRaises(SystemExit): cmd_config(args, self.path_to_tx)
def test_auto_remote_invalid_url(self): # no project_url args = [MAPPINGREMOTE] with self.assertRaises(SystemExit): cmd_config(args, self.path_to_tx) # invalid project_url args = [MAPPINGREMOTE, "http://some.random.url/"] with self.assertRaises(Exception): cmd_config(args, self.path_to_tx)
def test_auto_remote_invalid_url(self): # no project_url args = [MAPPINGREMOTE] with self.assertRaises(SystemExit): cmd_config(args, self.path_to_tx) # invalid project_url args = [MAPPINGREMOTE, "http://some.random.url/"] with self.assertRaises(Exception): cmd_config(args, self.path_to_tx)
def test_bare_set_source_no_file(self): with self.assertRaises(SystemExit): args = ["-r", "project1.resource1", '--is-source', '-l', 'en'] cmd_config(args, None) with self.assertRaises(Exception): args = [ '-r', 'project1.resource1', '--source', '-l', 'en', 'noexistent-file.txt' ] cmd_config(args, self.path_to_tx)
def test_auto_locale_execute(self): expected = ("[main]\nhost = https://foo.var\n\n[project1.resource1]\n" "file_filter = translations/<lang>/test.txt\n" "source_file = translations/en/test.txt\n" "source_lang = en\n\n") args = [MAPPING, "-r", "project1.resource1", '--source-language', 'en', '--execute', 'translations/<lang>/test.txt'] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_bulk(self): expected = ("[main]\nhost = https://foo.var\n\n" "[test-project.translations_en_test]\n" "file_filter = translations/<lang>/en/test.txt\n" "source_file = translations/en/test.txt\n" "source_lang = en\ntype = TXT\n\n") args = [MAPPINGBULK, "-p", "test-project", "--source-file-dir", "translations", "--source-language", "en", "-t", "TXT", "--file-extension", ".txt", "--execute", "--expression", "translations/<lang>/{filepath}/{filename}{extension}"] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_auto_locale_execute(self): expected = ("[main]\nhost = https://foo.var\n\n[project1.resource1]\n" "file_filter = translations/<lang>/test.txt\n" "source_file = translations/en/test.txt\n" "source_lang = en\n\n") args = [ MAPPING, "-r", "project1.resource1", '--source-language', 'en', '--execute', 'translations/<lang>/test.txt' ] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_bulk(self): expected = ("[main]\nhost = https://foo.var\n\n" "[test-project.translations_en_test]\n" "file_filter = translations/<lang>/en/test.txt\n" "source_file = translations/en/test.txt\n" "source_lang = en\ntype = TXT\n\n") args = [ MAPPINGBULK, "-p", "test-project", "--source-file-dir", "translations", "--source-language", "en", "-t", "TXT", "--file-extension", ".txt", "--execute", "--expression", "translations/<lang>/{filepath}/{filename}{extension}" ] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_bare_set_source_file(self): expected = ("[main]\nhost = https://foo.var\n\n[project1.resource1]\n" "source_file = test.txt\nsource_lang = en\n\n") args = ["-r", "project1.resource1", '--source', '-l', 'en', 'test.txt'] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected) # set translation file for de expected = ("[main]\nhost = https://foo.var\n\n[project1.resource1]\n" "source_file = test.txt\nsource_lang = en\n" "trans.de = translations/de.txt\n\n") args = ["-r", "project1.resource1", '-l', 'de', 'translations/de.txt'] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_bare_set_source_file(self): expected = ("[main]\nhost = https://foo.var\n\n[project1.resource1]\n" "source_file = test.txt\nsource_lang = en\n\n") args = ["-r", "project1.resource1", '--source', '-l', 'en', 'test.txt'] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected) # set translation file for de expected = ("[main]\nhost = https://foo.var\n\n[project1.resource1]\n" "source_file = test.txt\nsource_lang = en\n" "trans.de = translations/de.txt\n\n") args = ["-r", "project1.resource1", '-l', 'de', 'translations/de.txt'] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_invalid_expression_status(self): """ Test how the client handles invalid expressions in file_filter """ valid_expression = "test_expressions/<lang>/test.txt" invalid_expression = "test_expressions/{filename}{ext}" args = [ MAPPINGBULK, "-p", "test-project", "--source-file-dir", "test_expressions/en", "--source-language", "en", "-t", "TXT", "--file-extension", ".txt", "--execute", "--expression", valid_expression ] # Configure the client using a valid expression cmd_config(args, self.path_to_tx) # A trick to capture the standard output sys_stdout = sys.stdout out = StringIO() sys.stdout = out # Check for the expected status using the valid expression cmd_status([], None) output = out.getvalue().strip() sys.stdout = sys_stdout self.assertTrue("test_expressions/en/test.txt" in output) self.assertTrue("test_expressions/es/test.txt" in output) # Change the configuration file "by hand" and replace the file_filter # with an invalid expression for line in fileinput.input(self.config_file, inplace=True): print(line.replace(valid_expression, invalid_expression)) sys_stdout = sys.stdout out = StringIO() sys.stdout = out cmd_status([], None) output = out.getvalue().strip() sys.stdout = sys_stdout # Check that the Spanish translation file in not tracked self.assertTrue("test_expressions/en/test.txt" in output) self.assertFalse("test_expressions/es/test.txt" in output)
def test_auto_remote_is_backwards_compatible(self, extension_mock, get_details_mock): # change the host to tx open(self.config_file, "w").write('[main]\nhost = https://www.transifex.com\n') expected = ("[main]\nhost = https://www.transifex.com\n\n" "[proj.resource_1]\n" "file_filter = translations/proj.resource_1/<lang>.txt\n" "source_lang = fr\ntype = TXT\n\n[proj.resource_2]\n" "file_filter = translations/proj.resource_2/<lang>.txt\n" "source_lang = fr\ntype = TXT\n\n") extension_mock.return_value = ".txt" get_details_mock.side_effect = [ # project details { 'resources': [{ 'slug': 'resource_1', 'name': 'resource 1' }, { 'slug': 'resource_2', 'name': 'resource 2' }] }, # resources details { 'source_language_code': 'fr', 'i18n_type': 'TXT', 'slug': 'resource_1', }, { 'source_language_code': 'fr', 'i18n_type': 'TXT', 'slug': 'resource_2', } ] args = ["--auto-remote", "https://www.transifex.com/test-org/proj/"] cmd_config(args, self.path_to_tx) with open(self.config_file) as config: self.assertEqual(config.read(), expected)
def test_invalid_expression_status(self): """ Test how the client handles invalid expressions in file_filter """ valid_expression = "test_expressions/<lang>/test.txt" invalid_expression = "test_expressions/{filename}{ext}" args = [MAPPINGBULK, "-p", "test-project", "--source-file-dir", "test_expressions/en", "--source-language", "en", "-t", "TXT", "--file-extension", ".txt", "--execute", "--expression", valid_expression] # Configure the client using a valid expression cmd_config(args, self.path_to_tx) # A trick to capture the standard output sys_stdout = sys.stdout out = StringIO() sys.stdout = out # Check for the expected status using the valid expression cmd_status([], None) output = out.getvalue().strip() sys.stdout = sys_stdout self.assertTrue("test_expressions/en/test.txt" in output) self.assertTrue("test_expressions/es/test.txt" in output) # Change the configuration file "by hand" and replace the file_filter # with an invalid expression for line in fileinput.input(self.config_file, inplace=True): print(line.replace(valid_expression, invalid_expression)) sys_stdout = sys.stdout out = StringIO() sys.stdout = out cmd_status([], None) output = out.getvalue().strip() sys.stdout = sys_stdout # Check that the Spanish translation file in not tracked self.assertTrue("test_expressions/en/test.txt" in output) self.assertFalse("test_expressions/es/test.txt" in output)
def test_bulk_missing_options(self): with self.assertRaises(SystemExit): args = [MAPPINGBULK] cmd_config(args, self.path_to_tx) with self.assertRaises(SystemExit): args = [MAPPINGBULK, "-p", "test-project"] cmd_config(args, self.path_to_tx) with self.assertRaises(SystemExit): args = [MAPPINGBULK, "-p", "test-project", "--source-language", "en", "--t", "TXT", "--file-extension", ".txt"] cmd_config(args, self.path_to_tx)
def test_bulk_missing_options(self): with self.assertRaises(SystemExit): args = [MAPPINGBULK] cmd_config(args, self.path_to_tx) with self.assertRaises(SystemExit): args = [MAPPINGBULK, "-p", "test-project"] cmd_config(args, self.path_to_tx) with self.assertRaises(SystemExit): args = [ MAPPINGBULK, "-p", "test-project", "--source-language", "en", "--t", "TXT", "--file-extension", ".txt" ] cmd_config(args, self.path_to_tx)
def test_bare_set_too_few_arguments(self): with self.assertRaises(SystemExit): args = ["-r", "project1.resource1"] cmd_config(args, None)
def test_auto_locale_no_expression(self): error_msg = "You need to specify an expression" with assertRaisesRegex(self, Exception, error_msg): args = [MAPPING, "-r", "project1.resource1", '--source-language', 'en'] cmd_config(args, self.path_to_tx)
def test_auto_locale_no_expression(self): with self.assertRaises(SystemExit): args = [ MAPPING, "-r", "project1.resource1", '--source-language', 'en' ] cmd_config(args, self.path_to_tx)
def test_bare_set_too_few_arguments(self): with self.assertRaises(SystemExit): args = ["-r", "project1.resource1"] cmd_config(args, None)