-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcheck_dependency_floors_test.py
More file actions
428 lines (349 loc) · 20.6 KB
/
Copy pathcheck_dependency_floors_test.py
File metadata and controls
428 lines (349 loc) · 20.6 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# Copyright 2025-2026 mrveiss
# SPDX-License-Identifier: Apache-2.0
"""Unit tests for the declared-dependency-floor reporter (#15091).
Every case builds its own requirement tree and supplies its own installed-version
mapping. Nothing here reads the ambient interpreter: a test that asserted against
the real environment would pass on a box that is below floor and fail on one that
is not, which is the exact defect the subject exists to report.
"""
from __future__ import annotations
import check_dependency_floors as checker
import pytest
def _write(root, relative, body):
path = root / relative
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(body, encoding="utf-8")
return path
def _declaration(name="fastapi", operator=">=", required="0.141.1", source="r.txt:1"):
return checker.Declaration(source=source, name=name, operator=operator, required=required)
class TestSatisfies:
@pytest.mark.parametrize(
"installed,operator,required,expected",
[
("0.141.1", ">=", "0.141.1", True),
("0.141.2", ">=", "0.141.1", True),
("0.135.2", ">=", "0.141.1", False),
# 0.9 vs 0.10 is the comparison a string sort gets backwards.
("0.9.0", ">=", "0.10.0", False),
# Unequal segment counts must pad, not rank by length.
("1.6", ">=", "1.6.0", True),
("1.6.0", ">=", "1.6", True),
("1.6.1", ">=", "1.6", True),
# A pre-release is below the release it precedes.
("1.6.0rc1", ">=", "1.6.0", False),
("1.6.0", ">=", "1.6.0rc1", True),
("0.141.1", "==", "0.141.1", True),
("0.141.2", "==", "0.141.1", False),
("0.141.1", ">", "0.141.1", False),
("0.141.2", ">", "0.141.1", True),
("2.1.3", "~=", "2.1.0", True),
],
)
def test_ordering(self, installed, operator, required, expected):
assert checker.satisfies(installed, operator, required) is expected
def test_unparsable_installed_version_is_reported_not_accepted(self):
"""An unreadable version must not be allowed to pass as satisfying."""
assert checker.satisfies("not-a-version", ">=", "1.0.0") is False
class TestCanonicalName:
@pytest.mark.parametrize(
"raw,expected",
[
# Case folding alone -- PEP 503 introduces no separator.
("PyJWT", "pyjwt"),
("pyjwt", "pyjwt"),
# Every separator spelling collapses to a single hyphen.
("Py_JWT", "py-jwt"),
("py.jwt", "py-jwt"),
("PY-JWT", "py-jwt"),
("typing_extensions", "typing-extensions"),
("ruamel.yaml", "ruamel-yaml"),
("a__b", "a-b"),
],
)
def test_folds_to_one_key(self, raw, expected):
assert checker.canonical(raw) == expected
class TestParseDeclarations:
def test_captures_name_operator_version_and_position(self, tmp_path):
path = _write(tmp_path, "requirements.txt", "# a comment\n\nfastapi>=0.141.1 # SECURITY\n")
[declaration] = checker.parse_declarations([path], tmp_path)
assert declaration.name == "fastapi"
assert declaration.operator == ">="
assert declaration.required == "0.141.1"
assert declaration.source == "requirements.txt:3"
def test_include_and_option_lines_are_not_requirements(self, tmp_path):
path = _write(tmp_path, "r.txt", "-r other.txt\n--index-url https://example.invalid\n-e ./pkg\n")
assert checker.parse_declarations([path], tmp_path) == []
def test_first_specifier_of_a_capped_range_is_the_floor(self, tmp_path):
path = _write(tmp_path, "r.txt", "websockets>=15.0.1,<16\n")
[declaration] = checker.parse_declarations([path], tmp_path)
assert (declaration.operator, declaration.required) == (">=", "15.0.1")
def test_extras_do_not_break_the_name(self, tmp_path):
path = _write(tmp_path, "r.txt", "uvicorn[standard]>=0.52.4\n")
[declaration] = checker.parse_declarations([path], tmp_path)
assert declaration.name == "uvicorn"
class TestDeclarationFiles:
def test_follows_include_graph_transitively(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("top.txt",))
_write(tmp_path, "top.txt", "-r nested/mid.txt\n")
_write(tmp_path, "nested/mid.txt", "--requirement leaf.txt\n")
_write(tmp_path, "nested/leaf.txt", "fastapi>=0.141.1\n")
names = [path.name for path in checker.declaration_files(tmp_path)]
assert names == ["top.txt", "mid.txt", "leaf.txt"]
def test_absent_root_is_skipped_without_error(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("gone.txt", "here.txt"))
_write(tmp_path, "here.txt", "fastapi>=0.141.1\n")
assert [path.name for path in checker.declaration_files(tmp_path)] == ["here.txt"]
def test_include_cycle_terminates(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("a.txt",))
_write(tmp_path, "a.txt", "-r b.txt\n")
_write(tmp_path, "b.txt", "-r a.txt\n")
assert [path.name for path in checker.declaration_files(tmp_path)] == ["a.txt", "b.txt"]
class TestShortfalls:
def test_below_floor_is_reported_with_the_installed_version(self):
declaration = _declaration()
[shortfall] = checker.shortfalls([declaration], {"fastapi": "0.135.2"})
assert shortfall.installed == "0.135.2"
assert shortfall.declaration is declaration
def test_at_or_above_floor_is_silent(self):
assert checker.shortfalls([_declaration()], {"fastapi": "0.141.1"}) == []
def test_absent_distribution_is_not_a_shortfall(self):
"""Not installed says nothing about the version CI would resolve."""
assert checker.shortfalls([_declaration()], {}) == []
class TestIsExempt:
"""#16264: KNOWN_CROSS_VENV_EXEMPTIONS matches by (package, file), not by line."""
def test_exempted_pair_is_exempt(self):
declaration = checker.Declaration(
source="autobot-slm-backend/requirements.txt:37", name="websockets", operator=">=", required="17.1"
)
assert checker.is_exempt(checker.Shortfall(declaration, "15.0.1")) is True
def test_same_package_different_file_is_not_exempt(self):
declaration = checker.Declaration(
source="autobot-backend/requirements.txt:12", name="websockets", operator=">=", required="17.1"
)
assert checker.is_exempt(checker.Shortfall(declaration, "15.0.1")) is False
def test_different_package_same_file_is_not_exempt(self):
declaration = checker.Declaration(
source="autobot-slm-backend/requirements.txt:1", name="fastapi", operator=">=", required="0.141.1"
)
assert checker.is_exempt(checker.Shortfall(declaration, "0.135.2")) is False
def test_line_number_does_not_matter(self):
declaration = checker.Declaration(
source="autobot-slm-backend/requirements.txt:999", name="websockets", operator=">=", required="17.1"
)
assert checker.is_exempt(checker.Shortfall(declaration, "15.0.1")) is True
class TestAuditRefusesAnEmptyEnumeration:
def test_empty_tree_raises_instead_of_reporting_clean(self, tmp_path, monkeypatch):
"""#15087: a check that asserts over an enumeration must fail when it is empty.
Reporting "0 declarations checked, all satisfied" from a tree with no
requirements files is indistinguishable from a healthy environment, and
would make every caller silently stop checking.
"""
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("absent.txt",))
with pytest.raises(checker.EmptyEnumerationError):
checker.audit(tmp_path)
def test_requirement_files_that_declare_nothing_also_raise(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
_write(tmp_path, "r.txt", "# nothing but a comment\n")
with pytest.raises(checker.EmptyEnumerationError):
checker.audit(tmp_path)
def test_a_populated_tree_does_not_raise(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
_write(tmp_path, "r.txt", "fastapi>=0.141.1\n")
found, examined = checker.audit(tmp_path)
assert examined == 1
assert isinstance(found, list)
class TestRender:
def test_names_both_versions_and_the_remedy(self):
"""The acceptance criterion: the report must name installed AND declared."""
report = "\n".join(checker.render([checker.Shortfall(_declaration(), "0.135.2")], 206))
assert "0.135.2" in report
assert "0.141.1" in report
assert "fastapi" in report
assert "scripts/setup-ci-parity-env.sh" in report
def test_clean_environment_says_how_many_were_checked(self):
[line] = checker.render([], 206)
assert "206" in line and "all satisfied" in line
def test_detail_is_capped_and_the_remainder_counted(self):
found = [checker.Shortfall(_declaration(name=f"pkg{i}"), "0.1") for i in range(25)]
report = "\n".join(checker.render(found, 206, limit=10))
assert "pkg0" in report
assert "pkg24" not in report
assert "15 more" in report
def test_default_points_at_ci_as_a_different_environment(self):
"""#16264: off CI, the report describes some OTHER interpreter than CI's."""
report = "\n".join(checker.render([checker.Shortfall(_declaration(), "0.135.2")], 206))
assert "carries no information about CI" in report
assert "CI job's own environment" not in report
def test_in_ci_names_the_running_environment_as_ci_itself(self):
"""#16264: printed FROM CI, the interpreter making the report IS CI's own."""
report = "\n".join(checker.render([checker.Shortfall(_declaration(), "0.135.2")], 206, in_ci=True))
assert "CI job's own environment" in report
assert "carries no information about CI" not in report
class TestMainExitCodes:
def test_reporting_run_exits_zero_even_when_below_floor(self, tmp_path, monkeypatch, capsys):
"""Warn, do not gate: a below-floor box stays usable for ordinary work."""
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.135.2"})
_write(tmp_path, "r.txt", "fastapi>=0.141.1\n")
assert checker.main(["--root", str(tmp_path)]) == 0
assert "0.135.2" in capsys.readouterr().out
def test_strict_run_exits_one_when_below_floor(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.135.2"})
_write(tmp_path, "r.txt", "fastapi>=0.141.1\n")
assert checker.main(["--root", str(tmp_path), "--strict"]) == 1
def test_main_names_ci_as_itself_when_the_ci_env_var_is_set(self, tmp_path, monkeypatch, capsys):
"""#16264: this is the wording a CI job's own log actually prints."""
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.135.2"})
monkeypatch.setenv("CI", "true")
_write(tmp_path, "r.txt", "fastapi>=0.141.1\n")
checker.main(["--root", str(tmp_path), "--strict"])
assert "CI job's own environment" in capsys.readouterr().out
def test_main_omits_the_ci_wording_when_the_ci_env_var_is_absent(self, tmp_path, monkeypatch, capsys):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.135.2"})
monkeypatch.delenv("CI", raising=False)
_write(tmp_path, "r.txt", "fastapi>=0.141.1\n")
checker.main(["--root", str(tmp_path)])
assert "carries no information about CI" in capsys.readouterr().out
def test_strict_run_exits_zero_when_satisfied(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.141.1"})
_write(tmp_path, "r.txt", "fastapi>=0.141.1\n")
assert checker.main(["--root", str(tmp_path), "--strict"]) == 0
def test_empty_enumeration_exits_two_and_says_so(self, tmp_path, monkeypatch, capsys):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("absent.txt",))
assert checker.main(["--root", str(tmp_path)]) == 2
assert "no version declarations found" in capsys.readouterr().err
class TestKnownCrossVenvExemptions:
"""#16264: the SLM/backend websockets floor mismatch is a named exception, not a silent one."""
def test_exemption_set_is_exactly_the_expected_pair(self):
assert set(checker.KNOWN_CROSS_VENV_EXEMPTIONS) == {
("websockets", "autobot-slm-backend/requirements.txt"),
}
def test_exempted_pair_does_not_fail_strict(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("autobot-slm-backend/requirements.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"websockets": "15.0.1"})
_write(tmp_path, "autobot-slm-backend/requirements.txt", "websockets>=17.1,<18\n")
assert checker.main(["--root", str(tmp_path), "--strict"]) == 0
def test_non_exempted_below_floor_pin_still_fails(self, tmp_path, monkeypatch):
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"websockets": "15.0.1"})
_write(tmp_path, "r.txt", "websockets>=17.1,<18\n")
assert checker.main(["--root", str(tmp_path), "--strict"]) == 1
class TestScopedRoots:
"""``--roots`` narrows the sweep to what one caller actually installs (#15130).
``scripts/setup-ci-parity-env.sh`` installs two of the four entry points.
Judged against all four it looks permanently below floor, because the
backend declarations describe packages neither it nor CI's python suite
ever installs — so the number it reports has to be the number it can act on.
"""
def _tree(self, tmp_path):
_write(tmp_path, "installed.txt", "fastapi>=0.141.1\n")
_write(tmp_path, "never-installed.txt", "sqlalchemy>=2.0.52\n")
def test_a_narrowed_sweep_reads_only_the_named_files(self, tmp_path, monkeypatch):
self._tree(tmp_path)
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.141.1"})
found, examined = checker.audit(tmp_path, ("installed.txt",))
assert examined == 1
assert found == []
def test_the_same_tree_unscoped_still_reads_every_root(self, tmp_path, monkeypatch):
"""The default must not change: this option is additive, not a redefinition."""
self._tree(tmp_path)
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("installed.txt", "never-installed.txt"))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.141.1"})
_, examined = checker.audit(tmp_path)
assert examined == 2
def test_scoping_out_the_file_that_holds_the_shortfall_clears_it(self, tmp_path, monkeypatch):
"""The whole point: a shortfall you never installed is not your drift."""
self._tree(tmp_path)
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.141.1", "sqlalchemy": "2.0.51"})
wide, _ = checker.audit(tmp_path, ("installed.txt", "never-installed.txt"))
narrow, _ = checker.audit(tmp_path, ("installed.txt",))
assert [shortfall.declaration.name for shortfall in wide] == ["sqlalchemy"]
assert narrow == []
def test_include_graph_is_still_followed_from_a_narrowed_root(self, tmp_path, monkeypatch):
_write(tmp_path, "top.txt", "-r child.txt\nfastapi>=0.141.1\n")
_write(tmp_path, "child.txt", "starlette>=1.6.0\n")
monkeypatch.setattr(checker, "installed_versions", lambda names: {})
_, examined = checker.audit(tmp_path, ("top.txt",))
assert examined == 2
def test_a_narrowed_sweep_that_reads_nothing_still_raises(self, tmp_path):
"""Narrowing must not become a way to reach a vacuous clean report."""
self._tree(tmp_path)
with pytest.raises(checker.EmptyEnumerationError):
checker.audit(tmp_path, ("absent.txt",))
def test_cli_passes_the_narrowed_roots_through(self, tmp_path, monkeypatch):
self._tree(tmp_path)
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.141.1", "sqlalchemy": "2.0.51"})
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("installed.txt", "never-installed.txt"))
assert checker.main(["--root", str(tmp_path), "--strict"]) == 1
assert checker.main(["--root", str(tmp_path), "--strict", "--roots", "installed.txt"]) == 0
def test_cli_refuses_an_empty_roots_list(self, tmp_path, capsys):
"""An empty enumeration exits 2, never 0 — there was nothing to check."""
assert checker.main(["--root", str(tmp_path), "--roots"]) == 2
assert "nothing to check" in capsys.readouterr().err
class TestRequirePresent:
"""Absence is a shortfall only where the caller installed the file (#15130).
The default stays "absent says nothing" — an arbitrary box not having a
package is not evidence about CI. The parity venv is the exception: it
installs these files, so a declaration with nothing against it means the
install did not take, and the consequence is a silently smaller test run.
"""
DECLARATIONS = (
("fastapi", ">=", "0.141.1"),
("docker", ">=", "7.1.0"),
)
def _declarations(self):
assert self.DECLARATIONS, "no declarations to check — the cases below would be vacuous"
return [
_declaration(name=n, operator=o, required=r, source=f"r.txt:{i}")
for i, (n, o, r) in enumerate(self.DECLARATIONS, 1)
]
def test_absent_is_ignored_by_default(self):
found = checker.shortfalls(self._declarations(), {"fastapi": "0.141.1"})
assert found == []
def test_absent_is_reported_when_required_present(self):
found = checker.shortfalls(self._declarations(), {"fastapi": "0.141.1"}, require_present=True)
assert [shortfall.declaration.name for shortfall in found] == ["docker"]
assert found[0].installed == checker.ABSENT
def test_an_absent_package_reads_as_not_installed_not_as_a_version(self):
found = checker.shortfalls(self._declarations(), {"fastapi": "0.141.1"}, require_present=True)
described = found[0].describe()
assert "NOT INSTALLED" in described
assert ">=7.1.0" in described
assert checker.ABSENT not in described
def test_a_below_floor_package_is_still_reported_with_its_version(self):
"""Requiring presence must not swallow the ordinary case."""
found = checker.shortfalls(
self._declarations(), {"fastapi": "0.135.2", "docker": "7.1.0"}, require_present=True
)
assert [shortfall.installed for shortfall in found] == ["0.135.2"]
def test_cli_flag_changes_the_exit_code(self, tmp_path, monkeypatch):
_write(tmp_path, "r.txt", "fastapi>=0.141.1\ndocker>=7.1.0\n")
monkeypatch.setattr(checker, "DECLARATION_ROOTS", ("r.txt",))
monkeypatch.setattr(checker, "installed_versions", lambda names: {"fastapi": "0.141.1"})
assert checker.main(["--root", str(tmp_path), "--strict"]) == 0
assert checker.main(["--root", str(tmp_path), "--strict", "--require-present"]) == 1
class TestLocalVersionSegments:
"""A ``+local`` build satisfies the release it is built from (#15130).
``setup-ci-parity-env.sh`` installs torch from the PyTorch CPU index on
purpose, which yields ``2.13.0+cpu``. Reading that as a pre-release made it
a permanent, false shortfall against its own ``==2.13.0`` pin.
"""
@pytest.mark.parametrize(
"installed,operator,required,expected",
[
("2.13.0+cpu", "==", "2.13.0", True),
("2.13.0+cpu", ">=", "2.13.0", True),
("2.13.0+cpu", ">=", "2.12.0", True),
("2.13.0+cpu", ">=", "2.14.0", False),
("2.13.0+cpu", ">", "2.13.0", False),
# The pre-release rule is untouched by the local-segment handling.
("1.6.0rc1", ">=", "1.6.0", False),
("1.6.0rc1+local", ">=", "1.6.0", False),
],
)
def test_local_segment_does_not_demote_the_release(self, installed, operator, required, expected):
assert checker.satisfies(installed, operator, required) is expected