Skip to content

Commit bbcd31e

Browse files
aniketbhatnagarasottile
authored andcommitted
Handled multiline dependencies
1 parent 41e26ab commit bbcd31e

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

pre_commit_hooks/requirements_txt_fixer.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ def __lt__(self, requirement: 'Requirement') -> int:
3434
else:
3535
return self.name < requirement.name
3636

37+
def is_complete(self) -> bool:
38+
return (
39+
self.value is not None and
40+
not self.value.rstrip(b'\r\n').endswith(b'\\')
41+
)
42+
43+
def append_value(self, value: bytes) -> None:
44+
if self.value is not None:
45+
self.value += value
46+
else:
47+
self.value = value
48+
3749

3850
def fix_requirements(f: IO[bytes]) -> int:
3951
requirements: List[Requirement] = []
@@ -55,7 +67,7 @@ def fix_requirements(f: IO[bytes]) -> int:
5567
# If the most recent requirement object has a value, then it's
5668
# time to start building the next requirement object.
5769

58-
if not len(requirements) or requirements[-1].value is not None:
70+
if not len(requirements) or requirements[-1].is_complete():
5971
requirements.append(Requirement())
6072

6173
requirement = requirements[-1]
@@ -73,7 +85,7 @@ def fix_requirements(f: IO[bytes]) -> int:
7385
elif line.startswith(b'#') or line.strip() == b'':
7486
requirement.comments.append(line)
7587
else:
76-
requirement.value = line
88+
requirement.append_value(line)
7789

7890
# if a file ends in a comment, preserve it at the end
7991
if requirements[-1].value is None:

tests/requirements_txt_fixer_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@
5050
FAIL,
5151
b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
5252
),
53+
(
54+
b'b==1.0.0\n'
55+
b'c=2.0.0 \\\n'
56+
b' --hash=sha256:abcd\n'
57+
b'a=3.0.0 \\\n'
58+
b' --hash=sha256:a1b1c1d1',
59+
FAIL,
60+
b'a=3.0.0 \\\n'
61+
b' --hash=sha256:a1b1c1d1\n'
62+
b'b==1.0.0\n'
63+
b'c=2.0.0 \\\n'
64+
b' --hash=sha256:abcd\n',
65+
),
66+
(
67+
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
68+
PASS,
69+
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
70+
),
5371
),
5472
)
5573
def test_integration(input_s, expected_retval, output, tmpdir):

0 commit comments

Comments
 (0)