Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend pytest task to accept sequence #284

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[entries]]
id = "e3dd07f8-7520-45b9-9407-9f650e4d333c"
type = "feature"
description = "Extend pytest task to accept sequence"
sebimarkgraf marked this conversation as resolved.
Show resolved Hide resolved
author = "@sebimarkgraf"
20 changes: 15 additions & 5 deletions kraken-build/src/kraken/std/python/tasks/pytest_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PytestTask(EnvironmentAwareDispatchTask):
description = "Run unit tests using Pytest."
python_dependencies = ["pytest"]

tests_dir: Property[Path]
tests_dir: Property[Sequence[Path] | Path]
include_dirs: Property[Sequence[Path]] = Property.default(())
NiklasRosenstein marked this conversation as resolved.
Show resolved Hide resolved
ignore_dirs: Property[Sequence[Path]] = Property.default_factory(list)
allow_no_tests: Property[bool] = Property.default(False)
Expand All @@ -46,14 +46,19 @@ def is_skippable(self) -> bool:
def get_execute_command_v2(self, env: MutableMapping[str, str]) -> list[str] | TaskStatus:
tests_dir = self.tests_dir.get_or(None)
tests_dir = tests_dir or self.settings.get_tests_directory()
if not tests_dir:

if isinstance(tests_dir, Path):
tests_dir = [tests_dir]

if not tests_dir or len(tests_dir) == 0:
print("error: no test directory configured and none could be detected")
return TaskStatus.failed("no test directory configured and none could be detected")

command = [
"pytest",
"-vv",
str(self.project.directory / self.settings.source_directory),
str(self.project.directory / tests_dir),
*[str(self.project.directory / path) for path in tests_dir],
*[str(self.project.directory / path) for path in self.include_dirs.get()],
sebimarkgraf marked this conversation as resolved.
Show resolved Hide resolved
]
command += flatten(["--ignore", str(self.project.directory / path)] for path in self.ignore_dirs.get())
Expand Down Expand Up @@ -86,7 +91,7 @@ def pytest(
name: str = "pytest",
group: str = "test",
project: Project | None = None,
tests_dir: Path | str | None = None,
tests_dir: Sequence[Path | str] | Path | str | None = None,
include_dirs: Sequence[Path | str] = (),
ignore_dirs: Sequence[Path | str] = (),
allow_no_tests: bool = False,
Expand All @@ -96,7 +101,12 @@ def pytest(
) -> PytestTask:
project = project or Project.current()
task = project.task(name, PytestTask, group=group)
task.tests_dir = Path(tests_dir) if tests_dir is not None else None

if isinstance(tests_dir, Sequence):
task.tests_dir = list(map(Path, tests_dir))
else:
task.tests_dir = Path(tests_dir) if tests_dir is not None else None

task.include_dirs = list(map(Path, include_dirs))
task.ignore_dirs = list(map(Path, ignore_dirs))
task.allow_no_tests = allow_no_tests
Expand Down
42 changes: 42 additions & 0 deletions kraken-build/tests/kraken_std/python/tasks/test_pytest_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from kraken.core import Project

# Renamed import to prevent name collision
sebimarkgraf marked this conversation as resolved.
Show resolved Hide resolved
from kraken.std.python import pytest as pytest_task

FAKE_TEST = """
def test__fake():
assert True
"""


def test__pytest_single_path(kraken_project: Project) -> None:
"""
Backwards compatibility test
"""
(kraken_project.directory / "src").mkdir()
test_test_dir = kraken_project.directory / "tests"
test_test_dir.mkdir()
(test_test_dir / "test_mock.py").write_text(FAKE_TEST)

task = pytest_task(tests_dir=test_test_dir)

assert not task.is_skippable()

kraken_project.context.execute([":test"])


def test__pytest_multiple_paths(kraken_project: Project) -> None:
src_test_dir = kraken_project.directory / "src"
test_test_dir = kraken_project.directory / "tests"

src_test_dir.mkdir()
test_test_dir.mkdir()

(src_test_dir / "test_mock.py").write_text(FAKE_TEST)
(test_test_dir / "test_mock2.py").write_text(FAKE_TEST)

task = pytest_task(tests_dir=[src_test_dir, test_test_dir])

assert not task.is_skippable()

kraken_project.context.execute([":test"])
Loading