import time import os import signal import re import sys import subprocess 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__': os.environ["DB_FILE"] = "local.sqlite3" os.environ["OUTBOUND_SMTP_PORT"] = '1025' pid = run() result = subprocess.run(['python3', 'manage.py', 'migrate'], capture_output=True, text=True, env=os.environ) if result.returncode != 0: print('Migration failed', file=sys.stderr) print(result.stdout, file=sys.stderr) print(result.stderr, file=sys.stderr) os._exit(1) time.sleep(2) 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, env=os.environ) elif dotsh.match(file): result = subprocess.run(['bash', file_path], capture_output=True, text=True, env=os.environ) else: result = subprocess.run(['bash', '-c', file_path], capture_output=True, text=True, env=os.environ) print('{} returned {}'.format(file, result.returncode), file=sys.stderr) if result.returncode != 0: print(result.stderr, result.stdout, file=sys.stderr) failed += 1 total += 1 time.sleep(1) os.kill(pid, signal.SIGINT) print(f'{total - failed} out of {total} tests succeeded, {failed} failed', file=sys.stderr) os._exit(0 if failed == 0 else 1)