Skip to content

Commit 0ac1f8f

Browse files
authored
Fix check_is_initialized() used by tests and fix checking service agreement (now always global) for tests in client projects (#774)
1 parent dc2f368 commit 0ac1f8f

3 files changed

Lines changed: 71 additions & 90 deletions

File tree

pipelex/system/configuration/config_check.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
from rich.panel import Panel
22

3-
from pipelex.cli.commands.init.config_files import init_config
43
from pipelex.hub import get_console
5-
from pipelex.system.configuration.config_loader import config_manager
4+
from pipelex.system.configuration.config_loader import CONFIG_NAME, config_manager
65
from pipelex.tools.misc.file_utils import path_exists
76
from pipelex.urls import URLs
87

8+
# Config files that must be resolvable (in project or global dir) for pipelex to be initialized.
9+
PLXT_CONFIG_NAME = "plxt.toml"
910

10-
def check_is_initialized(print_warning_if_not: bool = True) -> bool:
11-
backends_toml_path = config_manager.backends_file_path
12-
routing_profiles_toml_path = config_manager.routing_profiles_file_path
1311

14-
# Check critical files
15-
config_exists = init_config(reset=False, dry_run=True) == 0
16-
backends_exists = path_exists(backends_toml_path)
17-
routing_exists = path_exists(routing_profiles_toml_path)
12+
def check_is_initialized(print_warning_if_not: bool = True) -> bool:
13+
# Use resolve_config_file for all checks — consistent with how backends and routing are resolved
14+
config_exists = path_exists(config_manager.resolve_config_file(CONFIG_NAME)) and path_exists(config_manager.resolve_config_file(PLXT_CONFIG_NAME))
15+
backends_exists = path_exists(config_manager.backends_file_path)
16+
routing_exists = path_exists(config_manager.routing_profiles_file_path)
1817

1918
is_initialized = config_exists and backends_exists and routing_exists
2019

pipelex/test_extras/shared_pytest_plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def pytest_configure(config: Config) -> None:
103103
if not is_pipelex_gateway_enabled():
104104
return
105105

106-
pipelex_service_config = load_pipelex_service_config_if_exists(config_dir=config_manager.pipelex_config_dir)
106+
pipelex_service_config = load_pipelex_service_config_if_exists(config_dir=config_manager.global_config_dir)
107107

108108
if pipelex_service_config is None or not pipelex_service_config.agreement.terms_accepted:
109109
console = Console()

tests/unit/pipelex/system/test_pipelex_check_initialization.py

Lines changed: 62 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,174 +3,156 @@
33
from typing import TYPE_CHECKING
44

55
if TYPE_CHECKING:
6+
from collections.abc import Callable
67
from pathlib import Path
78

89
from pytest_mock import MockerFixture
910

10-
from pipelex.system.configuration.config_check import check_is_initialized
11+
from pipelex.system.configuration.config_check import CONFIG_NAME, PLXT_CONFIG_NAME, check_is_initialized
12+
13+
14+
def _make_resolve_config_file(config_dir: Path) -> Callable[[str], Path]:
15+
def resolve_config_file(name: str) -> Path:
16+
return config_dir / name
17+
18+
return resolve_config_file
1119

1220

1321
class TestPipelexCheckInitialization:
1422
"""Test the check_is_initialized function from config_check module."""
1523

16-
def _mock_config_manager_paths(self, mocker: MockerFixture, backends_file: str, routing_file: str) -> None:
24+
def _setup_config_dir(self, tmp_path: Path, *, config_files: bool = False) -> Path:
25+
"""Create the config directory structure and optionally create config files."""
26+
config_dir = tmp_path / ".pipelex"
27+
inference_dir = config_dir / "inference"
28+
inference_dir.mkdir(parents=True)
29+
if config_files:
30+
(config_dir / CONFIG_NAME).write_text("[pipelex]\n")
31+
(config_dir / PLXT_CONFIG_NAME).write_text("[plxt]\n")
32+
return config_dir
33+
34+
def _mock_config_manager_paths(self, mocker: MockerFixture, config_dir: Path, backends_file: str, routing_file: str) -> None:
1735
"""Mock config_manager properties used by config_check."""
1836
mock_manager = mocker.MagicMock()
1937
mock_manager.backends_file_path = backends_file
2038
mock_manager.routing_profiles_file_path = routing_file
39+
mock_manager.resolve_config_file = _make_resolve_config_file(config_dir)
2140
mocker.patch("pipelex.system.configuration.config_check.config_manager", mock_manager)
2241

2342
def test_check_is_initialized_returns_true_when_all_files_exist(self, tmp_path: Path, mocker: MockerFixture) -> None:
2443
"""Test that check_is_initialized returns True when all required files exist."""
25-
# Setup test directories
26-
config_dir = tmp_path / ".pipelex" / "inference"
27-
config_dir.mkdir(parents=True)
28-
backends_file = config_dir / "backends.toml"
29-
routing_file = config_dir / "routing_profiles.toml"
44+
config_dir = self._setup_config_dir(tmp_path, config_files=True)
45+
inference_dir = config_dir / "inference"
46+
backends_file = inference_dir / "backends.toml"
47+
routing_file = inference_dir / "routing_profiles.toml"
3048
backends_file.write_text("[backends]\nconfig = 'value'")
3149
routing_file.write_text("[routing]\nconfig = 'value'")
3250

33-
# Mock config_manager to point to temp directory
34-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
51+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
3552

36-
# Test
3753
result = check_is_initialized()
3854

39-
# Verify
4055
assert result is True
4156

4257
def test_check_is_initialized_returns_false_when_backends_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
4358
"""Test that check_is_initialized returns False when backends.toml is missing."""
44-
# Setup test directories - only routing file exists
45-
config_dir = tmp_path / ".pipelex" / "inference"
46-
config_dir.mkdir(parents=True)
47-
backends_file = config_dir / "backends.toml"
48-
routing_file = config_dir / "routing_profiles.toml"
59+
config_dir = self._setup_config_dir(tmp_path, config_files=True)
60+
inference_dir = config_dir / "inference"
61+
backends_file = inference_dir / "backends.toml"
62+
routing_file = inference_dir / "routing_profiles.toml"
4963
routing_file.write_text("[routing]\nconfig = 'value'")
5064

51-
# Mock config_manager to point to temp directory
52-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
65+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
5366

54-
# Test
5567
result = check_is_initialized(print_warning_if_not=False)
5668

57-
# Verify
5869
assert result is False
5970

6071
def test_check_is_initialized_returns_false_when_routing_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
6172
"""Test that check_is_initialized returns False when routing_profiles.toml is missing."""
62-
# Setup test directories - only backends file exists
63-
config_dir = tmp_path / ".pipelex" / "inference"
64-
config_dir.mkdir(parents=True)
65-
backends_file = config_dir / "backends.toml"
66-
routing_file = config_dir / "routing_profiles.toml"
73+
config_dir = self._setup_config_dir(tmp_path, config_files=True)
74+
inference_dir = config_dir / "inference"
75+
backends_file = inference_dir / "backends.toml"
76+
routing_file = inference_dir / "routing_profiles.toml"
6777
backends_file.write_text("[backends]\nconfig = 'value'")
6878

69-
# Mock config_manager to point to temp directory
70-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
79+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
7180

72-
# Test
7381
result = check_is_initialized(print_warning_if_not=False)
7482

75-
# Verify
7683
assert result is False
7784

7885
def test_check_is_initialized_returns_false_when_all_files_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
79-
"""Test that check_is_initialized returns False when both required files are missing."""
80-
# Setup test directories - no files exist
81-
config_dir = tmp_path / ".pipelex" / "inference"
82-
config_dir.mkdir(parents=True)
83-
backends_file = config_dir / "backends.toml"
84-
routing_file = config_dir / "routing_profiles.toml"
86+
"""Test that check_is_initialized returns False when all required files are missing."""
87+
config_dir = self._setup_config_dir(tmp_path, config_files=False)
88+
inference_dir = config_dir / "inference"
89+
backends_file = inference_dir / "backends.toml"
90+
routing_file = inference_dir / "routing_profiles.toml"
8591

86-
# Mock config_manager to point to temp directory
87-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
92+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
8893

89-
# Test
9094
result = check_is_initialized(print_warning_if_not=False)
9195

92-
# Verify
9396
assert result is False
9497

9598
def test_check_is_initialized_prints_warning_when_not_initialized(self, tmp_path: Path, mocker: MockerFixture) -> None:
9699
"""Test that check_is_initialized prints warning and returns False when not initialized and print_warning_if_not is True."""
97-
# Setup test directories - no files exist
98-
config_dir = tmp_path / ".pipelex" / "inference"
99-
config_dir.mkdir(parents=True)
100-
backends_file = config_dir / "backends.toml"
101-
routing_file = config_dir / "routing_profiles.toml"
100+
config_dir = self._setup_config_dir(tmp_path, config_files=False)
101+
inference_dir = config_dir / "inference"
102+
backends_file = inference_dir / "backends.toml"
103+
routing_file = inference_dir / "routing_profiles.toml"
102104

103-
# Mock config_manager to point to temp directory
104-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
105+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
105106

106-
# Mock console.print to suppress output during test
107107
mock_console = mocker.MagicMock()
108108
mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mock_console)
109109

