Python 3 compatibility.

This commit is contained in:
Anthony Sottile 2014-04-12 18:19:57 -07:00
parent ddebb83a40
commit bb365a6e68
10 changed files with 30 additions and 27 deletions

View file

@ -2,6 +2,8 @@ language: python
env: # These should match the tox env list
- TOXENV=py26
- TOXENV=py27
- TOXENV=py33
- TOXENV=pypy
install: pip install tox --use-mirrors
script: tox

View file

@ -21,7 +21,7 @@ def extend_validator_cls(validator_cls, modify):
def default_values(properties, instance):
for property, subschema in properties.iteritems():
for property, subschema in properties.items():
if 'default' in subschema:
instance.setdefault(
property, copy.deepcopy(subschema['default']),
@ -29,7 +29,7 @@ def default_values(properties, instance):
def remove_default_values(properties, instance):
for property, subschema in properties.iteritems():
for property, subschema in properties.items():
if (
'default' in subschema and
instance.get(property) == subschema['default']

View file

@ -14,12 +14,13 @@ LOG_LEVEL_COLORS = {
class LoggingHandler(logging.Handler):
def __init__(self, use_color):
def __init__(self, use_color, print_fn=print):
logging.Handler.__init__(self)
self.use_color = use_color
self.__print_fn = print_fn
def emit(self, record):
print(
self.__print_fn(
u'{0}{1}'.format(
color.format_color(
'[{0}]'.format(record.levelname),

View file

@ -55,6 +55,9 @@ class PrefixedCommandRunner(object):
'stdout': subprocess.PIPE,
'stderr': subprocess.PIPE,
}
if stdin is not None:
stdin = stdin.encode('utf-8')
popen_kwargs.update(kwargs)
self._create_path_if_not_exists()
replaced_cmd = _replace_cmd(cmd, prefix=self.prefix_dir)

View file

@ -70,7 +70,7 @@ class Repository(object):
logger.info('Installing environment for {0}.'.format(self.repo_url))
logger.info('Once installed this environment will be reused.')
logger.info('This may take a few minutes...')
with clean_path_on_failure(unicode(local.path(self.sha))):
with clean_path_on_failure(str(local.path(self.sha))):
local['git']['clone', '--no-checkout', self.repo_url, self.sha]()
with self.in_checkout():
local['git']['checkout', self.sha]()

View file

@ -45,7 +45,6 @@ def _run_single_hook(runner, repository, hook_id, args):
get_filenames(hook['files'], hook['exclude']),
)
output = '\n'.join([stdout, stderr]).strip()
if retcode != repository.hooks[hook_id]['expected_return_value']:
retcode = 1
print_color = color.RED
@ -57,8 +56,12 @@ def _run_single_hook(runner, repository, hook_id, args):
print(color.format_color(pass_fail, print_color, args.color))
if output and (retcode or args.verbose):
print('\n' + output)
if (stdout or stderr) and (retcode or args.verbose):
print()
for output in (stdout, stderr):
if output.strip():
print(output.strip())
print()
return retcode

View file

@ -1,17 +1,9 @@
import __builtin__
import mock
import pytest
from pre_commit import color
from pre_commit.logging_handler import LoggingHandler
@pytest.yield_fixture
def print_mock():
with mock.patch.object(__builtin__, 'print', autospec=True) as print_mock:
yield print_mock
class FakeLogRecord(object):
def __init__(self, message, levelname, levelno):
self.message = message
@ -22,16 +14,18 @@ class FakeLogRecord(object):
return self.message
def test_logging_handler_color(print_mock):
handler = LoggingHandler(True)
def test_logging_handler_color():
print_mock = mock.Mock()
handler = LoggingHandler(True, print_mock)
handler.emit(FakeLogRecord('hi', 'WARNING', 30))
print_mock.assert_called_once_with(
color.YELLOW + '[WARNING]' + color.NORMAL + ' hi',
)
def test_logging_handler_no_color(print_mock):
handler = LoggingHandler(False)
def test_logging_handler_no_color():
print_mock = mock.Mock()
handler = LoggingHandler(False, print_mock)
handler.emit(FakeLogRecord('hi', 'WARNING', 30))
print_mock.assert_called_once_with(
'[WARNING] hi',

View file

@ -55,7 +55,7 @@ def test_run_a_python_hook(config_for_python_hooks_repo):
)
assert ret[0] == 0
assert ret[1] == "['/dev/null']\nHello World\n"
assert ret[1] == b"['/dev/null']\nHello World\n"
@pytest.mark.integration
@ -77,7 +77,7 @@ def test_cwd_of_hook(config_for_prints_cwd_repo):
)
assert ret[0] == 0
assert ret[1] == '{0}\n'.format(repo.repo_url)
assert ret[1] == repo.repo_url.encode('utf-8') + b'\n'
@pytest.mark.skipif(
@ -90,7 +90,7 @@ def test_run_a_node_hook(config_for_node_hooks_repo):
ret = repo.run_hook(PrefixedCommandRunner(C.HOOKS_WORKSPACE), 'foo', [])
assert ret[0] == 0
assert ret[1] == 'Hello World\n'
assert ret[1] == b'Hello World\n'
@pytest.mark.integration
@ -101,7 +101,7 @@ def test_run_a_script_hook(config_for_script_hooks_repo):
)
assert ret[0] == 0
assert ret[1] == 'bar\nHello World\n'
assert ret[1] == b'bar\nHello World\n'
@pytest.fixture

View file

@ -118,8 +118,8 @@ def img_staged(empty_git_dir):
def _test_img_state(path, expected_file='img1.jpg', status='A'):
assert os.path.exists(path.img_filename)
assert (
open(path.img_filename).read() ==
open(get_resource_path(expected_file)).read()
open(path.img_filename, 'rb').read() ==
open(get_resource_path(expected_file), 'rb').read()
)
actual_status = get_short_git_status()['img.jpg']
assert status == actual_status

View file

@ -1,7 +1,7 @@
[tox]
project = pre_commit
# These should match the travis env list
envlist = py26,py27
envlist = py26,py27,py33,pypy
[testenv]
install_command = pip install --use-wheel {opts} {packages}