diff --git a/Tools/scripts/patchcheck.py b/Tools/scripts/patchcheck.py --- a/Tools/scripts/patchcheck.py +++ b/Tools/scripts/patchcheck.py @@ -5,6 +5,9 @@ import os.path import subprocess import sysconfig +import linecache +import types +from collections import Counter import reindent import untabify @@ -70,6 +73,8 @@ lines.append(" {}".format(path)) return "\n".join(lines) +report_test_names = report_modified_files + @status("Fixing whitespace", info=report_modified_files) def normalize_whitespace(file_paths): @@ -115,6 +120,32 @@ return fixed +def testmethod_names(code, name=[]): + name = name + [code.co_name] + for c in code.co_consts: + if isinstance(c, types.CodeType): + for item in testmethod_names(c, name): + yield item + if name[-1].startswith('test_'): + yield '.'.join(name[1:]) + + +@status("Duplicate test names found in", info=report_test_names) +def duplicate_test_names(file_paths): + duplicates = [] + for path in file_paths: + abspath = os.path.join(SRCDIR, path) + try: + source_lines = linecache.getlines(abspath) + code = compile(''.join(source_lines), abspath, 'exec') + except: + continue + duplicates.extend("{}: test name '{}'".format(path, name) for + name, cnt in + Counter(testmethod_names(code)).items() if cnt > 1) + return duplicates + + @status("Docs modified", modal=True) def docs_modified(file_paths): """Report if any file in the Doc directory has been changed.""" @@ -151,6 +182,8 @@ def main(): file_paths = changed_files() python_files = [fn for fn in file_paths if fn.endswith('.py')] + test_files = [fn for fn in python_files if + fn.startswith(os.path.join('Lib', 'test'))] c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] doc_files = [fn for fn in file_paths if fn.startswith('Doc')] special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths) @@ -160,6 +193,8 @@ normalize_c_whitespace(c_files) # Doc whitespace enforcement. normalize_docs_whitespace(doc_files) + # Duplicate test names. + duplicate_test_names(test_files) # Docs updated. docs_modified(doc_files) # Misc/ACKS changed.