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

Fix matching of absolute paths in --include #3976

Merged
merged 9 commits into from
Oct 27, 2023
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

<!-- Changes to how Black can be configured -->

- Fix a bug in the matching of absolute path names in `--include` (#3976)

### Packaging

<!-- Changes to how Black is packaged, such as dependency requirements -->
Expand Down
4 changes: 4 additions & 0 deletions src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ def gen_python_files(
if normalized_path is None:
continue

normalized_path = "/" + normalized_path
if child.is_dir():
normalized_path += "/"

if child.is_dir():
# If gitignore is None, gitignore usage is disabled, while a Falsey
# gitignore is when the directory doesn't have a .gitignore file.
Expand Down
21 changes: 21 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,27 @@ def test_empty_include(self) -> None:
# Setting exclude explicitly to an empty string to block .gitignore usage.
assert_collected_sources(src, expected, include="", exclude="")

def test_include_absolute_path(self) -> None:
path = DATA_DIR / "include_exclude_tests"
src = [path]
expected = [
Path(path / "b/dont_exclude/a.pie"),
]
assert_collected_sources(
src, expected, root=path, include=r"^/b/dont_exclude/a\.pie$", exclude=""
)

def test_exclude_absolute_path(self) -> None:
path = DATA_DIR / "include_exclude_tests"
src = [path]
expected = [
Path(path / "b/dont_exclude/a.py"),
Path(path / "b/.definitely_exclude/a.py"),
]
assert_collected_sources(
src, expected, root=path, include=r"\.py$", exclude=r"^/b/exclude/a\.py$"
)

def test_extend_exclude(self) -> None:
path = DATA_DIR / "include_exclude_tests"
src = [path]
Expand Down
Loading