110-
# Test - should print warning and return False
111110
result = check_is_initialized(print_warning_if_not=True)
112111

113-
# Verify
114112
assert result is False
115113
assert mock_console.print.called
116114

117115
def test_check_is_initialized_returns_true_when_initialized_with_print_warning(self, tmp_path: Path, mocker: MockerFixture) -> None:
118116
"""Test that check_is_initialized returns True when initialized with print_warning_if_not=True."""
119-
# Setup test directories - all files exist
120-
config_dir = tmp_path / ".pipelex" / "inference"
121-
config_dir.mkdir(parents=True)
122-
backends_file = config_dir / "backends.toml"
123-
routing_file = config_dir / "routing_profiles.toml"
117+
config_dir = self._setup_config_dir(tmp_path, config_files=True)
118+
inference_dir = config_dir / "inference"
119+
backends_file = inference_dir / "backends.toml"
120+
routing_file = inference_dir / "routing_profiles.toml"
124121
backends_file.write_text("[backends]\nconfig = 'value'")
125122
routing_file.write_text("[routing]\nconfig = 'value'")
126123

127-
# Mock config_manager to point to temp directory
128-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
124+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
129125

130-
# Test
131126
result = check_is_initialized(print_warning_if_not=True)
132127

133-
# Verify
134128
assert result is True
135129

