Skip to content

Commit fe6d104

Browse files
committed
Add dj-cli and pre-commit hooks
1 parent f023183 commit fe6d104

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

.pre-commit-config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v3.2.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files
11+
- repo: https://github.com/astral-sh/ruff-pre-commit
12+
rev: v0.7.3
13+
hooks:
14+
- id: ruff
15+
args: [ --fix ]
16+
- id: ruff-format

django_mongodb_cli/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)