|
| 1 | +"""#185: `bx compare` warns when both revisions imported the project from the same file.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import logging |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from benchx import session |
| 9 | +from benchx.results import MemoryStore |
| 10 | + |
| 11 | +NUMPY = {"name": "numpy", "version": "2.3.0", "path": "/env/numpy/__init__.py"} |
| 12 | + |
| 13 | + |
| 14 | +def _side(tree): |
| 15 | + return {"identity": {"tree": tree, "dirty": "clean"}, "source": f"/src/{tree}", "benchmark": None} |
| 16 | + |
| 17 | + |
| 18 | +def _result(tmp_path, name, installed): |
| 19 | + path = tmp_path / f"{name}.json" |
| 20 | + path.write_text(json.dumps({"provenance": {"info": {"asv": {"installed": installed}}}})) |
| 21 | + return path |
| 22 | + |
| 23 | + |
| 24 | +def test_installed_comes_from_the_first_result_that_records_it(tmp_path): |
| 25 | + assert session._installed([_result(tmp_path, "a", {}), _result(tmp_path, "b", NUMPY)]) == NUMPY |
| 26 | + assert session._installed([_result(tmp_path, "c", {"name": "numpy"})]) is None |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize( |
| 30 | + ("trees", "other", "warned"), |
| 31 | + [ |
| 32 | + (("t1", "t2"), NUMPY, True), |
| 33 | + (("t1", "t2"), {**NUMPY, "path": "/other/numpy/__init__.py"}, False), |
| 34 | + (("t1", "t2"), {**NUMPY, "version": "2.4.0"}, False), |
| 35 | + (("t1", "t2"), None, False), # not asv: nothing recorded |
| 36 | + (("t1", "t1"), NUMPY, False), # A/A: one build on purpose |
| 37 | + ], |
| 38 | +) |
| 39 | +def test_same_build_warning(trees, other, warned, caplog): |
| 40 | + caplog.set_level(logging.WARNING, logger="benchx") |
| 41 | + session._warn_same_build([_side(t) for t in trees], [NUMPY, other]) |
| 42 | + assert bool(caplog.records) is warned |
| 43 | + if warned: |
| 44 | + assert NUMPY["path"] in caplog.records[0].getMessage() |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("profile", ["revisions", "environments"]) |
| 48 | +def test_compare_targets_checks_revisions_once(profile, tmp_path, monkeypatch, caplog): |
| 49 | + caplog.set_level(logging.WARNING, logger="benchx") |
| 50 | + sides = iter([_side("t1"), _side("t2")]) |
| 51 | + result = _result(tmp_path, "r", NUMPY) |
| 52 | + monkeypatch.setattr(session, "_side", lambda *a, **k: next(sides)) |
| 53 | + monkeypatch.setattr(session, "_source_uri", lambda *a: "uri") |
| 54 | + outcome = ({"files": ["order", result], "results": 1}, {"rejected": []}) |
| 55 | + monkeypatch.setattr(session, "_run_side", lambda *a, **k: outcome) |
| 56 | + monkeypatch.setattr(session.compare_mod, "compare", lambda *a, **k: {}) |
| 57 | + session.compare_targets( |
| 58 | + "A", |
| 59 | + "B", |
| 60 | + profile=profile, |
| 61 | + suite="s", |
| 62 | + rounds=3, |
| 63 | + adapter="asv", |
| 64 | + label=("env", "x", "y") if profile == "environments" else None, |
| 65 | + store=MemoryStore(), |
| 66 | + out=tmp_path / "out", |
| 67 | + ) |
| 68 | + warned = [r for r in caplog.records if "both sides imported" in r.getMessage()] |
| 69 | + assert len(warned) == (profile == "revisions") # environments may share an install by design |
0 commit comments