Skip to content

Commit

Permalink
Merge pull request #191 from transifex/logger_fixes
Browse files Browse the repository at this point in the history
Don't use logger for regular output like help and status
  • Loading branch information
SofiaMargariti authored Nov 2, 2017
2 parents 291a668 + c69dfeb commit 3d7a74a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
40 changes: 39 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import sys
from StringIO import StringIO
from mock import patch, MagicMock
from txclib.commands import _set_source_file, _set_translation, cmd_pull, \
UnInitializedError
UnInitializedError, cmd_help, cmd_status


class TestCommands(unittest.TestCase):
Expand All @@ -18,3 +21,38 @@ def test_set_translation_when_dir_not_initialized(self):
with self.assertRaises(UnInitializedError):
_set_translation(path_to_tx=None, resource="dummy_resource.md",
lang='en', path_to_file='invalid')


class TestStatusCommand(unittest.TestCase):
@patch('txclib.commands.project')
def test_status(self, mock_p):
"""Test status command"""
mock_project = MagicMock()
mock_project.get_chosen_resources.return_value = ['foo.bar']
mock_project.get_resource_files.return_value = {
"fr": "translations/foo.bar/fr.po",
"de": "translations/foo.bar/de.po"
}
mock_p.Project.return_value = mock_project
cmd_status([], None)
mock_project.get_chosen_resources.assert_called_once_with([])
self.assertEqual(mock_project.get_resource_files.call_count, 1)


class TestHelpCommand(unittest.TestCase):
def test_help(self):
out = StringIO()
sys.stdout = out
cmd_help([], None)
output = out.getvalue().strip()
self.assertTrue(
all(
c in output for c in
['delete', 'help', 'init', 'pull', 'push', 'set', 'status']
)
)

# call for specific command
with patch('txclib.commands.cmd_pull', spec=cmd_pull) as pull_mock:
cmd_help(['pull'], None)
pull_mock.assert_called_once_with(['--help'], None)
24 changes: 12 additions & 12 deletions txclib/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def cmd_init(argv, path_to_tx):
# if we already have a config file and we are not told to override it
# in the args we have to ask
if os.path.isdir(os.path.join(path_to_tx, ".tx")) and not save:
logger.info("tx: There is already a tx folder!")
logger.info("There is already a tx folder!")
if not utils.confirm(
prompt='Do you want to delete it and reinit the project?',
default=False
Expand Down Expand Up @@ -473,23 +473,23 @@ def cmd_status(argv, path_to_tx):
resources_num = len(resources)
for idx, res in enumerate(resources):
p, r = res.split('.')
logger.info("%s -> %s (%s of %s)" % (p, r, idx + 1, resources_num))
logger.info("Translation Files:")
print("%s -> %s (%s of %s)" % (p, r, idx + 1, resources_num))
print("Translation Files:")
slang = prj.get_resource_option(res, 'source_lang')
sfile = prj.get_resource_option(res, 'source_file') or "N/A"
lang_map = prj.get_resource_lang_mapping(res)
logger.info(" - %s: %s (%s)" % (utils.color_text(slang, "RED"),
sfile, utils.color_text("source", "YELLOW")))
print(" - %s: %s (%s)" % (utils.color_text(slang, "RED"),
sfile, utils.color_text("source", "YELLOW")))
files = prj.get_resource_files(res)
fkeys = list(files.keys())
fkeys.sort()
for lang in fkeys:
local_lang = lang
if lang in list(lang_map.values()):
local_lang = lang_map.flip[lang]
logger.info(" - %s: %s" % (utils.color_text(local_lang, "RED"),
files[lang]))
logger.info("")
print(" - %s: %s" % (utils.color_text(local_lang, "RED"),
files[lang]))
print("")


def cmd_help(argv, path_to_tx):
Expand All @@ -516,11 +516,11 @@ def cmd_help(argv, path_to_tx):
keys = list(fns.keys())
keys.sort()

logger.info("Transifex command line client.\n")
logger.info("Available commands are:")
print("Transifex command line client.\n")
print("Available commands are:")
for key in keys:
logger.info(" %-15s\t%s" % (key, getattr(fns[key], '__doc__')))
logger.info("\nFor more information run %s command --help" % sys.argv[0])
print(" %-15s\t%s" % (key, getattr(fns[key], '__doc__')))
print("\nFor more information run %s command --help" % sys.argv[0])


def cmd_delete(argv, path_to_tx):
Expand Down

0 comments on commit 3d7a74a

Please sign in to comment.