Skip to content

Commit

Permalink
fix regex escaping once more (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
lime-green authored Nov 13, 2020
1 parent ba01120 commit 04509a9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/unison_gitignore/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ def __repr__(self):

def __str__(self):
s = "-ignore" if self.include else "-ignorenot"
escaped_regex = self.regex.replace("\\.", "\\\\.")
escaped_regex = self.regex.replace("\\~", "~").replace("\\-", "-")
return f"{s}=Regex {escaped_regex}"
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_when_no_path_given_it_uses_local_root(mock_run_cmd):
assert args[0] == "unison"
assert args[1:3] == cmd[1:3]
assert len(args) == 4
assert args[3] == r"-ignore=Regex ^stuff/data/(.+/)?[^/]*\\.py[co](/.*)?$"
assert args[3] == r"-ignore=Regex ^stuff/data/(.+/)?[^/]*\.py[co](/.*)?$"


@pytest.mark.usefixtures("mock_files")
Expand All @@ -122,4 +122,4 @@ def generator(abs_path):
assert args[0] == "unison"
assert args[1:7] == cmd[1:7]
assert len(args) == 8
assert args[7] == r"-ignore=Regex ^path1/(.+/)?[^/]*\\.py[co](/.*)?$"
assert args[7] == r"-ignore=Regex ^path1/(.+/)?[^/]*\.py[co](/.*)?$"
10 changes: 5 additions & 5 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def mock_path():
return "/home/john_doe"


def test_escapes_backslash():
parsed = GitIgnoreToUnisonIgnore("/").parse_gitignore(StringIO("*.log"))
def test_special_characters():
parsed = GitIgnoreToUnisonIgnore("/").parse_gitignore(StringIO("~-*"))
assert len(parsed) == 1
assert str(parsed[0]) == r"-ignore=Regex ^(.+/)?[^/]*\\.log(/.*)?$"
assert str(parsed[0]) == r"-ignore=Regex ^(.+/)?~-[^/]*(/.*)?$"


def test_root_path_removes_slash():
Expand Down Expand Up @@ -54,8 +54,8 @@ def test_wildcard_and_negation_regex(mock_path):
"""
parsed = GitIgnoreToUnisonIgnore(mock_path).parse_gitignore(StringIO(contents))
assert len(parsed) == 2
assert str(parsed[0]) == r"-ignore=Regex ^home/john_doe/(.+/)?[^/]*\\.py[co](/.*)?$"
assert str(parsed[1]) == r"-ignorenot=Regex ^home/john_doe/(.+/)?test\\.pyc$"
assert str(parsed[0]) == r"-ignore=Regex ^home/john_doe/(.+/)?[^/]*\.py[co](/.*)?$"
assert str(parsed[1]) == r"-ignorenot=Regex ^home/john_doe/(.+/)?test\.pyc$"


def test_directories_regex(mock_path):
Expand Down

0 comments on commit 04509a9

Please sign in to comment.