Simplify debug-statemetns tests
diff --git a/testing/resources/file_with_debug.notpy b/testing/resources/file_with_debug.notpy deleted file mode 100644 index faa23a2..0000000 --- a/testing/resources/file_with_debug.notpy +++ /dev/null
@@ -1,5 +0,0 @@ - -def foo(obj): - import pdb; pdb.set_trace() - - return 5
diff --git a/tests/debug_statement_hook_test.py b/tests/debug_statement_hook_test.py index 8832245..44c2011 100644 --- a/tests/debug_statement_hook_test.py +++ b/tests/debug_statement_hook_test.py
@@ -4,72 +4,34 @@ import ast -import pytest - 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 ImportStatementParser from testing.util import get_resource_path -@pytest.fixture -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): +def test_no_debug_imports(): 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 == [] -def test_returns_one_form_1(ast_with_debug_import_form_1): +def test_finds_debug_import_attribute_access(): visitor = ImportStatementParser() - visitor.visit(ast_with_debug_import_form_1) - assert visitor.debug_import_statements == [ - DebugStatement('ipdb', 3, 0), - ] + visitor.visit(ast.parse('import ipdb; ipdb.set_trace()')) + assert visitor.debug_import_statements == [DebugStatement('ipdb', 1, 0)] -def test_returns_one_form_2(ast_with_debug_import_form_2): +def test_finds_debug_import_from_import(): visitor = ImportStatementParser() - visitor.visit(ast_with_debug_import_form_2) - assert visitor.debug_import_statements == [ - DebugStatement('pudb', 3, 0), - ] + visitor.visit(ast.parse('from pudb import set_trace; set_trace()')) + assert visitor.debug_import_statements == [DebugStatement('pudb', 1, 0)] -def test_returns_one_for_failing_file(): - ret = debug_statement_hook([get_resource_path('file_with_debug.notpy')]) +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 = debug_statement_hook([f_py.strpath]) assert ret == 1