-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
190 lines (153 loc) · 5.27 KB
/
noxfile.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import nox
# Default sessions
nox.options.sessions = ["lint", "typing", "test"]
# Other nox defaults
nox.options.default_venv_backend = "uv"
nox.options.reuse_existing_virtualenvs = True
# Pip installable dependencies
PIP_DEPENDENCIES = [
("-r", "requirements/main.txt"),
("-r", "requirements/dev.txt"),
("-e", "."),
]
def _install(session: nox.Session) -> None:
"""Install the application and all dependencies into the session."""
session.install("--upgrade", "uv")
for deps in PIP_DEPENDENCIES:
session.install(*deps)
def _make_env_vars(overrides: dict[str, str] | None = None) -> dict[str, str]:
"""Create a environment variable dictionary for test sessions that enables
the app to start up.
"""
env_vars = {
"KAFKA_BOOTSTRAP_SERVERS": "localhost:9092",
"TEMPLATEBOT_PROFILE": "development",
"TEMPLATEBOT_LOG_LEVEL": "DEBUG",
"TEMPLATEBOT_ENVIRONMENT_URL": "http://example.com/",
"TEMPLATEBOT_SLACK_TOKEN": "xoxb-testing-123",
"TEMPLATEBOT_SLACK_APP_ID": "A123456",
"TEMPLATEBOT_TEMPLATE_REPO_URL": "https://github.com/lsst/templates",
"TEMPLATEBOT_TEMPLATE_CACHE_DIR": ".tmp/template_cache",
"TEMPLATEBOT_GITHUB_APP_ID": "1234",
"TEMPLATEBOT_GITHUB_APP_PRIVATE_KEY": "test",
"TEMPLATEBOT_LTD_USERNAME": "test",
"TEMPLATEBOT_LTD_PASSWORD": "test",
}
if overrides:
env_vars.update(overrides)
return env_vars
def _install_dev(session: nox.Session, bin_prefix: str = "") -> None:
"""Install the application and all development dependencies into the
session.
"""
python = f"{bin_prefix}python"
precommit = f"{bin_prefix}pre-commit"
# Install dev dependencies
session.run(python, "-m", "pip", "install", "uv", external=True)
for deps in PIP_DEPENDENCIES:
session.run(python, "-m", "uv", "pip", "install", *deps, external=True)
session.run(
python,
"-m",
"uv",
"pip",
"install",
"nox",
"pre-commit",
external=True,
)
# Install pre-commit hooks
session.run(precommit, "install", external=True)
@nox.session(name="venv-init")
def init_dev(session: nox.Session) -> None:
"""Set up a development venv."""
# Create a venv in the current directory, replacing any existing one
session.run("python", "-m", "venv", ".venv", "--clear")
_install_dev(session, bin_prefix=".venv/bin/")
print(
"\nTo activate this virtual env, run:\n\n\tsource .venv/bin/activate\n"
)
@nox.session(name="init", venv_backend=None, python=False)
def init(session: nox.Session) -> None:
"""Set up the development environment in the current virtual env."""
_install_dev(session, bin_prefix="")
@nox.session
def lint(session: nox.Session) -> None:
"""Run pre-commit hooks."""
session.install("pre-commit")
session.run("pre-commit", "run", "--all-files", *session.posargs)
@nox.session
def typing(session: nox.Session) -> None:
"""Run mypy."""
_install(session)
session.install("mypy")
session.run("mypy", "noxfile.py", "src", "tests")
@nox.session
def test(session: nox.Session) -> None:
"""Run pytest."""
from testcontainers.kafka import KafkaContainer
_install(session)
with KafkaContainer().with_kraft() as kafka:
session.run(
"pytest",
"--cov=templatebot",
"--cov-branch",
*session.posargs,
env=_make_env_vars(
{"KAFKA_BOOTSTRAP_SERVERS": kafka.get_bootstrap_server()}
),
)
@nox.session(name="scriv-create")
def scriv_create(session: nox.Session) -> None:
"""Create a scriv entry."""
session.install("scriv")
session.run("scriv", "create")
@nox.session(name="scriv-collect")
def scriv_collect(session: nox.Session) -> None:
"""Collect scriv entries."""
session.install("scriv")
session.run("scriv", "collect", "--add", "--version", *session.posargs)
@nox.session(name="update-deps")
def update_deps(session: nox.Session) -> None:
"""Update pinned server dependencies and pre-commit hooks."""
session.install("--upgrade", "uv", "pre-commit")
session.run("pre-commit", "autoupdate")
# Dependencies are unpinned for compatibility with the unpinned client
# dependency.
session.run(
"uv",
"pip",
"compile",
"--upgrade",
"--universal",
"--generate-hashes",
"--output-file",
"requirements/main.txt",
"requirements/main.in",
)
session.run(
"uv",
"pip",
"compile",
"--upgrade",
"--universal",
"--generate-hashes",
"--output-file",
"requirements/dev.txt",
"requirements/dev.in",
)
print("\nTo refresh the development venv, run:\n\n\tnox -s init\n")
@nox.session(name="run")
def run(session: nox.Session) -> None:
"""Run the application in development mode."""
_install(session)
from testcontainers.kafka import KafkaContainer
with KafkaContainer().with_kraft() as kafka:
session.run(
"uvicorn",
"templatebot.main:app",
"--reload",
env=_make_env_vars(
{"KAFKA_BOOTSTRAP_SERVERS": kafka.get_bootstrap_server()}
),
)