mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-06 12:54:29 +08:00
Trying to add a testsuite in pytest for the tests of pydocstyle.
This commit is contained in:
30
conftest.py
30
conftest.py
@@ -2,6 +2,7 @@ import tempfile
|
|||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -34,6 +35,35 @@ def pytest_addoption(parser):
|
|||||||
help="Enables the logging output.")
|
help="Enables the logging output.")
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_generate_tests(metafunc):
|
||||||
|
if 'normalizer_issue_file' in metafunc.fixturenames:
|
||||||
|
base_dir = os.path.join(os.path.dirname(__file__), 'test', 'normalizer_issue_files')
|
||||||
|
|
||||||
|
cases = list(colllect_normalizer_tests(base_dir))
|
||||||
|
metafunc.parametrize(
|
||||||
|
'normalizer_issue_file',
|
||||||
|
cases,
|
||||||
|
ids=[c.name for c in cases]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NormalizerIssueCase(object):
|
||||||
|
"""
|
||||||
|
Static Analysis cases lie in the static_analysis folder.
|
||||||
|
The tests also start with `#!`, like the goto_definition tests.
|
||||||
|
"""
|
||||||
|
def __init__(self, path):
|
||||||
|
self.path = path
|
||||||
|
self.name = os.path.basename(path)
|
||||||
|
|
||||||
|
|
||||||
|
def colllect_normalizer_tests(base_dir):
|
||||||
|
for f_name in os.listdir(base_dir):
|
||||||
|
if f_name.endswith(".py"):
|
||||||
|
path = os.path.join(base_dir, f_name)
|
||||||
|
yield NormalizerIssueCase(path)
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure(config):
|
def pytest_configure(config):
|
||||||
if config.option.logging:
|
if config.option.logging:
|
||||||
root = logging.getLogger()
|
root = logging.getLogger()
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ class Normalizer(object):
|
|||||||
def normalize(self, leaf):
|
def normalize(self, leaf):
|
||||||
return leaf.prefix + leaf.value
|
return leaf.prefix + leaf.value
|
||||||
|
|
||||||
|
def add_issue(self, code, message, node):
|
||||||
|
issue = Issue(node, code, message)
|
||||||
|
self.issues.append(issue)
|
||||||
|
|
||||||
|
|
||||||
class NormalizerConfig(object):
|
class NormalizerConfig(object):
|
||||||
normalizer_class = Normalizer
|
normalizer_class = Normalizer
|
||||||
@@ -41,11 +45,12 @@ class NormalizerConfig(object):
|
|||||||
return rule
|
return rule
|
||||||
|
|
||||||
|
|
||||||
class Error(object):
|
class Issue(object):
|
||||||
def __init__(self, leaf, code, message):
|
def __init__(self, node, code, message):
|
||||||
self._leaf = leaf
|
self._node = node
|
||||||
self.code = code
|
self.code = code
|
||||||
self.message = message
|
self.message = message
|
||||||
|
self.start_pos = node.start_pos
|
||||||
|
|
||||||
|
|
||||||
class Rule(object):
|
class Rule(object):
|
||||||
|
|||||||
@@ -38,23 +38,23 @@ class PEP8Normalizer(Normalizer):
|
|||||||
self.indentation = 0
|
self.indentation = 0
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def visit_node(self, state, node):
|
def visit_node(self, node):
|
||||||
typ = node.type
|
typ = node.type
|
||||||
|
|
||||||
if typ in 'import_name':
|
if typ in 'import_name':
|
||||||
names = node.get_defined_names()
|
names = node.get_defined_names()
|
||||||
if len(names) > 1:
|
if len(names) > 1:
|
||||||
for name in names[:1]:
|
for name in names[:1]:
|
||||||
self.log_error(401, 'Multiple imports on one line', name)
|
self.add_issue(401, 'Multiple imports on one line', name)
|
||||||
elif typ == 'lambdef':
|
elif typ == 'lambdef':
|
||||||
if node.parent.type == 'expr_stmt':
|
if node.parent.type == 'expr_stmt':
|
||||||
self.log_error(731, 'Do not assign a lambda expression, use a def', node)
|
self.add_issue(731, 'Do not assign a lambda expression, use a def', node)
|
||||||
elif typ == 'try_stmt':
|
elif typ == 'try_stmt':
|
||||||
for child in node.children:
|
for child in node.children:
|
||||||
# Here we can simply check if it's an except, because otherwise
|
# Here we can simply check if it's an except, because otherwise
|
||||||
# it would be an except_clause.
|
# it would be an except_clause.
|
||||||
if child == 'except':
|
if child == 'except':
|
||||||
self.log_error(722, 'Do not use bare except, specify exception instead', node)
|
self.add_issue(722, 'Do not use bare except, specify exception instead', node)
|
||||||
elif typ == 'comparison':
|
elif typ == 'comparison':
|
||||||
odd = False
|
odd = False
|
||||||
for child in node.children:
|
for child in node.children:
|
||||||
@@ -62,7 +62,7 @@ class PEP8Normalizer(Normalizer):
|
|||||||
if child not in ('is', '=='):
|
if child not in ('is', '=='):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if child.type == 'atom_expr':
|
if child.type != 'atom_expr':
|
||||||
break
|
break
|
||||||
trailer = child.children[-1]
|
trailer = child.children[-1]
|
||||||
atom = child.children[-1]
|
atom = child.children[-1]
|
||||||
@@ -71,7 +71,7 @@ class PEP8Normalizer(Normalizer):
|
|||||||
break
|
break
|
||||||
odd = not odd
|
odd = not odd
|
||||||
else:
|
else:
|
||||||
self.log_error(721, "Do not compare types, use 'isinstance()", node)
|
self.add_issue(721, "Do not compare types, use 'isinstance()", node)
|
||||||
|
|
||||||
if typ in IMPORT_TYPES:
|
if typ in IMPORT_TYPES:
|
||||||
module = node.parent
|
module = node.parent
|
||||||
@@ -79,7 +79,7 @@ class PEP8Normalizer(Normalizer):
|
|||||||
index = module.children.index(node)
|
index = module.children.index(node)
|
||||||
for child in module.children[:index]:
|
for child in module.children[:index]:
|
||||||
if child.type not in IMPORT_TYPES:
|
if child.type not in IMPORT_TYPES:
|
||||||
self.log_error(402, 'Module level import not at top of file', node)
|
self.add_issue(402, 'Module level import not at top of file', node)
|
||||||
break
|
break
|
||||||
|
|
||||||
if typ == 'suite':
|
if typ == 'suite':
|
||||||
@@ -94,11 +94,11 @@ class PEP8Normalizer(Normalizer):
|
|||||||
if leaf.is_definition():
|
if leaf.is_definition():
|
||||||
message = "Do not define %s named 'l', 'O', or 'I' one line"
|
message = "Do not define %s named 'l', 'O', or 'I' one line"
|
||||||
if leaf.parent.type == 'class' and leaf.parent.name == leaf:
|
if leaf.parent.type == 'class' and leaf.parent.name == leaf:
|
||||||
self.log_error(742, message % 'classes', leaf)
|
self.add_issue(742, message % 'classes', leaf)
|
||||||
elif leaf.parent.type == 'function' and leaf.parent.name == leaf:
|
elif leaf.parent.type == 'function' and leaf.parent.name == leaf:
|
||||||
self.log_error(743, message % 'function', leaf)
|
self.add_issue(743, message % 'function', leaf)
|
||||||
else:
|
else:
|
||||||
self.log_error(741, message % 'variables', leaf)
|
self.add_issuadd_issue(741, message % 'variables', leaf)
|
||||||
|
|
||||||
for part in leaf._split_prefix():
|
for part in leaf._split_prefix():
|
||||||
part
|
part
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ class NodeOrLeaf(object):
|
|||||||
with normalizer.visit_node(self):
|
with normalizer.visit_node(self):
|
||||||
return ''.join(child._normalize(normalizer) for child in children)
|
return ''.join(child._normalize(normalizer) for child in children)
|
||||||
|
|
||||||
def _get_normalize_errors(self, normalizer_config=None):
|
def _get_normalizer_issues(self, normalizer_config=None):
|
||||||
normalizer = self._get_normalizer(normalizer_config)
|
normalizer = self._get_normalizer(normalizer_config)
|
||||||
self._normalize(normalizer)
|
self._normalize(normalizer)
|
||||||
return normalizer.issues
|
return normalizer.issues
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
addopts = --doctest-modules
|
addopts = --doctest-modules
|
||||||
|
|
||||||
# Ignore broken files inblackbox test directories
|
# Ignore broken files inblackbox test directories
|
||||||
norecursedirs = .* docs scripts old*
|
norecursedirs = .* docs scripts normalizer_issue_files
|
||||||
|
|
||||||
# Activate `clean_jedi_cache` fixture for all tests. This should be
|
# Activate `clean_jedi_cache` fixture for all tests. This should be
|
||||||
# fine as long as we are using `clean_jedi_cache` as a session scoped
|
# fine as long as we are using `clean_jedi_cache` as a session scoped
|
||||||
|
|||||||
Reference in New Issue
Block a user