Skip to content

Commit 5b2cd85

Browse files
committed
comments
1 parent fe45571 commit 5b2cd85

1 file changed

Lines changed: 53 additions & 6 deletions

File tree

spinnaker_testbase/root_script_builder.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class RootScriptBuilder(object):
3232

3333
def _add_script(self, test_file: TextIOBase, name: str, local_path: str,
3434
skip_imports: Optional[List[str]]) -> None:
35+
"""
36+
Adds a unit test that tests a script by importing it
37+
"""
3538
test_file.write("\n def test_")
3639
test_file.write(name)
3740
test_file.write("(self):\n")
@@ -54,6 +57,14 @@ def _add_script(self, test_file: TextIOBase, name: str, local_path: str,
5457

5558
def _add_split_script(self, test_file: TextIOBase, name: str,
5659
local_path: str, split: bool) -> None:
60+
"""
61+
Adds a test by running a scripts run_script method
62+
63+
:param test_file: Where to write the test
64+
:param name: Partial name for the test
65+
:param local_path: Path to find the script
66+
:param split: Flag to say if the test should be run split
67+
"""
5768
test_file.write("\n def test_")
5869
test_file.write(name)
5970
if split:
@@ -67,25 +78,44 @@ def _add_split_script(self, test_file: TextIOBase, name: str,
6778
test_file.write(f" run_script(split={split})\n")
6879

6980
def _extract_binaries(self, text: str) -> List[str]:
70-
print(text)
81+
"""
82+
Extracts the binaries from a comments
83+
84+
:param text: The line(s) that contain the info
85+
:return: List of the binaries expected
86+
"""
87+
# remove all whitespace
7188
text = "".join(text.split())
72-
print(text)
89+
# remove comment markers. There may be one between binaries
7390
text = text.replace("#", "")
91+
# ignore the part before the binaries and
7492
text = text[text.find("[")+1: text.find("]")]
75-
print(text.split(","))
7693
return text.split(",")
7794

7895
def _script_details(
7996
self, local_path: str) -> Tuple[bool, bool, List[str], List[str]]:
97+
"""
98+
Examine a script to see which tests should be added
99+
100+
:param local_path: path to find the script
101+
"""
102+
# Says if a run_script split has been found
80103
run_script = False
104+
# Says if there is an if def __main__
105+
has_main = False
106+
# List of binaries to check for if running not split
107+
combined_binaires = []
108+
# List of binaries to check for if running split
109+
split_binaires = []
110+
111+
# Temp variables when looking at multiline stiff
81112
in_combined = False
82113
in_split = False
83114
text = ""
84-
combined_binaires = []
85-
split_binaires = []
86-
has_main = False
115+
87116
with open(local_path, "r", encoding="utf-8") as script_file:
88117
for line in script_file:
118+
# second or more line of a comment
89119
if in_combined or in_split:
90120
text += line
91121
elif "def run_script(" in line and " split:" in line:
@@ -99,6 +129,7 @@ def _script_details(
99129
text = line
100130
elif "__name__" in line:
101131
has_main = True
132+
# Have we found the end of the binaires list
102133
if in_combined:
103134
if "]" in line:
104135
combined_binaires = self._extract_binaries(text)
@@ -113,11 +144,20 @@ def _script_details(
113144

114145
def _add_not_testing(
115146
self, test_file: TextIOBase, reason: str, local_path: str) -> None:
147+
"""
148+
Adds comments of what is not beiing tested and why
149+
"""
116150
test_file.write(f"\n # Not testing file due to: {reason}\n")
117151
test_file.write(f" # {local_path}\n")
118152

119153
def _add_binaries(
120154
self, test_file: TextIOBase, binaries: List[str]) -> None:
155+
"""
156+
Appends binaries checks to a test
157+
158+
:param test_file: Fle to write check to
159+
:param binaries: List possibly empty of binaries to check
160+
"""
121161
if binaries:
122162
binaries = [f'"{binary}"' for binary in binaries]
123163
test_file.write(f" self.check_binaries_used("
@@ -127,6 +167,9 @@ def _add_test_directory(
127167
self, a_dir: str, prefix_len: int, test_file: TextIOBase,
128168
too_long: Dict[str, str], exceptions: Dict[str, str],
129169
skip_exceptions: Dict[str, List[str]]) -> None:
170+
"""
171+
Adds any required tests for the scripts in a directory
172+
"""
130173
for a_script in os.listdir(a_dir):
131174
script_path = os.path.join(a_dir, a_script)
132175
if os.path.isdir(script_path) and not a_script.startswith("."):
@@ -152,21 +195,25 @@ def _add_test_directory(
152195
name = local_path[:-3].replace(os.sep, "_").replace(
153196
"-", "_")
154197
skip_imports = skip_exceptions.get(a_script, None)
198+
# use the run_Scripts method style
155199
if run_script:
156200
self._add_split_script(
157201
test_file, name, local_path, False)
158202
self._add_binaries(test_file, combined_binaires)
159203
self._add_split_script(
160204
test_file, name, local_path, True)
161205
self._add_binaries(test_file, split_binaires)
206+
# Due to a main the test will not run if imported
162207
elif has_main:
163208
self._add_not_testing(
164209
test_file, "Unhandled main", local_path)
165210
assert combined_binaires == []
166211
assert split_binaires == []
212+
# Use the import script style
167213
else:
168214
self._add_script(
169215
test_file, name, local_path, skip_imports)
216+
self._add_binaries(test_file, combined_binaires)
170217

171218
def create_test_scripts(
172219
self, dirs: Union[str, List[str]],

0 commit comments

Comments
 (0)