From 0ead3d68f91a0a219423dfb70667fb55ed816264 Mon Sep 17 00:00:00 2001 From: Zeid Zabaneh Date: Thu, 2 May 2024 09:11:10 -0400 Subject: [PATCH] tests: add tests command (bug 1887042) - remove hardcoding of test paths and move it to pyproject.toml - add management command that runs pytest from project directory - invoke new tests command from GitHub workflow --- .github/workflows/build.yml | 2 +- pyproject.toml | 3 ++ src/lando/utils/management/commands/tests.py | 33 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/lando/utils/management/commands/tests.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a147e2bf..32a47248 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,4 +41,4 @@ jobs: source env/bin/activate lando migrate lando test - pytest src/lando/api + lando tests diff --git a/pyproject.toml b/pyproject.toml index ce83f172..1c2c3f2f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,3 +35,6 @@ where = ["src"] [tool.pytest.ini_options] DJANGO_SETTINGS_MODULE = "lando.test_settings" +testpaths = [ + "src/lando/api", +] diff --git a/src/lando/utils/management/commands/tests.py b/src/lando/utils/management/commands/tests.py new file mode 100644 index 00000000..c8ca4370 --- /dev/null +++ b/src/lando/utils/management/commands/tests.py @@ -0,0 +1,33 @@ +import subprocess + +from django.conf import settings +from django.core.management.base import BaseCommand + +ROOT_DIR = settings.BASE_DIR.parent.parent + + +class Command(BaseCommand): + help = "Run pytest from project directory" + + def add_arguments(self, parser): + parser.add_argument( + "--exitfirst", + "-x", + action="store_true", + help="Exit instantly on first error or failed test", + ) + + parser.add_argument( + "paths", nargs="*", type=str, help="Files or directories to pass to pytest" + ) + + def handle(self, *args, **options): + command = ["pytest"] + + if options["exitfirst"]: + command.append("-x") + + if options["paths"]: + command += options["paths"] + + subprocess.call(command, cwd=ROOT_DIR)