mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1252 from pre-commit/normalize_crlf
Normalize crlf in tests
This commit is contained in:
commit
40e0b2c57f
7 changed files with 46 additions and 58 deletions
|
|
@ -2,6 +2,7 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import os.path
|
import os.path
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -24,9 +25,11 @@ def cmd_output_mocked_pre_commit_home(*args, **kwargs):
|
||||||
# keyword-only argument
|
# keyword-only argument
|
||||||
tempdir_factory = kwargs.pop('tempdir_factory')
|
tempdir_factory = kwargs.pop('tempdir_factory')
|
||||||
pre_commit_home = kwargs.pop('pre_commit_home', tempdir_factory.get())
|
pre_commit_home = kwargs.pop('pre_commit_home', tempdir_factory.get())
|
||||||
|
kwargs.setdefault('stderr', subprocess.STDOUT)
|
||||||
# Don't want to write to the home directory
|
# Don't want to write to the home directory
|
||||||
env = dict(kwargs.pop('env', os.environ), PRE_COMMIT_HOME=pre_commit_home)
|
env = dict(kwargs.pop('env', os.environ), PRE_COMMIT_HOME=pre_commit_home)
|
||||||
return cmd_output(*args, env=env, **kwargs)
|
ret, out, _ = cmd_output(*args, env=env, **kwargs)
|
||||||
|
return ret, out.replace('\r\n', '\n'), None
|
||||||
|
|
||||||
|
|
||||||
skipif_cant_run_docker = pytest.mark.skipif(
|
skipif_cant_run_docker = pytest.mark.skipif(
|
||||||
|
|
@ -137,8 +140,10 @@ def cwd(path):
|
||||||
def git_commit(*args, **kwargs):
|
def git_commit(*args, **kwargs):
|
||||||
fn = kwargs.pop('fn', cmd_output)
|
fn = kwargs.pop('fn', cmd_output)
|
||||||
msg = kwargs.pop('msg', 'commit!')
|
msg = kwargs.pop('msg', 'commit!')
|
||||||
|
kwargs.setdefault('stderr', subprocess.STDOUT)
|
||||||
|
|
||||||
cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', '-a') + args
|
cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', '-a') + args
|
||||||
if msg is not None: # allow skipping `-m` with `msg=None`
|
if msg is not None: # allow skipping `-m` with `msg=None`
|
||||||
cmd += ('-m', msg)
|
cmd += ('-m', msg)
|
||||||
return fn(*cmd, **kwargs)
|
ret, out, _ = fn(*cmd, **kwargs)
|
||||||
|
return ret, out.replace('\r\n', '\n')
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import os.path
|
import os.path
|
||||||
import subprocess
|
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
|
|
@ -30,11 +29,9 @@ def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
|
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
retcode, output, _ = git_commit(
|
retcode, output = git_commit(
|
||||||
fn=cmd_output_mocked_pre_commit_home,
|
fn=cmd_output_mocked_pre_commit_home,
|
||||||
tempdir_factory=tempdir_factory,
|
tempdir_factory=tempdir_factory,
|
||||||
# git commit puts pre-commit to stderr
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
)
|
)
|
||||||
assert retcode == 0
|
assert retcode == 0
|
||||||
assert 'Bash hook....' in output
|
assert 'Bash hook....' in output
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ from __future__ import unicode_literals
|
||||||
import io
|
import io
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
@ -121,30 +120,28 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
|
||||||
cmd_output('git', 'add', touch_file)
|
cmd_output('git', 'add', touch_file)
|
||||||
return git_commit(
|
return git_commit(
|
||||||
fn=cmd_output_mocked_pre_commit_home,
|
fn=cmd_output_mocked_pre_commit_home,
|
||||||
# git commit puts pre-commit to stderr
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
retcode=None,
|
retcode=None,
|
||||||
tempdir_factory=tempdir_factory,
|
tempdir_factory=tempdir_factory,
|
||||||
**kwargs
|
**kwargs
|
||||||
)[:2]
|
)
|
||||||
|
|
||||||
|
|
||||||
# osx does this different :(
|
# osx does this different :(
|
||||||
FILES_CHANGED = (
|
FILES_CHANGED = (
|
||||||
r'('
|
r'('
|
||||||
r' 1 file changed, 0 insertions\(\+\), 0 deletions\(-\)\r?\n'
|
r' 1 file changed, 0 insertions\(\+\), 0 deletions\(-\)\n'
|
||||||
r'|'
|
r'|'
|
||||||
r' 0 files changed\r?\n'
|
r' 0 files changed\n'
|
||||||
r')'
|
r')'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
NORMAL_PRE_COMMIT_RUN = re.compile(
|
NORMAL_PRE_COMMIT_RUN = re.compile(
|
||||||
r'^\[INFO\] Initializing environment for .+\.\r?\n'
|
r'^\[INFO\] Initializing environment for .+\.\n'
|
||||||
r'Bash hook\.+Passed\r?\n'
|
r'Bash hook\.+Passed\n'
|
||||||
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
|
r'\[master [a-f0-9]{7}\] commit!\n' +
|
||||||
FILES_CHANGED +
|
FILES_CHANGED +
|
||||||
r' create mode 100644 foo\r?\n$',
|
r' create mode 100644 foo\n$',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -265,7 +262,7 @@ def test_environment_not_sourced(tempdir_factory, store):
|
||||||
|
|
||||||
# Use a specific homedir to ignore --user installs
|
# Use a specific homedir to ignore --user installs
|
||||||
homedir = tempdir_factory.get()
|
homedir = tempdir_factory.get()
|
||||||
ret, stdout, stderr = git_commit(
|
ret, out = git_commit(
|
||||||
env={
|
env={
|
||||||
'HOME': homedir,
|
'HOME': homedir,
|
||||||
'PATH': _path_without_us(),
|
'PATH': _path_without_us(),
|
||||||
|
|
@ -278,22 +275,21 @@ def test_environment_not_sourced(tempdir_factory, store):
|
||||||
retcode=None,
|
retcode=None,
|
||||||
)
|
)
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert stdout == ''
|
assert out == (
|
||||||
assert stderr.replace('\r\n', '\n') == (
|
|
||||||
'`pre-commit` not found. '
|
'`pre-commit` not found. '
|
||||||
'Did you forget to activate your virtualenv?\n'
|
'Did you forget to activate your virtualenv?\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
FAILING_PRE_COMMIT_RUN = re.compile(
|
FAILING_PRE_COMMIT_RUN = re.compile(
|
||||||
r'^\[INFO\] Initializing environment for .+\.\r?\n'
|
r'^\[INFO\] Initializing environment for .+\.\n'
|
||||||
r'Failing hook\.+Failed\r?\n'
|
r'Failing hook\.+Failed\n'
|
||||||
r'- hook id: failing_hook\r?\n'
|
r'- hook id: failing_hook\n'
|
||||||
r'- exit code: 1\r?\n'
|
r'- exit code: 1\n'
|
||||||
r'\r?\n'
|
r'\n'
|
||||||
r'Fail\r?\n'
|
r'Fail\n'
|
||||||
r'foo\r?\n'
|
r'foo\n'
|
||||||
r'\r?\n$',
|
r'\n$',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -308,10 +304,10 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
|
||||||
|
|
||||||
|
|
||||||
EXISTING_COMMIT_RUN = re.compile(
|
EXISTING_COMMIT_RUN = re.compile(
|
||||||
r'^legacy hook\r?\n'
|
r'^legacy hook\n'
|
||||||
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
|
r'\[master [a-f0-9]{7}\] commit!\n' +
|
||||||
FILES_CHANGED +
|
FILES_CHANGED +
|
||||||
r' create mode 100644 baz\r?\n$',
|
r' create mode 100644 baz\n$',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -369,9 +365,9 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
|
||||||
|
|
||||||
|
|
||||||
FAIL_OLD_HOOK = re.compile(
|
FAIL_OLD_HOOK = re.compile(
|
||||||
r'fail!\r?\n'
|
r'fail!\n'
|
||||||
r'\[INFO\] Initializing environment for .+\.\r?\n'
|
r'\[INFO\] Initializing environment for .+\.\n'
|
||||||
r'Bash hook\.+Passed\r?\n',
|
r'Bash hook\.+Passed\n',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -465,10 +461,10 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
|
||||||
|
|
||||||
|
|
||||||
PRE_INSTALLED = re.compile(
|
PRE_INSTALLED = re.compile(
|
||||||
r'Bash hook\.+Passed\r?\n'
|
r'Bash hook\.+Passed\n'
|
||||||
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
|
r'\[master [a-f0-9]{7}\] commit!\n' +
|
||||||
FILES_CHANGED +
|
FILES_CHANGED +
|
||||||
r' create mode 100644 foo\r?\n$',
|
r' create mode 100644 foo\n$',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -527,8 +523,6 @@ def test_installed_from_venv(tempdir_factory, store):
|
||||||
def _get_push_output(tempdir_factory, opts=()):
|
def _get_push_output(tempdir_factory, opts=()):
|
||||||
return cmd_output_mocked_pre_commit_home(
|
return cmd_output_mocked_pre_commit_home(
|
||||||
'git', 'push', 'origin', 'HEAD:new_branch', *opts,
|
'git', 'push', 'origin', 'HEAD:new_branch', *opts,
|
||||||
# git push puts pre-commit to stderr
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
tempdir_factory=tempdir_factory,
|
tempdir_factory=tempdir_factory,
|
||||||
retcode=None
|
retcode=None
|
||||||
)[:2]
|
)[:2]
|
||||||
|
|
@ -648,7 +642,7 @@ def test_commit_msg_integration_failing(
|
||||||
install(C.CONFIG_FILE, store, hook_types=['commit-msg'])
|
install(C.CONFIG_FILE, store, hook_types=['commit-msg'])
|
||||||
retc, out = _get_commit_output(tempdir_factory)
|
retc, out = _get_commit_output(tempdir_factory)
|
||||||
assert retc == 1
|
assert retc == 1
|
||||||
assert out.replace('\r', '') == '''\
|
assert out == '''\
|
||||||
Must have "Signed off by:"...............................................Failed
|
Must have "Signed off by:"...............................................Failed
|
||||||
- hook id: must-have-signoff
|
- hook id: must-have-signoff
|
||||||
- exit code: 1
|
- exit code: 1
|
||||||
|
|
@ -695,7 +689,7 @@ def test_prepare_commit_msg_integration_failing(
|
||||||
install(C.CONFIG_FILE, store, hook_types=['prepare-commit-msg'])
|
install(C.CONFIG_FILE, store, hook_types=['prepare-commit-msg'])
|
||||||
retc, out = _get_commit_output(tempdir_factory)
|
retc, out = _get_commit_output(tempdir_factory)
|
||||||
assert retc == 1
|
assert retc == 1
|
||||||
assert out.replace('\r', '') == '''\
|
assert out == '''\
|
||||||
Add "Signed off by:".....................................................Failed
|
Add "Signed off by:".....................................................Failed
|
||||||
- hook id: add-signoff
|
- hook id: add-signoff
|
||||||
- exit code: 1
|
- exit code: 1
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ from __future__ import unicode_literals
|
||||||
import io
|
import io
|
||||||
import os.path
|
import os.path
|
||||||
import pipes
|
import pipes
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
@ -543,16 +542,14 @@ def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
|
||||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||||
|
|
||||||
# Have to use subprocess because pytest monkeypatches sys.stdout
|
# Have to use subprocess because pytest monkeypatches sys.stdout
|
||||||
_, stdout, _ = git_commit(
|
_, out = git_commit(
|
||||||
fn=cmd_output_mocked_pre_commit_home,
|
fn=cmd_output_mocked_pre_commit_home,
|
||||||
# git commit puts pre-commit to stderr
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
retcode=None,
|
|
||||||
tempdir_factory=tempdir_factory,
|
tempdir_factory=tempdir_factory,
|
||||||
|
retcode=None,
|
||||||
)
|
)
|
||||||
assert 'UnicodeEncodeError' not in stdout
|
assert 'UnicodeEncodeError' not in out
|
||||||
# Doesn't actually happen, but a reasonable assertion
|
# Doesn't actually happen, but a reasonable assertion
|
||||||
assert 'UnicodeDecodeError' not in stdout
|
assert 'UnicodeDecodeError' not in out
|
||||||
|
|
||||||
|
|
||||||
def test_lots_of_files(store, tempdir_factory):
|
def test_lots_of_files(store, tempdir_factory):
|
||||||
|
|
@ -574,8 +571,6 @@ def test_lots_of_files(store, tempdir_factory):
|
||||||
|
|
||||||
git_commit(
|
git_commit(
|
||||||
fn=cmd_output_mocked_pre_commit_home,
|
fn=cmd_output_mocked_pre_commit_home,
|
||||||
# git commit puts pre-commit to stderr
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
tempdir_factory=tempdir_factory,
|
tempdir_factory=tempdir_factory,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ def try_repo_opts(repo, ref=None, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
def _get_out(cap_out):
|
def _get_out(cap_out):
|
||||||
out = cap_out.get().replace('\r\n', '\n')
|
out = re.sub(r'\[INFO\].+\n', '', cap_out.get())
|
||||||
out = re.sub(r'\[INFO\].+\n', '', out)
|
|
||||||
start, using_config, config, rest = out.split('=' * 79 + '\n')
|
start, using_config, config, rest = out.split('=' * 79 + '\n')
|
||||||
assert using_config == 'Using config:\n'
|
assert using_config == 'Using config:\n'
|
||||||
return start, config, rest
|
return start, config, rest
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,7 @@ class Fixture(object):
|
||||||
data = self._stream.data.getvalue()
|
data = self._stream.data.getvalue()
|
||||||
self._stream.data.seek(0)
|
self._stream.data.seek(0)
|
||||||
self._stream.data.truncate()
|
self._stream.data.truncate()
|
||||||
return data
|
return data.replace(b'\r\n', b'\n')
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
"""Get the output assuming it was written as UTF-8 bytes"""
|
"""Get the output assuming it was written as UTF-8 bytes"""
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,7 @@ def test_error_handler_non_ascii_exception(mock_store_dir):
|
||||||
|
|
||||||
def test_error_handler_no_tty(tempdir_factory):
|
def test_error_handler_no_tty(tempdir_factory):
|
||||||
pre_commit_home = tempdir_factory.get()
|
pre_commit_home = tempdir_factory.get()
|
||||||
output = cmd_output_mocked_pre_commit_home(
|
ret, out, _ = cmd_output_mocked_pre_commit_home(
|
||||||
sys.executable,
|
sys.executable,
|
||||||
'-c',
|
'-c',
|
||||||
'from __future__ import unicode_literals\n'
|
'from __future__ import unicode_literals\n'
|
||||||
|
|
@ -156,8 +156,6 @@ def test_error_handler_no_tty(tempdir_factory):
|
||||||
pre_commit_home=pre_commit_home,
|
pre_commit_home=pre_commit_home,
|
||||||
)
|
)
|
||||||
log_file = os.path.join(pre_commit_home, 'pre-commit.log')
|
log_file = os.path.join(pre_commit_home, 'pre-commit.log')
|
||||||
output_lines = output[1].replace('\r', '').splitlines()
|
out_lines = out.splitlines()
|
||||||
assert (
|
assert out_lines[-2] == 'An unexpected error has occurred: ValueError: ☃'
|
||||||
output_lines[-2] == 'An unexpected error has occurred: ValueError: ☃'
|
assert out_lines[-1] == 'Check the log at {}'.format(log_file)
|
||||||
)
|
|
||||||
assert output_lines[-1] == 'Check the log at {}'.format(log_file)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue