From 3687882c7c77841e6f0abfe03f1ee475e04e3745 Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Wed, 17 Jul 2024 12:22:30 +0200 Subject: [PATCH] Always run tests in the `tests` directory of repos (#3784) Also silence pytest by redirecting stdout/stderr to a null device --- .../operations/requests/proposed_change.py | 52 ++++++++++++++----- backend/infrahub/pytest_plugin.py | 5 +- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/backend/infrahub/message_bus/operations/requests/proposed_change.py b/backend/infrahub/message_bus/operations/requests/proposed_change.py index fbf8b96df5..662c6fffba 100644 --- a/backend/infrahub/message_bus/operations/requests/proposed_change.py +++ b/backend/infrahub/message_bus/operations/requests/proposed_change.py @@ -1,6 +1,8 @@ from __future__ import annotations import asyncio +import os +import sys from enum import IntFlag from pathlib import Path from typing import TYPE_CHECKING, Union @@ -536,17 +538,43 @@ def _execute( directory: Path, repository: ProposedChangeRepository, proposed_change: InfrahubNode ) -> Union[int, pytest.ExitCode]: config_file = str(directory / ".infrahub.yml") - return pytest.main( - [ - str(directory), - f"--infrahub-repo-config={config_file}", - f"--infrahub-address={config.SETTINGS.main.internal_address}", - "--continue-on-collection-errors", # FIXME: Non-Infrahub tests should be ignored - "-qqqq", - "-s", - ], - plugins=[InfrahubBackendPlugin(service.client.config, repository.repository_id, proposed_change.id)], - ) + test_directory = directory / "tests" + + if not test_directory.is_dir(): + log.debug( + event="repository_tests_ignored", + proposed_change=proposed_change, + repository=repository.repository_name, + message="tests directory not found", + ) + return 1 + + # Redirect stdout/stderr to avoid showing pytest lines in the git agent + old_out = sys.stdout + old_err = sys.stderr + + with Path(os.devnull).open(mode="w", encoding="utf-8") as devnull: + sys.stdout = devnull + sys.stderr = devnull + + exit_code = pytest.main( + [ + str(test_directory), + f"--infrahub-repo-config={config_file}", + f"--infrahub-address={config.SETTINGS.main.internal_address}", + "-qqqq", + "-s", + ], + plugins=[ + InfrahubBackendPlugin(service.client.config, repository.repository_id, proposed_change.id) + ], + ) + + # Restore stdout/stderr back to their orignal states + sys.stdout = old_out + sys.stderr = old_err + + return exit_code for repository in message.branch_diff.repositories: log_line = "Skipping tests for data only branch" @@ -563,7 +591,7 @@ def _execute( return_code = await asyncio.to_thread(_execute, worktree_directory, repository, proposed_change) log.info( - "repository_tests_completed", + event="repository_tests_completed", proposed_change=message.proposed_change, repository=repository.repository_name, return_code=return_code, diff --git a/backend/infrahub/pytest_plugin.py b/backend/infrahub/pytest_plugin.py index c8bca1841c..5ddecbeed6 100644 --- a/backend/infrahub/pytest_plugin.py +++ b/backend/infrahub/pytest_plugin.py @@ -109,12 +109,13 @@ def pytest_runtest_setup(self, item: Item) -> None: If a check already exists, reset it to its default values. """ + created_at = Timestamp().to_string() check = self.checks.get(item.nodeid, None) if check: check.message.value = "" check.conclusion.value = "unknown" check.severity.value = "info" - check.created_at.value = Timestamp().to_string() + check.created_at.value = created_at else: check = self.client.create( kind=InfrahubKind.STANDARDCHECK, @@ -123,7 +124,7 @@ def pytest_runtest_setup(self, item: Item) -> None: "origin": item.nodeid, "kind": "TestReport", "validator": self.validator.id, - "created_at": Timestamp().to_string(), + "created_at": created_at, "severity": "info", }, )