Belle II Software  release-05-02-19
steering_files.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 """ Extended version of analysis/test/examples.py. """
5 
6 # std
7 import os
8 import sys
9 import subprocess
10 import unittest
11 import glob
12 from typing import Optional, List
13 
14 # basf2
15 from basf2 import find_file
16 from b2test_utils import clean_working_directory
17 
18 
19 def skip_expensive_tests() -> bool:
20  """ Returns true if we want to skip more expensive tests, e.g. on
21  bamboo.
22  """
23  return os.environ.get("SKIP_EXPENSIVE_TESTS", "no").lower() in [
24  "yes",
25  "1",
26  "y",
27  "on",
28  ]
29 
30 
31 class SteeringFileTest(unittest.TestCase):
32  """ Test steering files """
33 
35  self,
36  path_to_glob: str,
37  broken: Optional[List[str]] = None,
38  additional_arguments: Optional[List[str]] = None,
39  expensive_tests: Optional[List[str]] = None,
40  change_working_directory=True,
41  ):
42  """
43  Internal function to test a directory full of example scripts with an
44  optional list of broken scripts to be skipped.
45 
46  Parameters:
47  path_to_glob (str): the path to a directory to search for python
48  scripts (must end in .py)
49  broken (list(str)): (optional) names of scripts that are known to
50  be broken and can be skipped
51  expensive_tests (list(str)): (optional) names of scripts that take
52  longer and should e.g. not run on bamboo
53  additional_arguments (list(str)): (optional) additional arguments
54  for basf2 to be passed when testing the scripts
55  change_working_directory: Change to path_to_glob for execution
56  """
57  if additional_arguments is None:
58  additional_arguments = []
59  if broken is None:
60  broken = []
61  if expensive_tests is None:
62  expensive_tests = []
63  working_dir = find_file(path_to_glob)
64  all_egs = sorted(glob.glob(working_dir + "/*.py"))
65  for eg in all_egs:
66  filename = os.path.basename(eg)
67  if filename in broken:
68  continue
69  if skip_expensive_tests and filename in expensive_tests:
70  continue
71  with self.subTest(msg=filename):
72  result = subprocess.run(
73  ["basf2", "-n1", eg, *additional_arguments],
74  stdout=subprocess.PIPE,
75  stderr=subprocess.STDOUT,
76  cwd=working_dir,
77  )
78  if result.returncode != 0:
79  # failure running example so let's print the output
80  # on stderr so it's not split from output of unittest
81  # done like this since we don't want to decode/encode utf8
82  sys.stdout.buffer.write(result.stdout)
83  self.assertEqual(result.returncode, 0)
84 
85  # fixme: This should be made to run on buildbot, i.e. by adding the/some
86  # files to the examples/validation directory
87  @unittest.skipIf(
88  not os.path.exists(
89  find_file(
90  "starterkit/2021/1111540100_eph3_BGx0_1.root",
91  "examples",
92  silent=True,
93  )
94  ),
95  "Test data files not found.",
96  )
97  def test_lessons_1_to_5(self):
98  self._test_examples_dir(
99  path_to_glob="online_book/basf2/steering_files",
100  additional_arguments=["1"],
101  expensive_tests=[
102  "065_generate_mc.py",
103  "067_generate_mc.py"
104  ],
105  change_working_directory=True,
106  )
107 
108 
109 if __name__ == "__main__":
110  with clean_working_directory():
111  unittest.main()
steering_files.SteeringFileTest
Definition: steering_files.py:31
steering_files.SteeringFileTest._test_examples_dir
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, change_working_directory=True)
Definition: steering_files.py:34