136130
def test_check_is_initialized_returns_false_with_only_backends_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
137131
"""Test that check_is_initialized returns False when only backends file is missing."""
138-
# Setup test directories - only routing file exists
139-
config_dir = tmp_path / ".pipelex" / "inference"
140-
config_dir.mkdir(parents=True)
141-
backends_file = config_dir / "backends.toml"
142-
routing_file = config_dir / "routing_profiles.toml"
132+
config_dir = self._setup_config_dir(tmp_path, config_files=True)
133+
inference_dir = config_dir / "inference"
134+
backends_file = inference_dir / "backends.toml"
135+
routing_file = inference_dir / "routing_profiles.toml"
143136
routing_file.write_text("[routing]\nconfig = 'value'")
144137

145-
# Mock config_manager to point to temp directory
146-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
147-
148-
# Mock console.print to suppress output during test
138+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
149139
mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mocker.MagicMock())
150140

151-
# Test
152141
result = check_is_initialized(print_warning_if_not=True)
153142

154-
# Verify
155143
assert result is False
156144

157145
def test_check_is_initialized_returns_false_with_only_routing_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
158146
"""Test that check_is_initialized returns False when only routing file is missing."""
159-
# Setup test directories - only backends file exists
160-
config_dir = tmp_path / ".pipelex" / "inference"
161-
config_dir.mkdir(parents=True)
162-
backends_file = config_dir / "backends.toml"
163-
routing_file = config_dir / "routing_profiles.toml"
147+
config_dir = self._setup_config_dir(tmp_path, config_files=True)
148+
inference_dir = config_dir / "inference"
149+
backends_file = inference_dir / "backends.toml"
150+
routing_file = inference_dir / "routing_profiles.toml"
164151
backends_file.write_text("[backends]\nconfig = 'value'")
165152

166-
# Mock config_manager to point to temp directory
167-
self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
168-
169-
# Mock console.print to suppress output during test
153+
self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
170154
mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mocker.MagicMock())
171155

172-
# Test
173156
result = check_is_initialized(print_warning_if_not=True)
174157

175-
# Verify
176158
assert result is False

0 commit comments

Comments
 (0)