#!/usr/bin/env python3 import os from pprint import pprint import test.regrtest dir = os.path.dirname(test.regrtest.__file__) manual_skips = ['test_argparse', # needs a tearDownModule 'test_curses', # see #16000 'test_datetime', # tests pulled in from elsewhere 'test_distutils', # tests pulled in from elsewhere 'test_dummy_thread',# needs a setUpModule 'test_gc', # needs extra attention 'test_gdb', # need to keep verbose printing 'test_global', # need to deal with warnings 'test_httpservers', # needs current directory cleaning 'test_lib2to3', # tests pulled from elsewhere 'test_logging', # needs locale saving 'test_ossaudiodev', # needs extra attention 'test_pkgutil', # needs a tearDownModule 'test_re', # needs extra attention 'test_shelve', # probably needs load_tests 'test_socketserver',# needs a setUpModule 'test_threadsignals', # need extra attention 'test_tk', 'test_ttk_guionly', 'test_ttk_textonly',# tk tests loaded from elsewhere 'test_unittest', # tests loaded from elsewhere 'test_winreg', # has extra info to not lose 'test_xml_etree_c', # uses test_xml_etree 'test_selectors', # inheritance issues 'test_sys', # test failures 'test_pprint', # test_trace uses test_pprint.test_main ] skips = dict.fromkeys(['explicit', 'encoding', 'done', 'threads', 'doctests', 'argv', 'no change', 'TESTFN', 'requires', 'tail']) for k in skips: skips[k] = [] changed_tests = [] changed_other = [] for fn in os.listdir(dir): print(fn, end=' ... ') name = fn[:-3] if name in manual_skips: skips['explicit'].append(name) print('skipped, explicit skip') continue if not (fn.endswith('.py') and 'test' in fn): print('skipped, wrong kind of file') continue try: with open(os.path.join(dir, fn), encoding='utf-8') as in_file: data = in_file.read() except UnicodeError: skips['encoding'].append(name) print('skipped, encoding issues') continue if 'run_unittest' not in data and 'test_main' not in data: skips['done'].append(name) print('skipped, no run_unittest or test_main') continue if 'TESTFN' in data: skips['TESTFN'].append(name) print('skipped, uses TESTFN, might not clean up properly') continue new_data = data.replace('from test.support import run_unittest\n', '') new_data = new_data.replace('run_unittest, ', '') new_data = new_data.replace(' run_unittest,', '') new_data = new_data.replace('run_unittest,', '') new_data = new_data.replace(', run_unittest', '') new_data = new_data.replace(',run_unittest', '') idx = new_data.find('\ndef test_main(') if 'reap_threads' in new_data[idx-20:idx] or 'threading_setup()' in new_data[idx:]: skips['threads'].append(name) print("skipped, can't handle reap_threads") continue if idx != -1: quote_char = "'" if "'__main__'" in new_data[idx:] else '"' idx += 1 # don't take out the newline we matched to get idx if len(new_data[idx:]) > 1000: skips['tail'].append(name) print('skipped, long tail, probably significant') continue if 'doctest' in new_data[idx:] or 'DocTestSuite' in new_data[idx:]: skips['doctests'].append(name) print("skipped, can't handle doctests") continue if 'sys.argv' in new_data[idx:]: skips['argv'].append(name) print('skipped, command line args used') continue if 'requires' in new_data[idx:]: skips['requires'].append(name) print('skipped, requires some resource') continue if 'reap_children()' in new_data[idx:]: if 'import test.support' in new_data[:1000]: support = 'test.support.' elif 'from test import support' in new_data[:1000]: support = 'support.' else: support = '' new_data = new_data[:idx] if support is not None: new_data += "def tearDownModule():\n %sreap_children()\n\n" % support else: new_data = new_data[:idx] new_data += 'if __name__ == {0}__main__{0}:\n unittest.main()\n'.format(quote_char) else: idx = new_data.rfind('\nif __name__ ==') if idx != -1: idx += 1 # don't take out the newline we matched to get idx if "run_unittest" in new_data[idx:]: quote_char = "'" if "'__main__'" in new_data[idx:idx + 15] else '"' new_data = new_data[:idx] new_data += 'if __name__ == {0}__main__{0}:\n unittest.main()\n'.format(quote_char) if new_data == data: skips['no change'].append(name) print('skipped, no changes') continue if new_data.count('support') == 1: new_data = new_data.replace('import test.support\n', '') new_data = new_data.replace('from test import support\n', '') assert "run_unittest" not in new_data assert "\ndef test_main" not in new_data with open(os.path.join(dir, fn), 'w', encoding='utf-8') as out_file: out_file.write(new_data) if name.startswith('test_'): changed_tests.append(name) else: changed_other.append(name) print('done') pprint(skips) changed_tests.sort() with open('changed_tests.txt', 'w') as change_file: change_file.write('\n'.join(changed_tests) + '\n') print("Changed tests are in changed_tests.txt for use with regrtest's -f option") print("Other changed files:") pprint(changed_other)