Belle II Software  release-05-01-25
steering_files.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 """ Largely based on analysis/test/examples.py. """
5 
6 # std
7 import os
8 import sys
9 import subprocess
10 import unittest
11 import glob
12 
13 # basf2
14 from basf2 import find_file
15 from b2test_utils import clean_working_directory
16 
17 
18 class SteeringFileTest(unittest.TestCase):
19  """ Test steering files """
20 
21  def _test_examples_dir(self, path_to_glob, broken=None, additional_arguments=None):
22  """
23  Internal function to test a directory full of example scripts with an
24  optional list of broken scripts to be skipped.
25 
26  Parameters:
27  path_to_glob (str): the path to search for scripts
28  broken (list(str)): (optional) scripts that are known to be broken
29  and can be skipped
30  """
31  if additional_arguments is None:
32  additional_arguments = []
33  if broken is None:
34  broken = []
35  all_egs = sorted(glob.glob(find_file(path_to_glob) + "/*.py"))
36  for eg in all_egs:
37  filename = os.path.basename(eg)
38  if filename not in broken:
39  with self.subTest(msg=filename):
40  result = subprocess.run(
41  ["basf2", "-n1", eg, *additional_arguments],
42  stdout=subprocess.PIPE,
43  stderr=subprocess.STDOUT,
44  )
45  if result.returncode != 0:
46  # failure running example so let's print the output
47  # on stderr so it's not split from output of unittest
48  # done like this since we don't want to decode/encode utf8
49  sys.stdout.buffer.write(result.stdout)
50  self.assertEqual(result.returncode, 0)
51 
52  # fixme: This should be made to run on buildbot, i.e. by adding the/some
53  # files to the examples/validation directory
54  @unittest.skipIf(
55  not os.path.exists(
56  find_file("starterkit/2021/1111540100_eph3_BGx0_1.root", "examples", silent=True)
57  ),
58  "Test data files not found."
59  )
60  def test_lessons_1_to_5(self):
61  self._test_examples_dir(
62  "online_book/basf2/steering_files", additional_arguments=["1"]
63  )
64 
65 
66 if __name__ == "__main__":
67  with clean_working_directory():
68  unittest.main()
steering_files.SteeringFileTest
Definition: steering_files.py:18
steering_files.SteeringFileTest._test_examples_dir
def _test_examples_dir(self, path_to_glob, broken=None, additional_arguments=None)
Definition: steering_files.py:21