diff --git a/python/grass/gunittest/__main__.py b/python/grass/gunittest/__main__.py new file mode 100644 index 00000000000..c756fa837ae --- /dev/null +++ b/python/grass/gunittest/__main__.py @@ -0,0 +1,21 @@ +"""Main entry point + +From Python3's unittest module +""" + +import sys +if sys.argv[0].endswith("__main__.py"): + import os.path + # We change sys.argv[0] to make help message more useful + # use executable without path, unquoted + # (it's just a hint anyway) + # (if you have spaces in your executable you get what you deserve!) + executable = os.path.basename(sys.executable) + sys.argv[0] = executable + " -m grass.gunittest" + del os + +__unittest = True + +from .main import main # noqa: E402 + +main(module=None) \ No newline at end of file diff --git a/python/grass/gunittest/main.py b/python/grass/gunittest/main.py index f661a3f3c0d..667f2f2b53c 100644 --- a/python/grass/gunittest/main.py +++ b/python/grass/gunittest/main.py @@ -8,14 +8,14 @@ :authors: Vaclav Petras """ - +from __future__ import annotations import os import sys import argparse import configparser from pathlib import Path -from unittest.main import TestProgram +from unittest import TestProgram import grass.script.core as gs @@ -31,13 +31,13 @@ class GrassTestProgram(TestProgram): def __init__( self, - exit_at_end, - grass_location, - clean_outputs=True, + exit_at_end: bool = False, + grass_location=None, + clean_outputs: bool = True, unittest_argv=None, module=None, verbosity=2, - failfast=None, + failfast: bool | None = None, catchbreak=True, **kwargs, ): @@ -51,6 +51,8 @@ def __init__( # buffer stdout and stderr during tests buffer_stdout_stderr = False + if failfast is None: + failfast = False grass_loader = GrassTestLoader(grass_location=self.grass_location) text_result = TextTestResult( @@ -78,6 +80,29 @@ def __init__( buffer=buffer_stdout_stderr, **kwargs, ) + self._discovery_parser: argparse.ArgumentParser + + # def parseArgs(self, argv: list[str]) -> None: + # super().parseArgs(argv) + + def _getDiscoveryArgParser( + self, parent: argparse.ArgumentParser + ) -> argparse.ArgumentParser: + return super()._getDiscoveryArgParser(parent) # type: ignore + + def _getMainArgParser( + self, parent: argparse.ArgumentParser + ) -> argparse.ArgumentParser: + return super()._getMainArgParser(parent) # type: ignore + + def _initArgParsers(self): + self.parent_parser = self._getParentArgParser() # type: ignore + self.parent_parser = getGrassTestProgramParser(self.parent_parser) + self._main_parser = self._getMainArgParser(self.parent_parser) + self._discovery_parser = self._getDiscoveryArgParser(self.parent_parser) + # self._discovery_parser = getGrassTestProgramParser(self._discovery_parser) + # self._discovery_parser = getGrassTestProgramParser(self._discovery_parser) + # self._main_parser = getGrassTestProgramParser(self._main_parser) def test(): @@ -122,7 +147,7 @@ def discovery(): Runs using:: python main.py discovery [start_directory] """ - aaa = 1/0 + aaa = 1 / 0 program = GrassTestProgram( grass_location="nc", exit_at_end=False, verbosity=verbosity ) @@ -160,67 +185,9 @@ def get_config(start_directory, config_file): verbosity = 1 -def main(): +def main_function(): + parser = getGrassTestProgramParser() - parser = argparse.ArgumentParser( - description="Run test files in all testsuite directories starting" - " from the current one" - " (runs on active GRASS session)" - ) - parser.add_argument( - "--location", - dest="location", - action="store", - help="Name of location where to perform test", - required=True, - ) - parser.add_argument( - "--location-type", - dest="location_type", - action="store", - default="nc", - help="Type of tests which should be run" " (tag corresponding to location)", - ) - parser.add_argument( - "--grassdata", - dest="gisdbase", - action="store", - default=None, - help="GRASS data(base) (GISDBASE) directory" " (current GISDBASE by default)", - ) - parser.add_argument( - "--output", - dest="output", - action="store", - default="testreport", - help="Output directory", - ) - parser.add_argument( - "--min-success", - dest="min_success", - action="store", - default="100", - type=int, - help=( - "Minimum success percentage (lower percentage" - " than this will result in a non-zero return code; values 0-100)" - ), - ) - parser.add_argument( - "--config", - dest="config", - action="store", - type=str, - help=f"Path to a configuration file (default: {CONFIG_FILENAME})", - ) - parser.add_argument( - "-v", - "--verbose", - dest="verbosity", - action="store_const", - const=2, - help="Verbose output", - ) args = parser.parse_args() gisdbase = args.gisdbase if gisdbase is None: @@ -272,5 +239,129 @@ def main(): return 1 +def getGrassTestProgramParser(parent: argparse.ArgumentParser | None = None): + if parent is None: + parent = argparse.ArgumentParser() + + parser = argparse.ArgumentParser(parents=[parent], add_help=False) + parser.description = ( + "Run test files in all testsuite directories starting" + " from the current one" + " (runs on active GRASS session)" + ) + group = parser.add_argument_group( + "grass.gunittest options", "Options related to GRASS gunittest module" + ) + group.add_argument( + "--location", + dest="location", + action="store", + help="Name of location where to perform test", + required=True, + ) + group.add_argument( + "--location-type", + dest="location_type", + action="store", + default="nc", + help="Type of tests which should be run" " (tag corresponding to location)", + ) + group.add_argument( + "--grassdata", + dest="gisdbase", + action="store", + default=None, + help="GRASS data(base) (GISDBASE) directory" " (current GISDBASE by default)", + ) + group.add_argument( + "--output", + dest="output", + action="store", + default="testreport", + help="Output directory", + ) + group.add_argument( + "--min-success", + dest="min_success", + action="store", + default="100", + type=int, + help=( + "Minimum success percentage (lower percentage" + " than this will result in a non-zero return code; values 0-100)" + ), + ) + group.add_argument( + "--config", + dest="config", + action="store", + type=str, + help=f"Path to a configuration file (default: {CONFIG_FILENAME})", + ) + # parser.add_argument( + # "--location", + # dest="location", + # action="store", + # help="Name of location where to perform test", + # required=True, + # ) + # parser.add_argument( + # "--location-type", + # dest="location_type", + # action="store", + # default="nc", + # help="Type of tests which should be run" " (tag corresponding to location)", + # ) + # parser.add_argument( + # "--grassdata", + # dest="gisdbase", + # action="store", + # default=None, + # help="GRASS data(base) (GISDBASE) directory" " (current GISDBASE by default)", + # ) + # parser.add_argument( + # "--output", + # dest="output", + # action="store", + # default="testreport", + # help="Output directory", + # ) + # parser.add_argument( + # "--min-success", + # dest="min_success", + # action="store", + # default="100", + # type=int, + # help=( + # "Minimum success percentage (lower percentage" + # " than this will result in a non-zero return code; values 0-100)" + # ), + # ) + # parser.add_argument( + # "--config", + # dest="config", + # action="store", + # type=str, + # help=f"Path to a configuration file (default: {CONFIG_FILENAME})", + # ) + + return parser + + +main = GrassTestProgram + if __name__ == "__main__": - sys.exit(main()) + # print("hello") + # main(module=None) + + # sys.exit(main_function()) + import os.path + + # We change sys.argv[0] to make help message more useful + # use executable without path, unquoted + # (it's just a hint anyway) + # (if you have spaces in your executable you get what you deserve!) + executable = os.path.basename(sys.executable) + sys.argv[0] = executable + " -m grass.gunittest" + del os + main(module=None)