From ba01120fc1d55059ecbd328b5816c474eaccfbf4 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 16 Aug 2020 20:19:26 -0400 Subject: [PATCH] fix dot star output (#5) --- src/unison_gitignore/parser.py | 3 ++- tests/test_main.py | 4 ++-- tests/test_parser.py | 10 ++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/unison_gitignore/parser.py b/src/unison_gitignore/parser.py index 07527bb..2688e1a 100755 --- a/src/unison_gitignore/parser.py +++ b/src/unison_gitignore/parser.py @@ -104,4 +104,5 @@ def __repr__(self): def __str__(self): s = "-ignore" if self.include else "-ignorenot" - return f"{s}=Regex {self.regex}" + escaped_regex = self.regex.replace("\\.", "\\\\.") + return f"{s}=Regex {escaped_regex}" diff --git a/tests/test_main.py b/tests/test_main.py index 972ab30..7229646 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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] == "-ignore=Regex ^stuff/data/(.+/)?[^/]*\\.py[co](/.*)?$" + assert args[3] == r"-ignore=Regex ^stuff/data/(.+/)?[^/]*\\.py[co](/.*)?$" @pytest.mark.usefixtures("mock_files") @@ -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] == "-ignore=Regex ^path1/(.+/)?[^/]*\\.py[co](/.*)?$" + assert args[7] == r"-ignore=Regex ^path1/(.+/)?[^/]*\\.py[co](/.*)?$" diff --git a/tests/test_parser.py b/tests/test_parser.py index 144d3c3..25b4c3a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -10,6 +10,12 @@ def mock_path(): return "/home/john_doe" +def test_escapes_backslash(): + parsed = GitIgnoreToUnisonIgnore("/").parse_gitignore(StringIO("*.log")) + assert len(parsed) == 1 + assert str(parsed[0]) == r"-ignore=Regex ^(.+/)?[^/]*\\.log(/.*)?$" + + def test_root_path_removes_slash(): parsed = GitIgnoreToUnisonIgnore("/").parse_gitignore(StringIO("test.py")) assert len(parsed) == 1 @@ -48,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):