|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +import signal |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +import pytest |
| 8 | +import ROOT |
| 9 | + |
| 10 | +ROOT.gROOT.SetBatch(True) |
| 11 | + |
| 12 | +tutorial_dir = pathlib.Path(str(ROOT.gROOT.GetTutorialDir())) |
| 13 | + |
| 14 | +subdirs = ["analysis/dataframe", "analysis/tree", "hist", "io/ntuple", "roofit/roofit"] |
| 15 | + |
| 16 | +SKIP_TUTORIALS = { |
| 17 | + "ntpl004_dimuon.C", # require reading remote data via HTTP |
| 18 | + "ntpl008_import.C", # require reading remote data via HTTP |
| 19 | + "distrdf004_dask_lxbatch.py", # only works on lxplus |
| 20 | +} |
| 21 | + |
| 22 | +# ---------------------- |
| 23 | +# Python tutorials tests |
| 24 | +# ---------------------- |
| 25 | +py_tutorials = [] |
| 26 | +for sub in subdirs: |
| 27 | + sub_path = tutorial_dir / sub |
| 28 | + for f in sub_path.rglob("*.py"): |
| 29 | + if any(skip in f.name for skip in SKIP_TUTORIALS): |
| 30 | + print("Skipping Python tutorial:", f) |
| 31 | + continue |
| 32 | + py_tutorials.append(f) |
| 33 | + |
| 34 | + |
| 35 | +def test_tutorials_are_detected(): |
| 36 | + assert len(py_tutorials) > 0 |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("tutorial", py_tutorials, ids=lambda p: p.name) |
| 40 | +def test_tutorial(tutorial): |
| 41 | + env = dict(**os.environ) |
| 42 | + # force matplotlib to use a non-GUI backend |
| 43 | + env["MPLBACKEND"] = "Agg" |
| 44 | + print("Test env:", env) |
| 45 | + try: |
| 46 | + result = subprocess.run( |
| 47 | + [sys.executable, str(tutorial)], |
| 48 | + check=True, |
| 49 | + env=env, |
| 50 | + timeout=60, |
| 51 | + capture_output=True, |
| 52 | + text=True, |
| 53 | + ) |
| 54 | + print("Test stderr:", result.stderr) |
| 55 | + except subprocess.TimeoutExpired: |
| 56 | + pytest.skip(f"Tutorial {tutorial} timed out") |
| 57 | + except subprocess.CalledProcessError as e: |
| 58 | + # read stderr to see if EOFError occurred |
| 59 | + if "EOFError" in e.stderr: |
| 60 | + pytest.skip(f"Skipping {tutorial.name} (requires user input)") |
| 61 | + raise |
| 62 | + |
| 63 | + |
| 64 | +# ---------------------- |
| 65 | +# C++ tutorials tests |
| 66 | +# ---------------------- |
| 67 | +cpp_tutorials = [] |
| 68 | +for sub in subdirs: |
| 69 | + sub_path = tutorial_dir / sub |
| 70 | + for f in sub_path.rglob("*.C"): |
| 71 | + if any(skip in f.name for skip in SKIP_TUTORIALS): |
| 72 | + print("Skipping C++ tutorial:", f) |
| 73 | + continue |
| 74 | + cpp_tutorials.append(f) |
| 75 | + |
| 76 | + |
| 77 | +def test_cpp_tutorials_are_detected(): |
| 78 | + assert len(cpp_tutorials) > 0 |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("tutorial", cpp_tutorials, ids=lambda p: p.name) |
| 82 | +def test_cpp_tutorial(tutorial): |
| 83 | + try: |
| 84 | + result = subprocess.run( |
| 85 | + [sys.executable, "-c", f'import ROOT; ROOT.gROOT.ProcessLine(".x {tutorial}")'], |
| 86 | + check=True, |
| 87 | + timeout=60, |
| 88 | + capture_output=True, |
| 89 | + text=True, |
| 90 | + ) |
| 91 | + print("Test stderr:", result.stderr) |
| 92 | + except subprocess.TimeoutExpired: |
| 93 | + pytest.skip(f"Tutorial {tutorial} timed out") |
| 94 | + except subprocess.CalledProcessError as e: |
| 95 | + if e.returncode == -signal.SIGILL or e.returncode == 132: |
| 96 | + pytest.fail(f"Failing {tutorial.name} (illegal instruction on this platform)") |
| 97 | + elif "EOFError" in e.stderr: |
| 98 | + pytest.skip(f"Skipping {tutorial.name} (requires user input)") |
| 99 | + raise |
0 commit comments