Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor git-ls-files into a common utils function #23

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
9 changes: 3 additions & 6 deletions test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import nbformat
import pint
import pytest
from git.cmd import Git

from .utils import find_files

with warnings.catch_warnings():
warnings.filterwarnings("ignore")
Expand All @@ -25,11 +26,7 @@


@pytest.fixture(
params=(
path
for path in Git(Git(".").rev_parse("--show-toplevel")).ls_files().split("\n")
if path.endswith(".ipynb")
),
params=find_files(file_extension=".ipynb"),
name="notebook_filename",
)
def _notebook_filename(request):
Expand Down
8 changes: 3 additions & 5 deletions test_todos_annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from ghapi.all import GhApi, paged
from git.cmd import Git

from .utils import find_files


def _grep(filepath, regex):
reg_obj = re.compile(regex)
Expand All @@ -21,11 +23,7 @@ def _grep(filepath, regex):


@pytest.fixture(
params=(
path
for path in Git(Git(".").rev_parse("--show-toplevel")).ls_files().split("\n")
if os.path.isfile(path)
),
params=find_files(),
name="git_tracked_file",
)
def _git_tracked_file(request):
Expand Down
26 changes: 26 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Utils functions to reuse in different parts of the codebase
"""
import os

from git.cmd import Git


def find_files(path_to_folder_from_project_root=".", file_extension=None):
"""
Returns all files in a current git repo.
The list of returned files may be filtered with `file_extension` param.
"""
all_files = [
path
for path in Git(
Git(path_to_folder_from_project_root).rev_parse("--show-toplevel")
)
.ls_files()
.split("\n")
if os.path.isfile(path)
]
if file_extension is not None:
return list(filter(lambda path: path.endswith(file_extension), all_files))

return all_files