|
| 1 | +import click |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | + |
| 6 | + |
| 7 | +@click.group() |
| 8 | +def cli(): |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +@click.command() |
| 13 | +@click.argument("modules", nargs=-1) |
| 14 | +@click.option("-k", "--keyword", help="Filter tests by keyword") |
| 15 | +@click.option( |
| 16 | + "-l", "--list-tests", default=False, is_flag=True, help="List available tests" |
| 17 | +) |
| 18 | +def test(modules, keyword, list_tests): |
| 19 | + """ |
| 20 | + Run tests for specified modules with an optional keyword filter. |
| 21 | + """ |
| 22 | + if list_tests: |
| 23 | + click.echo(subprocess.run(["ls", os.path.join("src", "django", "tests")])) |
| 24 | + click.echo( |
| 25 | + subprocess.run(["ls", os.path.join("src", "django-mongodb", "tests")]) |
| 26 | + ) |
| 27 | + exit() |
| 28 | + |
| 29 | + shutil.copyfile( |
| 30 | + "src/django-mongodb/.github/workflows/mongodb_settings.py", |
| 31 | + "src/django/tests/mongodb_settings.py", |
| 32 | + ) |
| 33 | + |
| 34 | + command = ["src/django/tests/runtests.py"] |
| 35 | + command.extend(["--settings", "mongodb_settings"]) |
| 36 | + command.extend(["--parallel", "1"]) |
| 37 | + command.extend(["--verbosity", "3"]) |
| 38 | + command.extend(["--debug-sql"]) |
| 39 | + |
| 40 | + # Add modules to the command |
| 41 | + command.extend(modules) |
| 42 | + |
| 43 | + # Add keyword filter if provided |
| 44 | + if keyword: |
| 45 | + command.extend(["-k", keyword]) |
| 46 | + |
| 47 | + click.echo(f"Running command: {' '.join(command)}") |
| 48 | + |
| 49 | + # Start MongoDB |
| 50 | + mongodb = subprocess.Popen(["mongo-launch", "single"]) |
| 51 | + |
| 52 | + # Execute the test command |
| 53 | + subprocess.run(command, stdin=None, stdout=None, stderr=None) |
| 54 | + |
| 55 | + # Terminate MongoDB |
| 56 | + mongodb.terminate() |
| 57 | + |
| 58 | + |
| 59 | +cli.add_command(test) |
0 commit comments