aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-02-05 18:13:53 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-02-05 18:13:53 +0100
commitd8b99515b6c5e1e89c232834402c0bbaf5f034d8 (patch)
treedec0b9fe9b3b04276c615d5996e66febe644d1ae
parentb496ff9bb11949a4739ba44c75f2f5504a094fdb (diff)
downloadminishell_test-d8b99515b6c5e1e89c232834402c0bbaf5f034d8.tar.gz
minishell_test-d8b99515b6c5e1e89c232834402c0bbaf5f034d8.tar.bz2
minishell_test-d8b99515b6c5e1e89c232834402c0bbaf5f034d8.zip
Refactoring Suite with custom exception instead of print then exit
-rwxr-xr-xminishell_test/__main__.py8
-rw-r--r--minishell_test/suite/suite.py39
2 files changed, 35 insertions, 12 deletions
diff --git a/minishell_test/__main__.py b/minishell_test/__main__.py
index 424867c..0b01ba8 100755
--- a/minishell_test/__main__.py
+++ b/minishell_test/__main__.py
@@ -21,7 +21,7 @@ import subprocess
import minishell_test.config as config
import minishell_test.sandbox as sandbox
from minishell_test.args import parse_args
-from minishell_test.suite.suite import Suite
+from minishell_test.suite.suite import Suite, SuiteException
from minishell_test.suites import * # noqa: F403,F401
@@ -72,7 +72,11 @@ def main():
if config.RANGE is not None or config.CHECK_LEAKS:
config.SHOW_RANGE = True
- Suite.setup(args.suites)
+ try:
+ Suite.setup(args.suites)
+ except SuiteException as e:
+ print(e)
+ sys.exit(1)
try:
Suite.run_all()
except KeyboardInterrupt:
diff --git a/minishell_test/suite/suite.py b/minishell_test/suite/suite.py
index 8fd39bf..002f2b6 100644
--- a/minishell_test/suite/suite.py
+++ b/minishell_test/suite/suite.py
@@ -6,17 +6,42 @@
# By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/07/15 18:24:29 by charles #+# #+# #
-# Updated: 2021/02/05 18:01:33 by charles ### ########.fr #
+# Updated: 2021/02/05 18:13:00 by charles ### ########.fr #
# #
# ############################################################################ #
-import sys
from typing import List, Tuple, Optional, Callable
import minishell_test.config as config
from minishell_test.test import Test
+class SuiteException(Exception):
+ """ Base exception for suite """
+ pass
+
+
+class AmbiguousNameException(SuiteException):
+ def __init__(self, name: str, matches: List[str]):
+ self.name = name
+ self.matches = matches
+
+ def __str__(self) -> str:
+ return (("Ambiguous name `{}` match the following suites\n\t{}\n"
+ "Try to run with -l to see the available suites")
+ .format(self.name, ', '.join(self.matches)))
+
+
+class NoMatchException(SuiteException):
+ def __init__(self, name: str):
+ self.name = name
+
+ def __str__(self) -> str:
+ return (("Name `{}` doesn't match any suite/group name\n\t"
+ "Try to run with -l to see the available suites")
+ .format(self.name))
+
+
class Suite:
available: List['Suite'] = []
@@ -52,15 +77,9 @@ class Suite:
elif len(matches) != 0 and all(n.startswith(name) for n in matches):
names.extend(matches)
elif len(matches) > 2:
- print(("Ambiguous name `{}` match the following suites\n\t{}\n"
- "Try to run with -l to see the available suites")
- .format(name, ', '.join(matches)))
- sys.exit(1)
+ raise AmbiguousNameException(name, matches)
elif len(matches) == 0:
- print(("Name `{}` doesn't match any suite/group name\n\t"
- "Try to run with -l to see the available suites")
- .format(name))
- sys.exit(1)
+ raise NoMatchException(name)
cls.available = list(set(
[s for s in cls.available if s.name in names] +