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