How to use is_dir method in lisa

Best Python code snippet using lisa_python

test_match.py

Source:test_match.py Github

copy

Full Screen

1import io2import os3import sys4import tempfile5import unittest6import unittest.mock7import gitignorefile8class TestMatch(unittest.TestCase):9 def test_simple(self):10 matches = self.__parse_gitignore_string(["__pycache__/", "*.py[cod]"], mock_base_path="/home/michael")11 for is_dir in (False, True):12 with self.subTest(i=is_dir):13 self.assertFalse(matches("/home/michael/main.py", is_dir=is_dir))14 self.assertTrue(matches("/home/michael/main.pyc", is_dir=is_dir))15 self.assertTrue(matches("/home/michael/dir/main.pyc", is_dir=is_dir))16 self.assertFalse(matches("/home/michael/__pycache__", is_dir=False))17 self.assertTrue(matches("/home/michael/__pycache__", is_dir=True))18 def test_simple_without_trailing_slash(self):19 matches = self.__parse_gitignore_string(["__pycache__", "*.py[cod]"], mock_base_path="/home/michael")20 for is_dir in (False, True):21 with self.subTest(i=is_dir):22 self.assertFalse(matches("/home/michael/main.py", is_dir=is_dir))23 self.assertTrue(matches("/home/michael/main.pyc", is_dir=is_dir))24 self.assertTrue(matches("/home/michael/dir/main.pyc", is_dir=is_dir))25 self.assertTrue(matches("/home/michael/__pycache__", is_dir=is_dir))26 def test_wildcard(self):27 matches = self.__parse_gitignore_string(["hello.*"], mock_base_path="/home/michael")28 for is_dir in (False, True):29 with self.subTest(i=is_dir):30 self.assertTrue(matches("/home/michael/hello.txt", is_dir=is_dir))31 self.assertTrue(matches("/home/michael/hello.foobar", is_dir=is_dir))32 self.assertTrue(matches("/home/michael/dir/hello.txt", is_dir=is_dir))33 if os.name != "nt": # Invalid paths on Windows will be normalized in `os.path.abspath`.34 self.assertTrue(matches("/home/michael/hello.", is_dir=is_dir))35 self.assertFalse(matches("/home/michael/hello", is_dir=is_dir))36 self.assertFalse(matches("/home/michael/helloX", is_dir=is_dir))37 def test_anchored_wildcard(self):38 matches = self.__parse_gitignore_string(["/hello.*"], mock_base_path="/home/michael")39 for is_dir in (False, True):40 with self.subTest(i=is_dir):41 self.assertTrue(matches("/home/michael/hello.txt", is_dir=is_dir))42 self.assertTrue(matches("/home/michael/hello.c", is_dir=is_dir))43 self.assertFalse(matches("/home/michael/a/hello.java", is_dir=is_dir))44 def test_outside_of_base_path(self):45 matches = self.__parse_gitignore_string(["/hello.*"], mock_base_path="/home/michael")46 for is_dir in (False, True):47 with self.subTest(i=is_dir):48 self.assertFalse(matches("/home/heather/hello.txt", is_dir=is_dir))49 self.assertFalse(matches("/home/heather/hello.c", is_dir=is_dir))50 def test_trailingspaces(self):51 matches = self.__parse_gitignore_string(52 [53 "ignoretrailingspace ",54 "notignoredspace\\ ",55 "partiallyignoredspace\\ ",56 "partiallyignoredspace2 \\ ",57 "notignoredmultiplespace\\ \\ \\ ",58 ],59 mock_base_path="/home/michael",60 )61 for is_dir in (False, True):62 with self.subTest(i=is_dir):63 self.assertTrue(matches("/home/michael/ignoretrailingspace", is_dir=is_dir))64 if os.name != "nt": # Invalid paths on Windows will be normalized in `os.path.abspath`.65 self.assertFalse(matches("/home/michael/ignoretrailingspace ", is_dir=is_dir))66 self.assertTrue(matches("/home/michael/partiallyignoredspace ", is_dir=is_dir))67 self.assertFalse(matches("/home/michael/partiallyignoredspace ", is_dir=is_dir))68 self.assertTrue(matches("/home/michael/partiallyignoredspace2 ", is_dir=is_dir))69 self.assertFalse(matches("/home/michael/partiallyignoredspace2 ", is_dir=is_dir))70 self.assertFalse(matches("/home/michael/partiallyignoredspace2 ", is_dir=is_dir))71 self.assertTrue(matches("/home/michael/notignoredspace ", is_dir=is_dir))72 self.assertTrue(matches("/home/michael/notignoredmultiplespace ", is_dir=is_dir))73 self.assertFalse(matches("/home/michael/partiallyignoredspace", is_dir=is_dir))74 self.assertFalse(matches("/home/michael/partiallyignoredspace2", is_dir=is_dir))75 self.assertFalse(matches("/home/michael/notignoredspace", is_dir=is_dir))76 self.assertFalse(matches("/home/michael/notignoredmultiplespace", is_dir=is_dir))77 def test_comment(self):78 matches = self.__parse_gitignore_string(79 ["somematch", "#realcomment", "othermatch", "\\#imnocomment"],80 mock_base_path="/home/michael",81 )82 for is_dir in (False, True):83 with self.subTest(i=is_dir):84 self.assertTrue(matches("/home/michael/somematch", is_dir=is_dir))85 self.assertFalse(matches("/home/michael/#realcomment", is_dir=is_dir))86 self.assertTrue(matches("/home/michael/othermatch", is_dir=is_dir))87 self.assertTrue(matches("/home/michael/#imnocomment", is_dir=is_dir))88 def test_second_level_directories(self):89 """90 For example, a pattern `doc/frotz/` matches `doc/frotz` directory, but not `a/doc/frotz` directory;91 however `frotz/` matches `frotz` and `a/frotz` that is a directory (all paths are relative from the92 `.gitignore` file). See https://git-scm.com/docs/gitignore .93 """94 matches = self.__parse_gitignore_string(["doc/frotz/"], mock_base_path="/home/michael")95 self.assertFalse(matches("/home/michael/doc/frotz", is_dir=False))96 self.assertTrue(matches("/home/michael/doc/frotz", is_dir=True))97 for is_dir in (False, True):98 with self.subTest(i=is_dir):99 self.assertFalse(matches("/home/michael/a/doc/frotz", is_dir=is_dir))100 def test_second_level_directories_unchained(self):101 matches = self.__parse_gitignore_string(["**/doc/frotz/"], mock_base_path="/home/michael")102 self.assertFalse(matches("/home/michael/doc/frotz", is_dir=False))103 self.assertTrue(matches("/home/michael/doc/frotz", is_dir=True))104 self.assertFalse(matches("/home/michael/a/doc/frotz", is_dir=False))105 self.assertTrue(matches("/home/michael/a/doc/frotz", is_dir=True))106 self.assertFalse(matches("/home/michael/a/b/doc/frotz", is_dir=False))107 self.assertTrue(matches("/home/michael/a/b/doc/frotz", is_dir=True))108 for is_dir in (False, True):109 with self.subTest(i=is_dir):110 self.assertTrue(matches("/home/michael/doc/frotz/file", is_dir=False))111 self.assertTrue(matches("/home/michael/doc/frotz/file", is_dir=True))112 self.assertTrue(matches("/home/michael/a/doc/frotz/file", is_dir=False))113 self.assertTrue(matches("/home/michael/a/doc/frotz/file", is_dir=True))114 self.assertTrue(matches("/home/michael/a/b/doc/frotz/file", is_dir=False))115 self.assertTrue(matches("/home/michael/a/b/doc/frotz/file", is_dir=True))116 def test_second_level_files(self):117 matches = self.__parse_gitignore_string(["doc/frotz"], mock_base_path="/home/michael")118 for is_dir in (False, True):119 with self.subTest(i=is_dir):120 self.assertTrue(matches("/home/michael/doc/frotz", is_dir=is_dir))121 self.assertFalse(matches("/home/michael/a/doc/frotz", is_dir=is_dir))122 def test_ignore_file(self):123 matches = self.__parse_gitignore_string([".venv"], mock_base_path="/home/michael")124 for is_dir in (False, True):125 with self.subTest(i=is_dir):126 self.assertTrue(matches("/home/michael/.venv", is_dir=is_dir))127 self.assertTrue(matches("/home/michael/.venv/folder", is_dir=is_dir))128 self.assertTrue(matches("/home/michael/.venv/file.txt", is_dir=is_dir))129 def test_ignore_core_file(self):130 matches = self.__parse_gitignore_string(["core", "!core/"], mock_base_path="/home/michael")131 for is_dir in (False, True):132 with self.subTest(i=is_dir):133 self.assertFalse(matches("/home/michael/core/a", is_dir=is_dir))134 self.assertTrue(matches("/home/michael/core", is_dir=False))135 self.assertFalse(matches("/home/michael/core", is_dir=True))136 self.assertTrue(matches("/home/michael/a/core", is_dir=False))137 self.assertFalse(matches("/home/michael/a/core", is_dir=True))138 def test_ignore_directory(self):139 matches = self.__parse_gitignore_string([".venv/"], mock_base_path="/home/michael")140 for is_dir in (False, True):141 with self.subTest(i=is_dir):142 self.assertTrue(matches("/home/michael/.venv/folder", is_dir=is_dir))143 self.assertTrue(matches("/home/michael/.venv/file.txt", is_dir=is_dir))144 self.assertFalse(matches("/home/michael/.venv", is_dir=False))145 self.assertTrue(matches("/home/michael/.venv", is_dir=True))146 def test_ignore_directory_greedy(self):147 matches = self.__parse_gitignore_string([".venv"], mock_base_path="/home/michael")148 for is_dir in (False, True):149 with self.subTest(i=is_dir):150 self.assertFalse(matches("/home/michael/.venvlol", is_dir=is_dir))151 def test_ignore_file_greedy(self):152 matches = self.__parse_gitignore_string([".venv/"], mock_base_path="/home/michael")153 for is_dir in (False, True):154 with self.subTest(i=is_dir):155 self.assertFalse(matches("/home/michael/.venvlol", is_dir=is_dir))156 def test_ignore_directory_asterisk(self):157 matches = self.__parse_gitignore_string([".venv/*"], mock_base_path="/home/michael")158 for is_dir in (False, True):159 with self.subTest(i=is_dir):160 self.assertFalse(matches("/home/michael/.venv", is_dir=is_dir))161 self.assertTrue(matches("/home/michael/.venv/folder", is_dir=is_dir))162 def test_negation(self):163 matches = self.__parse_gitignore_string(164 ["*.ignore", "!keep.ignore"],165 mock_base_path="/home/michael",166 )167 for is_dir in (False, True):168 with self.subTest(i=is_dir):169 self.assertTrue(matches("/home/michael/trash.ignore", is_dir=is_dir))170 self.assertFalse(matches("/home/michael/keep.ignore", is_dir=is_dir))171 self.assertTrue(matches("/home/michael/waste.ignore", is_dir=is_dir))172 def test_double_asterisks(self):173 matches = self.__parse_gitignore_string(["foo/**/Bar"], mock_base_path="/home/michael")174 for is_dir in (False, True):175 with self.subTest(i=is_dir):176 self.assertTrue(matches("/home/michael/foo/hello/Bar", is_dir=is_dir))177 self.assertTrue(matches("/home/michael/foo/hello/world/Bar", is_dir=is_dir))178 self.assertTrue(matches("/home/michael/foo/world/Bar", is_dir=is_dir))179 self.assertTrue(matches("/home/michael/foo/Bar", is_dir=is_dir))180 def test_single_asterisk(self):181 matches = self.__parse_gitignore_string(["*"], mock_base_path="/home/michael")182 for is_dir in (False, True):183 with self.subTest(i=is_dir):184 self.assertTrue(matches("/home/michael/file.txt", is_dir=is_dir))185 self.assertTrue(matches("/home/michael/directory/file.txt", is_dir=is_dir))186 def test_spurious_matches(self):187 matches = self.__parse_gitignore_string(["abc"], mock_base_path="/home/michael")188 for is_dir in (False, True):189 with self.subTest(i=is_dir):190 self.assertFalse(matches("/home/michael/abc.txt", is_dir=is_dir))191 self.assertFalse(matches("/home/michael/file-abc.txt", is_dir=is_dir))192 self.assertFalse(matches("/home/michael/fileabc", is_dir=is_dir))193 self.assertFalse(matches("/home/michael/directoryabc-trailing", is_dir=is_dir))194 self.assertFalse(matches("/home/michael/abc-suffixed/file.txt", is_dir=is_dir))195 self.assertFalse(matches("/home/michael/subdir/abc.txt", is_dir=is_dir))196 self.assertFalse(matches("/home/michael/subdir/directoryabc", is_dir=is_dir))197 self.assertFalse(matches("/home/michael/subdir/directory-abc-trailing", is_dir=is_dir))198 self.assertFalse(matches("/home/michael/subdir/directory-abc-trailing/file.txt", is_dir=is_dir))199 def test_spurious_matches_with_asterisks(self):200 matches = self.__parse_gitignore_string(["xyz/**/abc"], mock_base_path="/home/michael")201 for is_dir in (False, True):202 with self.subTest(i=is_dir):203 self.assertTrue(matches("/home/michael/xyz/uvw/abc", is_dir=is_dir))204 self.assertFalse(matches("/home/michael/xyz/uvwabc", is_dir=is_dir))205 self.assertFalse(matches("/home/michael/xyzuvw/abc", is_dir=is_dir))206 self.assertFalse(matches("/home/michael/xyzuvwabc", is_dir=is_dir))207 def test_double_asterisks_start(self):208 matches = self.__parse_gitignore_string(["**/abc"], mock_base_path="/home/michael")209 for is_dir in (False, True):210 with self.subTest(i=is_dir):211 self.assertTrue(matches("/home/michael/xyz/uvw/abc", is_dir=is_dir))212 self.assertFalse(matches("/home/michael/xyz/uvwabc", is_dir=is_dir))213 self.assertTrue(matches("/home/michael/xyzuvw/abc", is_dir=is_dir))214 self.assertFalse(matches("/home/michael/xyzuvwabc", is_dir=is_dir))215 def test_double_asterisks_end(self):216 matches = self.__parse_gitignore_string(["xyz/**"], mock_base_path="/home/michael")217 for is_dir in (False, True):218 with self.subTest(i=is_dir):219 self.assertTrue(matches("/home/michael/xyz/uvw/abc", is_dir=is_dir))220 self.assertTrue(matches("/home/michael/xyz/uvwabc", is_dir=is_dir))221 self.assertFalse(matches("/home/michael/xyzuvw/abc", is_dir=is_dir))222 self.assertFalse(matches("/home/michael/xyzuvwabc", is_dir=is_dir))223 def test_single_asterisk_start(self):224 matches = self.__parse_gitignore_string(["*/abc"], mock_base_path="/home/michael")225 for is_dir in (False, True):226 with self.subTest(i=is_dir):227 self.assertFalse(matches("/home/michael/xyz/uvw/abc", is_dir=is_dir))228 self.assertFalse(matches("/home/michael/xyz/uvwabc", is_dir=is_dir))229 self.assertTrue(matches("/home/michael/xyzuvw/abc", is_dir=is_dir))230 self.assertFalse(matches("/home/michael/xyzuvwabc", is_dir=is_dir))231 def test_single_asterisk_end(self):232 matches = self.__parse_gitignore_string(["xyz/*"], mock_base_path="/home/michael")233 for is_dir in (False, True):234 with self.subTest(i=is_dir):235 self.assertTrue(matches("/home/michael/xyz/uvw/abc", is_dir=is_dir))236 self.assertTrue(matches("/home/michael/xyz/uvwabc", is_dir=is_dir))237 self.assertFalse(matches("/home/michael/xyzuvw/abc", is_dir=is_dir))238 self.assertFalse(matches("/home/michael/xyzuvwabc", is_dir=is_dir))239 def test_does_not_fail_with_symlinks(self):240 with tempfile.TemporaryDirectory() as d:241 matches = self.__parse_gitignore_string(["*.venv"], mock_base_path=d)242 os.makedirs(f"{d}/.venv/bin")243 os.symlink(sys.executable, f"{d}/.venv/bin/python")244 self.assertTrue(matches(f"{d}/.venv/bin/python"))245 def test_single_letter(self):246 matches = self.__parse_gitignore_string(["a"], mock_base_path="/home/michael")247 for is_dir in (False, True):248 with self.subTest(i=is_dir):249 self.assertTrue(matches("/home/michael/a", is_dir=is_dir))250 self.assertFalse(matches("/home/michael/b", is_dir=is_dir))251 self.assertTrue(matches("/home/michael/b/a", is_dir=is_dir))252 self.assertTrue(matches("/home/michael/a/b", is_dir=is_dir))253 def test_exclude_directories(self):254 matches = self.__parse_gitignore_string(["*.yaml", "!*.yaml/"], mock_base_path="/home/michael")255 self.assertTrue(matches("/home/michael/file.yaml", is_dir=False))256 self.assertFalse(matches("/home/michael/file.yaml", is_dir=True))257 self.assertFalse(matches("/home/michael/dir.yaml/file.sql", is_dir=False))258 def test_excludes_nested(self):259 matches = self.__parse_gitignore_string(["/*", "!/foo", "/foo/*", "!/foo/bar"], mock_base_path="/home/michael")260 for is_dir in (False, True):261 with self.subTest(i=is_dir):262 self.assertTrue(matches("/home/michael/oo", is_dir=is_dir))263 self.assertFalse(matches("/home/michael/foo", is_dir=is_dir))264 self.assertTrue(matches("/home/michael/foo/ar", is_dir=is_dir))265 self.assertFalse(matches("/home/michael/foo/bar", is_dir=is_dir))266 self.assertFalse(matches("/home/michael/foo/bar/hey", is_dir=is_dir))267 def test_excludes_direct(self):268 matches = self.__parse_gitignore_string(["/*", "!/foo/bar"], mock_base_path="/home/michael")269 for is_dir in (False, True):270 with self.subTest(i=is_dir):271 self.assertTrue(matches("/home/michael/oo", is_dir=is_dir))272 self.assertTrue(matches("/home/michael/foo", is_dir=is_dir))273 self.assertTrue(matches("/home/michael/foo/ar", is_dir=is_dir))274 self.assertFalse(matches("/home/michael/foo/bar", is_dir=is_dir))275 self.assertFalse(matches("/home/michael/foo/bar/hey", is_dir=is_dir))276 def test_exclude_from_subdirectory(self):277 matches = self.__parse_gitignore_string(278 ["*.log", "!important/*.log", "trace.*"], mock_base_path="/home/michael"279 )280 for is_dir in (False, True):281 with self.subTest(i=is_dir):282 self.assertTrue(matches("/home/michael/a.log", is_dir=is_dir))283 self.assertFalse(matches("/home/michael/b.txt", is_dir=is_dir))284 self.assertFalse(matches("/home/michael/important", is_dir=is_dir))285 self.assertFalse(matches("/home/michael/important/d.log", is_dir=is_dir))286 self.assertFalse(matches("/home/michael/important/e.txt", is_dir=is_dir))287 self.assertTrue(matches("/home/michael/trace.c", is_dir=is_dir))288 def test_ignore_all_subdirectories(self):289 matches = self.__parse_gitignore_string(["**/"], mock_base_path="/home/michael")290 for is_dir in (False, True):291 with self.subTest(i=is_dir):292 self.assertTrue(matches("/home/michael/directory/file", is_dir=is_dir))293 self.assertFalse(matches("/home/michael/file.txt", is_dir=False))294 self.assertTrue(matches("/home/michael/directory", is_dir=True))295 def test_robert_simple_rules(self):296 matches = self.__parse_gitignore_string(["__pycache__", "*.py[cod]", ".venv/"], mock_base_path="/home/robert")297 for is_dir in (False, True):298 with self.subTest(i=is_dir):299 self.assertFalse(matches("/home/robert/main.py", is_dir=is_dir))300 self.assertTrue(matches("/home/robert/dir/main.pyc", is_dir=is_dir))301 self.assertTrue(matches("/home/robert/__pycache__", is_dir=is_dir))302 self.assertTrue(matches("/home/robert/.venv/folder", is_dir=is_dir))303 self.assertTrue(matches("/home/robert/.venv/file.txt", is_dir=is_dir))304 self.assertTrue(matches("/home/robert/.venv/folder/file.txt", is_dir=is_dir))305 self.assertTrue(matches("/home/robert/.venv/folder/folder", is_dir=is_dir))306 self.assertTrue(matches("/home/robert/.venv", is_dir=True))307 self.assertFalse(matches("/home/robert/.venv", is_dir=False))308 def test_robert_comments(self):309 matches = self.__parse_gitignore_string(310 ["somematch", "#realcomment", "othermatch", "\\#imnocomment"], mock_base_path="/home/robert"311 )312 for is_dir in (False, True):313 with self.subTest(i=is_dir):314 self.assertTrue(matches("/home/robert/somematch", is_dir=is_dir))315 self.assertFalse(matches("/home/robert/#realcomment", is_dir=is_dir))316 self.assertTrue(matches("/home/robert/othermatch", is_dir=is_dir))317 self.assertFalse(matches("/home/robert", is_dir=is_dir))318 self.assertFalse(matches("/home/robert/", is_dir=is_dir))319 self.assertFalse(matches("/home/robert/\\", is_dir=is_dir))320 self.assertTrue(matches("/home/robert/#imnocomment", is_dir=is_dir))321 def test_robert_wildcard(self):322 matches = self.__parse_gitignore_string(["hello.*"], mock_base_path="/home/robert")323 for is_dir in (False, True):324 with self.subTest(i=is_dir):325 self.assertTrue(matches("/home/robert/hello.txt", is_dir=is_dir))326 self.assertTrue(matches("/home/robert/dir/hello.txt", is_dir=is_dir))327 self.assertFalse(matches("/home/robert/dir/shello.txt", is_dir=is_dir))328 if os.name != "nt": # Invalid paths on Windows will be normalized in `os.path.abspath`.329 self.assertTrue(matches("/home/robert/dir/hello.", is_dir=is_dir))330 self.assertFalse(matches("/home/robert/dir/hello", is_dir=is_dir))331 self.assertFalse(matches("/home/robert/dir/helloX", is_dir=is_dir))332 def test_robert_anchored_wildcard(self):333 matches = self.__parse_gitignore_string(["/hello.*"], mock_base_path="/home/robert")334 for is_dir in (False, True):335 with self.subTest(i=is_dir):336 self.assertTrue(matches("/home/robert/hello.txt", is_dir=is_dir))337 self.assertTrue(matches("/home/robert/hello.c", is_dir=is_dir))338 self.assertFalse(matches("/home/robert/a/hello.java", is_dir=is_dir))339 def test_robert_negation_rules(self):340 matches = self.__parse_gitignore_string(["*.ignore", "!keep.ignore"], mock_base_path="/home/robert")341 for is_dir in (False, True):342 with self.subTest(i=is_dir):343 self.assertTrue(matches("/home/robert/trash.ignore", is_dir=is_dir))344 self.assertTrue(matches("/home/robert/whatever.ignore", is_dir=is_dir))345 self.assertFalse(matches("/home/robert/keep.ignore", is_dir=is_dir))346 self.assertTrue(matches("/home/robert/!keep.ignore", is_dir=is_dir))347 def test_robert_match_does_not_resolve_symlinks(self):348 """Test match on files under symlinked directories349 This mimics how virtual environment sets up the .venv directory by350 symlinking to an interpreter. This test is to ensure that the symlink is351 being ignored (matched) correctly.352 """353 with tempfile.TemporaryDirectory() as d:354 matches = self.__parse_gitignore_string(["*.venv"], mock_base_path=d)355 os.makedirs(f"{d}/.venv/bin")356 os.symlink(sys.executable, f"{d}/.venv/bin/python")357 for is_dir in (False, True):358 with self.subTest(i=is_dir):359 self.assertTrue(matches(f"{d}/.venv", is_dir=is_dir))360 self.assertTrue(matches(f"{d}/.venv/", is_dir=is_dir))361 self.assertTrue(matches(f"{d}/.venv/bin", is_dir=is_dir))362 self.assertTrue(matches(f"{d}/.venv/bin/", is_dir=is_dir))363 self.assertTrue(matches(f"{d}/.venv/bin/python", is_dir=is_dir))364 self.assertFalse(matches(f"{d}/.venv2", is_dir=is_dir))365 self.assertFalse(matches(f"{d}/.venv2/", is_dir=is_dir))366 self.assertFalse(matches(f"{d}/.venv2/bin", is_dir=is_dir))367 self.assertFalse(matches(f"{d}/.venv2/bin/", is_dir=is_dir))368 self.assertFalse(matches(f"{d}/.venv2/bin/python", is_dir=is_dir))369 self.assertTrue(matches(f"{d}/a.venv", is_dir=is_dir))370 self.assertTrue(matches(f"{d}/a.venv/", is_dir=is_dir))371 self.assertTrue(matches(f"{d}/a.venv/bin", is_dir=is_dir))372 self.assertTrue(matches(f"{d}/a.venv/bin/", is_dir=is_dir))373 self.assertTrue(matches(f"{d}/a.venv/bin/python", is_dir=is_dir))374 def test_robert_match_files_under_symlink(self):375 # FIXME What's going on?376 """377 see: https://git-scm.com/docs/gitignore#_pattern_format378 The pattern foo/ will match a directory foo and paths underneath it,379 but will not match a regular file or a symbolic link foo380 (this is consistent with the way how pathspec works in general in Git)381 """382 pass383 def test_robert_handle_base_directories_with_a_symlink_in_their_components(self):384 """385 See https://github.com/bitranox/igittigitt/issues/28 .386 """387 with tempfile.TemporaryDirectory() as d:388 os.makedirs(f"{d}/igittigitt01")389 os.symlink(f"{d}/igittigitt01", f"{d}/symlink_to_igittigitt01", target_is_directory=True)390 matches = self.__parse_gitignore_string(["*.txt"], mock_base_path=f"{d}/symlink_to_igittigitt01")391 for is_dir in (False, True):392 with self.subTest(i=is_dir):393 self.assertTrue(matches(f"{d}/symlink_to_igittigitt01/file.txt", is_dir=is_dir))394 self.assertFalse(matches(f"{d}/symlink_to_igittigitt01/file.png", is_dir=is_dir))395 for path in (f"{d}/symlink_to_igittigitt01/file.txt", f"{d}/symlink_to_igittigitt01/file.png"):396 with open(path, "w"):397 pass398 self.assertTrue(matches(f"{d}/symlink_to_igittigitt01/file.txt"))399 self.assertFalse(matches(f"{d}/symlink_to_igittigitt01/file.png"))400 def test_robert_parse_rule_files(self):401 matches = self.__parse_gitignore_string(402 [403 "test__pycache__",404 "*.py[cod]",405 ".test_venv/",406 ".test_venv/**",407 ".test_venv/*",408 "!test_inverse",409 "!hello.pyc",410 ],411 mock_base_path="/home/robert",412 )413 for is_dir in (False, True):414 with self.subTest(i=is_dir):415 self.assertTrue(matches("/home/robert/test__pycache__", is_dir=is_dir))416 self.assertTrue(matches("/home/robert/test__pycache__/.test_gitignore", is_dir=is_dir))417 self.assertTrue(matches("/home/robert/test__pycache__/excluded", is_dir=is_dir))418 self.assertTrue(matches("/home/robert/test__pycache__/excluded/excluded", is_dir=is_dir))419 self.assertTrue(matches("/home/robert/test__pycache__/excluded/excluded/excluded.txt", is_dir=is_dir))420 # This file will actually be ignored by Git because it won't go into ignored directory421 # `test__pycache__` while globbing. If you are globbing through the directory tree, check that parent422 # directory is not ignored (`!/foo`, `/foo/*`, `!/foo/bar`) and if it is, don't call `matches` on423 # nested file.424 self.assertFalse(matches("/home/robert/test__pycache__/excluded/excluded/test_inverse"))425 self.assertFalse(matches("/home/robert/hello.pyc"))426 self.assertTrue(matches("/home/robert/test__pycache__/some_file.txt", is_dir=is_dir))427 self.assertTrue(matches("/home/robert/test__pycache__/test", is_dir=is_dir))428 self.assertFalse(matches("/home/robert/.test_gitignore", is_dir=is_dir))429 self.assertTrue(matches("/home/robert/.test_venv/some_file.txt", is_dir=is_dir))430 self.assertFalse(matches("/home/robert/not_excluded.txt", is_dir=is_dir))431 self.assertFalse(matches("/home/robert/not_excluded", is_dir=is_dir))432 self.assertTrue(matches("/home/robert/not_excluded/test__pycache__", is_dir=is_dir))433 self.assertFalse(matches("/home/robert/not_excluded/.test_gitignore", is_dir=is_dir))434 self.assertFalse(matches("/home/robert/not_excluded/excluded_not", is_dir=is_dir))435 self.assertFalse(matches("/home/robert/not_excluded/excluded_not/sub_excluded.txt", is_dir=is_dir))436 self.assertFalse(matches("/home/robert/not_excluded/excluded", is_dir=is_dir))437 self.assertFalse(matches("/home/robert/not_excluded/excluded/excluded.txt", is_dir=is_dir))438 self.assertFalse(matches("/home/robert/not_excluded/not_excluded2.txt", is_dir=is_dir))439 self.assertFalse(matches("/home/robert/not_excluded/not_excluded2", is_dir=is_dir))440 self.assertFalse(matches("/home/robert/not_excluded/not_excluded2/sub_excluded.txt", is_dir=is_dir))441 self.assertFalse(matches("/home/robert/not_excluded/excluded_not.txt", is_dir=is_dir))442 self.assertFalse(matches("/home/robert/.test_gitignore_empty", is_dir=is_dir))443 self.assertFalse(matches("/home/robert/.test_venv", is_dir=False))444 self.assertTrue(matches("/home/robert/.test_venv", is_dir=True))445 def __parse_gitignore_string(self, data, mock_base_path):446 with unittest.mock.patch("builtins.open", lambda _: io.StringIO("\n".join(data))):...

