From 5f2d62aee2e25fcf05cf8d81fd83ea597bd625ca Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Mon, 9 Feb 2026 15:50:42 +0000 Subject: [PATCH 1/6] split scipt maker --- spinnaker_testbase/root_script_builder.py | 113 ++++++++++++++++++---- 1 file changed, 96 insertions(+), 17 deletions(-) diff --git a/spinnaker_testbase/root_script_builder.py b/spinnaker_testbase/root_script_builder.py index 6c56388..a21c24a 100644 --- a/spinnaker_testbase/root_script_builder.py +++ b/spinnaker_testbase/root_script_builder.py @@ -52,13 +52,84 @@ def _add_script(self, test_file: TextIOBase, name: str, local_path: str, test_file.write("]") test_file.write(")\n") - def _add_scripts(self, a_dir: str, prefix_len: int, test_file: TextIOBase, - too_long: Dict[str, str], exceptions: Dict[str, str], - skip_exceptions: Dict[str, List[str]]) -> None: + def _add_split_script(self, test_file: TextIOBase, name: str, local_path: str, + split: bool) -> None: + test_file.write("\n def test_") + test_file.write(name) + if split: + test_file.write("_split") + else: + test_file.write("_combined") + test_file.write("(self):\n") + + import_text = local_path[:-3].replace(os.sep, ".") + test_file.write(f" from {import_text} import run_script\n") + test_file.write(f" run_script(split={split})\n") + + def _extract_binaries(self, text:str) -> List[str]: + print(text) + text = "".join(text.split()) + print(text) + text = text.replace("#", "") + text = text[text.find("[")+1: text.find("]")] + print(text.split(",")) + return text.split(",") + + def _script_details( + self, local_path: str) -> (bool, bool, List[str], List[str]): + run_script = False + in_combined = False + in_split = False + text = "" + combined_binaires = [] + split_binaires = [] + has_main = False + with open(local_path, "r", encoding="utf-8") as script_file: + for line in script_file: + if in_combined or in_split: + text += line + elif "def run_script(" in line and " split:" in line: + print("Split ", local_path) + run_script = True + elif "combined binaries" in line: + in_combined = True + text = line + elif "split binaries" in line: + in_split= True + text = line + elif "__name__" in line: + has_main = True + if in_combined: + if "]" in line: + combined_binaires = self._extract_binaries(text) + in_combined = False + text = "" + elif in_split: + if "]" in line: + split_binaires = self._extract_binaries(text) + in_split = False + text = "" + return (has_main, run_script, combined_binaires, split_binaires) + + def _add_not_testing( + self, test_file: TextIOBase, reason: str, local_path: str) -> None: + test_file.write(f"\n # Not testing file due to: {reason}\n") + test_file.write(f" # {local_path}\n") + + def _add_binaries(self, test_file: TextIOBase, binaries: List[str]): + if binaries: + binaries = [f'"{binary}"' for binary in binaries] + test_file.write(f" self.check_binaries_used(" + f"[{', '.join(binaries)}])\n") + + def _add_test_directory( + self, a_dir: str, prefix_len: int, test_file: TextIOBase, + too_long: Dict[str, str], exceptions: Dict[str, str], + skip_exceptions: Dict[str, List[str]]) -> None: for a_script in os.listdir(a_dir): script_path = os.path.join(a_dir, a_script) if os.path.isdir(script_path) and not a_script.startswith("."): - self._add_scripts( + self._add_test_directory( script_path, prefix_len, test_file, too_long, exceptions, skip_exceptions) if a_script.endswith(".py") and a_script != "__init__.py": @@ -69,22 +140,30 @@ def _add_scripts(self, a_dir: str, prefix_len: int, test_file: TextIOBase, local_path = local_path.replace("\\", "/") if a_script in too_long and len(sys.argv) > 1: # Lazy boolean distinction based on presence of parameter - test_file.write("\n # Not testing file due to: ") - test_file.write(too_long[a_script]) - test_file.write("\n # ") - test_file.write(local_path) - test_file.write("\n") + self._add_not_testing( + test_file, too_long[a_script], local_path) elif a_script in exceptions: - test_file.write("\n # Not testing file due to: ") - test_file.write(exceptions[a_script]) - test_file.write("\n # ") - test_file.write(local_path) - test_file.write("\n") + self._add_not_testing( + test_file, exceptions[a_script], local_path) else: + (has_main, run_script, combined_binaires, + split_binaires) = self._script_details(script_path) name = local_path[:-3].replace(os.sep, "_").replace( "-", "_") skip_imports = skip_exceptions.get(a_script, None) - self._add_script(test_file, name, local_path, skip_imports) + if run_script: + self._add_split_script(test_file, name, local_path, False) + self._add_binaries(test_file, combined_binaires) + self._add_split_script(test_file, name, local_path, True) + self._add_binaries(test_file, split_binaires) + elif has_main: + self._add_not_testing( + test_file, "Unhandled main", local_path) + assert combined_binaires == [] + assert split_binaires == [] + else: + self._add_script(test_file, name, local_path, skip_imports) + def create_test_scripts( self, dirs: Union[str, List[str]], @@ -138,5 +217,5 @@ def create_test_scripts( with open(test_script, "a", encoding="utf-8") as test_file: for script_dir in dirs: a_dir = os.path.join(repository_dir, script_dir) - self._add_scripts(a_dir, len(repository_dir)+1, test_file, - too_long, exceptions, skip_exceptions) + self._add_test_directory(a_dir, len(repository_dir) + 1, test_file, + too_long, exceptions, skip_exceptions) From 99d25091807d9d35ca60873f25137e0e9fc7d698 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 10 Feb 2026 08:40:19 +0000 Subject: [PATCH 2/6] assume binaries end with .aplx --- spinnaker_testbase/root_test_case.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spinnaker_testbase/root_test_case.py b/spinnaker_testbase/root_test_case.py index fc48696..80da5d3 100644 --- a/spinnaker_testbase/root_test_case.py +++ b/spinnaker_testbase/root_test_case.py @@ -149,5 +149,7 @@ def check_binaries_used(self, binaries: List[str]) -> None: _, file = os.path.split(target) files.add(file) for binary in binaries: + if not binary.endswith(".aplx"): + binary = binary + ".aplx" self.assertIn(binary, files) print(files) From ddee83feeb600e155ea5aef8a154c32c5cfc0c21 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 10 Feb 2026 08:55:42 +0000 Subject: [PATCH 3/6] typing fixes --- spinnaker_testbase/root_script_builder.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spinnaker_testbase/root_script_builder.py b/spinnaker_testbase/root_script_builder.py index a21c24a..3172caf 100644 --- a/spinnaker_testbase/root_script_builder.py +++ b/spinnaker_testbase/root_script_builder.py @@ -17,7 +17,7 @@ import platform from shutil import copyfile import sys -from typing import Dict, List, Optional, Union +from typing import Dict, List, Optional, Tuple, Union SKIP_TOO_LONG = " raise SkipTest(\"{}\")\n" NO_SKIP_TOO_LONG = " # raise SkipTest(\"{}\")\n" @@ -76,7 +76,7 @@ def _extract_binaries(self, text:str) -> List[str]: return text.split(",") def _script_details( - self, local_path: str) -> (bool, bool, List[str], List[str]): + self, local_path: str) -> Tuple[bool, bool, List[str], List[str]]: run_script = False in_combined = False in_split = False @@ -116,7 +116,8 @@ def _add_not_testing( test_file.write(f"\n # Not testing file due to: {reason}\n") test_file.write(f" # {local_path}\n") - def _add_binaries(self, test_file: TextIOBase, binaries: List[str]): + def _add_binaries( + self, test_file: TextIOBase, binaries: List[str]) -> None: if binaries: binaries = [f'"{binary}"' for binary in binaries] test_file.write(f" self.check_binaries_used(" From fe45571dc3cf65e307abd3c934183bedd2d5c151 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 10 Feb 2026 09:46:40 +0000 Subject: [PATCH 4/6] flake8 --- spinnaker_testbase/root_script_builder.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/spinnaker_testbase/root_script_builder.py b/spinnaker_testbase/root_script_builder.py index 3172caf..dfe3a4d 100644 --- a/spinnaker_testbase/root_script_builder.py +++ b/spinnaker_testbase/root_script_builder.py @@ -52,8 +52,8 @@ def _add_script(self, test_file: TextIOBase, name: str, local_path: str, test_file.write("]") test_file.write(")\n") - def _add_split_script(self, test_file: TextIOBase, name: str, local_path: str, - split: bool) -> None: + def _add_split_script(self, test_file: TextIOBase, name: str, + local_path: str, split: bool) -> None: test_file.write("\n def test_") test_file.write(name) if split: @@ -66,7 +66,7 @@ def _add_split_script(self, test_file: TextIOBase, name: str, local_path: str, test_file.write(f" from {import_text} import run_script\n") test_file.write(f" run_script(split={split})\n") - def _extract_binaries(self, text:str) -> List[str]: + def _extract_binaries(self, text: str) -> List[str]: print(text) text = "".join(text.split()) print(text) @@ -95,7 +95,7 @@ def _script_details( in_combined = True text = line elif "split binaries" in line: - in_split= True + in_split = True text = line elif "__name__" in line: has_main = True @@ -153,9 +153,11 @@ def _add_test_directory( "-", "_") skip_imports = skip_exceptions.get(a_script, None) if run_script: - self._add_split_script(test_file, name, local_path, False) + self._add_split_script( + test_file, name, local_path, False) self._add_binaries(test_file, combined_binaires) - self._add_split_script(test_file, name, local_path, True) + self._add_split_script( + test_file, name, local_path, True) self._add_binaries(test_file, split_binaires) elif has_main: self._add_not_testing( @@ -163,8 +165,8 @@ def _add_test_directory( assert combined_binaires == [] assert split_binaires == [] else: - self._add_script(test_file, name, local_path, skip_imports) - + self._add_script( + test_file, name, local_path, skip_imports) def create_test_scripts( self, dirs: Union[str, List[str]], @@ -218,5 +220,6 @@ def create_test_scripts( with open(test_script, "a", encoding="utf-8") as test_file: for script_dir in dirs: a_dir = os.path.join(repository_dir, script_dir) - self._add_test_directory(a_dir, len(repository_dir) + 1, test_file, - too_long, exceptions, skip_exceptions) + self._add_test_directory( + a_dir, len(repository_dir) + 1, test_file, + too_long, exceptions, skip_exceptions) From 5b2cd8530de3ece0522fdc7572db60995542ab32 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 10 Feb 2026 10:28:35 +0000 Subject: [PATCH 5/6] comments --- spinnaker_testbase/root_script_builder.py | 59 ++++++++++++++++++++--- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/spinnaker_testbase/root_script_builder.py b/spinnaker_testbase/root_script_builder.py index dfe3a4d..4f2a3e6 100644 --- a/spinnaker_testbase/root_script_builder.py +++ b/spinnaker_testbase/root_script_builder.py @@ -32,6 +32,9 @@ class RootScriptBuilder(object): def _add_script(self, test_file: TextIOBase, name: str, local_path: str, skip_imports: Optional[List[str]]) -> None: + """ + Adds a unit test that tests a script by importing it + """ test_file.write("\n def test_") test_file.write(name) test_file.write("(self):\n") @@ -54,6 +57,14 @@ def _add_script(self, test_file: TextIOBase, name: str, local_path: str, def _add_split_script(self, test_file: TextIOBase, name: str, local_path: str, split: bool) -> None: + """ + Adds a test by running a scripts run_script method + + :param test_file: Where to write the test + :param name: Partial name for the test + :param local_path: Path to find the script + :param split: Flag to say if the test should be run split + """ test_file.write("\n def test_") test_file.write(name) if split: @@ -67,25 +78,44 @@ def _add_split_script(self, test_file: TextIOBase, name: str, test_file.write(f" run_script(split={split})\n") def _extract_binaries(self, text: str) -> List[str]: - print(text) + """ + Extracts the binaries from a comments + + :param text: The line(s) that contain the info + :return: List of the binaries expected + """ + # remove all whitespace text = "".join(text.split()) - print(text) + # remove comment markers. There may be one between binaries text = text.replace("#", "") + # ignore the part before the binaries and text = text[text.find("[")+1: text.find("]")] - print(text.split(",")) return text.split(",") def _script_details( self, local_path: str) -> Tuple[bool, bool, List[str], List[str]]: + """ + Examine a script to see which tests should be added + + :param local_path: path to find the script + """ + # Says if a run_script split has been found run_script = False + # Says if there is an if def __main__ + has_main = False + # List of binaries to check for if running not split + combined_binaires = [] + # List of binaries to check for if running split + split_binaires = [] + + # Temp variables when looking at multiline stiff in_combined = False in_split = False text = "" - combined_binaires = [] - split_binaires = [] - has_main = False + with open(local_path, "r", encoding="utf-8") as script_file: for line in script_file: + # second or more line of a comment if in_combined or in_split: text += line elif "def run_script(" in line and " split:" in line: @@ -99,6 +129,7 @@ def _script_details( text = line elif "__name__" in line: has_main = True + # Have we found the end of the binaires list if in_combined: if "]" in line: combined_binaires = self._extract_binaries(text) @@ -113,11 +144,20 @@ def _script_details( def _add_not_testing( self, test_file: TextIOBase, reason: str, local_path: str) -> None: + """ + Adds comments of what is not beiing tested and why + """ test_file.write(f"\n # Not testing file due to: {reason}\n") test_file.write(f" # {local_path}\n") def _add_binaries( self, test_file: TextIOBase, binaries: List[str]) -> None: + """ + Appends binaries checks to a test + + :param test_file: Fle to write check to + :param binaries: List possibly empty of binaries to check + """ if binaries: binaries = [f'"{binary}"' for binary in binaries] test_file.write(f" self.check_binaries_used(" @@ -127,6 +167,9 @@ def _add_test_directory( self, a_dir: str, prefix_len: int, test_file: TextIOBase, too_long: Dict[str, str], exceptions: Dict[str, str], skip_exceptions: Dict[str, List[str]]) -> None: + """ + Adds any required tests for the scripts in a directory + """ for a_script in os.listdir(a_dir): script_path = os.path.join(a_dir, a_script) if os.path.isdir(script_path) and not a_script.startswith("."): @@ -152,6 +195,7 @@ def _add_test_directory( name = local_path[:-3].replace(os.sep, "_").replace( "-", "_") skip_imports = skip_exceptions.get(a_script, None) + # use the run_Scripts method style if run_script: self._add_split_script( test_file, name, local_path, False) @@ -159,14 +203,17 @@ def _add_test_directory( self._add_split_script( test_file, name, local_path, True) self._add_binaries(test_file, split_binaires) + # Due to a main the test will not run if imported elif has_main: self._add_not_testing( test_file, "Unhandled main", local_path) assert combined_binaires == [] assert split_binaires == [] + # Use the import script style else: self._add_script( test_file, name, local_path, skip_imports) + self._add_binaries(test_file, combined_binaires) def create_test_scripts( self, dirs: Union[str, List[str]], From 20b142b0f1ee8714c6d627bd3a172fe28b03aa06 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 10 Feb 2026 10:31:42 +0000 Subject: [PATCH 6/6] spelling --- spinnaker_testbase/root_script_builder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spinnaker_testbase/root_script_builder.py b/spinnaker_testbase/root_script_builder.py index 4f2a3e6..2b21874 100644 --- a/spinnaker_testbase/root_script_builder.py +++ b/spinnaker_testbase/root_script_builder.py @@ -129,7 +129,7 @@ def _script_details( text = line elif "__name__" in line: has_main = True - # Have we found the end of the binaires list + # Have we found the end of the binaries list if in_combined: if "]" in line: combined_binaires = self._extract_binaries(text) @@ -145,7 +145,7 @@ def _script_details( def _add_not_testing( self, test_file: TextIOBase, reason: str, local_path: str) -> None: """ - Adds comments of what is not beiing tested and why + Adds comments of what is not being tested and why """ test_file.write(f"\n # Not testing file due to: {reason}\n") test_file.write(f" # {local_path}\n") @@ -155,7 +155,7 @@ def _add_binaries( """ Appends binaries checks to a test - :param test_file: Fle to write check to + :param test_file: File to write check to :param binaries: List possibly empty of binaries to check """ if binaries: