|
| 1 | +"""Regression tests for fep-report provenance extraction. |
| 2 | +
|
| 3 | +The shell wrapper scripts evolved over time in where they tee the |
| 4 | +`git commit:` line — earlier versions sent it to stdout only, then |
| 5 | +into run.log, then into env.log. `cellsim fep-report` has to find |
| 6 | +it wherever it landed, because a biologist handed a tarball from |
| 7 | +someone else's machine cannot re-run git commands: the commit hash |
| 8 | +IS the provenance. |
| 9 | +
|
| 10 | +Pins: _parse_run_log scans run.log, env.log, and doctor.log and |
| 11 | +returns the first git_commit / platform it finds. |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import sys |
| 16 | +import tempfile |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +REPO = Path(__file__).resolve().parents[2] |
| 20 | +sys.path.insert(0, str(REPO)) |
| 21 | + |
| 22 | +from src.fep.report import ( |
| 23 | + _parse_run_log, |
| 24 | + _scan_sibling_logs_for_provenance, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def test_git_commit_picked_up_from_run_log(): |
| 29 | + with tempfile.TemporaryDirectory(prefix="fep_prov_") as tmp: |
| 30 | + d = Path(tmp) |
| 31 | + (d / "run.log").write_text( |
| 32 | + "CellSim\n" |
| 33 | + " git commit: abc1234567890abcdef\n" |
| 34 | + "FEP sampling platform: CUDA\n" |
| 35 | + "[freesolv] methane C\n") |
| 36 | + meta = _parse_run_log(d / "run.log", rows=[]) |
| 37 | + assert meta["git_commit"] == "abc1234567890abcdef", meta |
| 38 | + assert meta["platform"] == "CUDA", meta |
| 39 | + |
| 40 | + |
| 41 | +def test_git_commit_fallback_to_env_log(): |
| 42 | + """Old scripts sent the header to stdout and env.log. run.log |
| 43 | + contains only bench output. fep-report must still populate |
| 44 | + git_commit by falling back to env.log.""" |
| 45 | + with tempfile.TemporaryDirectory(prefix="fep_prov_") as tmp: |
| 46 | + d = Path(tmp) |
| 47 | + (d / "env.log").write_text( |
| 48 | + "CellSim — FreeSolv FEP gate\n" |
| 49 | + " git commit: deadbeefcafe0000111122223333\n" |
| 50 | + " openmm 8.2.0\n") |
| 51 | + (d / "run.log").write_text( |
| 52 | + "[freesolv] methane C\n" |
| 53 | + "...bench output without provenance...\n" |
| 54 | + "FEP sampling platform: Metal\n") |
| 55 | + meta = _parse_run_log(d / "run.log", rows=[]) |
| 56 | + assert meta["git_commit"] == "deadbeefcafe0000111122223333", meta |
| 57 | + assert meta["platform"] == "Metal", meta |
| 58 | + |
| 59 | + |
| 60 | +def test_git_commit_fallback_when_no_run_log(): |
| 61 | + """Malformed tarball (no run.log) — only env.log has provenance. |
| 62 | + Parser must still recover git_commit via sibling scan.""" |
| 63 | + with tempfile.TemporaryDirectory(prefix="fep_prov_") as tmp: |
| 64 | + d = Path(tmp) |
| 65 | + (d / "env.log").write_text( |
| 66 | + " git commit: 1111222233334444aaaabbbbccccdddd\n") |
| 67 | + # Intentionally no run.log. |
| 68 | + meta = _parse_run_log(d / "run.log", rows=[]) |
| 69 | + assert meta["git_commit"] == "1111222233334444aaaabbbbccccdddd", meta |
| 70 | + |
| 71 | + |
| 72 | +def test_doctor_log_as_last_resort(): |
| 73 | + """Some runs only landed provenance in doctor.log.""" |
| 74 | + with tempfile.TemporaryDirectory(prefix="fep_prov_") as tmp: |
| 75 | + d = Path(tmp) |
| 76 | + (d / "doctor.log").write_text( |
| 77 | + "cellsim doctor\n" |
| 78 | + "git commit: feedface99887766\n" |
| 79 | + "OK\n") |
| 80 | + meta = _parse_run_log(d / "run.log", rows=[]) |
| 81 | + assert meta["git_commit"] == "feedface99887766", meta |
| 82 | + |
| 83 | + |
| 84 | +def test_no_logs_at_all_returns_none(): |
| 85 | + """Nothing to find — git_commit stays None, doesn't crash.""" |
| 86 | + with tempfile.TemporaryDirectory(prefix="fep_prov_") as tmp: |
| 87 | + d = Path(tmp) |
| 88 | + meta = _parse_run_log(d / "run.log", rows=[]) |
| 89 | + assert meta["git_commit"] is None |
| 90 | + assert meta["platform"] is None |
| 91 | + |
| 92 | + |
| 93 | +def test_run_log_wins_over_sibling(): |
| 94 | + """When both run.log and env.log have git commit, run.log wins |
| 95 | + (most local to the bench process).""" |
| 96 | + with tempfile.TemporaryDirectory(prefix="fep_prov_") as tmp: |
| 97 | + d = Path(tmp) |
| 98 | + (d / "run.log").write_text( |
| 99 | + "git commit: 0000000aaaaaaaabbbbbbbbbbcccccc\n") |
| 100 | + (d / "env.log").write_text( |
| 101 | + "git commit: ffffffffffffffffffffffffffffffff\n") |
| 102 | + meta = _parse_run_log(d / "run.log", rows=[]) |
| 103 | + assert meta["git_commit"] == "0000000aaaaaaaabbbbbbbbbbcccccc", meta |
| 104 | + |
| 105 | + |
| 106 | +def test_sibling_scan_direct_helper(): |
| 107 | + """_scan_sibling_logs_for_provenance is a public-ish helper; |
| 108 | + cover it directly so the fallback policy (env > doctor > header) |
| 109 | + is pinned.""" |
| 110 | + with tempfile.TemporaryDirectory(prefix="fep_prov_") as tmp: |
| 111 | + d = Path(tmp) |
| 112 | + (d / "env.log").write_text("git commit: aaaaaaaaaaaaaaaa\n") |
| 113 | + (d / "doctor.log").write_text("git commit: bbbbbbbbbbbbbbbb\n") |
| 114 | + meta = _scan_sibling_logs_for_provenance( |
| 115 | + d, {"git_commit": None, "platform": None, |
| 116 | + "ghmc_means": [], "ghmc_mins": []}) |
| 117 | + # env.log scanned first. |
| 118 | + assert meta["git_commit"] == "aaaaaaaaaaaaaaaa", meta |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + funcs = [ |
| 123 | + test_git_commit_picked_up_from_run_log, |
| 124 | + test_git_commit_fallback_to_env_log, |
| 125 | + test_git_commit_fallback_when_no_run_log, |
| 126 | + test_doctor_log_as_last_resort, |
| 127 | + test_no_logs_at_all_returns_none, |
| 128 | + test_run_log_wins_over_sibling, |
| 129 | + test_sibling_scan_direct_helper, |
| 130 | + ] |
| 131 | + fails = [] |
| 132 | + for f in funcs: |
| 133 | + try: |
| 134 | + f() |
| 135 | + print(f"[PASS] {f.__name__}") |
| 136 | + except AssertionError as e: |
| 137 | + print(f"[FAIL] {f.__name__}: {e}") |
| 138 | + fails.append(f.__name__) |
| 139 | + except Exception as e: |
| 140 | + import traceback |
| 141 | + traceback.print_exc() |
| 142 | + print(f"[ERROR] {f.__name__}: {e}") |
| 143 | + fails.append(f.__name__) |
| 144 | + print(f"{len(funcs) - len(fails)}/{len(funcs)} PASS") |
| 145 | + sys.exit(0 if not fails else 1) |
0 commit comments