Full Screen

Full Screen

test_jujuignore.py

Source:test_jujuignore.py Github

copy

Full Screen

1# Copyright 2020 Canonical Ltd.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14#15# For further info, check https://github.com/canonical/charmcraft16import io17import pathlib18import subprocess19import sys20import textwrap21import tempfile22import pytest23from charmcraft import jujuignore24def test_default_important_files():25 """Don't ignore important files by default."""26 ignore = jujuignore.JujuIgnore(jujuignore.default_juju_ignore)27 assert not ignore.match("version", is_dir=False)28def test_jujuignore_only_dir():29 ignore = jujuignore.JujuIgnore(["target/"])30 assert ignore.match("target", is_dir=True)31 assert ignore.match("foo/target", is_dir=True)32 assert not ignore.match("foo/1target", is_dir=True)33 assert not ignore.match("foo/target", is_dir=False)34def test_jujuignore_any_target():35 ignore = jujuignore.JujuIgnore(["target"])36 assert ignore.match("target", is_dir=True)37 assert ignore.match("/foo/target", is_dir=True)38 assert not ignore.match("/foo/1target", is_dir=True)39 assert ignore.match("/foo/target", is_dir=False)40def test_jujuignore_only_root_target_dir():41 ignore = jujuignore.JujuIgnore(["/target/"])42 assert ignore.match("/target", is_dir=True)43 assert not ignore.match("/foo/target", is_dir=True)44 assert not ignore.match("/foo/1target", is_dir=True)45 assert not ignore.match("/foo/target", is_dir=False)46 assert not ignore.match("/target", is_dir=False)47def test_jujuignore_only_root_target_file_or_dir():48 ignore = jujuignore.JujuIgnore(["/target"])49 assert ignore.match("/target", is_dir=True)50 assert not ignore.match("/foo/target", is_dir=True)51 assert not ignore.match("/foo/1target", is_dir=True)52 assert not ignore.match("/foo/target", is_dir=False)53 assert ignore.match("/target", is_dir=False)54def test_jujuignore_all_py_files():55 ignore = jujuignore.JujuIgnore(["*.py"])56 assert not ignore.match("/target", is_dir=True)57 assert ignore.match("/target.py", is_dir=True)58 assert ignore.match("/target.py", is_dir=False)59 assert ignore.match("/foo/target.py", is_dir=True)60 assert ignore.match("/foo/target.py", is_dir=False)61 assert not ignore.match("/target.pyT", is_dir=True)62 assert not ignore.match("/target.pyT", is_dir=False)63def test_jujuignore_all_py_files_in_foo():64 ignore = jujuignore.JujuIgnore(["/foo/*.py"])65 assert not ignore.match("/target.py", is_dir=True)66 assert not ignore.match("/target.py", is_dir=False)67 assert ignore.match("/foo/target.py", is_dir=True)68 assert ignore.match("/foo/target.py", is_dir=False)69 assert not ignore.match("/foo/sub/target.py", is_dir=False)70def test_jujuignore_ignore_comment():71 ignore = jujuignore.JujuIgnore(72 [73 "#comment",74 "target",75 ]76 )77 assert ignore.match("/target", is_dir=True)78 assert not ignore.match("/comment", is_dir=False)79 assert not ignore.match("/#comment", is_dir=False)80 assert not ignore.match("#comment", is_dir=False)81 assert not ignore.match("/foo/comment", is_dir=False)82 assert not ignore.match("/foo/#comment", is_dir=False)83def test_jujuignore_escaped_comment():84 ignore = jujuignore.JujuIgnore(85 [86 "\\#comment",87 "target",88 ]89 )90 assert ignore.match("/target", is_dir=True)91 assert ignore.match("/#comment", is_dir=False)92 assert ignore.match("#comment", is_dir=False)93 assert not ignore.match("/foo/comment", is_dir=False)94 assert ignore.match("/foo/#comment", is_dir=False)95def test_jujuignore_subdirectory_match():96 ignore = jujuignore.JujuIgnore(["apps/logs/"])97 assert not ignore.match("/logs", is_dir=True)98 assert not ignore.match("/apps", is_dir=True)99 assert ignore.match("/apps/logs", is_dir=True)100 assert not ignore.match("/apps/logs", is_dir=False)101 assert not ignore.match("/apps/foo/logs", is_dir=True)102def test_jujuignore_sub_subdirectory_match():103 ignore = jujuignore.JujuIgnore(["apps/*/logs/"])104 assert not ignore.match("/logs", is_dir=True)105 assert not ignore.match("/apps", is_dir=True)106 assert not ignore.match("/apps/logs", is_dir=True)107 assert not ignore.match("/apps/logs", is_dir=False)108 assert ignore.match("/apps/foo/logs", is_dir=True)109 assert ignore.match("/apps/bar/logs", is_dir=True)110 assert not ignore.match("/apps/baz/logs", is_dir=False)111 assert not ignore.match("/apps/foo/bar/logs", is_dir=True)112def test_jujuignore_any_subdirectory_match():113 ignore = jujuignore.JujuIgnore(["apps/**/logs/"])114 assert not ignore.match("/logs", is_dir=True)115 assert not ignore.match("/apps", is_dir=True)116 assert ignore.match("/apps/logs", is_dir=True)117 assert not ignore.match("/apps/logs", is_dir=False)118 assert ignore.match("/apps/foo/logs", is_dir=True)119 assert ignore.match("/apps/bar/logs", is_dir=True)120 assert ignore.match("/apps/bar/bing/logs", is_dir=True)121 assert not ignore.match("/apps/baz/logs", is_dir=False)122def test_jujuignore_everything_under_foo():123 ignore = jujuignore.JujuIgnore(["foo/**"])124 assert not ignore.match("/foo", is_dir=True)125 assert ignore.match("/foo/a", is_dir=True)126 assert ignore.match("/foo/a", is_dir=False)127 assert ignore.match("/foo/a/b", is_dir=False)128def test_jujuignore_everything_under_foo_but_readme():129 ignore = jujuignore.JujuIgnore(130 [131 "foo/**",132 "!foo/README.md",133 ]134 )135 assert not ignore.match("/foo", is_dir=True)136 assert ignore.match("/foo/a", is_dir=True)137 assert ignore.match("/foo/a", is_dir=False)138 assert ignore.match("/foo/a/b", is_dir=False)139 assert not ignore.match("/foo/README.md", is_dir=False)140def test_jujuignore_negation():141 ignore = jujuignore.JujuIgnore(142 [143 "*.py",144 "!foo.py",145 "!!!bar.py",146 ]147 )148 assert not ignore.match("bar.py", is_dir=False)149 assert not ignore.match("foo.py", is_dir=False)150 assert ignore.match("baz.py", is_dir=False)151 assert not ignore.match("foo/bar.py", is_dir=False)152 assert not ignore.match("foo/foo.py", is_dir=False)153 assert ignore.match("foo/baz.py", is_dir=False)154def test_jujuignore_multiple_doublestar():155 ignore = jujuignore.JujuIgnore(["foo/**/bar/**/baz"])156 assert ignore.match("/foo/1/2/bar/baz", is_dir=True)157 assert ignore.match("/foo/1/2/bar/1/2/baz", is_dir=True)158 assert ignore.match("/foo/bar/1/2/baz", is_dir=True)159 assert ignore.match("/foo/1/bar/baz", is_dir=True)160 assert ignore.match("/foo/bar/baz", is_dir=True)161def test_jujuignore_trim_unescaped_trailing_space():162 ignore = jujuignore.JujuIgnore(163 [164 r"foo ",165 r"bar\ \ ",166 ]167 )168 assert ignore.match("/foo", is_dir=True)169 assert not ignore.match("/bar", is_dir=True)170 assert ignore.match("/bar ", is_dir=True)171def test_jujuignore_escaped_bang():172 ignore = jujuignore.JujuIgnore(173 [174 r"foo",175 r"!/foo",176 r"\!foo",177 ]178 )179 assert not ignore.match("/foo", is_dir=True)180 assert ignore.match("/bar/foo", is_dir=True)181 assert ignore.match("!foo", is_dir=True)182def test_jujuignore_leading_whitespace():183 ignore = jujuignore.JujuIgnore(184 [185 r"foo",186 r" bar",187 r" #comment",188 ]189 )190 assert ignore.match("/foo", is_dir=True)191 assert ignore.match("/bar", is_dir=True)192 assert ignore.match("/bar/foo", is_dir=True)193 assert not ignore.match("#comment", is_dir=True)194 assert not ignore.match("comment", is_dir=True)195def test_jujuignore_bracket():196 ignore = jujuignore.JujuIgnore(197 [198 r"*.py[cod]",199 ]200 )201 assert not ignore.match("foo.py", is_dir=False)202 assert ignore.match("foo.pyc", is_dir=False)203 assert ignore.match("foo.pyo", is_dir=False)204 assert ignore.match("foo.pyd", is_dir=False)205def test_jujuignore_simple_match():206 ignore = jujuignore.JujuIgnore(207 [208 r"foo",209 ]210 )211 assert ignore.match("/foo", is_dir=True)212 assert ignore.match("/bar/foo", is_dir=True)213 assert not ignore.match("/bar/bfoo", is_dir=True)214 assert not ignore.match("/bfoo", is_dir=True)215def test_jujuignore_star_match():216 ignore = jujuignore.JujuIgnore(217 [218 r"foo*.py",219 ]220 )221 assert ignore.match("/foo.py", is_dir=False)222 assert ignore.match("/foo2.py", is_dir=False)223 assert ignore.match("/foobar.py", is_dir=False)224 assert ignore.match("/bar/foo.py", is_dir=False)225 assert not ignore.match("/foo/2.py", is_dir=False)226def test_jujuignore_paths_with_newlines():227 ignore = jujuignore.JujuIgnore([r"bar/**/*.py"])228 assert ignore.match("/bar/foo.py", is_dir=False)229 assert ignore.match("/bar/f\noo.py", is_dir=False)230 assert ignore.match("/bar/baz/f\noo.py", is_dir=False)231 assert ignore.match("/bar/b\nz/f\noo.py", is_dir=False)232def test_rstrip_unescaped():233 assert jujuignore._rstrip_unescaped(r"") == ""234 assert jujuignore._rstrip_unescaped(r" ") == ""235 assert jujuignore._rstrip_unescaped(r"a") == "a"236 assert jujuignore._rstrip_unescaped(r"a ") == "a"237 assert jujuignore._rstrip_unescaped(r"a ") == "a"238 assert jujuignore._rstrip_unescaped(r"a\ ") == r"a\ "239 assert jujuignore._rstrip_unescaped(r"a foo\ ") == r"a foo\ "240def test_unescape_rule():241 assert jujuignore._unescape_rule(r"") == ""242 assert jujuignore._unescape_rule(r" ") == ""243 assert jujuignore._unescape_rule(r"\#") == "#"244 assert jujuignore._unescape_rule(r"\!") == "!"245 assert jujuignore._unescape_rule(r"\ ") == " "246 assert jujuignore._unescape_rule(r" foo\ ") == "foo "247def test_from_file():248 content = io.StringIO(249 textwrap.dedent(250 """\251 foo252 /bar253 """254 )255 )256 ignore = jujuignore.JujuIgnore(content)257 assert ignore.match("foo", is_dir=False)258 assert ignore.match("/foo", is_dir=False)259 assert ignore.match("/bar", is_dir=False)260 assert not ignore.match("/foo/bar", is_dir=False)261def test_log_matching_rule(assert_output):262 jujuignore.JujuIgnore(["foo/bar\n"])263 assert_output(r"Translated .jujuignore 1 'foo/bar\n' => '.*/foo/bar\\Z'")264def assertMatchedAndNonMatched(globs, matched, unmatched, skip_git=False):265 """For a given set of globs, check that it does and doesn't match as expected"""266 ignore = jujuignore.JujuIgnore(globs)267 for m in matched:268 assert ignore.match(m, is_dir=False), "{} should have matched".format(m)269 for m in unmatched:270 assert not ignore.match(m, is_dir=False), "{} should not have matched".format(m)271 if skip_git:272 return273 with tempfile.TemporaryDirectory() as tmpdir:274 subprocess.run(["git", "init", tmpdir], check=True)275 with open(str(pathlib.Path(tmpdir) / ".gitignore"), "wt") as gitignore:276 gitignore.writelines([g + "\n" for g in globs])277 input = "".join([m.lstrip("/") + "\n" for m in matched + unmatched])278 check = True279 if len(matched) == 0:280 # We don't check git return value because it returns nonzero if no paths match281 check = False282 p = subprocess.run(283 ["git", "check-ignore", "--stdin"],284 check=check,285 input=input,286 stdout=subprocess.PIPE,287 cwd=tmpdir,288 universal_newlines=True,289 )290 matched_out = p.stdout.splitlines()291 assert sorted(matched) == sorted(matched_out), "expected exactly {} to match not {}".format(292 matched, matched_out293 )294@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")295def test_star_vs_star_start():296 assertMatchedAndNonMatched(297 ["/*.py", "**/foo"],298 # Only top level .py files, but foo at any level299 ["a.py", "b.py", "foo", "bar/foo"],300 ["bar/b.py"],301 # 'foo/a.py', git matches foo/a.py because of foo, ours doesn't but I don't think it302 # matters because the whole directory would have already been skipped303 )304@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")305def test_questionmark():306 assertMatchedAndNonMatched(307 ["foo?.py"],308 ["fooa.py", "foob.py"],309 ["foo.py", "footwo.py", "foo/.py"],310 )311@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")312def test_brackets():313 assertMatchedAndNonMatched(314 ["*.py[cod]"],315 ["a.pyc", "b.pyo", "d.pyd", "foo/.pyc", "bar/__pycache__.pyc"],316 ["a.py", "b.pyq", "c.so", "foo/__pycache__/bar.py"],317 )318@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")319def test_bracket_ranges():320 assertMatchedAndNonMatched(321 ["foo[1-9].py"],322 ["foo1.py", "foo2.py", "foo9.py"],323 ["foo0.py", "foo10.py", "fooa.py"],324 )325@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")326def test_bracket_inverted():327 assertMatchedAndNonMatched(328 ["foo[!1-9].py", "bar[!a].py"],329 ["fooa.py", "foob.py", "fooc.py", "barb.py", "barc.py"],330 ["foo1.py", "foo2.py", "foo10.py", "bara.py"],331 )332def test_slashes_in_brackets():333 assertMatchedAndNonMatched(334 [r"foo[\\].py"],335 [r"foo\.py"],336 [r"fooa.py"],337 # We don't test against git here, because it replies with "foo\\.py"338 # which is an escaped form that we'd have to interpret339 skip_git=True,340 )341@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")342def test_special_chars_in_brackets():343 assertMatchedAndNonMatched(344 [r"foo[a|b].py"],345 [r"fooa.py", "foob.py", "foo|.py"],346 [r"foo.py", r"fooc.py"],347 )348 assertMatchedAndNonMatched(349 [r"foo[ab|cd].py"],350 [r"fooa.py", "foob.py", "fooc.py", "food.py", "foo|.py"],351 [r"foo.py", "fooe.py", "fooab.py", "fooac.py"],352 )353 assertMatchedAndNonMatched(354 [r"foo[a&].py"],355 [r"fooa.py", r"foo&.py"],356 [r"foo.py", r"fooa&.py", "foob.py"],357 )358 assertMatchedAndNonMatched(359 [r"foo[a~].py"],360 [r"fooa.py", r"foo~.py"],361 [r"foo.py", r"fooa~.py", "foob.py"],362 )363 assertMatchedAndNonMatched(364 [r"foo[[a].py"],365 [r"fooa.py", r"foo[.py"],366 [r"foo.py", r"fooa[.py", "foob.py"],367 )368 # Git allows ! or ^ to mean negate the glob369 assertMatchedAndNonMatched(370 [r"foo[^a].py"],371 ["foob.py", "fooc.py", "foo^.py"],372 [r"foo.py", r"fooa.py", r"fooa^.py"],373 )374def test_extend_patterns():375 ignore = jujuignore.JujuIgnore(["foo"])376 assert ignore.match("foo", is_dir=False)377 assert not ignore.match("bar", is_dir=False)378 ignore.extend_patterns(["bar"])379 assert ignore.match("foo", is_dir=False)...

