mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 12:06:53 +00:00
Simplify debug-statemetns tests
This commit is contained in:
parent
904a06196d
commit
6e2e83a409
2 changed files with 12 additions and 55 deletions
|
|
@ -1,5 +0,0 @@
|
||||||
|
|
||||||
def foo(obj):
|
|
||||||
import pdb; pdb.set_trace()
|
|
||||||
|
|
||||||
return 5
|
|
||||||
|
|
@ -4,72 +4,34 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from pre_commit_hooks.debug_statement_hook import debug_statement_hook
|
from pre_commit_hooks.debug_statement_hook import debug_statement_hook
|
||||||
from pre_commit_hooks.debug_statement_hook import DebugStatement
|
from pre_commit_hooks.debug_statement_hook import DebugStatement
|
||||||
from pre_commit_hooks.debug_statement_hook import ImportStatementParser
|
from pre_commit_hooks.debug_statement_hook import ImportStatementParser
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def test_no_debug_imports():
|
||||||
def ast_with_no_debug_imports():
|
|
||||||
return ast.parse(
|
|
||||||
"""
|
|
||||||
import foo
|
|
||||||
import bar
|
|
||||||
import baz
|
|
||||||
from foo import bar
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def ast_with_debug_import_form_1():
|
|
||||||
return ast.parse(
|
|
||||||
"""
|
|
||||||
|
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def ast_with_debug_import_form_2():
|
|
||||||
return ast.parse(
|
|
||||||
"""
|
|
||||||
|
|
||||||
from pudb import set_trace; set_trace()
|
|
||||||
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_returns_no_debug_statements(ast_with_no_debug_imports):
|
|
||||||
visitor = ImportStatementParser()
|
visitor = ImportStatementParser()
|
||||||
visitor.visit(ast_with_no_debug_imports)
|
visitor.visit(ast.parse('import os\nfrom foo import bar\n'))
|
||||||
assert visitor.debug_import_statements == []
|
assert visitor.debug_import_statements == []
|
||||||
|
|
||||||
|
|
||||||
def test_returns_one_form_1(ast_with_debug_import_form_1):
|
def test_finds_debug_import_attribute_access():
|
||||||
visitor = ImportStatementParser()
|
visitor = ImportStatementParser()
|
||||||
visitor.visit(ast_with_debug_import_form_1)
|
visitor.visit(ast.parse('import ipdb; ipdb.set_trace()'))
|
||||||
assert visitor.debug_import_statements == [
|
assert visitor.debug_import_statements == [DebugStatement('ipdb', 1, 0)]
|
||||||
DebugStatement('ipdb', 3, 0),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def test_returns_one_form_2(ast_with_debug_import_form_2):
|
def test_finds_debug_import_from_import():
|
||||||
visitor = ImportStatementParser()
|
visitor = ImportStatementParser()
|
||||||
visitor.visit(ast_with_debug_import_form_2)
|
visitor.visit(ast.parse('from pudb import set_trace; set_trace()'))
|
||||||
assert visitor.debug_import_statements == [
|
assert visitor.debug_import_statements == [DebugStatement('pudb', 1, 0)]
|
||||||
DebugStatement('pudb', 3, 0),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def test_returns_one_for_failing_file():
|
def test_returns_one_for_failing_file(tmpdir):
|
||||||
ret = debug_statement_hook([get_resource_path('file_with_debug.notpy')])
|
f_py = tmpdir.join('f.py')
|
||||||
|
f_py.write('def f():\n import pdb; pdb.set_trace()')
|
||||||
|
ret = debug_statement_hook([f_py.strpath])
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue