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