Belle II Software  release-08-01-10
test_changed_decfiles.py
1 
8 
9 '''
10 Simple launcher for decfiles/tests/test_changed_decfiles.py_noexec. This tests whether new decay files work.
11 '''
12 
13 import basf2
14 import b2test_utils
15 import os
16 import subprocess
17 
18 from git import Repo
19 from pathlib import Path
20 from tempfile import NamedTemporaryFile
21 
22 
23 def add_hint(errorstring: str):
24 
25  hint = "\nDon't forget to add custom (i.e. not yet discovered or measured) particles to " \
26  "decfiles/tests/test_changed_decfiles.pdl, otherwise the test will not pass." \
27  " Already discovered particles should go in framework/particledb/data/evt.pdl instead."
28 
29  if 'Unknown particle name' in errorstring:
30  return errorstring + hint
31  else:
32  return errorstring
33 
34 
35 if __name__ == '__main__':
36 
38 
39  if not b2test_utils.is_ci():
40  b2test_utils.skip_test("Will not test changed decfiles because $BELLE2_IS_CI is not set.")
41 
42  if not os.environ.get('BELLE2_LOCAL_DIR'):
43  b2test_utils.skip_test("Test for changed decfiles failed because $BELLE2_LOCAL_DIR is not set.")
44 
45  topdir = Path(os.environ['BELLE2_LOCAL_DIR'])
46  assert topdir.is_dir()
47 
48  repo = Repo(topdir)
49  merge_base = repo.merge_base('origin/main', repo.head)
50  diff_to_main = repo.head.commit.diff(merge_base)
51 
52  added_or_modified_decfiles = [topdir / new_file.a_path for new_file in diff_to_main
53  if (Path(new_file.a_path).suffix == '.dec')
54  and (Path('decfiles/dec') in Path(new_file.a_path).parents)]
55 
56  steering_file = basf2.find_file('decfiles/tests/test_changed_decfiles.py_noexec')
57  custom_evtpdl = basf2.find_file("decfiles/tests/test_changed_decfiles.pdl")
58  default_evtpdl = basf2.find_file('data/framework/particledb/evt.pdl')
59 
60  run_results = []
61  if added_or_modified_decfiles:
62  changed_file_string = '\n'.join(str(p) for p in added_or_modified_decfiles)
63  print(f"Changed decayfiles: \n{changed_file_string}")
64 
65  with NamedTemporaryFile(mode='w', suffix='.pdl') as tempfile:
66 
67  for fname in [custom_evtpdl, default_evtpdl]:
68  with open(fname, 'r') as infile:
69  tempfile.write(infile.read())
70 
71  for decfile in added_or_modified_decfiles:
73  run_results.append(subprocess.run(['basf2', steering_file, str(decfile), tempfile.name],
74  capture_output=True))
75 
76  files_and_errors = [f'Decfile {added_or_modified_decfiles[i]} failed with output \n'
77  f'{ret.stdout.decode()} \n and error \n {add_hint(ret.stderr.decode())}'
78  for i, ret in enumerate(run_results) if ret.returncode != 0]
79 
80  if len(files_and_errors):
81  raise RuntimeError("At least one added decfile has failed.\n"
82  + '\n'.join(files_and_errors))
def configure_logging_for_tests(user_replacements=None)
Definition: __init__.py:106
bool is_ci()
Definition: __init__.py:403
def clean_working_directory()
Definition: __init__.py:189
def skip_test(reason, py_case=None)
Definition: __init__.py:31