mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 12:06:53 +00:00
Merge pull request #469 from aniketbhatnagar/465-Handle-Hash
[#465] Handled multiline dependencies
This commit is contained in:
commit
e56e5bb4e9
2 changed files with 32 additions and 2 deletions
|
|
@ -34,6 +34,18 @@ class Requirement:
|
||||||
else:
|
else:
|
||||||
return self.name < requirement.name
|
return self.name < requirement.name
|
||||||
|
|
||||||
|
def is_complete(self) -> bool:
|
||||||
|
return (
|
||||||
|
self.value is not None and
|
||||||
|
not self.value.rstrip(b'\r\n').endswith(b'\\')
|
||||||
|
)
|
||||||
|
|
||||||
|
def append_value(self, value: bytes) -> None:
|
||||||
|
if self.value is not None:
|
||||||
|
self.value += value
|
||||||
|
else:
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
|
||||||
def fix_requirements(f: IO[bytes]) -> int:
|
def fix_requirements(f: IO[bytes]) -> int:
|
||||||
requirements: List[Requirement] = []
|
requirements: List[Requirement] = []
|
||||||
|
|
@ -55,7 +67,7 @@ def fix_requirements(f: IO[bytes]) -> int:
|
||||||
# If the most recent requirement object has a value, then it's
|
# If the most recent requirement object has a value, then it's
|
||||||
# time to start building the next requirement object.
|
# time to start building the next requirement object.
|
||||||
|
|
||||||
if not len(requirements) or requirements[-1].value is not None:
|
if not len(requirements) or requirements[-1].is_complete():
|
||||||
requirements.append(Requirement())
|
requirements.append(Requirement())
|
||||||
|
|
||||||
requirement = requirements[-1]
|
requirement = requirements[-1]
|
||||||
|
|
@ -73,7 +85,7 @@ def fix_requirements(f: IO[bytes]) -> int:
|
||||||
elif line.startswith(b'#') or line.strip() == b'':
|
elif line.startswith(b'#') or line.strip() == b'':
|
||||||
requirement.comments.append(line)
|
requirement.comments.append(line)
|
||||||
else:
|
else:
|
||||||
requirement.value = line
|
requirement.append_value(line)
|
||||||
|
|
||||||
# if a file ends in a comment, preserve it at the end
|
# if a file ends in a comment, preserve it at the end
|
||||||
if requirements[-1].value is None:
|
if requirements[-1].value is None:
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,24 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
|
||||||
FAIL,
|
FAIL,
|
||||||
b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
|
b'Django\nijk\ngit+ssh://git_url@tag#egg=ocflib\n',
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
b'b==1.0.0\n'
|
||||||
|
b'c=2.0.0 \\\n'
|
||||||
|
b' --hash=sha256:abcd\n'
|
||||||
|
b'a=3.0.0 \\\n'
|
||||||
|
b' --hash=sha256:a1b1c1d1',
|
||||||
|
FAIL,
|
||||||
|
b'a=3.0.0 \\\n'
|
||||||
|
b' --hash=sha256:a1b1c1d1\n'
|
||||||
|
b'b==1.0.0\n'
|
||||||
|
b'c=2.0.0 \\\n'
|
||||||
|
b' --hash=sha256:abcd\n',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
|
||||||
|
PASS,
|
||||||
|
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_integration(input_s, expected_retval, output, tmpdir):
|
def test_integration(input_s, expected_retval, output, tmpdir):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue