Belle II Software development
test_changed_tauolabelle2.py
1
8
9
10'''
11Simple launcher for decfiles/tests/test_changed_datfiles.py_noexec. This tests whether new decay files work for TauolaBelle2.
12'''
13
14import basf2
15import b2test_utils
16import os
17import subprocess
18
19from git import Repo
20from pathlib import Path
21
22
23def add_hint(exit_code):
24 if exit_code == 2:
25 return "[ERROR] The given datfile can't be interpreted by basf2."
26 elif exit_code == 1:
27 return "[ERROR] The given datfile contains value that doesn't exist yet."
28 elif exit_code == 0:
29 return "[ERROR] The given datfile contains unknown value."
30 else:
31 return "[ERROR] Unknown error occurred. Please Debug your Datfile"
32
33
34if __name__ == '__main__':
35
37
38 if not b2test_utils.is_ci(): # to determine whether the current execution environment is a Continuous Integration (CI) system
39 b2test_utils.skip_test("Will not test changed decfiles because $BELLE2_IS_CI is not set.")
40
41 if not os.environ.get('BELLE2_LOCAL_DIR'):
42 b2test_utils.skip_test("Test for changed decfiles failed because $BELLE2_LOCAL_DIR is not set.")
43
44 topdir = Path(os.environ['BELLE2_LOCAL_DIR'])
45 assert topdir.is_dir()
46 # Initialize the repository object
47 repo = Repo(topdir)
48 # Get the merge base of 'origin/main' and current HEAD
49 merge_base = repo.merge_base('origin/main', repo.head)
50
51 # Get the difference between the current HEAD and 'origin/main'
52 diff_to_main = repo.head.commit.diff(merge_base)
53
54 added_or_modified_decfiles = [topdir / new_file.a_path for new_file in diff_to_main
55 if (Path(new_file.a_path).suffix == '.dat')
56 and (Path('decfiles/dec/TauolaBelle2') in Path(new_file.a_path).parents)]
57 # in case some decfiles are removed, they end up in the list of modified files:
58 # let's keep only the decfiles that are actually found by basf2.find_file
59 added_or_modified_decfiles = [decfile for decfile in added_or_modified_decfiles
60 if basf2.find_file(decfile.as_posix(), silent=True)]
61
62 steering_file = basf2.find_file('decfiles/tests/test_changed_tauolabelle2.py_noexec')
63
64 run_results = []
65 if added_or_modified_decfiles:
66 changed_file_string = '\n'.join(str(p) for p in added_or_modified_decfiles)
67 print(f"Changed decayfiles: \n{changed_file_string}")
68
69 for decfile in added_or_modified_decfiles:
71 run_results.append(subprocess.run(['basf2', steering_file, str(decfile)], capture_output=True))
72
73 # The condition '5/ 5 events' is essential because in certain cases, if a user inputs a value that does
74 # not exist in the TauolaBelle2 database, the basf2 program may terminate with a return code of 0.
75 # This behavior occurs because the KKMC generator, utilized by TauolaBelle2, is written in Fortran,
76 # and in such instances, it abruptly halts the execution of the code with a return code of 0.
77 files_and_errors = [f'Decfile {added_or_modified_decfiles[i]} failed with output \n'
78 f'{ret.stdout.decode()} \n and {add_hint(ret.returncode)}'
79 for i, ret in enumerate(run_results)
80 if ret.returncode != 0 or (b'5/ 5 events' not in ret.stdout and ret.returncode == 0)]
81
82 if len(files_and_errors):
83 raise RuntimeError("At least one added decfile has failed.\n"
84 + '\n'.join(files_and_errors))
def clean_working_directory()
Definition: __init__.py:194
def configure_logging_for_tests(user_replacements=None)
Definition: __init__.py:106
def skip_test(reason, py_case=None)
Definition: __init__.py:31
bool is_ci()
Definition: __init__.py:421