Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:21:42

0001 """Check metadata output interfaces and deterministic generation."""
0002 
0003 import json
0004 import os
0005 import re
0006 from pathlib import Path
0007 import subprocess
0008 import sys
0009 
0010 import pytest
0011 
0012 ROOT = Path(__file__).resolve().parent.parent
0013 ENTRIES = [
0014     entry
0015     for entry in json.loads((ROOT / "codegen/manifest.json").read_text())[
0016         "units"
0017     ].values()
0018     if entry["script"] == "Detray/detectors/codegen/generate_metadata.py"
0019 ]
0020 
0021 
0022 @pytest.mark.parametrize("entry", ENTRIES, ids=lambda entry: entry["arguments"][-1])
0023 def test_metadata_output_interfaces_match(tmp_path, entry):
0024     detector = Path(entry["output"]).stem.removesuffix("_metadata")
0025     original = tmp_path / "original"
0026     original.mkdir()
0027     subprocess.run(
0028         [
0029             sys.executable,
0030             str(ROOT / entry["script"]),
0031             *entry["arguments"],
0032             "--no-format",
0033             "--output",
0034             str(original),
0035         ],
0036         env={
0037             **os.environ,
0038             "PYTHONPATH": str(ROOT / "Detray/detectors/codegen"),
0039             "PYTHONHASHSEED": "0",
0040         },
0041         check=True,
0042     )
0043     # The common generator contract accepts any output filename, not just the
0044     # detector script's implicit <detector>_metadata.hpp name.
0045     output = tmp_path / "generated" / "renamed.hpp"
0046     subprocess.run(
0047         [sys.executable, str(ROOT / entry["script"]), str(output), *entry["arguments"]],
0048         cwd=tmp_path,
0049         env={
0050             **os.environ,
0051             "PYTHONPATH": str(ROOT / "Detray/detectors/codegen"),
0052             "PYTHONHASHSEED": "1",
0053         },
0054         check=True,
0055     )
0056     assert output.read_bytes() == (original / f"{detector}_metadata.hpp").read_bytes()
0057 
0058 
0059 @pytest.mark.parametrize("function", ["configure", "missing"])
0060 def test_external_metadata(tmp_path, function):
0061     configuration = tmp_path / "custom_detector.py"
0062     configuration.write_text(
0063         "from detray_metadata.configurations import toy_detector\n"
0064         "def configure(md):\n"
0065         "    toy_detector.configure(md)\n"
0066     )
0067     output = tmp_path / "custom_metadata.hpp"
0068     result = subprocess.run(
0069         [
0070             sys.executable,
0071             str(ROOT / "Detray/detectors/codegen/generate_metadata.py"),
0072             str(output),
0073             "--detector",
0074             f"{configuration}:{function}",
0075         ],
0076         cwd=tmp_path,
0077         env={**os.environ, "PYTHONPATH": str(ROOT / "Detray/detectors/codegen")},
0078         capture_output=True,
0079         text=True,
0080     )
0081     if function == "missing":
0082         assert result.returncode != 0
0083         assert "Invalid detector configuration" in result.stderr
0084     else:
0085         assert result.returncode == 0, result.stderr
0086         assert "struct custom_metadata" in output.read_text()
0087 
0088 
0089 def test_shipped_configurations_match_manifest():
0090     """Every packaged configuration has a manifest unit and is always generated."""
0091     configurations = ROOT / "Detray/detectors/codegen/detray_metadata/configurations"
0092     names = sorted(
0093         path.stem.removesuffix("_detector")
0094         for path in configurations.glob("*_detector.py")
0095     )
0096     assert sorted(entry["arguments"][-1] for entry in ENTRIES) == names
0097     for entry in ENTRIES:
0098         assert (
0099             entry["output"] == f"detray/detectors/{entry['arguments'][-1]}_metadata.hpp"
0100         )
0101     cmake = (ROOT / "Detray/detectors/CMakeLists.txt").read_text()
0102     shipped = re.search(r"set\(_detray_shipped_metadata\s+([^)]*)\)", cmake)
0103     assert shipped is not None
0104     assert sorted(shipped.group(1).split()) == names