# Path: pre_commit_hooks/debug_statement_hook.py
# class Debug(NamedTuple):
#     line: int
#     col: int
#     name: str
#     reason: str
# 
# Path: pre_commit_hooks/debug_statement_hook.py
# class DebugStatementParser(ast.NodeVisitor):
#     def __init__(self) -> None:
#         self.breakpoints: list[Debug] = []
# 
#     def visit_Import(self, node: ast.Import) -> None:
#         for name in node.names:
#             if name.name in DEBUG_STATEMENTS:
#                 st = Debug(node.lineno, node.col_offset, name.name, 'imported')
#                 self.breakpoints.append(st)
# 
#     def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
#         if node.module in DEBUG_STATEMENTS:
#             st = Debug(node.lineno, node.col_offset, node.module, 'imported')
#             self.breakpoints.append(st)
# 
#     def visit_Call(self, node: ast.Call) -> None:
#         """python3.7+ breakpoint()"""
#         if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
#             st = Debug(node.lineno, node.col_offset, node.func.id, 'called')
#             self.breakpoints.append(st)
#         self.generic_visit(node)
# 
# Path: pre_commit_hooks/debug_statement_hook.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument('filenames', nargs='*', help='Filenames to run')
#     args = parser.parse_args(argv)
# 
#     retv = 0
#     for filename in args.filenames:
#         retv |= check_file(filename)
#     return retv
# 
# Path: testing/util.py
# def get_resource_path(path):
#     return os.path.join(TESTING_DIR, 'resources', path)


import ast
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
from testing.util import get_resource_path



def test_no_breakpoints():
    visitor = DebugStatementParser()
    visitor.visit(ast.parse('import os\nfrom foo import bar\n'))
    assert visitor.breakpoints == []


def test_finds_debug_import_attribute_access():
    visitor = DebugStatementParser()
    visitor.visit(ast.parse('import ipdb; ipdb.set_trace()'))
    assert visitor.breakpoints == [Debug(1, 0, 'ipdb', 'imported')]


def test_finds_debug_import_from_import():
    visitor = DebugStatementParser()
    visitor.visit(ast.parse('from pudb import set_trace; set_trace()'))
    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')]


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()')

# Path: pre_commit_hooks/debug_statement_hook.py
# class Debug(NamedTuple):
#     line: int
#     col: int
#     name: str
#     reason: str
# 
# Path: pre_commit_hooks/debug_statement_hook.py
# class DebugStatementParser(ast.NodeVisitor):
#     def __init__(self) -> None:
#         self.breakpoints: list[Debug] = []
# 
#     def visit_Import(self, node: ast.Import) -> None:
#         for name in node.names:
#             if name.name in DEBUG_STATEMENTS:
#                 st = Debug(node.lineno, node.col_offset, name.name, 'imported')
#                 self.breakpoints.append(st)
# 
#     def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
#         if node.module in DEBUG_STATEMENTS:
#             st = Debug(node.lineno, node.col_offset, node.module, 'imported')
#             self.breakpoints.append(st)
# 
#     def visit_Call(self, node: ast.Call) -> None:
#         """python3.7+ breakpoint()"""
#         if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
#             st = Debug(node.lineno, node.col_offset, node.func.id, 'called')
#             self.breakpoints.append(st)
#         self.generic_visit(node)
# 
# Path: pre_commit_hooks/debug_statement_hook.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument('filenames', nargs='*', help='Filenames to run')
#     args = parser.parse_args(argv)
# 
#     retv = 0
#     for filename in args.filenames:
#         retv |= check_file(filename)
#     return retv
# 
# Path: testing/util.py
# def get_resource_path(path):
#     return os.path.join(TESTING_DIR, 'resources', path)


# Path: tests/debug_statement_hook_test.py
import ast
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
from testing.util import get_resource_path




def test_no_breakpoints():
    visitor = DebugStatementParser()
    visitor.visit(ast.parse('import os\nfrom foo import bar\n'))
    assert visitor.breakpoints == []


def test_finds_debug_import_attribute_access():
    visitor = DebugStatementParser()
    visitor.visit(ast.parse('import ipdb; ipdb.set_trace()'))
    assert visitor.breakpoints == [Debug(1, 0, 'ipdb', 'imported')]


def test_finds_debug_import_from_import():
    visitor = DebugStatementParser()
    visitor.visit(ast.parse('from pudb import set_trace; set_trace()'))
    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')]


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()')

    ret = main([str(f_py)])