Page MenuHomePhabricator
Paste P8702

Python script to fix unit tests
ActivePublic

Authored by Ladsgroup on Jul 2 2019, 4:50 PM.
Referenced Files
F29732369: raw.txt
Jul 10 2019, 5:02 PM
F29721351: raw.txt
Jul 9 2019, 9:02 PM
F29676426: raw.txt
Jul 2 2019, 4:50 PM
import os
import sys
import re
import subprocess
from subprocess import PIPE
def find_test_files(path):
files = []
for r, d, f in os.walk(path):
for file_name in f:
if file_name.endswith('Test.php'):
files.append(os.path.join(r, file_name))
return files
def move_things(path, sep):
if len(sys.argv) > 2:
d = sys.argv[2]
else:
d = sys.argv[1]
new_path = '{0}{1}/{2}/{3}'.format(path.split(d)[0], d, sep, path.split(d)[1])
new_path = new_path.replace('//','/')
res = subprocess.run(["mkdir", "-p", '/'.join(new_path.split('/')[:-1])], stdout=PIPE, stderr=PIPE)
if res.returncode != 0:
print(res.stdout.decode('utf-8'))
sys.exit()
res = subprocess.run(["mv", path, new_path], stdout=PIPE, stderr=PIPE)
if res.returncode != 0:
print(res.stdout.decode('utf-8'))
sys.exit()
def check_file(path):
with open(path, 'r') as f:
content = f.read()
old_content = content
search_result = re.findall(r'(\nclass (\S+) extends \\?MediaWikiTestCase)', content)
if not search_result and 'PHPUnit\Framework\TestCase' not in content:
return
with open(path, 'w') as f:
new_content = content.replace('MediaWikiTestCase', 'MediaWikiUnitTestCase')
new_content = new_content.replace('PHPUnit\Framework\TestCase', 'MediaWikiUnitTestCase')
new_content = re.sub(r'\n\t*?use \\?(PHPUnit4And6Compat|MediaWikiCoversValidator)\;', '', new_content)
new_content = re.sub(r' extends (\\?)TestCase', r' extends \1MediaWikiUnitTestCase', new_content)
f.write(new_content)
res = subprocess.run(["composer", "phpunit:unit", path], stdout=PIPE, stderr=PIPE)
if res.returncode != 0 or 'markTestSkipped' in content:
try:
errors = re.findall(r'Tests\: (\d+)(?:.+), Assertions\: (?:\d+)(?:.+?), (.+\: \d+)', res.stdout.decode('utf-8'))
except:
errors = []
if errors:
errs = sum([int(i) for i in re.findall(r' (\d+)', errors[0][1])])
print(errors[0][0] + ',' + str(errs) +','+ path)
#print(res.stdout.decode('utf-8'))
print('It did not work :((')
with open(path, 'w') as f:
f.write(old_content)
move_things(path, 'integration')
else:
print('It worked, moving')
move_things(path, 'unit')
for f in find_test_files(sys.argv[1]):
#print('Checking ' + f)
check_file(f)
#break