44
55from gitignore_parser import parse_gitignore
66
7- from unittest import TestCase , main
7+ from unittest import TestCase , main , SkipTest
88
99
1010class Test (TestCase ):
@@ -86,17 +86,15 @@ def test_comment(self):
8686 self .assertTrue (matches ('/home/michael/#imnocomment' ))
8787
8888 def test_ignore_directory (self ):
89- matches = \
90- _parse_gitignore_string ('.venv/' , fake_base_dir = '/home/michael' )
89+ matches = _parse_gitignore_string ('.venv/' , fake_base_dir = '/home/michael' )
9190 self .assertTrue (matches ('/home/michael/.venv' ))
9291 self .assertTrue (matches ('/home/michael/.venv/folder' ))
9392 self .assertTrue (matches ('/home/michael/.venv/file.txt' ))
9493 self .assertFalse (matches ('/home/michael/.venv_other_folder' ))
9594 self .assertFalse (matches ('/home/michael/.venv_no_folder.py' ))
9695
9796 def test_ignore_directory_asterisk (self ):
98- matches = \
99- _parse_gitignore_string ('.venv/*' , fake_base_dir = '/home/michael' )
97+ matches = _parse_gitignore_string ('.venv/*' , fake_base_dir = '/home/michael' )
10098 self .assertFalse (matches ('/home/michael/.venv' ))
10199 self .assertTrue (matches ('/home/michael/.venv/folder' ))
102100 self .assertTrue (matches ('/home/michael/.venv/file.txt' ))
@@ -114,25 +112,20 @@ def test_negation(self):
114112 self .assertTrue (matches ('/home/michael/waste.ignore' ))
115113
116114 def test_literal_exclamation_mark (self ):
117- matches = _parse_gitignore_string (
118- '\\ !ignore_me!' , fake_base_dir = '/home/michael'
119- )
115+ matches = _parse_gitignore_string ('\\ !ignore_me!' , fake_base_dir = '/home/michael' )
120116 self .assertTrue (matches ('/home/michael/!ignore_me!' ))
121117 self .assertFalse (matches ('/home/michael/ignore_me!' ))
122118 self .assertFalse (matches ('/home/michael/ignore_me' ))
123119
124120 def test_double_asterisks (self ):
125- matches = _parse_gitignore_string (
126- 'foo/**/Bar' , fake_base_dir = '/home/michael'
127- )
121+ matches = _parse_gitignore_string ('foo/**/Bar' , fake_base_dir = '/home/michael' )
128122 self .assertTrue (matches ('/home/michael/foo/hello/Bar' ))
129123 self .assertTrue (matches ('/home/michael/foo/world/Bar' ))
130124 self .assertTrue (matches ('/home/michael/foo/Bar' ))
131125 self .assertFalse (matches ('/home/michael/foo/BarBar' ))
132126
133127 def test_double_asterisk_without_slashes_handled_like_single_asterisk (self ):
134- matches = \
135- _parse_gitignore_string ('a/b**c/d' , fake_base_dir = '/home/michael' )
128+ matches = _parse_gitignore_string ('a/b**c/d' , fake_base_dir = '/home/michael' )
136129 self .assertTrue (matches ('/home/michael/a/bc/d' ))
137130 self .assertTrue (matches ('/home/michael/a/bXc/d' ))
138131 self .assertTrue (matches ('/home/michael/a/bbc/d' ))
@@ -143,12 +136,10 @@ def test_double_asterisk_without_slashes_handled_like_single_asterisk(self):
143136 self .assertFalse (matches ('/home/michael/a/bb/XX/cc/d' ))
144137
145138 def test_more_asterisks_handled_like_single_asterisk (self ):
146- matches = \
147- _parse_gitignore_string ('***a/b' , fake_base_dir = '/home/michael' )
139+ matches = _parse_gitignore_string ('***a/b' , fake_base_dir = '/home/michael' )
148140 self .assertTrue (matches ('/home/michael/XYZa/b' ))
149141 self .assertFalse (matches ('/home/michael/foo/a/b' ))
150- matches = \
151- _parse_gitignore_string ('a/b***' , fake_base_dir = '/home/michael' )
142+ matches = _parse_gitignore_string ('a/b***' , fake_base_dir = '/home/michael' )
152143 self .assertTrue (matches ('/home/michael/a/bXYZ' ))
153144 self .assertFalse (matches ('/home/michael/a/b/foo' ))
154145
@@ -166,9 +157,7 @@ def test_directory_only_negation(self):
166157 self .assertFalse (matches ('/home/michael/data/01_raw/raw_file.csv' ))
167158 self .assertFalse (matches ('/home/michael/data/02_processed/' ))
168159 self .assertFalse (matches ('/home/michael/data/02_processed/.gitkeep' ))
169- self .assertTrue (
170- matches ('/home/michael/data/02_processed/processed_file.csv' )
171- )
160+ self .assertTrue (matches ('/home/michael/data/02_processed/processed_file.csv' ))
172161
173162 def test_single_asterisk (self ):
174163 matches = _parse_gitignore_string ('*' , fake_base_dir = '/home/michael' )
@@ -177,16 +166,12 @@ def test_single_asterisk(self):
177166 self .assertTrue (matches ('/home/michael/directory-trailing/' ))
178167
179168 def test_supports_path_type_argument (self ):
180- matches = _parse_gitignore_string (
181- 'file1\n !file2' , fake_base_dir = '/home/michael'
182- )
169+ matches = _parse_gitignore_string ('file1\n !file2' , fake_base_dir = '/home/michael' )
183170 self .assertTrue (matches (Path ('/home/michael/file1' )))
184171 self .assertFalse (matches (Path ('/home/michael/file2' )))
185172
186173 def test_slash_in_range_does_not_match_dirs (self ):
187- matches = _parse_gitignore_string (
188- 'abc[X-Z/]def' , fake_base_dir = '/home/michael'
189- )
174+ matches = _parse_gitignore_string ('abc[X-Z/]def' , fake_base_dir = '/home/michael' )
190175 self .assertFalse (matches ('/home/michael/abcdef' ))
191176 self .assertTrue (matches ('/home/michael/abcXdef' ))
192177 self .assertTrue (matches ('/home/michael/abcYdef' ))
@@ -195,32 +180,32 @@ def test_slash_in_range_does_not_match_dirs(self):
195180 self .assertFalse (matches ('/home/michael/abcXYZdef' ))
196181
197182 def test_symlink_to_another_directory (self ):
198- with TemporaryDirectory () as project_dir :
199- with TemporaryDirectory () as another_dir :
200- matches = \
201- _parse_gitignore_string ('link' , fake_base_dir = project_dir )
202-
203- # Create a symlink to another directory.
204- link = Path (project_dir , 'link' )
205- target = Path (another_dir , 'target' )
183+ """Test the behavior of a symlink to another directory.
184+
185+ The issue https://github.com/mherrmann/gitignore_parser/issues/29 describes how
186+ a symlink to another directory caused an exception to be raised during matching.
187+
188+ This test ensures that the issue is now fixed.
189+ """
190+ with TemporaryDirectory () as project_dir , TemporaryDirectory () as another_dir :
191+ project_dir = Path (project_dir ).resolve ()
192+ another_dir = Path (another_dir ).resolve ()
193+ matches = _parse_gitignore_string ('link' , fake_base_dir = project_dir )
194+
195+ # Create a symlink to another directory.
196+ link = project_dir / 'link'
197+ target = another_dir / 'target'
198+
199+ try :
206200 link .symlink_to (target )
207-
208- # Check the intended behavior according to
209- # https://git-scm.com/docs/gitignore#_notes:
210- # Symbolic links are not followed and are matched as if they
211- # were regular files.
212- self .assertTrue (matches (link ))
213-
214- def test_symlink_to_symlink_directory (self ):
215- with TemporaryDirectory () as project_dir :
216- with TemporaryDirectory () as link_dir :
217- link = Path (link_dir , 'link' )
218- link .symlink_to (project_dir )
219- file = Path (link , 'file.txt' )
220- matches = \
221- _parse_gitignore_string ('file.txt' , fake_base_dir = str (link ))
222- self .assertTrue (matches (file ))
223-
201+ except OSError :
202+ e = "Current user does not have permissions to perform symlink."
203+ raise SkipTest (e )
204+ # Check the intended behavior according to
205+ # https://git-scm.com/docs/gitignore#_notes:
206+ # Symbolic links are not followed and are matched as if they were regular
207+ # files.
208+ self .assertTrue (matches (link ))
224209
225210def _parse_gitignore_string (data : str , fake_base_dir : str = None ):
226211 with patch ('builtins.open' , mock_open (read_data = data )):
@@ -229,3 +214,4 @@ def _parse_gitignore_string(data: str, fake_base_dir: str = None):
229214
230215if __name__ == '__main__' :
231216 main ()
217+
0 commit comments