Skip to content

Commit d4ec142

Browse files
committed
Fix pytest invocation
`pytest` was invoked with all source and extra paths, but it shouldn't. Instead the path should be configured by the user in pyproject.toml. If the user pass any positional arguments to the `pytest` sessions, then those arguments will be forwarded verbatim to `pytest`. Since `pytest` works with the default path `tests`, we leave that in the extra paths in case the user is not defining it explicitly in the configuration, but for more esoteric setups, the test paths should be automatically added. This commit also updates the default configuration for `api` to replace `tests` with `pytests` in the extra paths (even when the user should configure it explicitly, we want to remove `tests` as it might be for something else) and also to replace `src` with `py` in the source paths. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent bd9f573 commit d4ec142

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/frequenz/repo/config/nox/default.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
method.
2828
"""
2929

30+
import dataclasses
31+
3032
from . import config as _config
33+
from . import util
3134

3235
common_command_options: _config.CommandsOptions = _config.CommandsOptions(
3336
black=[
@@ -86,8 +89,16 @@
8689
api_command_options: _config.CommandsOptions = common_command_options.copy()
8790
"""Default command-line options for APIs."""
8891

89-
api_config: _config.Config = common_config.copy()
90-
"""Default configuration for APIs."""
92+
api_config: _config.Config = dataclasses.replace(
93+
common_config,
94+
source_paths=list(util.replace(common_config.source_paths, {"src": "py"})),
95+
extra_paths=list(util.replace(common_config.extra_paths, {"tests": "pytests"})),
96+
)
97+
"""Default configuration for APIs.
98+
99+
Same as `common_config`, but with `source_paths` replacing `"src"` with `"py"`
100+
and `extra_paths` replacing `"tests"` with `"pytests"`.
101+
"""
91102

92103
actor_command_options: _config.CommandsOptions = common_command_options.copy()
93104
"""Default command-line options for actors."""

src/frequenz/repo/config/nox/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def _pytest_impl(
145145
session: nox.Session, max_or_min_deps: str # pylint: disable=unused-argument
146146
) -> None:
147147
conf = config.get()
148-
session.run("pytest", *conf.opts.pytest, *conf.path_args(session))
148+
session.run("pytest", *conf.opts.pytest, *session.posargs)
149149

150150
# pylint: disable=fixme
151151
# TODO: Implement coverage reporting, we need to research this a bit and it

src/frequenz/repo/config/nox/util.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import pathlib
1212
import tomllib
13-
from collections.abc import Iterable
13+
from collections.abc import Iterable, Mapping, Set
1414
from typing import TypeVar
1515

1616
_T = TypeVar("_T")
@@ -31,6 +31,27 @@ def flatten(iterables: Iterable[Iterable[_T]], /) -> Iterable[_T]:
3131
return (item for sublist in iterables for item in sublist)
3232

3333

34+
def replace(iterable: Iterable[_T], replacements: Mapping[_T, _T], /) -> Iterable[_T]:
35+
"""Replace elements in an iterable.
36+
37+
Args:
38+
iterable: The iterable to replace elements in.
39+
old: The elements to replace.
40+
new: The elements to replace with.
41+
42+
Returns:
43+
An iterable with the elements in `iterable` replaced.
44+
45+
Example:
46+
>>> assert list(replace([1, 2, 3], old={1, 2}, new={4, 5})) == [4, 5, 3]
47+
"""
48+
for item in iterable:
49+
if item in replacements:
50+
yield replacements[item]
51+
else:
52+
yield item
53+
54+
3455
def existing_paths(paths: Iterable[str], /) -> Iterable[pathlib.Path]:
3556
"""Filter paths to only leave valid paths that exist.
3657

0 commit comments

Comments
 (0)