Skip to content

Commit

Permalink
Avoid crash on custom rules without doc string (#96)
Browse files Browse the repository at this point in the history
* 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 <allburov@gmail.com>

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Aleksey <allburov@gmail.com>
  • Loading branch information
3 people authored Feb 27, 2023
1 parent d336d13 commit 808e211
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions artifactory_cleanup/rules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
19 changes: 19 additions & 0 deletions tests/test_custom_rules.py
Original file line number Diff line number Diff line change
@@ -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() == ""

0 comments on commit 808e211

Please sign in to comment.