Coverage for /builds/svesch/cikit/src/cikit/systemtest/fixtures.py: 93%
23 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-09 14:17 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-09 14:17 +0000
1# SPDX-License-Identifier: GPL-3.0-or-later
2"""Pytest fixtures for cikit system tests.
4This module provides shared ``env`` and ``run_env`` fixtures for system
5tests. The fixtures can be enabled for a specific test folder via
6``conftest.py`` using:
8 pytest_plugins = ["cikit.systemtest.fixtures"]
10The ``env`` fixture provides a regular ``CiEnvironment`` for tests that
11only need resolved configuration and command execution. The ``run_env``
12fixture additionally provides a ``CiRunEnvironment`` with an associated
13run context for macros that store run data or register artifacts.
15Author:
16 Sven Schlender
17Date:
18 2026-08-27
19"""
21import os
22from pathlib import Path
24import pytest
26from cikit.env import CiEnvironment, CiRunEnvironment, get_env_vars
29@pytest.fixture(scope="module")
30def env(request):
31 """Provide a CI environment for one system test module.
33 The default working directory is set to the directory containing the
34 current test module. The environment is created once per module to avoid
35 repeated provider and metadata resolution.
37 Args:
38 request: Pytest request object.
40 Returns:
41 Initialized CI environment for the current system test module.
42 """
43 cwd = Path(str(request.fspath)).parent
44 return CiEnvironment(get_env_vars(), cwd=cwd)
47@pytest.fixture(scope="module", name="without_remote_cikit_data")
48def _without_remote_cikit_data():
49 """Disable remote cikit data access for one system test module.
51 The remote cikit data environment variables are removed before module
52 scoped CI environments are created and restored afterwards.
53 """
54 keys = ("CIKIT_DATA_BASE_URL", "CIKIT_DATA_API_TOKEN")
55 old_values = {key: os.environ.get(key) for key in keys}
57 for key in keys:
58 os.environ.pop(key, None)
60 yield
62 for key, value in old_values.items():
63 if value is None: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true
64 os.environ.pop(key, None)
65 else:
66 os.environ[key] = value
69@pytest.fixture(scope="module")
70def run_env(request, without_remote_cikit_data): # @UnusedVariable pylint: disable=unused-argument
71 """Provide a cache-only CI run environment for one system test module.
73 Remote cikit data access is disabled before the environment is resolved,
74 ensuring that system tests cannot update the configured remote service.
76 Args:
77 request: Pytest request object used to determine the current module
78 and its directory.
79 without_remote_cikit_data: Fixture disabling remote cikit data access.
81 Returns:
82 Resolved cache-only ``CiRunEnvironment`` for the test module.
83 """
84 cwd = Path(str(request.fspath)).parent
85 return CiRunEnvironment(get_env_vars(), run_name=request.module.__name__, cwd=cwd)