12Test all the steering files used in the online_book lessons.
13Proudly based on analysis/test/examples.py.
23from typing
import Optional, List, Dict
24from pathlib
import Path
30from basf2
import find_file
31from b2test_utils
import clean_working_directory, is_ci
34def light_release() -> bool:
35 """Returns true if we're in a light release"""
40 except ModuleNotFoundError:
45def _touch_file_default(path: str):
49def _touch_file_with_root(path: str) ->
None:
50 f = TFile(path,
"NEW")
52 assert Path(path).is_file()
55def _touch_file_with_subprocess(path: str) ->
None:
56 subprocess.run([
"touch", path]).check_returncode()
59def _touch_file_with_subprocess_and_root(path: str) ->
None:
60 filename = Path(path).name
61 working_dir = Path(path).parent
62 cmd = [
"root",
"-x",
"-l",
"-q",
"-e", f
"TFile f(\"{filename}\", \"NEW\"); if (not f.IsOpen()) gSystem->Exit(1);"]
63 subprocess.run(cmd, cwd=working_dir).check_returncode()
66def _touch_file_test(method, path: str, **kwargs):
68 method(path, **kwargs)
69 except Exception
as e:
70 print(f
"{method.__name__}: Tried to touch file with, but failed: {e}")
72 print(f
"{method.__name__}: Successfully touched file")
76def _permission_report(folder: str) ->
None:
77 """Quick helper function to show permissions of folder and a selection
82 print(f
"Permissions of {folder}: {folder.stat()}")
83 content = list(folder.iterdir())
86 f
"Permission of one of its contents. {content[0]}: "
87 f
"{content[0].stat()}"
89 test_file = folder /
"Bd2JpsiKS.root"
92 _touch_file_with_root,
93 _touch_file_with_subprocess,
94 _touch_file_with_subprocess_and_root
96 for method
in methods:
97 _touch_file_test(method, str(test_file))
102 """Test steering files"""
107 broken: Optional[List[str]] =
None,
108 additional_arguments: Optional[List[str]] =
None,
109 expensive_tests: Optional[List[str]] =
None,
110 skip_in_light: Optional[List[str]] =
None,
111 skip: Optional[List[str]] =
None,
112 n_events: Optional[Dict[str, int]] =
None,
115 Internal function to test a directory full of example scripts with an
116 optional list of broken scripts to be skipped.
119 path_to_glob (str): the path to a directory to search
for python
120 scripts (must end
in .py)
121 broken (list(str)): (optional) names of scripts that are known to
122 be broken
and can be skipped
123 additional_arguments (list(str)): (optional) additional arguments
124 for basf2 to be passed when testing the scripts
125 expensive_tests (list(str)): (optional) names of scripts that take
126 longer
and should e.g.
not run
in GitLab pipeline
127 skip_in_light (list(str)): (optional) names of scripts that have to
128 be excluded
in light builds
129 skip (list(str)): (optional) names of scripts to always skip
130 n_events (dict(str, int)): mapping of name of script to number of
131 required events
for it to run (`-n` argument). If a filename
132 isn
't listed, we assume 1
134 if additional_arguments
is None:
135 additional_arguments = []
138 if expensive_tests
is None:
140 if skip_in_light
is None:
149 original_dir = find_file(path_to_glob)
150 print(f
"Our user id: {os.getuid()}")
151 _permission_report(original_dir)
152 working_dir = find_file(shutil.copytree(original_dir,
"working_dir"))
153 _permission_report(working_dir)
155 os.chmod(working_dir, 0o744)
156 _permission_report(working_dir)
157 all_egs = sorted(glob.glob(working_dir +
"/*.py"))
159 filename = os.path.basename(eg)
160 if filename
in broken:
162 if is_ci()
and filename
in expensive_tests:
164 if light_release()
and filename
in skip_in_light:
168 with self.subTest(msg=filename):
170 result = subprocess.run(
174 str(n_events.get(filename, 1)),
176 *additional_arguments,
178 stdout=subprocess.PIPE,
179 stderr=subprocess.STDOUT,
182 if result.returncode != 0:
186 sys.stdout.buffer.write(result.stdout)
187 self.assertEqual(result.returncode, 0)
194 "starterkit/2021/1111540100_eph3_BGx0_1.root",
199 "Test data files not found.",
202 """Test lesson on basf2 basics."""
204 path_to_glob=
"online_book/basf2/steering_files",
205 additional_arguments=[
"1"],
206 expensive_tests=[
"065_generate_mc.py",
"067_generate_mc.py"],
208 "065_generate_mc.py",
209 "067_generate_mc.py",
220if __name__ ==
"__main__":
221 with clean_working_directory():
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)