Belle II Software light-2406-ragdoll
awesome_examples.py
1#!/usr/bin/env python3
2
3
10
11"""
12Test all the examples of the awesome package.
13Proudly based on analysis/test/examples.py.
14"""
15
16# std
17import os
18import sys
19import subprocess
20import unittest
21import glob
22
23# basf2
24from basf2 import find_file
25from b2test_utils import clean_working_directory
26
27
28def light_release() -> bool:
29 """ Returns true if we're in a light release """
30 try:
31 import generators # noqa
32 except ModuleNotFoundError:
33 return True
34 return False
35
36
37class AwesomeExamplesTest(unittest.TestCase):
38 """ Test our awesome examples. """
39
41 self,
42 path_to_glob: str
43 ):
44 """
45 Internal function to test a directory full of example scripts.
46
47 Parameters:
48 path_to_glob (str): the path to a directory to search for python
49 scripts.
50 """
51 # Do not run the test if we are in a light release.
52 if light_release():
53 return
54 working_dir = find_file(path_to_glob)
55 examples = sorted(glob.glob(f'{working_dir}/*.py'))
56 for example in examples:
57 filename = os.path.basename(example)
58 with self.subTest(msg=filename):
59 result = subprocess.run(
60 ["basf2", "-n1", example],
61 stdout=subprocess.PIPE,
62 stderr=subprocess.STDOUT,
63 )
64 if result.returncode != 0:
65 # failure running example so let's print the output
66 # on stderr so it's not split from output of unittest
67 # done like this since we don't want to decode/encode utf8
68 sys.stdout.buffer.write(result.stdout)
69 self.assertEqual(result.returncode, 0)
70
71 def test_examples(self):
72 """Run the examples of the awesome package. """
73
75 path_to_glob='online_book/awesome/examples',
76 )
77
78
79if __name__ == "__main__":
80 with clean_working_directory():
81 unittest.main()
def _test_examples_dir(self, str path_to_glob)