From ab810bd5d6c8c2ce3ec00c9e6ebb234baebb61ea Mon Sep 17 00:00:00 2001 From: Stephan Hohe Date: Fri, 27 Oct 2023 14:29:17 +0200 Subject: [PATCH 1/4] Test interaction between --force-exclude and symlinks as cli parameters --- tests/test_black.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_black.py b/tests/test_black.py index 56c20243020..26f634ea6ff 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1409,6 +1409,24 @@ def test_invalid_cli_regex(self) -> None: for option in ["--include", "--exclude", "--extend-exclude", "--force-exclude"]: self.invokeBlack(["-", option, "**()(!!*)"], exit_code=2) + def test_force_exclude_symlink_parameter(self) -> None: + with TemporaryDirectory() as workspace: + root = Path(workspace) + (root / "pyproject.toml").touch() + + symlink_target = root / "target.py" + symlink_target.write_text("print ( )\n", encoding="utf-8") + + symlink = root / "symlink.py" + symlink.symlink_to(symlink_target) + + # target.py would need to be reformatted, and symlink.py points to + # it, but since we force-exclude symlink.py, black should not check + # the pointed-to file. + self.invokeBlack( + ["--check", r"--force-exclude=sym", str(symlink)], exit_code=0 + ) + def test_required_version_matches_version(self) -> None: self.invokeBlack( ["--required-version", black.__version__, "-c", "0"], From d534b1701f3b0665dea10995905048387408342c Mon Sep 17 00:00:00 2001 From: Stephan Hohe Date: Fri, 27 Oct 2023 13:38:37 +0200 Subject: [PATCH 2/4] Check excludes before symlink resolution for command line parameters This adjusts exclusion checking for command line parameters the same way as #3846 did for other files. Specifically, --force-exclude is now checked before symlink resolution. This way symlink handling is consistent between command line parameters and other collected files. Fixes #3826. --- src/black/__init__.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 7cf93b89e42..fb73063177e 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -50,6 +50,7 @@ get_gitignore, normalize_path_maybe_ignore, parse_pyproject_toml, + path_is_excluded, wrap_stream_for_windows, ) from black.handle_ipynb_magics import ( @@ -639,25 +640,26 @@ def get_sources( is_stdin = False if is_stdin or p.is_file(): + root_relative_path = p.absolute().relative_to(root).as_posix() + + root_relative_path = "/" + root_relative_path + if p.is_dir(): + root_relative_path += "/" + + # Hard-exclude any files that matches the `--force-exclude` regex. + if path_is_excluded(root_relative_path, force_exclude): + report.path_ignored(p, "matches the --force-exclude regular expression") + continue + normalized_path: Optional[str] = normalize_path_maybe_ignore( p, root, report ) if normalized_path is None: if verbose: - out(f'Skipping invalid source: "{normalized_path}"', fg="red") + out(f'Skipping invalid source: "{p}"', fg="red") continue if verbose: - out(f'Found input source: "{normalized_path}"', fg="blue") - - normalized_path = "/" + normalized_path - # Hard-exclude any files that matches the `--force-exclude` regex. - if force_exclude: - force_exclude_match = force_exclude.search(normalized_path) - else: - force_exclude_match = None - if force_exclude_match and force_exclude_match.group(0): - report.path_ignored(p, "matches the --force-exclude regular expression") - continue + out(f'Found input source: "{p}"', fg="blue") if is_stdin: p = Path(f"{STDIN_PLACEHOLDER}{str(p)}") From 2ef832dd4eb854504ea25c167cb243111ae34cec Mon Sep 17 00:00:00 2001 From: Stephan Hohe Date: Fri, 27 Oct 2023 14:04:30 +0200 Subject: [PATCH 3/4] Add to CHANGES.md --- CHANGES.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 71f62d0e11f..bef5ba5974c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -18,7 +18,8 @@ ### Configuration - Add support for single line format skip with other comments on the same line (#3959) - +- File exclusion and inclusion logic is now consistently applied before symlink + resolution. (#3987, #3976) - Fix a bug in the matching of absolute path names in `--include` (#3976) ### Packaging From ded8f3d3ac4c967425b814e067517f9409de0d1e Mon Sep 17 00:00:00 2001 From: Stephan Hohe Date: Fri, 27 Oct 2023 15:09:41 +0200 Subject: [PATCH 4/4] Change CHANGES formatting to match CI check --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index bef5ba5974c..cb74dafa6fb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,7 +19,7 @@ - Add support for single line format skip with other comments on the same line (#3959) - File exclusion and inclusion logic is now consistently applied before symlink - resolution. (#3987, #3976) + resolution. (#3987) (#3976) - Fix a bug in the matching of absolute path names in `--include` (#3976) ### Packaging