import time import os import signal import re import sys import subprocess from django.db.models.expressions import result sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def run(): while True: newpid = os.fork() if newpid == 0: import coverage cov = coverage.Coverage() cov.load() cov.start() signal.signal(signal.SIGINT, signal.default_int_handler) try: from server import main main() except KeyboardInterrupt: pass cov.stop() cov.save() os._exit(0) else: return newpid if __name__ == '__main__': pid = run() time.sleep(1) script_dir = os.path.dirname(os.path.abspath(__file__)) pattern = re.compile(r'^test_.*\.(sh|py)$') dotpy = re.compile(r'^.*\.py$') dotsh = re.compile(r'^.*\.sh$') matching_files = [ filename for filename in os.listdir(script_dir) if os.path.isfile(os.path.join(script_dir, filename)) and pattern.match(filename) ] total = 0 failed = 0 for file in matching_files: file_path = os.path.join(script_dir, file) if dotpy.match(file): result = subprocess.run(['python3', file_path], capture_output=True, text=True) elif dotsh.match(file): result = subprocess.run(['bash', file_path], capture_output=True, text=True) else: result = subprocess.run(['bash', '-c', file_path], capture_output=True, text=True) print('{} returned {}'.format(file, result.returncode)) if result.returncode != 0: print(result.stderr, result.stdout) failed += 1 total += 1 time.sleep(1) os.kill(pid, signal.SIGINT) print(f'{total - failed} out of {total} tests succeeded, {failed} failed') os._exit(0 if failed == 0 else 1)