Belle II Software development
test_script_discovery.py
1#!/usr/bin/env python3
2
3
10
11import sys
12import os
13from unittest.mock import Mock
14import tempfile
15import validationfunctions
16
17from validationtestutil import create_fake_scripts
18
19
20def main():
21 """
22 Tests the automated validation script discovery
23 """
24
25 # will create a temporary folder and delete it once this block is left
26 with tempfile.TemporaryDirectory() as tmpdir:
27 print(f"Created temporary test folder {tmpdir}")
28
29 # create a couple of fake validation scripts
30 create_fake_scripts(
31 os.path.join(tmpdir, "pkg1", "validation"), "pkg1_a.py"
32 )
33 create_fake_scripts(
34 os.path.join(tmpdir, "pkg1", "validation"), "pkg1_b.py"
35 )
36 create_fake_scripts(
37 os.path.join(tmpdir, "pkg1", "validation"), "pkg1_c.py"
38 )
39
40 create_fake_scripts(
41 os.path.join(tmpdir, "pkg2", "validation"), "pkg2_b.py"
42 )
43 create_fake_scripts(
44 os.path.join(tmpdir, "pkg2", "validation"), "pkg2_c.py"
45 )
46
47 # this should not be found !
48 create_fake_scripts(
49 os.path.join(tmpdir, "some_other_folder", "pkgother", "validation"),
50 "pkgother_b.py",
51 )
52
53 # fake the basepath
54 basepath = {"local": str(tmpdir)}
55
57 location="local", basepaths=basepath, log=Mock()
58 )
59
60 if (
61 "pkg1" not in folders
62 or "pkg2" not in folders
63 or "some_other_folder" in folders
64 ):
65 print("scripts were discovered in the wrong folders")
66 sys.exit(1)
67
69 folders["pkg1"], Mock(), ".py"
70 )
71
72 if (
73 len([s for s in scripts if s.endswith("pkg1/validation/pkg1_a.py")])
74 == 0
75 ):
76 print("script file was not discovered")
77 sys.exit(1)
78
79 print(scripts)
80
81 # test for release and local case
82 with tempfile.TemporaryDirectory() as tmpdir:
83 print(f"Created temporary test folder {tmpdir}")
84
85 # create a couple of fake validation scripts
86 create_fake_scripts(
87 os.path.join(tmpdir, "pkg1", "validation"), "pkg1_a.py"
88 )
89 create_fake_scripts(
90 os.path.join(tmpdir, "pkg1", "validation"), "pkg1_b.py"
91 )
92 create_fake_scripts(
93 os.path.join(tmpdir, "pkg1", "validation"), "pkg1_c.py"
94 )
95
96 create_fake_scripts(
97 os.path.join(tmpdir, "pkg2", "validation"), "pkg2_b.py"
98 )
99 create_fake_scripts(
100 os.path.join(tmpdir, "pkg2", "validation"), "pkg2_c.py"
101 )
102
103 # this should not be found !
104 create_fake_scripts(
105 os.path.join(tmpdir, "some_other_folder", "pkgother", "validation"),
106 "pkgother_b.py",
107 )
108
109 # fake the basepath
110 basepath = {"local": str(tmpdir), "central": str(tmpdir)}
111
113 location="central", basepaths=basepath, log=Mock()
114 )
115
116 if (
117 "pkg1" not in folders
118 or "pkg2" not in folders
119 or "some_other_folder" in folders
120 ):
121 print("scripts were discovered in the wrong folders")
122 sys.exit(1)
123
124
125if __name__ == "__main__":
126 main()
Definition: main.py:1
Dict[str, str] get_validation_folders(str location, Dict[str, str] basepaths, logging.Logger log)
List[str] scripts_in_dir(str dirpath, logging.Logger log, ext="*")