def parser_test_comments(base_path: pathlib.Path) -> igittigitt.IgnoreParser: ignore_parser = igittigitt.IgnoreParser() ignore_parser.add_rule("somematch", base_path=base_path) ignore_parser.add_rule("#realcomment", base_path=base_path) ignore_parser.add_rule("othermatch", base_path=base_path) ignore_parser.add_rule("\\#imnocomment", base_path=base_path) return ignore_parser
def test_shutil_ignore_function() -> None: """ >>> test_shutil_ignore_function() """ # Setup path_test_dir = pathlib.Path(__file__).parent.resolve() path_source_dir = path_test_dir / "example" path_target_dir = path_test_dir / "target" shutil.rmtree(path_target_dir, ignore_errors=True) # Test ignore_parser = igittigitt.IgnoreParser() ignore_parser.parse_rule_files(base_dir=path_source_dir, filename=".test_gitignore") shutil.copytree( path_source_dir, path_target_dir, ignore=ignore_parser.shutil_ignore, ) assert len(list(path_target_dir.glob("**/*"))) == 9 # Teardown shutil.rmtree(path_target_dir, ignore_errors=True)
def parser_simple_git_rules(base_path): ignore_parser = igittigitt.IgnoreParser() # add rules with base path as pathlib.Path ignore_parser.add_rule("__pycache__", base_path=base_path) ignore_parser.add_rule("*.py[cod]", base_path=base_path) ignore_parser.add_rule(".venv/", base_path=base_path) # add rules with base path as string ignore_parser.add_rule("__pycache__", base_path=str(base_path)) ignore_parser.add_rule("*.py[cod]", base_path=str(base_path)) ignore_parser.add_rule(".venv/", base_path=str(base_path)) return ignore_parser
def test_match_does_not_resolve_symlinks(tmp_path: pathlib.Path) -> None: """Test match on files under symlinked directories This mimics how virtual environment sets up the .venv directory by symlinking to an interpreter. This test is to ensure that the symlink is being ignored (matched) correctly. """ gitignore = igittigitt.IgnoreParser() gitignore.add_rule(".venv", tmp_path) linked_python = tmp_path / ".venv" / "bin" / "python" linked_python.parent.mkdir(parents=True) linked_python.symlink_to(pathlib.Path(sys.executable).resolve()) assert gitignore.match(linked_python)
def test_handle_base_directories_with_a_symlink_in_their_components( tmp_path: pathlib.Path) -> None: """ see https://github.com/bitranox/igittigitt/issues/28 """ # setup dir01 = pathlib.Path(tmp_path / "igittigitt01") dir01.mkdir(parents=True) dir02 = pathlib.Path(tmp_path / "symlink_to_igittigitt01") dir02.symlink_to(dir01) (dir02 / "file.txt").touch() # Test gitignore = igittigitt.IgnoreParser() gitignore.add_rule("*.txt", dir02) assert gitignore.match(dir02 / "file.txt")
def walk_git_project(*args, exlude_git_dir=True, **kwargs): parser = igittigitt.IgnoreParser() parser.parse_rule_files(PROJECT_ROOT) is_ignored = parser.match for root, dirs, files in os.walk(*args, **kwargs): if is_ignored(root): continue ignored_dirs = [ dir for dir in dirs if is_ignored(dir) or (exlude_git_dir and dir == ".git") ] # remove them in-place to avoid further exploration for dir in ignored_dirs: dirs.remove(dir) files = [file for file in files if not is_ignored(file)] yield root, dirs, files
def get_files_in_dir(directory_path: str, ignore_file: str = None) -> set: file_list = set() ignore_parser = igittigitt.IgnoreParser() if ignore_file: ignore_parser.parse_rule_files(os.path.dirname(ignore_file), os.path.basename(ignore_file)) # Iterate over all the files in directory for root, _, files in os.walk(directory_path): for file in files: # Create complete filepath of file in directory file_path = os.path.join(root, file) if file_path.startswith("./"): file_path = file_path[2:] # can we add this file to the collection? if not ignore_parser.match(file_path): file_list.add(file_path) return file_list
def test_parse_rule_files() -> None: path_test_dir = pathlib.Path(__file__).parent.resolve() / "example" ignore_parser = igittigitt.IgnoreParser() ignore_parser.parse_rule_files(base_dir=path_test_dir, filename=".test_gitignore") paths_unfiltered = path_test_dir.glob("**/*") paths_filtered = list() for path in paths_unfiltered: if not ignore_parser.match(path): paths_filtered.append(path.name) assert sorted(paths_filtered) == [ ".test_gitignore", ".test_gitignore", ".test_gitignore_empty", "excluded_not", "excluded_not.txt", "not_excluded", "not_excluded.txt", "not_excluded2", "not_excluded2.txt", ]
def parser_negation_git_rules( base_path: pathlib.Path) -> igittigitt.IgnoreParser: ignore_parser = igittigitt.IgnoreParser() ignore_parser.add_rule("*.ignore", base_path=base_path) ignore_parser.add_rule("!keep.ignore", base_path=base_path) return ignore_parser
def parser_test_anchored_wildcard( base_path: pathlib.Path) -> igittigitt.IgnoreParser: ignore_parser = igittigitt.IgnoreParser() ignore_parser.add_rule("/hello.*", base_path=base_path) return ignore_parser
def test_match_expands_user() -> None: """Test match expands `~` in path.""" gitignore = igittigitt.IgnoreParser() gitignore.add_rule("test.txt", pathlib.Path("~").expanduser()) assert gitignore.match(pathlib.Path("~") / "test.txt") assert gitignore.match("~/test.txt")
def parser_test_wildcard(base_path): ignore_parser = igittigitt.IgnoreParser() ignore_parser.add_rule("hello.*", base_path=base_path) return ignore_parser