Skip to content

Commit 3a04068

Browse files
committed
fix excess whitespace in traceback in error
1 parent 918821b commit 3a04068

File tree

6 files changed

+59
-48
lines changed

6 files changed

+59
-48
lines changed

pre_commit/error_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
5252
_log_line('```')
5353
_log_line()
5454
_log_line('```')
55-
_log_line(formatted)
55+
_log_line(formatted.rstrip())
5656
_log_line('```')
5757
raise SystemExit(1)
5858

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ covdefaults
22
coverage
33
pytest
44
pytest-env
5+
re-assert

tests/commands/install_uninstall_test.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import sys
44
from unittest import mock
55

6+
import re_assert
7+
68
import pre_commit.constants as C
79
from pre_commit import git
810
from pre_commit.commands import install_uninstall
@@ -143,7 +145,7 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
143145
)
144146

145147

146-
NORMAL_PRE_COMMIT_RUN = re.compile(
148+
NORMAL_PRE_COMMIT_RUN = re_assert.Matches(
147149
fr'^\[INFO\] Initializing environment for .+\.\n'
148150
fr'Bash hook\.+Passed\n'
149151
fr'\[master [a-f0-9]{{7}}\] commit!\n'
@@ -159,7 +161,7 @@ def test_install_pre_commit_and_run(tempdir_factory, store):
159161

160162
ret, output = _get_commit_output(tempdir_factory)
161163
assert ret == 0
162-
assert NORMAL_PRE_COMMIT_RUN.match(output)
164+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
163165

164166

165167
def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
@@ -171,7 +173,7 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
171173

172174
ret, output = _get_commit_output(tempdir_factory)
173175
assert ret == 0
174-
assert NORMAL_PRE_COMMIT_RUN.match(output)
176+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
175177

176178

177179
def test_install_in_submodule_and_run(tempdir_factory, store):
@@ -185,7 +187,7 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
185187
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
186188
ret, output = _get_commit_output(tempdir_factory)
187189
assert ret == 0
188-
assert NORMAL_PRE_COMMIT_RUN.match(output)
190+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
189191

190192

191193
def test_install_in_worktree_and_run(tempdir_factory, store):
@@ -198,7 +200,7 @@ def test_install_in_worktree_and_run(tempdir_factory, store):
198200
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
199201
ret, output = _get_commit_output(tempdir_factory)
200202
assert ret == 0
201-
assert NORMAL_PRE_COMMIT_RUN.match(output)
203+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
202204

203205

204206
def test_commit_am(tempdir_factory, store):
@@ -243,7 +245,7 @@ def test_install_idempotent(tempdir_factory, store):
243245

244246
ret, output = _get_commit_output(tempdir_factory)
245247
assert ret == 0
246-
assert NORMAL_PRE_COMMIT_RUN.match(output)
248+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
247249

248250

249251
def _path_without_us():
@@ -297,7 +299,7 @@ def test_environment_not_sourced(tempdir_factory, store):
297299
)
298300

299301

300-
FAILING_PRE_COMMIT_RUN = re.compile(
302+
FAILING_PRE_COMMIT_RUN = re_assert.Matches(
301303
r'^\[INFO\] Initializing environment for .+\.\n'
302304
r'Failing hook\.+Failed\n'
303305
r'- hook id: failing_hook\n'
@@ -316,10 +318,10 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
316318

317319
ret, output = _get_commit_output(tempdir_factory)
318320
assert ret == 1
319-
assert FAILING_PRE_COMMIT_RUN.match(output)
321+
FAILING_PRE_COMMIT_RUN.assert_matches(output)
320322

321323

322-
EXISTING_COMMIT_RUN = re.compile(
324+
EXISTING_COMMIT_RUN = re_assert.Matches(
323325
fr'^legacy hook\n'
324326
fr'\[master [a-f0-9]{{7}}\] commit!\n'
325327
fr'{FILES_CHANGED}'
@@ -342,7 +344,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
342344
# Make sure we installed the "old" hook correctly
343345
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
344346
assert ret == 0
345-
assert EXISTING_COMMIT_RUN.match(output)
347+
EXISTING_COMMIT_RUN.assert_matches(output)
346348

347349
# Now install pre-commit (no-overwrite)
348350
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
@@ -351,7 +353,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
351353
ret, output = _get_commit_output(tempdir_factory)
352354
assert ret == 0
353355
assert output.startswith('legacy hook\n')
354-
assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])
356+
NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
355357

356358

357359
def test_legacy_overwriting_legacy_hook(tempdir_factory, store):
@@ -377,10 +379,10 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
377379
ret, output = _get_commit_output(tempdir_factory)
378380
assert ret == 0
379381
assert output.startswith('legacy hook\n')
380-
assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])
382+
NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
381383

382384

383-
FAIL_OLD_HOOK = re.compile(
385+
FAIL_OLD_HOOK = re_assert.Matches(
384386
r'fail!\n'
385387
r'\[INFO\] Initializing environment for .+\.\n'
386388
r'Bash hook\.+Passed\n',
@@ -401,7 +403,7 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
401403
# We should get a failure from the legacy hook
402404
ret, output = _get_commit_output(tempdir_factory)
403405
assert ret == 1
404-
assert FAIL_OLD_HOOK.match(output)
406+
FAIL_OLD_HOOK.assert_matches(output)
405407

406408

407409
def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
@@ -413,7 +415,7 @@ def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
413415

414416
ret, output = _get_commit_output(tempdir_factory)
415417
assert ret == 0
416-
assert NORMAL_PRE_COMMIT_RUN.match(output)
418+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
417419

418420

419421
def test_install_overwrite(tempdir_factory, store):
@@ -426,7 +428,7 @@ def test_install_overwrite(tempdir_factory, store):
426428

427429
ret, output = _get_commit_output(tempdir_factory)
428430
assert ret == 0
429-
assert NORMAL_PRE_COMMIT_RUN.match(output)
431+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
430432

431433

432434
def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
@@ -441,7 +443,7 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
441443
# Make sure we installed the "old" hook correctly
442444
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
443445
assert ret == 0
444-
assert EXISTING_COMMIT_RUN.match(output)
446+
EXISTING_COMMIT_RUN.assert_matches(output)
445447

446448

447449
def test_replace_old_commit_script(tempdir_factory, store):
@@ -463,7 +465,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
463465

464466
ret, output = _get_commit_output(tempdir_factory)
465467
assert ret == 0
466-
assert NORMAL_PRE_COMMIT_RUN.match(output)
468+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
467469

468470

469471
def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
@@ -476,7 +478,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
476478
assert pre_commit.exists()
477479

478480

479-
PRE_INSTALLED = re.compile(
481+
PRE_INSTALLED = re_assert.Matches(
480482
fr'Bash hook\.+Passed\n'
481483
fr'\[master [a-f0-9]{{7}}\] commit!\n'
482484
fr'{FILES_CHANGED}'
@@ -493,7 +495,7 @@ def test_installs_hooks_with_hooks_True(tempdir_factory, store):
493495
)
494496

495497
assert ret == 0
496-
assert PRE_INSTALLED.match(output)
498+
PRE_INSTALLED.assert_matches(output)
497499

498500

499501
def test_install_hooks_command(tempdir_factory, store):
@@ -506,7 +508,7 @@ def test_install_hooks_command(tempdir_factory, store):
506508
)
507509

508510
assert ret == 0
509-
assert PRE_INSTALLED.match(output)
511+
PRE_INSTALLED.assert_matches(output)
510512

511513

512514
def test_installed_from_venv(tempdir_factory, store):
@@ -533,7 +535,7 @@ def test_installed_from_venv(tempdir_factory, store):
533535
},
534536
)
535537
assert ret == 0
536-
assert NORMAL_PRE_COMMIT_RUN.match(output)
538+
NORMAL_PRE_COMMIT_RUN.assert_matches(output)
537539

538540

539541
def _get_push_output(tempdir_factory, remote='origin', opts=()):
@@ -880,7 +882,7 @@ def test_prepare_commit_msg_legacy(
880882

881883

882884
def test_pre_merge_commit_integration(tempdir_factory, store):
883-
expected = re.compile(
885+
output_pattern = re_assert.Matches(
884886
r'^\[INFO\] Initializing environment for .+\n'
885887
r'Bash hook\.+Passed\n'
886888
r"Merge made by the 'recursive' strategy.\n"
@@ -902,7 +904,7 @@ def test_pre_merge_commit_integration(tempdir_factory, store):
902904
tempdir_factory=tempdir_factory,
903905
)
904906
assert ret == 0
905-
assert expected.match(output)
907+
output_pattern.assert_matches(output)
906908

907909

908910
def test_install_disallow_missing_config(tempdir_factory, store):

tests/commands/try_repo_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import time
44
from unittest import mock
55

6+
import re_assert
7+
68
from pre_commit import git
79
from pre_commit.commands.try_repo import try_repo
810
from pre_commit.util import cmd_output
@@ -43,16 +45,16 @@ def test_try_repo_repo_only(cap_out, tempdir_factory):
4345
_run_try_repo(tempdir_factory, verbose=True)
4446
start, config, rest = _get_out(cap_out)
4547
assert start == ''
46-
assert re.match(
48+
config_pattern = re_assert.Matches(
4749
'^repos:\n'
4850
'- repo: .+\n'
4951
' rev: .+\n'
5052
' hooks:\n'
5153
' - id: bash_hook\n'
5254
' - id: bash_hook2\n'
5355
' - id: bash_hook3\n$',
54-
config,
5556
)
57+
config_pattern.assert_matches(config)
5658
assert rest == '''\
5759
Bash hook............................................(no files to check)Skipped
5860
- hook id: bash_hook
@@ -71,14 +73,14 @@ def test_try_repo_with_specific_hook(cap_out, tempdir_factory):
7173
_run_try_repo(tempdir_factory, hook='bash_hook', verbose=True)
7274
start, config, rest = _get_out(cap_out)
7375
assert start == ''
74-
assert re.match(
76+
config_pattern = re_assert.Matches(
7577
'^repos:\n'
7678
'- repo: .+\n'
7779
' rev: .+\n'
7880
' hooks:\n'
7981
' - id: bash_hook\n$',
80-
config,
8182
)
83+
config_pattern.assert_matches(config)
8284
assert rest == '''\
8385
Bash hook............................................(no files to check)Skipped
8486
- hook id: bash_hook
@@ -128,14 +130,14 @@ def test_try_repo_uncommitted_changes(cap_out, tempdir_factory):
128130

129131
start, config, rest = _get_out(cap_out)
130132
assert start == '[WARNING] Creating temporary repo with uncommitted changes...\n' # noqa: E501
131-
assert re.match(
133+
config_pattern = re_assert.Matches(
132134
'^repos:\n'
133135
'- repo: .+shadow-repo\n'
134136
' rev: .+\n'
135137
' hooks:\n'
136138
' - id: bash_hook\n$',
137-
config,
138139
)
140+
config_pattern.assert_matches(config)
139141
assert rest == 'modified name!...........................................................Passed\n' # noqa: E501
140142

141143

tests/error_handler_test.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import os.path
2-
import re
32
import stat
43
import sys
54
from unittest import mock
65

76
import pytest
7+
import re_assert
88

99
from pre_commit import error_handler
1010
from pre_commit.store import Store
@@ -37,16 +37,16 @@ def test_error_handler_fatal_error(mocked_log_and_exit):
3737
mock.ANY,
3838
)
3939

40-
assert re.match(
40+
pattern = re_assert.Matches(
4141
r'Traceback \(most recent call last\):\n'
4242
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
4343
r' yield\n'
4444
r' File ".+tests.error_handler_test.py", line \d+, '
4545
r'in test_error_handler_fatal_error\n'
4646
r' raise exc\n'
4747
r'(pre_commit\.error_handler\.)?FatalError: just a test\n',
48-
mocked_log_and_exit.call_args[0][2],
4948
)
49+
pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
5050

5151

5252
def test_error_handler_uncaught_error(mocked_log_and_exit):
@@ -60,16 +60,16 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
6060
# Tested below
6161
mock.ANY,
6262
)
63-
assert re.match(
63+
pattern = re_assert.Matches(
6464
r'Traceback \(most recent call last\):\n'
6565
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
6666
r' yield\n'
6767
r' File ".+tests.error_handler_test.py", line \d+, '
6868
r'in test_error_handler_uncaught_error\n'
6969
r' raise exc\n'
7070
r'ValueError: another test\n',
71-
mocked_log_and_exit.call_args[0][2],
7271
)
72+
pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
7373

7474

7575
def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
@@ -83,23 +83,27 @@ def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
8383
# Tested below
8484
mock.ANY,
8585
)
86-
assert re.match(
86+
pattern = re_assert.Matches(
8787
r'Traceback \(most recent call last\):\n'
8888
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
8989
r' yield\n'
9090
r' File ".+tests.error_handler_test.py", line \d+, '
9191
r'in test_error_handler_keyboardinterrupt\n'
9292
r' raise exc\n'
9393
r'KeyboardInterrupt\n',
94-
mocked_log_and_exit.call_args[0][2],
9594
)
95+
pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
9696

9797

9898
def test_log_and_exit(cap_out, mock_store_dir):
99+
tb = (
100+
'Traceback (most recent call last):\n'
101+
' File "<stdin>", line 2, in <module>\n'
102+
'pre_commit.error_handler.FatalError: hai\n'
103+
)
104+
99105
with pytest.raises(SystemExit):
100-
error_handler._log_and_exit(
101-
'msg', error_handler.FatalError('hai'), "I'm a stacktrace",
102-
)
106+
error_handler._log_and_exit('msg', error_handler.FatalError('hai'), tb)
103107

104108
printed = cap_out.get()
105109
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
@@ -108,7 +112,7 @@ def test_log_and_exit(cap_out, mock_store_dir):
108112
assert os.path.exists(log_file)
109113
with open(log_file) as f:
110114
logged = f.read()
111-
expected = (
115+
pattern = re_assert.Matches(
112116
r'^### version information\n'
113117
r'\n'
114118
r'```\n'
@@ -127,10 +131,12 @@ def test_log_and_exit(cap_out, mock_store_dir):
127131
r'```\n'
128132
r'\n'
129133
r'```\n'
130-
r"I'm a stacktrace\n"
131-
r'```\n'
134+
r'Traceback \(most recent call last\):\n'
135+
r' File "<stdin>", line 2, in <module>\n'
136+
r'pre_commit\.error_handler\.FatalError: hai\n'
137+
r'```\n',
132138
)
133-
assert re.match(expected, logged)
139+
pattern.assert_matches(logged)
134140

135141

136142
def test_error_handler_non_ascii_exception(mock_store_dir):

0 commit comments

Comments
 (0)