Add unit test runner

This commit is contained in:
Matthew Gordon 2024-03-02 20:06:15 -04:00
parent 4b2d297367
commit d458be52ac
2 changed files with 35 additions and 7 deletions

28
dev.py
View File

@ -3,19 +3,33 @@ import os
import subprocess import subprocess
import sys 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): def devupdate(args):
subprocess.run( subprocess.run(
[sys.executable, '-m', 'pip', 'install', '-r', [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): def run(args):
import scripts.run import_run().run(args)
scripts.run.ROOT_DIR = root_dir # TODO: FIXME
scripts.run.run(args)
def unit_tests(args):
import_run().unit_tests(args)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -24,6 +38,10 @@ run_parser = subparsers.add_parser(
'run', help='Run a test instance of locality' 'run', help='Run a test instance of locality'
) )
run_parser.set_defaults(func=run) 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_parser = subparsers.add_parser(
'devupdate', help='Install or update packages used by this script' 'devupdate', help='Install or update packages used by this script'
) )

View File

@ -8,20 +8,22 @@ from .postgres_container import PostgresContainer
ROOT_DIR = None ROOT_DIR = None
def run(args): def cargo(*args):
global ROOT_DIR global ROOT_DIR
with PostgresContainer() as postgres: with PostgresContainer() as postgres:
locality_env = { locality_env = {
'LOCALITY_DATABASE_URL': postgres.get_url(), 'LOCALITY_DATABASE_URL': postgres.get_url(),
'LOCALITY_TEST_DATABASE_URL': postgres.get_url(),
'LOCALITY_STATIC_FILE_PATH': os.path.join( 'LOCALITY_STATIC_FILE_PATH': os.path.join(
ROOT_DIR, ROOT_DIR,
'static'), 'static'),
'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3' 'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3'
} }
locality_env = os.environ.copy() | locality_env
cargo_bin = shutil.which('cargo') cargo_bin = shutil.which('cargo')
locality_process = subprocess.Popen( locality_process = subprocess.Popen(
[cargo_bin, 'run'], env=locality_env, cwd=ROOT_DIR [cargo_bin, *args], env=locality_env, cwd=ROOT_DIR
) )
try: try:
@ -32,3 +34,11 @@ def run(args):
finally: finally:
if locality_process.poll() is None: if locality_process.poll() is None:
locality_process.terminate() locality_process.terminate()
def run(args):
cargo('run')
def unit_tests(args):
cargo("test")