Belle II Software  release-06-02-00
steering_files.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 Test all the steering files used in the online_book lessons.
14 Proudly based on analysis/test/examples.py.
15 """
16 
17 # std
18 import os
19 import sys
20 import subprocess
21 import unittest
22 import glob
23 import shutil
24 from typing import Optional, List
25 
26 # basf2
27 from basf2 import find_file
28 from b2test_utils import clean_working_directory, is_ci
29 
30 
31 def light_release() -> bool:
32  """ Returns true if we're in a light release """
33  try:
34  import generators # noqa
35  except ModuleNotFoundError:
36  return True
37  return False
38 
39 
40 class SteeringFileTest(unittest.TestCase):
41  """ Test steering files """
42 
44  self,
45  path_to_glob: str,
46  broken: Optional[List[str]] = None,
47  additional_arguments: Optional[List[str]] = None,
48  expensive_tests: Optional[List[str]] = None,
49  skip_in_light: Optional[List[str]] = None,
50  ):
51  """
52  Internal function to test a directory full of example scripts with an
53  optional list of broken scripts to be skipped.
54 
55  Parameters:
56  path_to_glob (str): the path to a directory to search for python
57  scripts (must end in .py)
58  broken (list(str)): (optional) names of scripts that are known to
59  be broken and can be skipped
60  additional_arguments (list(str)): (optional) additional arguments
61  for basf2 to be passed when testing the scripts
62  expensive_tests (list(str)): (optional) names of scripts that take
63  longer and should e.g. not run on bamboo
64  skip_in_light (list(str)): (optional) names of scripts that have to
65  be excluded in light builds
66  """
67  if additional_arguments is None:
68  additional_arguments = []
69  if broken is None:
70  broken = []
71  if expensive_tests is None:
72  expensive_tests = []
73  if skip_in_light is None:
74  skip_in_light = []
75  # we have to copy all the steering files (plus other stuffs, like decfiles) we want to test
76  # into a new directory and then cd it as working directory when subprocess.run is executed,
77  # otherwise the test will fail horribly if find_file is called by one of the tested steerings.
78  original_dir = find_file(path_to_glob)
79  working_dir = find_file(shutil.copytree(original_dir, 'working_dir'))
80  all_egs = sorted(glob.glob(working_dir + "/*.py"))
81  for eg in all_egs:
82  filename = os.path.basename(eg)
83  if filename in broken:
84  continue
85  if is_ci() and filename in expensive_tests:
86  continue
87  if light_release() and filename in skip_in_light:
88  continue
89  with self.subTest(msg=filename):
90  result = subprocess.run(
91  ["basf2", "-n1", eg, *additional_arguments],
92  stdout=subprocess.PIPE,
93  stderr=subprocess.STDOUT,
94  cwd=working_dir,
95  )
96  if result.returncode != 0:
97  # failure running example so let's print the output
98  # on stderr so it's not split from output of unittest
99  # done like this since we don't want to decode/encode utf8
100  sys.stdout.buffer.write(result.stdout)
101  self.assertEqual(result.returncode, 0)
102 
103  # fixme: This should be made to run on buildbot, i.e. by adding the/some
104  # files to the examples/validation directory
105  @unittest.skipIf( not os.path.exists( find_file( "starterkit/2021/1111540100_eph3_BGx0_1.root", "examples", silent=True, )
106  ),
107  "Test data files not found.",
108  )
110  """Test lesson on basf2 basics."""
111  self._test_examples_dir_test_examples_dir(
112  path_to_glob="online_book/basf2/steering_files",
113  additional_arguments=["1"],
114  expensive_tests=[
115  "065_generate_mc.py",
116  "067_generate_mc.py"
117  ],
118  skip_in_light=[
119  "065_generate_mc.py",
120  "067_generate_mc.py",
121  "085_module.py",
122  "087_module.py"
123  ],
124  )
125 
126 
127 if __name__ == "__main__":
128  with clean_working_directory():
129  unittest.main()
130 
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)