Merge pull request #1592 from pre-commit/ws_traceback · precommit/pre-commit@8e9f927 · GitHub
Skip to content

Commit 8e9f927

Browse files
authored
Merge pull request pre-commit#1592 from pre-commit/ws_traceback
fix excess whitespace in traceback in error
2 parents 918821b + 3a04068 commit 8e9f927

6 files changed

Lines changed: 59 additions & 48 deletions

File tree

pre_commit/error_handler.py

Lines changed: 1 addition & 1 deletion

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

0 commit comments

Comments
 (0)