Skip to content

Commit

Permalink
utils/escape_attr: enclose with quotes all elements but the first + t…
Browse files Browse the repository at this point in the history
…ests

Signed-off-by: lucasew <[email protected]>
  • Loading branch information
lucasew authored and Mic92 committed Aug 6, 2023
1 parent 720dea6 commit 6252c4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions nixpkgs_review/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_escape_attr.py
Original file line number Diff line number Diff line change
@@ -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"'

0 comments on commit 6252c4f

Please sign in to comment.