Skip to content

Various housekeeping tasks #354

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

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions changelog/+pytest-httpx.housekeeping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update Pytest-httpx and set all responses as reusable
1 change: 1 addition & 0 deletions changelog/+pytest.housekeeping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a fixture to always reset some environment variables before running tests
2 changes: 1 addition & 1 deletion infrahub_sdk/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InfrahubFileData(BaseModel):
api_version: InfrahubFileApiVersion = Field(InfrahubFileApiVersion.V1, alias="apiVersion")
kind: InfrahubFileKind
spec: dict
metadata: dict | None = Field(default_factory=dict)
metadata: dict = Field(default_factory=dict)


class LocalFile(BaseModel):
Expand Down
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import asyncio
import os

import pytest

from infrahub_sdk.ctl import config

pytest_plugins = ["pytester"]

ENV_VARS_TO_CLEAN = ["INFRAHUB_ADDRESS", "INFRAHUB_TOKEN", "INFRAHUB_BRANCH", "INFRAHUB_USERNAME", "INFRAHUB_PASSWORD"]


@pytest.fixture(scope="session")
def event_loop():
Expand All @@ -20,3 +23,19 @@ def event_loop():
def execute_before_any_test():
config.SETTINGS.load_and_exit()
config.SETTINGS.active.server_address = "http://mock"


@pytest.fixture(scope="session", autouse=True)
def clean_env_vars():
"""Cleans the environment variables before any test is run."""
original_values = {}
for name in ENV_VARS_TO_CLEAN:
original_values[name] = os.environ.get(name)
if original_values[name] is not None:
del os.environ[name]

yield

for name in ENV_VARS_TO_CLEAN:
if original_values[name] is not None:
os.environ[name] = original_values[name]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this will run until the session has completed I don't know if there's a point of resetting the environment variables after everything has completed. There should be nothing left that would use them at that point.

1 change: 1 addition & 0 deletions tests/unit/sdk/checks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ async def mock_gql_query_my_query(httpx_mock: HTTPXMock) -> HTTPXMock:
method="POST",
json=response,
url="http://localhost:8000/api/query/my_query?branch=main&update_group=false",
is_reusable=True,
)
return httpx_mock
Loading