Skip to content

Commit 6672a32

Browse files
committed
more nox
* add support for backend-only * main runner only runs one database at a time, there is no multiple db for one pytest run feature here * use match/case! Change-Id: I2f206593c33389d355e0e6eb0ac0dea38a5e4653
1 parent 470a7da commit 6672a32

2 files changed

Lines changed: 47 additions & 39 deletions

File tree

noxfile.py

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import os
77
import shutil
88
import sys
9-
from typing import Optional
10-
from typing import Sequence
119

1210
import nox
1311
from packaging.version import parse as parse_version
@@ -34,6 +32,7 @@
3432
]
3533
DATABASES = ["sqlite", "postgresql", "mysql", "oracle", "mssql"]
3634
SQLALCHEMY_VERSIONS = ["default", "sqla14", "sqla20", "sqlamain"]
35+
BACKEND = ["_nobackend", "backendonly"]
3736

3837
pyproject = nox.project.load_toml("pyproject.toml")
3938

@@ -42,7 +41,10 @@
4241

4342

4443
def filter_sqla(
45-
python: str, sqlalchemy: str, database: Optional[str] = None
44+
python: str,
45+
sqlalchemy: str,
46+
database: str | None = None,
47+
backendonly: str | None = None,
4648
) -> bool:
4749
python_version = parse_version(python.rstrip("t"))
4850
if sqlalchemy == "sqla14":
@@ -55,40 +57,52 @@ def filter_sqla(
5557

5658
@nox.session()
5759
@tox_parameters(
58-
["python", "sqlalchemy", "database"],
59-
[PYTHON_VERSIONS, SQLALCHEMY_VERSIONS, DATABASES],
60+
["python", "sqlalchemy", "database", "backendonly"],
61+
[PYTHON_VERSIONS, SQLALCHEMY_VERSIONS, DATABASES, BACKEND],
6062
filter_=filter_sqla,
6163
)
6264
def tests(
63-
session: nox.Session, python: str, sqlalchemy: str, database: str
65+
session: nox.Session,
66+
python: str,
67+
sqlalchemy: str,
68+
database: str,
69+
backendonly: str,
6470
) -> None:
6571
"""Run the main test suite against one database at a time"""
6672

67-
_tests(session, sqlalchemy, [database], python=python)
73+
_tests(
74+
session,
75+
sqlalchemy,
76+
database,
77+
python=python,
78+
backendonly=backendonly == "backendonly",
79+
)
6880

6981

7082
@nox.session(name="coverage")
7183
@tox_parameters(["database"], [DATABASES], base_tag="coverage")
7284
def coverage(session: nox.Session, database: str) -> None:
7385
"""Run tests with coverage."""
7486

75-
_tests(session, "default", [database], coverage=True)
87+
_tests(session, "default", database, coverage=True)
7688

7789

7890
def _tests(
7991
session: nox.Session,
8092
sqlalchemy: str,
81-
databases: Sequence[str],
93+
database: str,
8294
*,
8395
python: str = OUR_PYTHON,
8496
coverage: bool = False,
97+
backendonly: bool = False,
8598
) -> None:
86-
if sqlalchemy == "sqla14":
87-
session.install(f"{SQLA_REPO}@rel_1_4#egg=sqlalchemy")
88-
elif sqlalchemy == "sqla20":
89-
session.install(f"{SQLA_REPO}@rel_2_0#egg=sqlalchemy")
90-
elif sqlalchemy == "sqlamain":
91-
session.install(f"{SQLA_REPO}#egg=sqlalchemy")
99+
match sqlalchemy:
100+
case "sqla14":
101+
session.install(f"{SQLA_REPO}@rel_1_4#egg=sqlalchemy")
102+
case "sqla20":
103+
session.install(f"{SQLA_REPO}@rel_2_0#egg=sqlalchemy")
104+
case "sqlamain":
105+
session.install(f"{SQLA_REPO}#egg=sqlalchemy")
92106

93107
# for sqlalchemy == "default", the alembic install will install
94108
# current released SQLAlchemy version as a dependency
@@ -120,22 +134,22 @@ def _tests(
120134

121135
cmd.extend(os.environ.get("TOX_WORKERS", "-n4").split())
122136

123-
for database in databases:
124-
if database == "sqlite":
137+
match database:
138+
case "sqlite":
125139
cmd.extend(os.environ.get("TOX_SQLITE", "--db sqlite").split())
126-
elif database == "postgresql":
140+
case "postgresql":
127141
session.install(
128142
*nox.project.dependency_groups(pyproject, "tests_postgresql")
129143
)
130144
cmd.extend(
131145
os.environ.get("TOX_POSTGRESQL", "--db postgresql").split()
132146
)
133-
elif database == "mysql":
147+
case "mysql":
134148
session.install(
135149
*nox.project.dependency_groups(pyproject, "tests_mysql")
136150
)
137151
cmd.extend(os.environ.get("TOX_MYSQL", "--db mysql").split())
138-
elif database == "oracle":
152+
case "oracle":
139153
# we'd like to use oracledb but SQLAlchemy 1.4 does not have
140154
# oracledb support...
141155
session.install(
@@ -147,23 +161,21 @@ def _tests(
147161
session.env["NLS_LANG"] = os.environ.get("NLS_LANG")
148162
cmd.extend(os.environ.get("TOX_ORACLE", "--db oracle").split())
149163
cmd.extend("--write-idents db_idents.txt".split())
150-
elif database == "mssql":
164+
case "mssql":
151165
session.install(
152166
*nox.project.dependency_groups(pyproject, "tests_mssql")
153167
)
154168
cmd.extend(os.environ.get("TOX_MSSQL", "--db mssql").split())
155169
cmd.extend("--write-idents db_idents.txt".split())
156170

171+
if backendonly:
172+
cmd.append("--backend-only")
173+
157174
posargs, opts = extract_opts(session.posargs, "generate-junit")
158175

159176
if opts.generate_junit:
160-
# produce individual junit files that are per-database (or as close
161-
# as we can get). jenkins junit plugin will merge all the files...
162-
if len(databases) == 1:
163-
tag = "-".join(databases)
164-
junitfile = f"junit-{tag}.xml"
165-
else:
166-
junitfile = "junit-general.xml"
177+
# produce individual junit files that are per-database
178+
junitfile = f"junit-{database}.xml"
167179
cmd.extend(["--junitxml", junitfile])
168180

169181
cmd.extend(posargs)
@@ -172,9 +184,8 @@ def _tests(
172184
session.run(*cmd)
173185
finally:
174186
# Run cleanup for oracle/mssql
175-
for database in databases:
176-
if database in ["oracle", "mssql"]:
177-
session.run("python", "reap_dbs.py", "db_idents.txt")
187+
if database in ["oracle", "mssql"]:
188+
session.run("python", "reap_dbs.py", "db_idents.txt")
178189

179190
# Clean up scratch directories
180191
for scratch_dir in glob("scratch*"):
@@ -232,7 +243,7 @@ def test_pyoptimize(session: nox.Session) -> None:
232243

233244
posargs, opts = extract_opts(session.posargs, "generate-junit")
234245
if opts.generate_junit:
235-
cmd.extend(["--junitxml", "junit-general.xml"])
246+
cmd.extend(["--junitxml", "junit-pyoptimize.xml"])
236247

237248
cmd.extend(posargs)
238249

tools/toxnox.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
import sys
1616
from typing import Any
1717
from typing import Callable
18-
from typing import List
19-
from typing import Optional
2018
from typing import Sequence
21-
from typing import Tuple
2219

2320
import nox
2421

@@ -29,9 +26,9 @@ def tox_parameters(
2926
names: Sequence[str],
3027
token_lists: Sequence[Sequence[str]],
3128
*,
32-
base_tag: Optional[str] = None,
33-
filter_: Optional[Callable[..., bool]] = None,
34-
always_include_in_tag: Optional[Sequence[str]] = None,
29+
base_tag: str | None = None,
30+
filter_: Callable[..., bool] | None = None,
31+
always_include_in_tag: Sequence[str] | None = None,
3532
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
3633
r"""Decorator to create a parameter/tagging structure for a nox session
3734
function that acts to a large degree like tox's generative environments.
@@ -197,7 +194,7 @@ def _recur_param(prevtokens, prevtags, token_lists):
197194
return nox.parametrize(names, params)
198195

199196

200-
def extract_opts(posargs: List[str], *args: str) -> Tuple[List[str], Any]:
197+
def extract_opts(posargs: list[str], *args: str) -> tuple[list[str], Any]:
201198

202199
underscore_args = [arg.replace("-", "_") for arg in args]
203200
return_tuple = collections.namedtuple("options", underscore_args)

0 commit comments

Comments
 (0)