Skip to content

Commit

Permalink
adjust error outputs to be more standardized
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Apr 6, 2022
1 parent 48c60be commit 8915216
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pre_commit_hooks/check_docstring_first.py
Expand Up @@ -28,13 +28,13 @@ def check_docstring_first(src: bytes, filename: str = '<unknown>') -> int:
if tok_type == tokenize.STRING and scol == 0:
if found_docstring_line is not None:
print(
f'{filename}:{sline} Multiple module docstrings '
f'{filename}:{sline}: Multiple module docstrings '
f'(first docstring on line {found_docstring_line}).',
)
return 1
elif found_code_line is not None:
print(
f'{filename}:{sline} Module docstring appears after code '
f'{filename}:{sline}: Module docstring appears after code '
f'(code seen on line {found_code_line}).',
)
return 1
Expand Down
6 changes: 3 additions & 3 deletions pre_commit_hooks/check_merge_conflict.py
Expand Up @@ -39,12 +39,12 @@ def main(argv: Sequence[str] | None = None) -> int:
retcode = 0
for filename in args.filenames:
with open(filename, 'rb') as inputfile:
for i, line in enumerate(inputfile):
for i, line in enumerate(inputfile, start=1):
for pattern in CONFLICT_PATTERNS:
if line.startswith(pattern):
print(
f'Merge conflict string "{pattern.decode()}" '
f'found in {filename}:{i + 1}',
f'{filename}:{i}: Merge conflict string '
f'{pattern.strip().decode()!r} found',
)
retcode = 1

Expand Down
2 changes: 1 addition & 1 deletion pre_commit_hooks/debug_statement_hook.py
Expand Up @@ -65,7 +65,7 @@ def check_file(filename: str) -> int:
visitor.visit(ast_obj)

for bp in visitor.breakpoints:
print(f'{filename}:{bp.line}:{bp.col} - {bp.name} {bp.reason}')
print(f'{filename}:{bp.line}:{bp.col}: {bp.name} {bp.reason}')

return int(bool(visitor.breakpoints))

Expand Down
6 changes: 3 additions & 3 deletions tests/check_docstring_first_test.py
Expand Up @@ -17,7 +17,7 @@
b'from __future__ import unicode_literals\n'
b'"foo"\n',
1,
'{filename}:2 Module docstring appears after code '
'{filename}:2: Module docstring appears after code '
'(code seen on line 1).\n',
),
# Test double docstring
Expand All @@ -26,7 +26,7 @@
b'from __future__ import absolute_import\n'
b'"fake docstring"\n',
1,
'{filename}:3 Multiple module docstrings '
'{filename}:3: Multiple module docstrings '
'(first docstring on line 1).\n',
),
# Test multiple lines of code above
Expand All @@ -35,7 +35,7 @@
b'import sys\n'
b'"docstring"\n',
1,
'{filename}:3 Module docstring appears after code '
'{filename}:3: Module docstring appears after code '
'(code seen on line 1).\n',
),
# String literals in expressions are ok.
Expand Down
12 changes: 9 additions & 3 deletions tests/check_merge_conflict_test.py
Expand Up @@ -101,8 +101,14 @@ def repository_pending_merge(tmpdir):


@pytest.mark.usefixtures('f1_is_a_conflict_file')
def test_merge_conflicts_git():
def test_merge_conflicts_git(capsys):
assert main(['f1']) == 1
out, _ = capsys.readouterr()
assert out == (
"f1:1: Merge conflict string '<<<<<<<' found\n"
"f1:3: Merge conflict string '=======' found\n"
"f1:5: Merge conflict string '>>>>>>>' found\n"
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -139,7 +145,7 @@ def test_care_when_assumed_merge(tmpdir):
assert main([str(f.realpath()), '--assume-in-merge']) == 1


def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir):
def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir, capsys):
worktree = tmpdir.join('worktree')
cmd_output('git', 'worktree', 'add', str(worktree))
with worktree.as_cwd():
Expand All @@ -148,4 +154,4 @@ def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir):
)
msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG')
assert msg.exists()
test_merge_conflicts_git()
test_merge_conflicts_git(capsys)
4 changes: 3 additions & 1 deletion tests/debug_statement_hook_test.py
Expand Up @@ -55,7 +55,9 @@ def test_non_utf8_file(tmpdir):
assert main((str(f_py),)) == 0


def test_py37_breakpoint(tmpdir):
def test_py37_breakpoint(tmpdir, capsys):
f_py = tmpdir.join('f.py')
f_py.write('def f():\n breakpoint()\n')
assert main((str(f_py),)) == 1
out, _ = capsys.readouterr()
assert out == f'{f_py}:2:4: breakpoint called\n'

0 comments on commit 8915216

Please sign in to comment.