Skip to content

Commit

Permalink
Throwing a more descriptive warning when reading a bad .gitignore
Browse files Browse the repository at this point in the history
A particularly unhelpful error was thrown before, I feel this will
be a lot more helpful to users trying to understand what went wrong.
  • Loading branch information
john-science committed Aug 19, 2021
1 parent ef7c45f commit f519abb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ def get_gitignore(root: Path) -> PathSpec:
if gitignore.is_file():
with gitignore.open(encoding="utf-8") as gf:
lines = gf.readlines()
return PathSpec.from_lines("gitwildmatch", lines)

try:
return PathSpec.from_lines("gitwildmatch", lines)
except (IndexError, ValueError, TypeError) as e:
raise SyntaxError("Problem parsing .gitignore file: {e}")


def normalize_path_maybe_ignore(
Expand Down
12 changes: 12 additions & 0 deletions tests/data/broken_gitignore_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# No non-source python resources.
*.pyc
*.pyo
*.pyd
*.pyx

# This should break things
!

# No build artifacts
build
wheelhouse
5 changes: 5 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,11 @@ def test_nested_gitignore(self) -> None:
)
self.assertEqual(sorted(expected), sorted(sources))

def test_broken_gitignore(self) -> None:
path = Path(THIS_DIR / "data" / "broken_gitignore_test")
with self.assertRaises(SyntaxError):
root_gitignore = black.files.get_gitignore(path)

def test_empty_include(self) -> None:
path = THIS_DIR / "data" / "include_exclude_tests"
report = black.Report()
Expand Down

0 comments on commit f519abb

Please sign in to comment.