13 Test all the steering files used in the online_book lessons.
14 Proudly based on analysis/test/examples.py.
24 from typing
import Optional, List, Dict
25 from pathlib
import Path
28 from ROOT
import TFile
31 from basf2
import find_file
32 from b2test_utils
import clean_working_directory, is_ci
35 def light_release() -> bool:
36 """Returns true if we're in a light release"""
41 except ModuleNotFoundError:
46 def _touch_file_default(path: str):
50 def _touch_file_with_root(path: str) ->
None:
51 f = TFile(path,
"NEW")
53 assert Path(path).is_file()
56 def _touch_file_with_subprocess(path: str) ->
None:
57 subprocess.run([
"touch", path]).check_returncode()
60 def _touch_file_with_subprocess_and_root(path: str) ->
None:
61 filename = Path(path).name
62 working_dir = Path(path).parent
63 cmd = [
"root",
"-x",
"-l",
"-q",
"-e", f
"TFile f(\"{filename}\", \"NEW\"); if (not f.IsOpen()) gSystem->Exit(1);"]
64 subprocess.run(cmd, cwd=working_dir).check_returncode()
67 def _touch_file_test(method, path: str, **kwargs):
69 method(path, **kwargs)
70 except Exception
as e:
71 print(f
"{method.__name__}: Tried to touch file with, but failed: {e}")
73 print(f
"{method.__name__}: Successfully touched file")
77 def _permission_report(folder: str) ->
None:
78 """Quick helper function to show permissions of folder and a selection
83 print(f
"Permissions of {folder}: {folder.stat()}")
84 content = list(folder.iterdir())
87 f
"Permission of one of its contents. {content[0]}: "
88 f
"{content[0].stat()}"
90 test_file = folder /
"Bd2JpsiKS.root"
93 _touch_file_with_root,
94 _touch_file_with_subprocess,
95 _touch_file_with_subprocess_and_root
97 for method
in methods:
98 _touch_file_test(method, str(test_file))
103 """Test steering files"""
108 broken: Optional[List[str]] =
None,
109 additional_arguments: Optional[List[str]] =
None,
110 expensive_tests: Optional[List[str]] =
None,
111 skip_in_light: Optional[List[str]] =
None,
112 skip: Optional[List[str]] =
None,
113 n_events: Optional[Dict[str, int]] =
None,
116 Internal function to test a directory full of example scripts with an
117 optional list of broken scripts to be skipped.
120 path_to_glob (str): the path to a directory to search for python
121 scripts (must end in .py)
122 broken (list(str)): (optional) names of scripts that are known to
123 be broken and can be skipped
124 additional_arguments (list(str)): (optional) additional arguments
125 for basf2 to be passed when testing the scripts
126 expensive_tests (list(str)): (optional) names of scripts that take
127 longer and should e.g. not run on bamboo
128 skip_in_light (list(str)): (optional) names of scripts that have to
129 be excluded in light builds
130 skip (list(str)): (optional) names of scripts to always skip
131 n_events (dict(str, int)): mapping of name of script to number of
132 required events for it to run (`-n` argument). If a filename
133 isn't listed, we assume 1
135 if additional_arguments
is None:
136 additional_arguments = []
139 if expensive_tests
is None:
141 if skip_in_light
is None:
150 original_dir = find_file(path_to_glob)
151 print(f
"Our user id: {os.getuid()}")
152 _permission_report(original_dir)
153 working_dir = find_file(shutil.copytree(original_dir,
"working_dir"))
154 _permission_report(working_dir)
156 os.chmod(working_dir, 0o744)
157 _permission_report(working_dir)
158 all_egs = sorted(glob.glob(working_dir +
"/*.py"))
160 filename = os.path.basename(eg)
161 if filename
in broken:
163 if is_ci()
and filename
in expensive_tests:
165 if light_release()
and filename
in skip_in_light:
169 with self.subTest(msg=filename):
171 result = subprocess.run(
175 str(n_events.get(filename, 1)),
177 *additional_arguments,
179 stdout=subprocess.PIPE,
180 stderr=subprocess.STDOUT,
183 if result.returncode != 0:
187 sys.stdout.buffer.write(result.stdout)
188 self.assertEqual(result.returncode, 0)
192 @unittest.skipIf(
not os.path.exists(
find_file(
"starterkit/2021/1111540100_eph3_BGx0_1.root",
"examples",
silent=True,
)
194 "Test data files not found.",
197 """Test lesson on basf2 basics."""
199 path_to_glob=
"online_book/basf2/steering_files",
200 additional_arguments=[
"1"],
201 expensive_tests=[
"065_generate_mc.py",
"067_generate_mc.py"],
203 "065_generate_mc.py",
204 "067_generate_mc.py",
215 if __name__ ==
"__main__":
216 with clean_working_directory():
218
def _test_examples_dir(self, str path_to_glob, Optional[List[str]] broken=None, Optional[List[str]] additional_arguments=None, Optional[List[str]] expensive_tests=None, Optional[List[str]] skip_in_light=None, Optional[List[str]] skip=None, Optional[Dict[str, int]] n_events=None)
def test_lessons_1_to_5(self)