-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
155 lines (132 loc) · 5.57 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
# ICON4Py - ICON inspired code in Python and GT4Py
#
# Copyright (c) 2022-2024, ETH Zurich and MeteoSwiss
# All rights reserved.
#
# Please, refer to the LICENSE file in the root directory.
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations
import re
from collections.abc import Sequence
from typing import Final, Literal, TypeAlias
import nox
# -- Parameter sets --
ModelSubpackagePath: TypeAlias = Literal[
"atmosphere/advection",
"atmosphere/diffusion",
"atmosphere/dycore",
"atmosphere/subgrid_scale_physics/microphysics",
"common",
"driver",
# "testing", #TODO: Add tests to testing subpackage
]
ModelTestsSubset: TypeAlias = Literal["datatest", "stencils", "basic"]
MODEL_SUBPACKAGE_PATHS: Final[Sequence[str]] = ModelSubpackagePath.__args__
MODEL_TESTS_SUBSETS: Final[Sequence[str]] = ModelTestsSubset.__args__
# -- nox configuration --
nox.options.default_venv_backend = "uv"
nox.options.sessions = ["test_model", "test_tools"]
# -- nox sessions --
# Model benchmark sessions
# TODO(egparedes): Add backend parameter
# TODO(edopao,egparedes): Change 'extras' back to 'all' once mpi4py can be compiled with hpc_sdk
@nox.session(python=["3.10", "3.11"])
@nox.parametrize("subpackage", MODEL_SUBPACKAGE_PATHS)
def benchmark_model(session: nox.Session, subpackage: ModelSubpackagePath) -> None:
"""Run pytest benchmarks for selected icon4py model subpackages."""
_install_session_venv(session, extras=["dace", "io", "testing"], groups=["test"])
with session.chdir(f"model/{subpackage}"):
session.run(*"pytest -sv --benchmark-only".split(), *session.posargs)
# Model test sessions
# TODO(egparedes): Add backend parameter
# TODO(edopao,egparedes): Change 'extras' back to 'all' once mpi4py can be compiled with hpc_sdk
@nox.session(python=["3.10", "3.11"])
@nox.parametrize("subpackage", MODEL_SUBPACKAGE_PATHS)
@nox.parametrize("selection", MODEL_TESTS_SUBSETS)
def test_model(session: nox.Session, selection: ModelTestsSubset, subpackage: ModelSubpackagePath) -> None:
"""Run tests for selected icon4py model subpackages."""
_install_session_venv(session, extras=["dace", "fortran", "io", "testing"], groups=["test"])
pytest_args = _selection_to_pytest_args(selection)
with session.chdir(f"model/{subpackage}"):
session.run(
*f"pytest -sv --benchmark-skip -n {session.env.get('NUM_PROCESSES', 'auto')}".split(),
*pytest_args,
*session.posargs
)
@nox.session(python=["3.10", "3.11"])
@nox.parametrize("subpackage", MODEL_SUBPACKAGE_PATHS)
def test_model_datatest(session: nox.Session, subpackage: ModelSubpackagePath) -> None:
session.notify(
f"test_model-{session.python}(selection='datatest', subpackage='{subpackage}')"
)
@nox.session(python=["3.10", "3.11"])
@nox.parametrize("subpackage", MODEL_SUBPACKAGE_PATHS)
def test_model_stencils(session: nox.Session, subpackage: ModelSubpackagePath) -> None:
notest_subpackages = { # test discovery fails because no stencil tests found
"atmosphere/subgrid_scale_physics/microphysics",
"driver",
}
if subpackage in notest_subpackages:
session.skip(f"no tests configured")
elif subpackage == "common":
session.skip(f"tests broken") # TODO: Enable tests
else:
session.notify(
f"test_model-{session.python}(selection='stencils', subpackage='{subpackage}')"
)
# @nox.session(python=["3.10", "3.11"])
# @nox.parametrize("selection", MODEL_TEST_SELECTION)
# def test_testing(session: nox.Session, selection: ModelTestKind) -> None:
# session.notify(
# f"test_model-{session.python}(selection='{selection}', subpackage='testing')"
# )
# Tools test sessions
# TODO(edopao,egparedes): Change 'extras' back to 'all' once mpi4py can be compiled with hpc_sdk
@nox.session(python=["3.10", "3.11"])
@nox.parametrize("datatest", [False, True])
def test_tools(session: nox.Session, datatest: bool) -> None:
"""Run tests for the Fortran integration tools."""
_install_session_venv(session, extras=["fortran", "io", "testing"], groups=["test"])
with session.chdir("tools"):
session.run(
*f"pytest -sv --benchmark-skip -n {session.env.get('NUM_PROCESSES', 'auto')} {'--datatest' if datatest else ''}".split(),
*session.posargs
)
# -- utils --
def _install_session_venv(
session: nox.Session,
*args: str | Sequence[str],
extras: Sequence[str] = (),
groups: Sequence[str] = (),
) -> None:
"""Install session packages using uv."""
if (env_extras := session.env.get("ICON4PY_NOX_UV_CUSTOM_SESSION_EXTRAS", "")):
extras = [*extras, *re.split(r'\W+', env_extras)]
session.run_install(
"uv",
"sync",
"--no-dev",
*(f"--extra={e}" for e in extras),
*(f"--group={g}" for g in groups),
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
for item in args:
session.run_install(
"uv",
"pip",
"install",
*((item,) if isinstance(item, str) else item),
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
def _selection_to_pytest_args(selection: ModelTestsSubset) -> list[str]:
pytest_args = []
match selection:
case "datatest":
pytest_args.append("--datatest")
case "stencils":
pytest_args.extend(["-k", "stencil_tests"])
case "basic":
pytest_args.extend(["-k", "not stencil_tests"])
case _:
raise AssertionError(f"Invalid selection: {selection}")
return pytest_args