From 9782c3331ae952a69a1ff867ab3bb027c2a146ae 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 three cases: 1) when the .gitignore with the special rule to ignore every subfolder and its contents (*/*) is in the root, 2) when the file is inside a subfolder relative to root (nested), and 3) when the target folder contains the .gitignore and root is a parent folder of the target. In all of these 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, these tests failed when the .gitignore file was nested (second case). Now, the test is passed for all 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 | 26 +++++++++++++++++++ 5 files changed, 27 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..59c6fea422c 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2056,6 +2056,32 @@ 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) + + # If command is executed from outer dir + root = Path(DATA_DIR / "ignore_subfolders_gitignore_tests") + target = root / "subdir" + expected = [target / "b.py"] + ctx = FakeContext() + ctx.obj["root"] = root + assert_collected_sources([target], expected, ctx=ctx) + def test_empty_include(self) -> None: path = DATA_DIR / "include_exclude_tests" src = [path]