From 47535e9ab46b402acfb4a479e94328087161064c Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Aug 24 2017 15:07:18 +0000 Subject: Fix encoding in new command In previous commit, new_diff returned from GitPython API is unicode string and has to be encoded in encoding UTF-8. That works well with GitPython>=1.0, but not with version GitPython<1.0 which returns string in basestring type. This failure case happens in EL6 with 0.3.2-0.6.RC1.el6. Signed-off-by: Chenxiong Qi --- diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 8ae2707..35d9878 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -23,6 +23,7 @@ import time import koji import pyrpkg.utils as utils +import six from pyrpkg import rpkgError, log as rpkgLogger from six.moves import xmlrpc_client, configparser @@ -1329,7 +1330,12 @@ see API KEY section of copr-cli(1) man page. def new(self): new_diff = self.cmd.new() - print(new_diff.encode('utf-8')) + # When running rpkg with old version GitPython<1.0 which returns string + # in type basestring, no need to encode. + if isinstance(new_diff, six.string_types): + print(new_diff) + else: + print(new_diff.encode('utf-8')) def new_sources(self): # Check to see if the files passed exist diff --git a/tests/test_cli.py b/tests/test_cli.py index a380d65..f4b44ec 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -753,6 +753,21 @@ class TestNew(CliTestCase): output = sys.stdout.getvalue() self.assertTrue('+New change' in output) + @patch('sys.stdout', new=StringIO()) + @patch('pyrpkg.Commands.new') + def test_diff_returned_as_bytestring(self, new): + # diff is return from Commands.new as bytestring when using + # GitPython<1.0. So, mock new method directly to test diff in + # bytestring can be printed correctly. + new.return_value = b'New content' + cli_cmd = ['rpkg', '--path', self.cloned_repo_path, 'new'] + with patch('sys.argv', new=cli_cmd): + cli = self.new_cli() + cli.new() + + output = sys.stdout.getvalue() + self.assertTrue(b'New content' in output) + class TestNewPrintUnicode(CliTestCase): """Test new diff contains unicode characters