From 6252c4f75127a494c9822111469e77b2d8baeab8 Mon Sep 17 00:00:00 2001 From: lucasew Date: Sat, 5 Aug 2023 12:59:20 -0300 Subject: [PATCH] utils/escape_attr: enclose with quotes all elements but the first + tests Signed-off-by: lucasew --- nixpkgs_review/utils.py | 8 ++++---- tests/test_escape_attr.py | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 tests/test_escape_attr.py diff --git a/nixpkgs_review/utils.py b/nixpkgs_review/utils.py index c13cbae..125c0cb 100644 --- a/nixpkgs_review/utils.py +++ b/nixpkgs_review/utils.py @@ -39,10 +39,10 @@ def verify_commit_hash(commit: str) -> str: def escape_attr(attr: str) -> str: - index = attr.rfind(".") - if index == -1: - return attr - return f'{attr[:index]}."{attr[index+1:]}"' + attr_parts = attr.split(".") + first = attr_parts[0] + rest = [f'"{item}"' for item in attr_parts[1:]] + return ".".join([first, *rest]) @functools.lru_cache(maxsize=1) diff --git a/tests/test_escape_attr.py b/tests/test_escape_attr.py new file mode 100644 index 0000000..6a20727 --- /dev/null +++ b/tests/test_escape_attr.py @@ -0,0 +1,7 @@ +from nixpkgs_review.utils import escape_attr + + +def test_escape_attr(test_input: str, expected: str) -> None: + assert escape_attr("hello") == "hello" + assert escape_attr("haskellPackages.if") == 'haskellPackages."if"' + assert escape_attr("haskellPackages.if.doc") == 'haskellPackages."if"."doc"'