diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/manpage_completion_test.py | 3 | ||||
-rw-r--r-- | tests/pylint/py2_compat.py | 20 | ||||
-rw-r--r-- | tests/pylint/test_py2_compat.py | 26 |
3 files changed, 47 insertions, 2 deletions
diff --git a/tests/manpage_completion_test.py b/tests/manpage_completion_test.py index b9504d06..f5e5c335 100755 --- a/tests/manpage_completion_test.py +++ b/tests/manpage_completion_test.py @@ -26,7 +26,8 @@ def get_path_of_man_page(): def read_manpage(): path = get_path_of_man_page() - return open(path, 'r').read() + with open(path, 'r') as man_page: + return man_page.read() def get_sections(): diff --git a/tests/pylint/py2_compat.py b/tests/pylint/py2_compat.py index f3c4398c..7e136148 100644 --- a/tests/pylint/py2_compat.py +++ b/tests/pylint/py2_compat.py @@ -45,7 +45,12 @@ class Py2CompatibilityChecker(BaseChecker): "E4220": ('Use explicit format spec numbering', "implicit-format-spec", 'Python 2.6 does not support implicit format spec numbering' - ' "{}", use explicit numbering "{0}" or keywords "{key}".') + ' "{}", use explicit numbering "{0}" or keywords "{key}".'), + "E4230": ("Use popen23.Popen with with-statements", + "with-popen23", + "Python 2 subprocess.Popen objects were not contextmanagers," + "popen23.Popen wraps them to enable use with" + "with-statements."), } # This class variable declares the options # that are configurable by the user. @@ -116,6 +121,19 @@ class Py2CompatibilityChecker(BaseChecker): self.add_message("implicit-format-spec", node=node, confidence=HIGH) + def visit_with(self, node): + """Make sure subprocess.Popen objects aren't used in with-statements""" + for (cm, _) in node.items: + if isinstance(cm, astroid.nodes.Call): + if ((isinstance(cm.func, astroid.nodes.Name) + and cm.func.name.endswith("Popen") + and (node.root().scope_lookup(node.root(), "Popen")[1][0] + ).modname == "subprocess") + or (isinstance(cm.func, astroid.nodes.Attribute) + and cm.func.expr == "subprocess" + and cm.func.attrname == "Popen")): + self.add_message("with-popen23", node=node, confidence=HIGH) + def register(linter): """This required method auto registers the checker. diff --git a/tests/pylint/test_py2_compat.py b/tests/pylint/test_py2_compat.py index bd1ace65..7156aba7 100644 --- a/tests/pylint/test_py2_compat.py +++ b/tests/pylint/test_py2_compat.py @@ -115,6 +115,32 @@ class TestPy2CompatibilityChecker(pylint.testutils.CheckerTestCase): ): self.checker.visit_call(implicit_format_spec) + def test_with_Popen(self): + with_subprocess_Popen, with_Popen, with_Popen23 = astroid.extract_node(""" + import subprocess + with subprocess.Popen(): #@ + pass + + from subprocess import Popen + with Popen(): #@ + pass + + from ranger.ext.popen23 import Popen23 + with Popen23(): #@ + pass + """) + + with self.assertAddsMessages( + pylint.testutils.Message( + msg_id='with-popen23', + node=with_Popen, + ), + ): + self.checker.visit_with(with_subprocess_Popen) + self.checker.visit_with(with_Popen) + with self.assertNoMessages(): + self.checker.visit_with(with_Popen23) + # # These checks still exist as old-division and no-absolute-import # def test_division_without_import(self): # division = astroid.extract_node(""" |