Skip to content

Commit b1f6b65

Browse files
committed
Handle bytestring decode in 'git_config' helper
Don't split this over two places, lest we forget to do it. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent 8d575c8 commit b1f6b65

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

git_pw/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def __getattribute__(self, name):
3030
value = utils.git_config('pw.{}'.format(name))
3131
if value:
3232
LOG.debug("Retrieved '{}' setting from git-config".format(name))
33-
value = value.decode('utf-8')
3433

3534
setattr(self, name, value)
3635

git_pw/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import subprocess
88
import sys
99

10+
if sys.version_info < (3, 0):
11+
_text = unicode # noqa
12+
else:
13+
_text = str # noqa
14+
1015

1116
def trim(string, length=70): # type: (str, int) -> str
1217
"""Trim a string to the given length."""
@@ -22,9 +27,9 @@ def git_config(value):
2227
try:
2328
output = subprocess.check_output(['git', 'config', value])
2429
except subprocess.CalledProcessError:
25-
output = ''
30+
output = b''
2631

27-
return output.strip()
32+
return output.decode('utf-8').strip()
2833

2934

3035
def git_am(mbox, args):

tests/test_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@
88
from git_pw import utils
99

1010

11-
@mock.patch.object(utils.subprocess, 'check_output', return_value=' bar ')
11+
@mock.patch.object(utils.subprocess, 'check_output', return_value=b' bar ')
1212
def test_git_config(mock_subprocess):
1313
value = utils.git_config('foo')
1414

1515
assert value == 'bar'
1616
mock_subprocess.assert_called_once_with(['git', 'config', 'foo'])
1717

1818

19+
@mock.patch.object(utils.subprocess, 'check_output',
20+
return_value=b'\xf0\x9f\xa4\xb7')
21+
def test_git_config_unicode(mock_subprocess):
22+
value = utils.git_config('foo')
23+
24+
assert value == u'\U0001f937'
25+
mock_subprocess.assert_called_once_with(['git', 'config', 'foo'])
26+
27+
1928
@mock.patch.object(utils.subprocess, 'check_output',
2029
side_effect=subprocess.CalledProcessError(1, 'xyz', '123'))
2130
def test_git_config_error(mock_subprocess):

0 commit comments

Comments
 (0)