Skip to content

Commit 4941ed5

Browse files
committed
Normalize crlf in tests
1 parent 517c314 commit 4941ed5

File tree

7 files changed

+46
-58
lines changed

7 files changed

+46
-58
lines changed

testing/util.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import contextlib
44
import os.path
5+
import subprocess
56
import sys
67

78
import pytest
@@ -24,9 +25,11 @@ def cmd_output_mocked_pre_commit_home(*args, **kwargs):
2425
# keyword-only argument
2526
tempdir_factory = kwargs.pop('tempdir_factory')
2627
pre_commit_home = kwargs.pop('pre_commit_home', tempdir_factory.get())
28+
kwargs.setdefault('stderr', subprocess.STDOUT)
2729
# Don't want to write to the home directory
2830
env = dict(kwargs.pop('env', os.environ), PRE_COMMIT_HOME=pre_commit_home)
29-
return cmd_output(*args, env=env, **kwargs)
31+
ret, out, _ = cmd_output(*args, env=env, **kwargs)
32+
return ret, out.replace('\r\n', '\n'), None
3033

3134

3235
skipif_cant_run_docker = pytest.mark.skipif(
@@ -137,8 +140,10 @@ def cwd(path):
137140
def git_commit(*args, **kwargs):
138141
fn = kwargs.pop('fn', cmd_output)
139142
msg = kwargs.pop('msg', 'commit!')
143+
kwargs.setdefault('stderr', subprocess.STDOUT)
140144

141145
cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', '-a') + args
142146
if msg is not None: # allow skipping `-m` with `msg=None`
143147
cmd += ('-m', msg)
144-
return fn(*cmd, **kwargs)
148+
ret, out, _ = fn(*cmd, **kwargs)
149+
return ret, out.replace('\r\n', '\n')

tests/commands/init_templatedir_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os.path
2-
import subprocess
32

43
import mock
54

@@ -30,11 +29,9 @@ def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
3029
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
3130

3231
with cwd(path):
33-
retcode, output, _ = git_commit(
32+
retcode, output = git_commit(
3433
fn=cmd_output_mocked_pre_commit_home,
3534
tempdir_factory=tempdir_factory,
36-
# git commit puts pre-commit to stderr
37-
stderr=subprocess.STDOUT,
3835
)
3936
assert retcode == 0
4037
assert 'Bash hook....' in output

tests/commands/install_uninstall_test.py

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io
66
import os.path
77
import re
8-
import subprocess
98
import sys
109

1110
import mock
@@ -121,30 +120,28 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
121120
cmd_output('git', 'add', touch_file)
122121
return git_commit(
123122
fn=cmd_output_mocked_pre_commit_home,
124-
# git commit puts pre-commit to stderr
125-
stderr=subprocess.STDOUT,
126123
retcode=None,
127124
tempdir_factory=tempdir_factory,
128125
**kwargs
129-
)[:2]
126+
)
130127

131128

132129
# osx does this different :(
133130
FILES_CHANGED = (
134131
r'('
135-
r' 1 file changed, 0 insertions\(\+\), 0 deletions\(-\)\r?\n'
132+
r' 1 file changed, 0 insertions\(\+\), 0 deletions\(-\)\n'
136133
r'|'
137-
r' 0 files changed\r?\n'
134+
r' 0 files changed\n'
138135
r')'
139136
)
140137

141138

142139
NORMAL_PRE_COMMIT_RUN = re.compile(
143-
r'^\[INFO\] Initializing environment for .+\.\r?\n'
144-
r'Bash hook\.+Passed\r?\n'
145-
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
140+
r'^\[INFO\] Initializing environment for .+\.\n'
141+
r'Bash hook\.+Passed\n'
142+
r'\[master [a-f0-9]{7}\] commit!\n' +
146143
FILES_CHANGED +
147-
r' create mode 100644 foo\r?\n$',
144+
r' create mode 100644 foo\n$',
148145
)
149146

150147

@@ -265,7 +262,7 @@ def test_environment_not_sourced(tempdir_factory, store):
265262

266263
# Use a specific homedir to ignore --user installs
267264
homedir = tempdir_factory.get()
268-
ret, stdout, stderr = git_commit(
265+
ret, out = git_commit(
269266
env={
270267
'HOME': homedir,
271268
'PATH': _path_without_us(),
@@ -278,22 +275,21 @@ def test_environment_not_sourced(tempdir_factory, store):
278275
retcode=None,
279276
)
280277
assert ret == 1
281-
assert stdout == ''
282-
assert stderr.replace('\r\n', '\n') == (
278+
assert out == (
283279
'`pre-commit` not found. '
284280
'Did you forget to activate your virtualenv?\n'
285281
)
286282

287283

288284
FAILING_PRE_COMMIT_RUN = re.compile(
289-
r'^\[INFO\] Initializing environment for .+\.\r?\n'
290-
r'Failing hook\.+Failed\r?\n'
291-
r'- hook id: failing_hook\r?\n'
292-
r'- exit code: 1\r?\n'
293-
r'\r?\n'
294-
r'Fail\r?\n'
295-
r'foo\r?\n'
296-
r'\r?\n$',
285+
r'^\[INFO\] Initializing environment for .+\.\n'
286+
r'Failing hook\.+Failed\n'
287+
r'- hook id: failing_hook\n'
288+
r'- exit code: 1\n'
289+
r'\n'
290+
r'Fail\n'
291+
r'foo\n'
292+
r'\n$',
297293
)
298294

299295

@@ -308,10 +304,10 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
308304

309305

310306
EXISTING_COMMIT_RUN = re.compile(
311-
r'^legacy hook\r?\n'
312-
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
307+
r'^legacy hook\n'
308+
r'\[master [a-f0-9]{7}\] commit!\n' +
313309
FILES_CHANGED +
314-
r' create mode 100644 baz\r?\n$',
310+
r' create mode 100644 baz\n$',
315311
)
316312

317313

@@ -369,9 +365,9 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
369365

370366

371367
FAIL_OLD_HOOK = re.compile(
372-
r'fail!\r?\n'
373-
r'\[INFO\] Initializing environment for .+\.\r?\n'
374-
r'Bash hook\.+Passed\r?\n',
368+
r'fail!\n'
369+
r'\[INFO\] Initializing environment for .+\.\n'
370+
r'Bash hook\.+Passed\n',
375371
)
376372

377373

@@ -465,10 +461,10 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
465461

466462

467463
PRE_INSTALLED = re.compile(
468-
r'Bash hook\.+Passed\r?\n'
469-
r'\[master [a-f0-9]{7}\] commit!\r?\n' +
464+
r'Bash hook\.+Passed\n'
465+
r'\[master [a-f0-9]{7}\] commit!\n' +
470466
FILES_CHANGED +
471-
r' create mode 100644 foo\r?\n$',
467+
r' create mode 100644 foo\n$',
472468
)
473469

474470

@@ -527,8 +523,6 @@ def test_installed_from_venv(tempdir_factory, store):
527523
def _get_push_output(tempdir_factory, opts=()):
528524
return cmd_output_mocked_pre_commit_home(
529525
'git', 'push', 'origin', 'HEAD:new_branch', *opts,
530-
# git push puts pre-commit to stderr
531-
stderr=subprocess.STDOUT,
532526
tempdir_factory=tempdir_factory,
533527
retcode=None
534528
)[:2]
@@ -648,7 +642,7 @@ def test_commit_msg_integration_failing(
648642
install(C.CONFIG_FILE, store, hook_types=['commit-msg'])
649643
retc, out = _get_commit_output(tempdir_factory)
650644
assert retc == 1
651-
assert out.replace('\r', '') == '''\
645+
assert out == '''\
652646
Must have "Signed off by:"...............................................Failed
653647
- hook id: must-have-signoff
654648
- exit code: 1
@@ -695,7 +689,7 @@ def test_prepare_commit_msg_integration_failing(
695689
install(C.CONFIG_FILE, store, hook_types=['prepare-commit-msg'])
696690
retc, out = _get_commit_output(tempdir_factory)
697691
assert retc == 1
698-
assert out.replace('\r', '') == '''\
692+
assert out == '''\
699693
Add "Signed off by:".....................................................Failed
700694
- hook id: add-signoff
701695
- exit code: 1

tests/commands/run_test.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io
55
import os.path
66
import pipes
7-
import subprocess
87
import sys
98

109
import mock
@@ -543,16 +542,14 @@ def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
543542
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
544543

545544
# Have to use subprocess because pytest monkeypatches sys.stdout
546-
_, stdout, _ = git_commit(
545+
_, out = git_commit(
547546
fn=cmd_output_mocked_pre_commit_home,
548-
# git commit puts pre-commit to stderr
549-
stderr=subprocess.STDOUT,
550-
retcode=None,
551547
tempdir_factory=tempdir_factory,
548+
retcode=None,
552549
)
553-
assert 'UnicodeEncodeError' not in stdout
550+
assert 'UnicodeEncodeError' not in out
554551
# Doesn't actually happen, but a reasonable assertion
555-
assert 'UnicodeDecodeError' not in stdout
552+
assert 'UnicodeDecodeError' not in out
556553

557554

558555
def test_lots_of_files(store, tempdir_factory):
@@ -574,8 +571,6 @@ def test_lots_of_files(store, tempdir_factory):
574571

575572
git_commit(
576573
fn=cmd_output_mocked_pre_commit_home,
577-
# git commit puts pre-commit to stderr
578-
stderr=subprocess.STDOUT,
579574
tempdir_factory=tempdir_factory,
580575
)
581576

tests/commands/try_repo_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def try_repo_opts(repo, ref=None, **kwargs):
2121

2222

2323
def _get_out(cap_out):
24-
out = cap_out.get().replace('\r\n', '\n')
25-
out = re.sub(r'\[INFO\].+\n', '', out)
24+
out = re.sub(r'\[INFO\].+\n', '', cap_out.get())
2625
start, using_config, config, rest = out.split('=' * 79 + '\n')
2726
assert using_config == 'Using config:\n'
2827
return start, config, rest

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def get_bytes(self):
249249
data = self._stream.data.getvalue()
250250
self._stream.data.seek(0)
251251
self._stream.data.truncate()
252-
return data
252+
return data.replace(b'\r\n', b'\n')
253253

254254
def get(self):
255255
"""Get the output assuming it was written as UTF-8 bytes"""

tests/error_handler_test.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_error_handler_non_ascii_exception(mock_store_dir):
144144

145145
def test_error_handler_no_tty(tempdir_factory):
146146
pre_commit_home = tempdir_factory.get()
147-
output = cmd_output_mocked_pre_commit_home(
147+
ret, out, _ = cmd_output_mocked_pre_commit_home(
148148
sys.executable,
149149
'-c',
150150
'from __future__ import unicode_literals\n'
@@ -156,8 +156,6 @@ def test_error_handler_no_tty(tempdir_factory):
156156
pre_commit_home=pre_commit_home,
157157
)
158158
log_file = os.path.join(pre_commit_home, 'pre-commit.log')
159-
output_lines = output[1].replace('\r', '').splitlines()
160-
assert (
161-
output_lines[-2] == 'An unexpected error has occurred: ValueError: ☃'
162-
)
163-
assert output_lines[-1] == 'Check the log at {}'.format(log_file)
159+
out_lines = out.splitlines()
160+
assert out_lines[-2] == 'An unexpected error has occurred: ValueError: ☃'
161+
assert out_lines[-1] == 'Check the log at {}'.format(log_file)

0 commit comments

Comments
 (0)