From 1bf22a0544f35d64249cdc7c03957087fdafc685 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 31 Oct 2023 23:18:57 -0700 Subject: [PATCH 1/3] Apply force exclude logic before symlink resolution --- src/black/__init__.py | 24 +++++++++++++----------- tests/test_black.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index c11a66b7bc8..0800f74c4fe 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -639,25 +639,25 @@ def get_sources( path = Path(s) is_stdin = False + # Compare the logic here to the logic in `gen_python_files`. if is_stdin or path.is_file(): - normalized_path: Optional[str] = normalize_path_maybe_ignore( - path, root, report - ) - if normalized_path is None: - if verbose: - out(f'Skipping invalid source: "{normalized_path}"', fg="red") - continue - if verbose: - out(f'Found input source: "{normalized_path}"', fg="blue") + root_relative_path = path.absolute().relative_to(root).as_posix() + + root_relative_path = "/" + root_relative_path - normalized_path = "/" + normalized_path # Hard-exclude any files that matches the `--force-exclude` regex. - if path_is_excluded(normalized_path, force_exclude): + if path_is_excluded(root_relative_path, force_exclude): report.path_ignored( path, "matches the --force-exclude regular expression" ) continue + normalized_path = normalize_path_maybe_ignore(path, root, report) + if normalized_path is None: + if verbose: + out(f'Skipping invalid source: "{normalized_path}"', fg="red") + continue + if is_stdin: path = Path(f"{STDIN_PLACEHOLDER}{str(path)}") @@ -666,6 +666,8 @@ def get_sources( ): continue + if verbose: + out(f'Found input source: "{normalized_path}"', fg="blue") sources.add(path) elif path.is_dir(): path = root / (path.resolve().relative_to(root)) diff --git a/tests/test_black.py b/tests/test_black.py index c7196098e14..e1f88f76da3 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2552,6 +2552,23 @@ def test_get_sources_with_stdin_filename_and_force_exclude(self) -> None: stdin_filename=stdin_filename, ) + @patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None)) + def test_get_sources_with_stdin_filename_and_force_exclude_and_symlink( + self, + ) -> None: + # Force exclude should exclude a symlink based on the symlink, not its target + path = THIS_DIR / "data" / "include_exclude_tests" + stdin_filename = str(path / "symlink.py") + expected = [f"__BLACK_STDIN_FILENAME__{stdin_filename}"] + target = path / "b/exclude/a.py" + with patch("pathlib.Path.resolve", return_value=target): + assert_collected_sources( + src=["-"], + expected=expected, + force_exclude=r"exclude/a\.py", + stdin_filename=stdin_filename, + ) + class TestDeFactoAPI: """Test that certain symbols that are commonly used externally keep working. From 4aa9bd4950d1ae39a18e9c70c980eb7ae280d5aa Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 1 Nov 2023 12:51:51 -0700 Subject: [PATCH 2/3] changelog --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index f365f1c239b..d21320977d7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,7 +21,7 @@ ### Configuration - Add support for single-line format skip with other comments on the same line (#3959) - +- Consistently apply force exclusion logic before resolving symlinks (#4015) - Fix a bug in the matching of absolute path names in `--include` (#3976) ### Packaging From 24238dcef1af58ad16ddf4453b7405519b3cbac2 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 1 Nov 2023 12:56:17 -0700 Subject: [PATCH 3/3] okay mypyc --- src/black/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 0800f74c4fe..08a907d50c5 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -652,7 +652,9 @@ def get_sources( ) continue - normalized_path = normalize_path_maybe_ignore(path, root, report) + normalized_path: Optional[str] = normalize_path_maybe_ignore( + path, root, report + ) if normalized_path is None: if verbose: out(f'Skipping invalid source: "{normalized_path}"', fg="red")