|
4 | 4 |
|
5 | 5 | import ast |
6 | 6 |
|
7 | | -import pytest |
8 | | - |
9 | | -from pre_commit_hooks.debug_statement_hook import debug_statement_hook |
10 | | -from pre_commit_hooks.debug_statement_hook import DebugStatement |
11 | | -from pre_commit_hooks.debug_statement_hook import ImportStatementParser |
| 7 | +from pre_commit_hooks.debug_statement_hook import Debug |
| 8 | +from pre_commit_hooks.debug_statement_hook import DebugStatementParser |
| 9 | +from pre_commit_hooks.debug_statement_hook import main |
12 | 10 | from testing.util import get_resource_path |
13 | 11 |
|
14 | 12 |
|
15 | | -@pytest.fixture |
16 | | -def ast_with_no_debug_imports(): |
17 | | - return ast.parse( |
18 | | - """ |
19 | | -import foo |
20 | | -import bar |
21 | | -import baz |
22 | | -from foo import bar |
23 | | -""", |
24 | | - ) |
25 | | - |
26 | | - |
27 | | -@pytest.fixture |
28 | | -def ast_with_debug_import_form_1(): |
29 | | - return ast.parse( |
30 | | - """ |
31 | | -
|
32 | | -import ipdb; ipdb.set_trace() |
33 | | -
|
34 | | -""", |
35 | | - ) |
36 | | - |
37 | | - |
38 | | -@pytest.fixture |
39 | | -def ast_with_debug_import_form_2(): |
40 | | - return ast.parse( |
41 | | - """ |
| 13 | +def test_no_breakpoints(): |
| 14 | + visitor = DebugStatementParser() |
| 15 | + visitor.visit(ast.parse('import os\nfrom foo import bar\n')) |
| 16 | + assert visitor.breakpoints == [] |
42 | 17 |
|
43 | | -from pudb import set_trace; set_trace() |
44 | 18 |
|
45 | | -""", |
46 | | - ) |
| 19 | +def test_finds_debug_import_attribute_access(): |
| 20 | + visitor = DebugStatementParser() |
| 21 | + visitor.visit(ast.parse('import ipdb; ipdb.set_trace()')) |
| 22 | + assert visitor.breakpoints == [Debug(1, 0, 'ipdb', 'imported')] |
47 | 23 |
|
48 | 24 |
|
49 | | -def test_returns_no_debug_statements(ast_with_no_debug_imports): |
50 | | - visitor = ImportStatementParser() |
51 | | - visitor.visit(ast_with_no_debug_imports) |
52 | | - assert visitor.debug_import_statements == [] |
| 25 | +def test_finds_debug_import_from_import(): |
| 26 | + visitor = DebugStatementParser() |
| 27 | + visitor.visit(ast.parse('from pudb import set_trace; set_trace()')) |
| 28 | + assert visitor.breakpoints == [Debug(1, 0, 'pudb', 'imported')] |
53 | 29 |
|
54 | 30 |
|
55 | | -def test_returns_one_form_1(ast_with_debug_import_form_1): |
56 | | - visitor = ImportStatementParser() |
57 | | - visitor.visit(ast_with_debug_import_form_1) |
58 | | - assert visitor.debug_import_statements == [ |
59 | | - DebugStatement('ipdb', 3, 0), |
60 | | - ] |
| 31 | +def test_finds_breakpoint(): |
| 32 | + visitor = DebugStatementParser() |
| 33 | + visitor.visit(ast.parse('breakpoint()')) |
| 34 | + assert visitor.breakpoints == [Debug(1, 0, 'breakpoint', 'called')] |
61 | 35 |
|
62 | 36 |
|
63 | | -def test_returns_one_form_2(ast_with_debug_import_form_2): |
64 | | - visitor = ImportStatementParser() |
65 | | - visitor.visit(ast_with_debug_import_form_2) |
66 | | - assert visitor.debug_import_statements == [ |
67 | | - DebugStatement('pudb', 3, 0), |
68 | | - ] |
69 | | - |
70 | | - |
71 | | -def test_returns_one_for_failing_file(): |
72 | | - ret = debug_statement_hook([get_resource_path('file_with_debug.notpy')]) |
| 37 | +def test_returns_one_for_failing_file(tmpdir): |
| 38 | + f_py = tmpdir.join('f.py') |
| 39 | + f_py.write('def f():\n import pdb; pdb.set_trace()') |
| 40 | + ret = main([f_py.strpath]) |
73 | 41 | assert ret == 1 |
74 | 42 |
|
75 | 43 |
|
76 | 44 | def test_returns_zero_for_passing_file(): |
77 | | - ret = debug_statement_hook([__file__]) |
| 45 | + ret = main([__file__]) |
78 | 46 | assert ret == 0 |
79 | 47 |
|
80 | 48 |
|
81 | 49 | def test_syntaxerror_file(): |
82 | | - ret = debug_statement_hook([get_resource_path('cannot_parse_ast.notpy')]) |
| 50 | + ret = main([get_resource_path('cannot_parse_ast.notpy')]) |
83 | 51 | assert ret == 1 |
84 | 52 |
|
85 | 53 |
|
86 | 54 | def test_non_utf8_file(tmpdir): |
87 | 55 | f_py = tmpdir.join('f.py') |
88 | 56 | f_py.write_binary('# -*- coding: cp1252 -*-\nx = "€"\n'.encode('cp1252')) |
89 | | - assert debug_statement_hook((f_py.strpath,)) == 0 |
| 57 | + assert main((f_py.strpath,)) == 0 |
| 58 | + |
| 59 | + |
| 60 | +def test_py37_breakpoint(tmpdir): |
| 61 | + f_py = tmpdir.join('f.py') |
| 62 | + f_py.write('def f():\n breakpoint()\n') |
| 63 | + assert main((f_py.strpath,)) == 1 |
0 commit comments