-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
89 lines (70 loc) · 2.03 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sys
from pathlib import Path
from shutil import copy
from adit_radis_shared.invoke_tasks import show_outdated # noqa: F401
from invoke.context import Context
from invoke.tasks import task
project_dir = Path(__file__).resolve().parent
###
# Helper functions
###
def check_container_up(ctx: Context, container_name):
result = ctx.run("docker ps", hide=True, warn=True)
assert result and result.ok
for line in result.stdout.splitlines():
if container_name in line:
return True
return False
###
# Tasks
###
@task
def init_workspace(ctx: Context):
"""Initialize workspace"""
copy(f"{project_dir}/example.env", f"{project_dir}/.env")
@task
def lint(ctx: Context):
"""Lint the source code (ruff, djlint, pyright)"""
print("Linting Python code with ruff...")
ctx.run("poetry run ruff check .", pty=True)
print("Linting Python code with pyright...")
ctx.run("poetry run pyright", pty=True)
@task
def format(ctx: Context):
"""Format the source code with ruff and djlint"""
print("Formatting Python code with ruff...")
ctx.run("poetry run ruff format .", pty=True)
print("Sorting Python imports with ruff...")
ctx.run("poetry run ruff check . --fix --select I", pty=True)
@task
def test(
ctx: Context,
path: str | None = None,
cov: bool | str = False,
html: bool = False,
keyword: str | None = None,
mark: str | None = None,
stdout: bool = False,
failfast: bool = False,
):
"""Run the test suite"""
if not check_container_up(ctx, "postgres"):
sys.exit("Tests need a running PostgreSQL database.\nRun 'docker compose up -d' first.")
cmd = "pytest "
if cov:
cmd += "--cov "
if isinstance(cov, str):
cmd += f"={cov} "
if html:
cmd += "--cov-report=html"
if keyword:
cmd += f"-k {keyword} "
if mark:
cmd += f"-m {mark} "
if stdout:
cmd += "-s "
if failfast:
cmd += "-x "
if path:
cmd += path
ctx.run(cmd, pty=True)