Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/black/ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def parse_line_ranges(line_ranges: Sequence[str]) -> list[tuple[int, int]]:
f" {lines_str!r}"
) from None
else:
if start < 1 or end < 1:
raise ValueError(
"Incorrect --line-ranges value, "
f"expect positive integers, found {lines_str!r}"
)
if start > end:
raise ValueError(
"Incorrect --line-ranges value, expect START <= END, "
f"found {lines_str!r}"
)
lines.append((start, end))
return lines

Expand Down
28 changes: 27 additions & 1 deletion tests/test_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,33 @@

import pytest

from black.ranges import adjusted_lines, sanitized_lines
from black.ranges import adjusted_lines, parse_line_ranges, sanitized_lines


@pytest.mark.parametrize(
"lines_str, expected",
[
(["1-5"], [(1, 5)]),
(["1-1"], [(1, 1)]),
(["1-3", "5-7"], [(1, 3), (5, 7)]),
],
)
def test_parse_line_ranges_valid(lines_str, expected):
assert parse_line_ranges(lines_str) == expected


@pytest.mark.parametrize(
"lines_str",
[
["5-3"],
["0-5"],
["-1-5"],
["5-0"],
],
)
def test_parse_line_ranges_invalid(lines_str):
with pytest.raises(ValueError, match="Incorrect --line-ranges"):
parse_line_ranges(lines_str)


@pytest.mark.parametrize(
Expand Down
Loading