2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-03-15 05:23:08 +08:00
|
|
|
import ast
|
2015-01-05 08:05:20 +08:00
|
|
|
|
2018-05-14 06:24:42 +08:00
|
|
|
from pre_commit_hooks.debug_statement_hook import Debug
|
|
|
|
from pre_commit_hooks.debug_statement_hook import DebugStatementParser
|
|
|
|
from pre_commit_hooks.debug_statement_hook import main
|
2014-04-14 13:21:42 +08:00
|
|
|
from testing.util import get_resource_path
|
2014-03-15 05:23:08 +08:00
|
|
|
|
|
|
|
|
2018-05-14 06:24:42 +08:00
|
|
|
def test_no_breakpoints():
|
|
|
|
visitor = DebugStatementParser()
|
2018-05-14 06:07:31 +08:00
|
|
|
visitor.visit(ast.parse('import os\nfrom foo import bar\n'))
|
2018-05-14 06:24:42 +08:00
|
|
|
assert visitor.breakpoints == []
|
2014-03-15 05:23:08 +08:00
|
|
|
|
|
|
|
|
2018-05-14 06:07:31 +08:00
|
|
|
def test_finds_debug_import_attribute_access():
|
2018-05-14 06:24:42 +08:00
|
|
|
visitor = DebugStatementParser()
|
2018-05-14 06:07:31 +08:00
|
|
|
visitor.visit(ast.parse('import ipdb; ipdb.set_trace()'))
|
2018-05-14 06:24:42 +08:00
|
|
|
assert visitor.breakpoints == [Debug(1, 0, 'ipdb', 'imported')]
|
2014-03-15 05:23:08 +08:00
|
|
|
|
|
|
|
|
2018-05-14 06:07:31 +08:00
|
|
|
def test_finds_debug_import_from_import():
|
2018-05-14 06:24:42 +08:00
|
|
|
visitor = DebugStatementParser()
|
2018-05-14 06:07:31 +08:00
|
|
|
visitor.visit(ast.parse('from pudb import set_trace; set_trace()'))
|
2018-05-14 06:24:42 +08:00
|
|
|
assert visitor.breakpoints == [Debug(1, 0, 'pudb', 'imported')]
|
|
|
|
|
|
|
|
|
|
|
|
def test_finds_breakpoint():
|
|
|
|
visitor = DebugStatementParser()
|
|
|
|
visitor.visit(ast.parse('breakpoint()'))
|
|
|
|
assert visitor.breakpoints == [Debug(1, 0, 'breakpoint', 'called')]
|
2014-04-14 13:21:42 +08:00
|
|
|
|
|
|
|
|
2018-05-14 06:07:31 +08:00
|
|
|
def test_returns_one_for_failing_file(tmpdir):
|
|
|
|
f_py = tmpdir.join('f.py')
|
|
|
|
f_py.write('def f():\n import pdb; pdb.set_trace()')
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main([str(f_py)])
|
2014-04-14 13:21:42 +08:00
|
|
|
assert ret == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_returns_zero_for_passing_file():
|
2018-05-14 06:24:42 +08:00
|
|
|
ret = main([__file__])
|
2014-04-14 13:21:42 +08:00
|
|
|
assert ret == 0
|
2014-06-20 08:41:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_syntaxerror_file():
|
2018-05-14 06:24:42 +08:00
|
|
|
ret = main([get_resource_path('cannot_parse_ast.notpy')])
|
2014-06-20 08:41:53 +08:00
|
|
|
assert ret == 1
|
2017-08-15 11:20:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_non_utf8_file(tmpdir):
|
|
|
|
f_py = tmpdir.join('f.py')
|
|
|
|
f_py.write_binary('# -*- coding: cp1252 -*-\nx = "€"\n'.encode('cp1252'))
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(f_py),)) == 0
|
2018-05-14 06:24:42 +08:00
|
|
|
|
|
|
|
|
2022-04-07 04:55:26 +08:00
|
|
|
def test_py37_breakpoint(tmpdir, capsys):
|
2018-05-14 06:24:42 +08:00
|
|
|
f_py = tmpdir.join('f.py')
|
|
|
|
f_py.write('def f():\n breakpoint()\n')
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(f_py),)) == 1
|
2022-04-07 04:55:26 +08:00
|
|
|
out, _ = capsys.readouterr()
|
|
|
|
assert out == f'{f_py}:2:4: breakpoint called\n'
|