SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # License: MIT
  2. # Run like: "python phpunit4_killer.py /var/lib/mediawiki/extensions/Wikibase/"
  3. import os
  4. import sys
  5. import re
  6. import subprocess
  7.  
  8. def find_files(path):
  9.     files = []
  10.     for r, d, f in os.walk(path):
  11.         for file_name in f:
  12.             if file_name.endswith('.php'):
  13.                 files.append(os.path.join(r, file_name))
  14.  
  15.     return files
  16.  
  17. def check_file(path):
  18.     with open(path, 'r', encoding="utf-8") as f:
  19.         content = f.read()
  20.         old_content = content
  21.     cases = re.findall(r'\n\t*?use \\?PHPUnit4And6Compat;', content)
  22.     literal = '\'(?:[^\'\\\\]|\\\\\')+\'|"(?:[^"\\\\]|\\\\")+"'
  23.     clname = '(?:[a-zA-Z\\\\]+::class|\$\w+|' + literal + ')'
  24.     content = re.sub(r'(\$this->)setExpected(Exception\(\s+' + clname + '\s+\);)', r'\1expect\2', content)
  25.     content = re.sub(r'^(\\t*)(\$this->)setExpectedException\(\s+(' + clname + '),\s+(' + clname + ')\s+\);', r'\1\2expectException( \3 );\n\1\$this->expectExceptionMessage( \4 );', content)
  26.     content = re.sub(r'(\$this->)get(Mock\(\s+' + clname + '\s+\))', r'\1create\2', content)
  27.     content = re.sub(r'(\$this->)getMock\(\s+(' + clname + '),\s+\[\],\s+\[\],\s+[\'"]{2},\s+false\s+\)', r'\1createMock( \2 )', content)
  28.     content = re.sub(r'^(\\t*)(\$this->)getMock\(\s+(' + clname + '),\s+(\[(?:\s*' + literal + ',?)+\])\s+\);', r'\1\2createMock( \3 )\n\1\t->setMethods( \4 )\n\1\t->getMock();', content)
  29.     if '$this->getMock(' in content or '$this->setExpectedException(' in content:
  30.         print('Nooooo /o\\')
  31.         with open(path, 'w', encoding="utf-8") as f:
  32.             print(path)
  33.             f.write(content)
  34.         return
  35.     for case in cases:
  36.         content = content.replace(case, '')
  37.     content = content.replace(', PHPUnit4And6Compat, ', ', ')
  38.     content = content.replace('use PHPUnit4And6Compat, ', 'use ')
  39.     content = content.replace(', PHPUnit4And6Compat;', ';')
  40.     if content == old_content:
  41.         return
  42.     with open(path, 'w', encoding="utf-8") as f:
  43.         print(path)
  44.         f.write(content)
  45.  
  46. for f in find_files(sys.argv[1]):
  47.     check_file(f)
  48.  
  49. subprocess.run(["composer", "update"], cwd=sys.argv[1], shell=True)
  50. subprocess.run(["composer", "fix"], cwd=sys.argv[1], shell=True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top