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 committed Aug 5, 2023
1 parent 720dea6 commit d9e8b49
Show file tree
Hide file tree
Showing 2 changed files with 18 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
14 changes: 14 additions & 0 deletions tests/test_escape_attr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest

from nixpkgs_review.utils import escape_attr


@pytest.mark.parametrize(
"test_input,expected",
[
("haskellPackages.if", 'haskellPackages."if"'),
("haskellPackages.if.doc", 'haskellPackages."if"."doc"'),
],
)
def test_escape_attr(test_input: str, expected: str) -> None:
assert escape_attr(test_input) == expected

0 comments on commit d9e8b49

Please sign in to comment.