From 808e211fd35ec255ada45adf804e08bd638f65c5 Mon Sep 17 00:00:00 2001 From: offa Date: Mon, 27 Feb 2023 04:18:54 +0000 Subject: [PATCH] Avoid crash on custom rules without doc string (#96) * Avoid crash on custom rules without doc string * Add custom rule doc string to docs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Handle edge case first Co-authored-by: Aleksey --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Aleksey --- README.md | 6 +++++- artifactory_cleanup/rules/base.py | 5 +++-- tests/test_custom_rules.py | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/test_custom_rules.py diff --git a/README.md b/README.md index ab576e3..c0a63ec 100644 --- a/README.md +++ b/README.md @@ -388,7 +388,11 @@ from artifactory_cleanup.rules import Rule, ArtifactsList class MySimpleRule(Rule): - """For more methods look at Rule source code""" + """ + This doc string is used as rule title + + For more methods look at Rule source code + """ def __init__(self, my_param: str, value: int): self.my_param = my_param diff --git a/artifactory_cleanup/rules/base.py b/artifactory_cleanup/rules/base.py index 44ed48d..7a69f29 100644 --- a/artifactory_cleanup/rules/base.py +++ b/artifactory_cleanup/rules/base.py @@ -91,8 +91,9 @@ def name(cls) -> str: @classmethod def title(cls) -> str: """Cut the docstring to show only the very first important line""" - docs = [x.strip() for x in cls.__doc__.splitlines() if x][0] - return docs + if not cls.__doc__: + return "" + return [x.strip() for x in cls.__doc__.splitlines() if x][0] def init(self, session, today, *args, **kwargs) -> None: """ diff --git a/tests/test_custom_rules.py b/tests/test_custom_rules.py new file mode 100644 index 0000000..ac4bdb4 --- /dev/null +++ b/tests/test_custom_rules.py @@ -0,0 +1,19 @@ +from artifactory_cleanup.rules import Rule + + +class CustomRuleWithDocs(Rule): + """Example with doc string""" + + +class CustomRuleWithoutDocs(Rule): + pass + + +def test_custom_rule_with_docstring_adds_to_title(): + rule = CustomRuleWithDocs() + assert rule.title() == "Example with doc string" + + +def test_custom_rule_without_docstring(): + rule = CustomRuleWithoutDocs() + assert rule.title() == ""