mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 21:04:17 +00:00
Add support for sections in requirements-fixer
This commit is contained in:
parent
429455474b
commit
cc5d4504d0
2 changed files with 58 additions and 28 deletions
|
|
@ -66,10 +66,7 @@ class Requirement:
|
||||||
|
|
||||||
|
|
||||||
def fix_requirements(f: IO[bytes]) -> int:
|
def fix_requirements(f: IO[bytes]) -> int:
|
||||||
requirements: list[Requirement] = []
|
|
||||||
before = list(f)
|
before = list(f)
|
||||||
after: list[bytes] = []
|
|
||||||
|
|
||||||
before_string = b''.join(before)
|
before_string = b''.join(before)
|
||||||
|
|
||||||
# adds new line in case one is missing
|
# adds new line in case one is missing
|
||||||
|
|
@ -81,17 +78,21 @@ def fix_requirements(f: IO[bytes]) -> int:
|
||||||
if before_string.strip() == b'':
|
if before_string.strip() == b'':
|
||||||
return PASS
|
return PASS
|
||||||
|
|
||||||
|
# split requirements into sections based on newlines
|
||||||
|
sections: list[list[Requirement]] = [[]]
|
||||||
|
|
||||||
for line in before:
|
for line in before:
|
||||||
|
requirements = sections[-1]
|
||||||
|
|
||||||
# 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].is_complete():
|
if not len(requirements) or requirements[-1].is_complete():
|
||||||
requirements.append(Requirement())
|
requirements.append(Requirement())
|
||||||
|
|
||||||
requirement = requirements[-1]
|
requirement = requirements[-1]
|
||||||
|
|
||||||
# If we see a newline before any requirements, then this is a
|
# If we see a newline before any requirements, then this is a
|
||||||
# top of file comment.
|
# top of section comment.
|
||||||
if len(requirements) == 1 and line.strip() == b'':
|
if len(requirements) == 1 and line.strip() == b'':
|
||||||
if (
|
if (
|
||||||
len(requirement.comments) and
|
len(requirement.comments) and
|
||||||
|
|
@ -100,12 +101,22 @@ def fix_requirements(f: IO[bytes]) -> int:
|
||||||
requirement.value = b'\n'
|
requirement.value = b'\n'
|
||||||
else:
|
else:
|
||||||
requirement.comments.append(line)
|
requirement.comments.append(line)
|
||||||
|
elif line.lstrip() == b'':
|
||||||
|
sections.append([])
|
||||||
elif line.lstrip().startswith(b'#') or line.strip() == b'':
|
elif line.lstrip().startswith(b'#') or line.strip() == b'':
|
||||||
requirement.comments.append(line)
|
requirement.comments.append(line)
|
||||||
else:
|
else:
|
||||||
requirement.append_value(line)
|
requirement.append_value(line)
|
||||||
|
|
||||||
# if a file ends in a comment, preserve it at the end
|
after_string = b''
|
||||||
|
|
||||||
|
for requirements in sections:
|
||||||
|
after: list[bytes] = []
|
||||||
|
|
||||||
|
if len(after_string) > 0:
|
||||||
|
after_string += b'\n'
|
||||||
|
|
||||||
|
# if a section ends in a comment, preserve it at the end
|
||||||
if requirements[-1].value is None:
|
if requirements[-1].value is None:
|
||||||
rest = requirements.pop().comments
|
rest = requirements.pop().comments
|
||||||
else:
|
else:
|
||||||
|
|
@ -131,7 +142,7 @@ def fix_requirements(f: IO[bytes]) -> int:
|
||||||
prev = requirement
|
prev = requirement
|
||||||
after.extend(rest)
|
after.extend(rest)
|
||||||
|
|
||||||
after_string = b''.join(after)
|
after_string += b''.join(after)
|
||||||
|
|
||||||
if before_string == after_string:
|
if before_string == after_string:
|
||||||
return PASS
|
return PASS
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,25 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
|
||||||
PASS,
|
PASS,
|
||||||
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
|
b'a=2.0.0 \\\n --hash=sha256:abcd\nb==1.0.0\n',
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
(
|
||||||
|
b'-c external.txt\n'
|
||||||
|
b'\n'
|
||||||
|
b'Peach\n'
|
||||||
|
b'apple\n'
|
||||||
|
b'banana\n'
|
||||||
|
b'pear\n'
|
||||||
|
),
|
||||||
|
FAIL,
|
||||||
|
(
|
||||||
|
b'-c external.txt\n'
|
||||||
|
b'\n'
|
||||||
|
b'apple\n'
|
||||||
|
b'banana\n'
|
||||||
|
b'Peach\n'
|
||||||
|
b'pear\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