import os import shutil import subprocess import time from .postgres_container import PostgresContainer ROOT_DIR = None def cargo(*args, env=None): global ROOT_DIR if env is None: env = { 'LOCALITY_DATABASE_URL': "", 'LOCALITY_TEST_DATABASE_URL': "", 'LOCALITY_STATIC_FILE_PATH': os.path.join( ROOT_DIR, 'static'), 'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3' } env = os.environ.copy() | env cargo_bin = shutil.which('cargo') locality_process = subprocess.Popen( [cargo_bin] + list(args), env=env, cwd=ROOT_DIR ) try: while locality_process.poll() is None: time.sleep(0.5) except KeyboardInterrupt: pass finally: if locality_process.poll() is None: locality_process.terminate() def cargo_with_db(*args): global ROOT_DIR with PostgresContainer() as postgres: locality_env = { 'LOCALITY_DATABASE_URL': postgres.get_url(), 'LOCALITY_TEST_DATABASE_URL': postgres.get_url(), 'LOCALITY_STATIC_FILE_PATH': os.path.join( ROOT_DIR, 'static'), 'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3' } cargo(env=locality_env, *args) def run(*args): cargo_with_db('run') def database_tests(*args): cargo_with_db("test", "db::migrations::test", "--", "--include-ignored") def unit_tests(*args): cargo("test")