Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c369e44

Browse files
authored
Fix matching of absolute paths in --include (psf#3976)
1 parent 7bfa35c commit c369e44

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
- Add support for single line format skip with other comments on the same line (#3959)
2121

22+
- Fix a bug in the matching of absolute path names in `--include` (#3976)
23+
2224
### Packaging
2325

2426
<!-- Changes to how Black is packaged, such as dependency requirements -->

src/black/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def gen_python_files(
389389
warn=verbose or not quiet
390390
):
391391
continue
392-
include_match = include.search(normalized_path) if include else True
392+
include_match = include.search(root_relative_path) if include else True
393393
if include_match:
394394
yield child
395395

tests/test_black.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,27 @@ def test_empty_include(self) -> None:
23882388
# Setting exclude explicitly to an empty string to block .gitignore usage.
23892389
assert_collected_sources(src, expected, include="", exclude="")
23902390

2391+
def test_include_absolute_path(self) -> None:
2392+
path = DATA_DIR / "include_exclude_tests"
2393+
src = [path]
2394+
expected = [
2395+
Path(path / "b/dont_exclude/a.pie"),
2396+
]
2397+
assert_collected_sources(
2398+
src, expected, root=path, include=r"^/b/dont_exclude/a\.pie$", exclude=""
2399+
)
2400+
2401+
def test_exclude_absolute_path(self) -> None:
2402+
path = DATA_DIR / "include_exclude_tests"
2403+
src = [path]
2404+
expected = [
2405+
Path(path / "b/dont_exclude/a.py"),
2406+
Path(path / "b/.definitely_exclude/a.py"),
2407+
]
2408+
assert_collected_sources(
2409+
src, expected, root=path, include=r"\.py$", exclude=r"^/b/exclude/a\.py$"
2410+
)
2411+
23912412
def test_extend_exclude(self) -> None:
23922413
path = DATA_DIR / "include_exclude_tests"
23932414
src = [path]
@@ -2401,27 +2422,51 @@ def test_extend_exclude(self) -> None:
24012422

24022423
@pytest.mark.incompatible_with_mypyc
24032424
def test_symlinks(self) -> None:
2404-
path = MagicMock()
24052425
root = THIS_DIR.resolve()
24062426
include = re.compile(black.DEFAULT_INCLUDES)
24072427
exclude = re.compile(black.DEFAULT_EXCLUDES)
24082428
report = black.Report()
24092429
gitignore = PathSpec.from_lines("gitwildmatch", [])
24102430

24112431
regular = MagicMock()
2412-
outside_root_symlink = MagicMock()
2413-
ignored_symlink = MagicMock()
2414-
2415-
path.iterdir.return_value = [regular, outside_root_symlink, ignored_symlink]
2416-
24172432
regular.absolute.return_value = root / "regular.py"
24182433
regular.resolve.return_value = root / "regular.py"
24192434
regular.is_dir.return_value = False
2435+
regular.is_file.return_value = True
24202436

2437+
outside_root_symlink = MagicMock()
24212438
outside_root_symlink.absolute.return_value = root / "symlink.py"
24222439
outside_root_symlink.resolve.return_value = Path("/nowhere")
2440+
outside_root_symlink.is_dir.return_value = False
2441+
outside_root_symlink.is_file.return_value = True
24232442

2443+
ignored_symlink = MagicMock()
24242444
ignored_symlink.absolute.return_value = root / ".mypy_cache" / "symlink.py"
2445+
ignored_symlink.is_dir.return_value = False
2446+
ignored_symlink.is_file.return_value = True
2447+
2448+
# A symlink that has an excluded name, but points to an included name
2449+
symlink_excluded_name = MagicMock()
2450+
symlink_excluded_name.absolute.return_value = root / "excluded_name"
2451+
symlink_excluded_name.resolve.return_value = root / "included_name.py"
2452+
symlink_excluded_name.is_dir.return_value = False
2453+
symlink_excluded_name.is_file.return_value = True
2454+
2455+
# A symlink that has an included name, but points to an excluded name
2456+
symlink_included_name = MagicMock()
2457+
symlink_included_name.absolute.return_value = root / "included_name.py"
2458+
symlink_included_name.resolve.return_value = root / "excluded_name"
2459+
symlink_included_name.is_dir.return_value = False
2460+
symlink_included_name.is_file.return_value = True
2461+
2462+
path = MagicMock()
2463+
path.iterdir.return_value = [
2464+
regular,
2465+
outside_root_symlink,
2466+
ignored_symlink,
2467+
symlink_excluded_name,
2468+
symlink_included_name,
2469+
]
24252470

24262471
files = list(
24272472
black.gen_python_files(
@@ -2437,7 +2482,7 @@ def test_symlinks(self) -> None:
24372482
quiet=False,
24382483
)
24392484
)
2440-
assert files == [regular]
2485+
assert files == [regular, symlink_included_name]
24412486

24422487
path.iterdir.assert_called_once()
24432488
outside_root_symlink.resolve.assert_called_once()

0 commit comments

Comments
 (0)