Full Screen

Full Screen

test_package.py

Source:test_package.py Github

copy

Full Screen

...9 def __init__(self, name, source, is_dir):10 self.name = name11 self.path = os.path.join(source, name)12 self._is_dir = is_dir13 def is_dir(self):14 return self._is_dir15@pytest.fixture()16def sample_hathi_package(monkeypatch):17 def mockreturn(path):18 return [19 mockDirEntry(name="00000001.jp2", source=os.path.join(ROOT, "721932"), is_dir=False),20 mockDirEntry(name="00000001.txt", source=os.path.join(ROOT, "721932"), is_dir=False),21 mockDirEntry(name="00000002.jp2", source=os.path.join(ROOT, "721932"), is_dir=False),22 mockDirEntry(name="00000002.txt", source=os.path.join(ROOT, "721932"), is_dir=False),23 mockDirEntry(name="00000003.jp2", source=os.path.join(ROOT, "721932"), is_dir=False),24 mockDirEntry(name="00000003.txt", source=os.path.join(ROOT, "721932"), is_dir=False),25 mockDirEntry(name="00000004.jp2", source=os.path.join(ROOT, "721932"), is_dir=False),26 mockDirEntry(name="00000004.txt", source=os.path.join(ROOT, "721932"), is_dir=False),27 mockDirEntry(name="00000005.jp2", source=os.path.join(ROOT, "721932"), is_dir=False),...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lisa automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful