From d458be52ac028b87bf87601145b492e7d9db4d86 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 2 Mar 2024 20:06:15 -0400 Subject: [PATCH] Add unit test runner --- dev.py | 28 +++++++++++++++++++++++----- scripts/run.py | 14 ++++++++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/dev.py b/dev.py index cd13cf5..180f1d7 100644 --- a/dev.py +++ b/dev.py @@ -3,19 +3,33 @@ import os import subprocess import sys -root_dir = sys.path[0] +# The directory containing this script should be the root directory of the +# locality repository. +ROOT_DIR = sys.path[0] def devupdate(args): subprocess.run( [sys.executable, '-m', 'pip', 'install', '-r', - os.path.join(root_dir, 'dev-python-requirements.txt'), ]) + os.path.join(ROOT_DIR, 'dev-python-requirements.txt'), ]) + + +def import_run(): + """Import the scripts.run module and return it. + + We do this in a function so that devupdate() can be run without everything + needed by this module being available.""" + import scripts.run + scripts.run.ROOT_DIR = ROOT_DIR + return scripts.run def run(args): - import scripts.run - scripts.run.ROOT_DIR = root_dir # TODO: FIXME - scripts.run.run(args) + import_run().run(args) + + +def unit_tests(args): + import_run().unit_tests(args) parser = argparse.ArgumentParser() @@ -24,6 +38,10 @@ run_parser = subparsers.add_parser( 'run', help='Run a test instance of locality' ) run_parser.set_defaults(func=run) +run_parser = subparsers.add_parser( + 'unit_test', help='Run unit tests' +) +run_parser.set_defaults(func=unit_tests) devupdate_parser = subparsers.add_parser( 'devupdate', help='Install or update packages used by this script' ) diff --git a/scripts/run.py b/scripts/run.py index a6fb878..1f19ad3 100644 --- a/scripts/run.py +++ b/scripts/run.py @@ -8,20 +8,22 @@ from .postgres_container import PostgresContainer ROOT_DIR = None -def run(args): +def cargo(*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' } + locality_env = os.environ.copy() | locality_env cargo_bin = shutil.which('cargo') locality_process = subprocess.Popen( - [cargo_bin, 'run'], env=locality_env, cwd=ROOT_DIR + [cargo_bin, *args], env=locality_env, cwd=ROOT_DIR ) try: @@ -32,3 +34,11 @@ def run(args): finally: if locality_process.poll() is None: locality_process.terminate() + + +def run(args): + cargo('run') + + +def unit_tests(args): + cargo("test")