Created
October 19, 2021 03:59
-
-
Save Advik-B/0e822cebab97258c5ce8a859f0965108 to your computer and use it in GitHub Desktop.
run unit tests (windows-only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from fnmatch import fnmatch | |
from subprocess import run, TimeoutExpired | |
import subprocess as cmd | |
from termcolor import cprint | |
os.system('py -m pip install termcolor pyflakes pylint pytest') | |
def listfiles(root, pattern:str='*.*'): | |
A = [] | |
for path, subdirs, files in os.walk(root): | |
for name in files: | |
if fnmatch(name, pattern): | |
A.append(os.path.join(path, name)) | |
return A | |
cprint('LINTING:', 'blue', attrs=['bold', 'reverse']) | |
files_to_test = listfiles(os.getcwd(), '*.py') | |
cprint('The following file(s) will be checked:\n', 'green', attrs=['bold', 'underline']) | |
for file in files_to_test: | |
cprint(f' {file}' , 'yellow', attrs=['bold']) | |
print() | |
print('-'*80) | |
print('*'*80) | |
print('-'*80) | |
print() | |
for file in files_to_test: | |
cprint(f'Checking: {file} 🔎 ', 'green', attrs=['bold', 'underline']) | |
print() | |
o = cmd.getoutput(f'py -m pyflakes {file}') # NOTE: Only works on linux | |
p = cmd.getoutput(f'py -m pylint {file}') # NOTE: Only works on linux | |
if not o or not p: | |
cprint('No problems found!','green', attrs=['bold', 'underline']) | |
else: | |
print('pyflakes:\n' , o) | |
print() | |
print() | |
print('*'*80) | |
print() | |
print() | |
print('pylint:\n\t' , p) | |
print() | |
print('-'*80) | |
print() | |
print('Checking Done!') | |
cprint('TESTING:', 'blue', attrs=['bold', 'reverse']) | |
fnames = [] | |
for filename in files_to_test: | |
fnames.append("%s\tin '%s'" % (filename.split('\\')[-1], filename)) | |
cprint('Running tests for the following file(s):', color='green' ,attrs=['bold', 'underline']) | |
print() | |
for file in fnames: | |
cprint(':: '+file, color='yellow') | |
print() | |
for file in files_to_test: | |
if file.split('\\')[-1] == 'runtests.py': | |
pass | |
else: | |
cprint('Testing: %s' % file#. | |
, 'green', attrs=['bold']) | |
print() | |
cprint((f'{":"*40}: START :{":"*40}').center(80) ,'cyan', attrs=['reverse', 'bold']) | |
try: | |
run('py -m pytest %s' % file, timeout=120, shell=True)# NOTE: Only works on linux | |
except TimeoutExpired as error: | |
cprint(error, color='red', attrs=['bold']) | |
cprint((f'{":"*40}: END :{":"*40}').center(80) ,'cyan', attrs=['reverse', 'bold']) | |
print('\n'*4) | |
cprint('ALL TESTS COMPLETED', color='green', attrs=['bold', 'reverse', 'underline']) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment