diff --git a/gitignore_parser.py b/gitignore_parser.py index f5c9425..716d039 100644 --- a/gitignore_parser.py +++ b/gitignore_parser.py @@ -193,7 +193,7 @@ def fnmatch_pathname_to_regex( if j >= n: res.append('\\[') else: - stuff = pattern[i:j].replace('\\', '\\\\') + stuff = pattern[i:j].replace('\\', '\\\\').replace('/', '') i = j + 1 if stuff[0] == '!': stuff = ''.join(['^', stuff[1:]]) diff --git a/tests.py b/tests.py index 3ae78b5..7311eef 100644 --- a/tests.py +++ b/tests.py @@ -135,6 +135,15 @@ def test_supports_path_type_argument(self): self.assertTrue(matches(Path('/home/michael/file1'))) self.assertFalse(matches(Path('/home/michael/file2'))) + def test_slash_in_range_does_not_match_dirs(self): + matches = _parse_gitignore_string('abc[X-Z/]def', fake_base_dir='/home/michael') + self.assertFalse(matches('/home/michael/abcdef')) + self.assertTrue(matches('/home/michael/abcXdef')) + self.assertTrue(matches('/home/michael/abcYdef')) + self.assertTrue(matches('/home/michael/abcZdef')) + self.assertFalse(matches('/home/michael/abc/def')) + self.assertFalse(matches('/home/michael/abcXYZdef')) + def _parse_gitignore_string(data: str, fake_base_dir: str = None): with patch('builtins.open', mock_open(read_data=data)): success = parse_gitignore(f'{fake_base_dir}/.gitignore', fake_base_dir)