| 1 |
# Copyright 2012 Facebook |
| 2 |
# |
| 3 |
# This software may be used and distributed according to the terms of the |
| 4 |
# GNU General Public License version 2 or any later version. |
| 5 |
"""Find tests that newly pass under Python 3. |
| 6 |
|
| 7 |
The approach is simple: we maintain a whitelist of Python 3 passing |
| 8 |
tests in the repository, and periodically run all the /other/ tests |
| 9 |
and look for new passes. Any newly passing tests get automatically |
| 10 |
added to the whitelist. |
| 11 |
|
| 12 |
You probably want to run it like this: |
| 13 |
|
| 14 |
$ cd tests |
| 15 |
$ python3 ../contrib/python3-ratchet.py \ |
| 16 |
> --working-tests=../contrib/python3-whitelist |
| 17 |
""" |
| 18 |
|
| 19 |
import argparse |
| 20 |
import json |
| 21 |
import os |
| 22 |
import subprocess |
| 23 |
import sys |
| 24 |
|
| 25 |
_hgenv = dict(os.environ) |
| 26 |
_hgenv.update( |
| 27 |
{ |
| 28 |
'HGPLAIN': '1', |
| 29 |
} |
| 30 |
) |
| 31 |
|
| 32 |
_HG_FIRST_CHANGE = '9117c6561b0bd7792fa13b50d28239d51b78e51f' |
| 33 |
|
| 34 |
|
| 35 |
def _runhg(*args): |
| 36 |
return subprocess.check_output(args, env=_hgenv) |
| 37 |
|
| 38 |
|
| 39 |
def _is_hg_repo(path): |
| 40 |
return ( |
| 41 |
_runhg('hg', 'log', '-R', path, '-r0', '--template={node}').strip() |
| 42 |
== _HG_FIRST_CHANGE |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
def _py3default(): |
| 47 |
if sys.version_info[0] >= 3: |
| 48 |
return sys.executable |
| 49 |
return 'python3' |
| 50 |
|
| 51 |
|
| 52 |
def main(argv=()): |
| 53 |
p = argparse.ArgumentParser() |
| 54 |
p.add_argument( |
| 55 |
'--working-tests', help='List of tests that already work in Python 3.' |
| 56 |
) |
| 57 |
p.add_argument( |
| 58 |
'--commit-to-repo', |
| 59 |
help='If set, commit newly fixed tests to the given repo', |
| 60 |
) |
| 61 |
p.add_argument( |
| 62 |
'-j', |
| 63 |
default=os.sysconf('SC_NPROCESSORS_ONLN'), |
| 64 |
type=int, |
| 65 |
help='Number of parallel tests to run.', |
| 66 |
) |
| 67 |
p.add_argument( |
| 68 |
'--python3', |
| 69 |
default=_py3default(), |
| 70 |
help='python3 interpreter to use for test run', |
| 71 |
) |
| 72 |
p.add_argument( |
| 73 |
'--commit-user', |
| 74 |
default='[email protected]', |
| 75 |
help='Username to specify when committing to a repo.', |
| 76 |
) |
| 77 |
opts = p.parse_args(argv) |
| 78 |
if opts.commit_to_repo: |
| 79 |
if not _is_hg_repo(opts.commit_to_repo): |
| 80 |
print('abort: specified repository is not the hg repository') |
| 81 |
sys.exit(1) |
| 82 |
if not opts.working_tests or not os.path.isfile(opts.working_tests): |
| 83 |
print( |
| 84 |
'abort: --working-tests must exist and be a file (got %r)' |
| 85 |
% opts.working_tests |
| 86 |
) |
| 87 |
sys.exit(1) |
| 88 |
elif opts.commit_to_repo: |
| 89 |
root = _runhg('hg', 'root').strip() |
| 90 |
if not opts.working_tests.startswith(root): |
| 91 |
print( |
| 92 |
'abort: if --commit-to-repo is given, ' |
| 93 |
'--working-tests must be from that repo' |
| 94 |
) |
| 95 |
sys.exit(1) |
| 96 |
|
| 97 |
rt = subprocess.Popen( |
| 98 |
[ |
| 99 |
opts.python3, |
| 100 |
'run-tests.py', |
| 101 |
'-j', |
| 102 |
str(opts.j), |
| 103 |
'--blacklist', |
| 104 |
opts.working_tests, |
| 105 |
'--json', |
| 106 |
] |
| 107 |
) |
| 108 |
rt.wait() |
| 109 |
with open('report.json') as f: |
| 110 |
data = f.read() |
| 111 |
report = json.loads(data.split('=', 1)[1]) |
| 112 |
newpass = set() |
| 113 |
for test, result in report.items(): |
| 114 |
if result['result'] != 'success': |
| 115 |
continue |
| 116 |
# A new passing test! Huzzah! |
| 117 |
newpass.add(test) |
| 118 |
if newpass: |
| 119 |
# We already validated the repo, so we can just dive right in |
| 120 |
# and commit. |
| 121 |
if opts.commit_to_repo: |
| 122 |
print(len(newpass), 'new passing tests on Python 3!') |
| 123 |
with open(opts.working_tests) as f: |
| 124 |
oldpass = {l for l in f.read().splitlines() if l} |
| 125 |
with open(opts.working_tests, 'w') as f: |
| 126 |
for p in sorted(oldpass | newpass): |
| 127 |
f.write('%s\n' % p) |
| 128 |
_runhg( |
| 129 |
'hg', |
| 130 |
'commit', |
| 131 |
'-R', |
| 132 |
opts.commit_to_repo, |
| 133 |
'--user', |
| 134 |
opts.commit_user, |
| 135 |
'--message', |
| 136 |
'python3: expand list of passing tests', |
| 137 |
) |
| 138 |
else: |
| 139 |
print('Newly passing tests:', '\n'.join(sorted(newpass))) |
| 140 |
sys.exit(2) |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == '__main__': |
| 144 |
main(sys.argv[1:]) |