Files
Crypto_Scam/scripts/run_tests.py

35 lines
1.1 KiB
Python

"""Run offline tests. Loopback HTTP fixtures are allowed; exchange connections are forbidden."""
from pathlib import Path
import os
import subprocess
import sys
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.dont_write_bytecode = True
def offline(event, args):
if event == 'socket.connect':
host = args[1][0]
elif event == 'socket.getaddrinfo':
host = args[0]
else:
return
if host not in (None, 'localhost', '::1') and not str(host).startswith('127.'):
raise RuntimeError('External network is disabled during tests')
if __name__ == '__main__':
if len(sys.argv) == 1:
for suite in ('tests', 'meme_matcher/tests'):
result = subprocess.run([sys.executable, '-B', __file__, suite], cwd=ROOT)
if result.returncode:
sys.exit(result.returncode)
else:
os.chdir(ROOT)
sys.path.insert(0, str(ROOT))
sys.addaudithook(offline)
suite = unittest.defaultTestLoader.discover(str(ROOT / sys.argv[1]))
sys.exit(not unittest.TextTestRunner(verbosity=1).run(suite).wasSuccessful())