From 2648fd6a4be19596c745c04bbac2b2dfc1ddf95b Mon Sep 17 00:00:00 2001 From: Antonio Ossa Guerra Date: Mon, 17 Oct 2022 16:24:13 -0300 Subject: [PATCH] Test .gitignore with `*/*` is applied correctly The test contains two cases: when the .gitignore with the special rule to ignore every subfolder and its contents (*/*) is in the root, and when the file is inside a subfolder relative to root (nested). In both cases, we compare the files that are visible by Black with a known list of paths containing the expected values. Before the fix introduced in the previous commit, this test failed when the .gitignore file was nested (second case). Now, the test is passed for both cases. Signed-off-by: Antonio Ossa Guerra --- .../ignore_subfolders_gitignore_tests/a.py | 0 .../subdir/.gitignore | 1 + .../subdir/b.py | 0 .../subdir/subdir/c.py | 0 tests/test_black.py | 18 ++++++++++++++++++ 5 files changed, 19 insertions(+) create mode 100644 tests/data/ignore_subfolders_gitignore_tests/a.py create mode 100644 tests/data/ignore_subfolders_gitignore_tests/subdir/.gitignore create mode 100644 tests/data/ignore_subfolders_gitignore_tests/subdir/b.py create mode 100644 tests/data/ignore_subfolders_gitignore_tests/subdir/subdir/c.py diff --git a/tests/data/ignore_subfolders_gitignore_tests/a.py b/tests/data/ignore_subfolders_gitignore_tests/a.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/data/ignore_subfolders_gitignore_tests/subdir/.gitignore b/tests/data/ignore_subfolders_gitignore_tests/subdir/.gitignore new file mode 100644 index 00000000000..150f68c80f5 --- /dev/null +++ b/tests/data/ignore_subfolders_gitignore_tests/subdir/.gitignore @@ -0,0 +1 @@ +*/* diff --git a/tests/data/ignore_subfolders_gitignore_tests/subdir/b.py b/tests/data/ignore_subfolders_gitignore_tests/subdir/b.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/data/ignore_subfolders_gitignore_tests/subdir/subdir/c.py b/tests/data/ignore_subfolders_gitignore_tests/subdir/subdir/c.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/test_black.py b/tests/test_black.py index 83c705a869f..f05337e59e8 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2056,6 +2056,24 @@ def test_invalid_nested_gitignore(self) -> None: gitignore = path / "a" / ".gitignore" assert f"Could not parse {gitignore}" in result.stderr_bytes.decode() + def test_gitignore_that_ignores_subfolders(self) -> None: + # If gitignore with */* is in root + root = Path(DATA_DIR / "ignore_subfolders_gitignore_tests" / "subdir") + expected = [root / "b.py"] + ctx = FakeContext() + ctx.obj["root"] = root + assert_collected_sources([root], expected, ctx=ctx) + + # If .gitignore with */* is nested + root = Path(DATA_DIR / "ignore_subfolders_gitignore_tests") + expected = [ + root / "a.py", + root / "subdir" / "b.py", + ] + ctx = FakeContext() + ctx.obj["root"] = root + assert_collected_sources([root], expected, ctx=ctx) + def test_empty_include(self) -> None: path = DATA_DIR / "include_exclude_tests" src = [path]