-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_cli_local_test.py
More file actions
285 lines (205 loc) · 9.93 KB
/
test_cli_local_test.py
File metadata and controls
285 lines (205 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os
from types import SimpleNamespace
import pytest
def test_local_test_runs_host_pytest_with_entry(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
# Create a dummy test file
test_file = project / "metric" / "test_one.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
# Import module under test
from eval_protocol.cli_commands import local_test as lt
# Avoid Docker path
monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [])
captured = {"target": ""}
def _fake_host(target: str) -> int:
captured["target"] = target
return 0
monkeypatch.setattr(lt, "_run_pytest_host", _fake_host)
args = SimpleNamespace(entry=str(test_file), ignore_docker=False, yes=True)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 0
# Expect relative path target
assert captured["target"] == os.path.relpath(str(test_file), str(project))
def test_local_test_ignores_docker_when_flag_set(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
test_file = project / "metric" / "test_two.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
# Pretend we have Dockerfile(s), but ignore_docker=True should skip
monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile")])
called = {"host": False}
def _fake_host(target: str) -> int:
called["host"] = True
return 0
monkeypatch.setattr(lt, "_run_pytest_host", _fake_host)
args = SimpleNamespace(entry=str(test_file), ignore_docker=True, yes=True)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 0
assert called["host"] is True
def test_local_test_errors_on_multiple_dockerfiles(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
test_file = project / "metric" / "test_three.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
monkeypatch.setattr(
lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile"), str(project / "another" / "Dockerfile")]
)
args = SimpleNamespace(entry=str(test_file), ignore_docker=False, yes=True)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 1
def test_local_test_builds_and_runs_in_docker(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
test_file = project / "metric" / "test_four.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile")])
monkeypatch.setattr(lt, "_build_docker_image", lambda dockerfile, tag, build_extras=None: True)
captured = {"target": "", "image": ""}
def _fake_run_docker(root: str, image_tag: str, pytest_target: str, run_extras=None) -> int:
captured["target"] = pytest_target
captured["image"] = image_tag
return 0
monkeypatch.setattr(lt, "_run_pytest_in_docker", _fake_run_docker)
args = SimpleNamespace(entry=str(test_file), ignore_docker=False, yes=True)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 0
assert captured["image"] == "ep-evaluator:local"
assert captured["target"] == os.path.relpath(str(test_file), str(project))
def test_local_test_selector_single_test(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
test_file = project / "metric" / "test_sel.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
# No entry; force discover + selector
disc = SimpleNamespace(qualname="metric.test_sel", file_path=str(test_file))
monkeypatch.setattr(lt, "_discover_and_select_tests", lambda cwd, non_interactive=False: [disc])
monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [])
called = {"host": False}
def _fake_host(target: str) -> int:
called["host"] = True
return 0
monkeypatch.setattr(lt, "_run_pytest_host", _fake_host)
args = SimpleNamespace(entry=None, ignore_docker=False, yes=True)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 0
assert called["host"] is True
def test_local_test_passes_docker_build_extra(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
test_file = project / "metric" / "test_build_extra.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile")])
captured = {"extras": None}
def _fake_build(dockerfile, tag, build_extras=None):
captured["extras"] = build_extras
return True
def _fake_run_docker(root: str, image_tag: str, pytest_target: str, run_extras=None) -> int:
return 0
monkeypatch.setattr(lt, "_build_docker_image", _fake_build)
monkeypatch.setattr(lt, "_run_pytest_in_docker", _fake_run_docker)
# Extras string with multiple flags and equals-arg
args = SimpleNamespace(
entry=str(test_file),
ignore_docker=False,
yes=True,
docker_build_extra="--no-cache --pull --progress=plain --build-arg KEY=VAL",
docker_run_extra="",
)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 0
# Expect split list preserving tokens order
assert captured["extras"] == ["--no-cache", "--pull", "--progress=plain", "--build-arg", "KEY=VAL"]
def test_local_test_passes_docker_run_extra(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
test_file = project / "metric" / "test_run_extra.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile")])
monkeypatch.setattr(lt, "_build_docker_image", lambda dockerfile, tag, build_extras=None: True)
captured = {"extras": None}
def _fake_run_docker(root: str, image_tag: str, pytest_target: str, run_extras=None) -> int:
captured["extras"] = run_extras
return 0
monkeypatch.setattr(lt, "_run_pytest_in_docker", _fake_run_docker)
args = SimpleNamespace(
entry=str(test_file),
ignore_docker=False,
yes=True,
docker_build_extra="",
docker_run_extra="--env-file .env --memory=8g --cpus=2 --add-host=host.docker.internal:host-gateway",
)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 0
assert captured["extras"] == [
"--env-file",
".env",
"--memory=8g",
"--cpus=2",
"--add-host=host.docker.internal:host-gateway",
]
def test_local_test_normalizes_entry_with_selector(tmp_path, monkeypatch):
project = tmp_path / "proj"
project.mkdir()
monkeypatch.chdir(project)
# Create a dummy test file
test_file = project / "metric" / "test_sel_abs.py"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8")
abs_entry = f"{str(test_file)}::test_dummy"
from eval_protocol.cli_commands import local_test as lt
# Avoid Docker path
monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [])
captured = {"target": ""}
def _fake_host(target: str) -> int:
captured["target"] = target
return 0
monkeypatch.setattr(lt, "_run_pytest_host", _fake_host)
args = SimpleNamespace(entry=abs_entry, ignore_docker=False, yes=True)
rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType]
assert rc == 0
# Expect project-relative path plus selector
rel = os.path.relpath(str(test_file), str(project))
assert captured["target"] == f"{rel}::test_dummy"
def test_find_dockerfiles_top_level_only(tmp_path, monkeypatch):
# Project root with both top-level and nested Dockerfiles – only top-level should be returned.
project = tmp_path / "proj"
project.mkdir()
top_level = project / "Dockerfile"
top_level.write_text("FROM python:3.11-slim\n", encoding="utf-8")
nested_dir = project / "nested"
nested_dir.mkdir()
(nested_dir / "Dockerfile").write_text("FROM python:3.11-slim\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
dockerfiles = lt._find_dockerfiles(str(project))
assert dockerfiles == [str(top_level)]
def test_find_dockerfiles_ignores_nested_only(tmp_path, monkeypatch):
# Project root without top-level Dockerfile but with nested ones – should return empty.
project = tmp_path / "proj"
project.mkdir()
nested_dir = project / "nested"
nested_dir.mkdir()
(nested_dir / "Dockerfile").write_text("FROM python:3.11-slim\n", encoding="utf-8")
from eval_protocol.cli_commands import local_test as lt
dockerfiles = lt._find_dockerfiles(str(project))
assert dockerfiles == []