| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
# Copyright 2018 The Meson development team |
| 4 |
|
| 5 |
'''Renames test case directories using Git from this: |
| 6 |
|
| 7 |
1 something |
| 8 |
3 other |
| 9 |
3 foo |
| 10 |
3 bar |
| 11 |
|
| 12 |
to this: |
| 13 |
|
| 14 |
1 something |
| 15 |
2 other |
| 16 |
3 foo |
| 17 |
4 bar |
| 18 |
|
| 19 |
This directory must be run from source root as it touches run_unittests.py. |
| 20 |
''' |
| 21 |
|
| 22 |
import typing as T |
| 23 |
import os |
| 24 |
import sys |
| 25 |
import subprocess |
| 26 |
|
| 27 |
from glob import glob |
| 28 |
|
| 29 |
def get_entries() -> T.List[T.Tuple[int, str]]: |
| 30 |
entries = [] |
| 31 |
for e in glob('*'): |
| 32 |
if not os.path.isdir(e): |
| 33 |
raise SystemExit('Current directory must not contain any files.') |
| 34 |
(number, rest) = e.split(' ', 1) |
| 35 |
try: |
| 36 |
numstr = int(number) |
| 37 |
except ValueError: |
| 38 |
raise SystemExit(f'Dir name {e} does not start with a number.') |
| 39 |
if 'includedirxyz' in e: |
| 40 |
continue |
| 41 |
entries.append((numstr, rest)) |
| 42 |
entries.sort() |
| 43 |
return entries |
| 44 |
|
| 45 |
def replace_source(sourcefile: str, replacements: T.List[T.Tuple[str, str]]) -> None: |
| 46 |
with open(sourcefile, encoding='utf-8') as f: |
| 47 |
contents = f.read() |
| 48 |
for old_name, new_name in replacements: |
| 49 |
contents = contents.replace(old_name, new_name) |
| 50 |
with open(sourcefile, 'w', encoding='utf-8') as f: |
| 51 |
f.write(contents) |
| 52 |
|
| 53 |
def condense(dirname: str) -> None: |
| 54 |
curdir = os.getcwd() |
| 55 |
os.chdir(dirname) |
| 56 |
entries = get_entries() |
| 57 |
replacements = [] |
| 58 |
for _i, e in enumerate(entries): |
| 59 |
i = _i + 1 |
| 60 |
if e[0] != i: |
| 61 |
old_name = str(e[0]) + ' ' + e[1] |
| 62 |
new_name = str(i) + ' ' + e[1] |
| 63 |
#print('git mv "%s" "%s"' % (old_name, new_name)) |
| 64 |
subprocess.check_call(['git', 'mv', old_name, new_name]) |
| 65 |
replacements.append((old_name, new_name)) |
| 66 |
# update any appearances of old_name in expected stdout in test.json |
| 67 |
json = os.path.join(new_name, 'test.json') |
| 68 |
if os.path.isfile(json): |
| 69 |
replace_source(json, [(old_name, new_name)]) |
| 70 |
os.chdir(curdir) |
| 71 |
replace_source('run_unittests.py', replacements) |
| 72 |
replace_source('run_project_tests.py', replacements) |
| 73 |
replace_source('run_format_tests.py', replacements) |
| 74 |
replace_source('run_shell_checks.py', replacements) |
| 75 |
for f in glob('unittests/*.py'): |
| 76 |
replace_source(f, replacements) |
| 77 |
|
| 78 |
if __name__ == '__main__': |
| 79 |
if len(sys.argv) != 1: |
| 80 |
raise SystemExit('This script takes no arguments.') |
| 81 |
for d in glob('test cases/*'): |
| 82 |
condense(d) |