Skip to content

Commit

Permalink
Always run tests in the tests directory of repos (#3784)
Browse files Browse the repository at this point in the history
Also silence pytest by redirecting stdout/stderr to a null device
  • Loading branch information
gmazoyer authored Jul 17, 2024
1 parent 7dd1773 commit 3687882
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
52 changes: 40 additions & 12 deletions backend/infrahub/message_bus/operations/requests/proposed_change.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions backend/infrahub/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
},
)
Expand Down

0 comments on commit 3687882

Please sign in to comment.